|
| 1 | +import { afterEach, describe, expect, expectTypeOf, it, vi } from "vitest"; |
| 2 | +import type { CoachAgent } from "../src/agent/coach-agent.js"; |
| 3 | +import type { CoachEngineSeam } from "../src/agent/coach-engine.js"; |
| 4 | + |
| 5 | +describe("createCoachEngine delegates verbatim to CoachAgent", () => { |
| 6 | + afterEach(() => { |
| 7 | + vi.doUnmock("../src/agent/coach-agent.js"); |
| 8 | + vi.resetModules(); |
| 9 | + }); |
| 10 | + |
| 11 | + async function setup() { |
| 12 | + const chat = vi.fn(async () => "canned-reply"); |
| 13 | + const hasSession = vi.fn(() => true); |
| 14 | + const resetSession = vi.fn(async () => ({ memoryFlushed: false })); |
| 15 | + const memorySentinel = { sentinel: "memory" }; |
| 16 | + const ctorArgs: unknown[][] = []; |
| 17 | + vi.doMock("../src/agent/coach-agent.js", () => ({ |
| 18 | + CoachAgent: class { |
| 19 | + chat = chat; |
| 20 | + hasSession = hasSession; |
| 21 | + resetSession = resetSession; |
| 22 | + getMemory = () => memorySentinel; |
| 23 | + constructor(...args: unknown[]) { |
| 24 | + ctorArgs.push(args); |
| 25 | + } |
| 26 | + }, |
| 27 | + })); |
| 28 | + const { createCoachEngine } = await import("../src/agent/coach-engine.js"); |
| 29 | + return { createCoachEngine, chat, hasSession, resetSession, memorySentinel, ctorArgs }; |
| 30 | + } |
| 31 | + |
| 32 | + it("constructs exactly one CoachAgent with verbatim args", async () => { |
| 33 | + const { createCoachEngine, ctorArgs } = await setup(); |
| 34 | + const sportSentinel = { sentinel: "sport" } as never; |
| 35 | + const configSentinel = { sentinel: "config" } as never; |
| 36 | + createCoachEngine(sportSentinel, configSentinel); |
| 37 | + expect(ctorArgs).toHaveLength(1); |
| 38 | + expect(ctorArgs[0]).toHaveLength(2); |
| 39 | + expect(ctorArgs[0]![0]).toBe(sportSentinel); |
| 40 | + expect(ctorArgs[0]![1]).toBe(configSentinel); |
| 41 | + }); |
| 42 | + |
| 43 | + it("chat forwards all three args and the result", async () => { |
| 44 | + const { createCoachEngine, chat } = await setup(); |
| 45 | + const engine = createCoachEngine({} as never, {} as never); |
| 46 | + const turnSentinel = { resolvedCs: null }; |
| 47 | + const result = await engine.chat("chat-1", "hello", turnSentinel); |
| 48 | + expect(chat).toHaveBeenCalledTimes(1); |
| 49 | + expect(chat).toHaveBeenCalledWith("chat-1", "hello", turnSentinel); |
| 50 | + expect((chat.mock.calls[0] as unknown[])[2]).toBe(turnSentinel); |
| 51 | + expect(result).toBe("canned-reply"); |
| 52 | + }); |
| 53 | + |
| 54 | + it("chat with the third arg omitted forwards undefined", async () => { |
| 55 | + const { createCoachEngine, chat } = await setup(); |
| 56 | + const engine = createCoachEngine({} as never, {} as never); |
| 57 | + await engine.chat("chat-1", "hello"); |
| 58 | + expect(chat).toHaveBeenCalledTimes(1); |
| 59 | + expect((chat.mock.calls[0] as unknown[])[2]).toBe(undefined); |
| 60 | + }); |
| 61 | + |
| 62 | + it("hasSession forwards and returns", async () => { |
| 63 | + const { createCoachEngine, hasSession } = await setup(); |
| 64 | + const engine = createCoachEngine({} as never, {} as never); |
| 65 | + expect(engine.hasSession("chat-2")).toBe(true); |
| 66 | + expect(hasSession).toHaveBeenCalledTimes(1); |
| 67 | + expect(hasSession).toHaveBeenCalledWith("chat-2"); |
| 68 | + }); |
| 69 | + |
| 70 | + it("resetSession forwards and returns the delegate's exact object", async () => { |
| 71 | + const { createCoachEngine, resetSession } = await setup(); |
| 72 | + const engine = createCoachEngine({} as never, {} as never); |
| 73 | + const result = await engine.resetSession("chat-3"); |
| 74 | + expect(resetSession).toHaveBeenCalledTimes(1); |
| 75 | + expect(resetSession).toHaveBeenCalledWith("chat-3"); |
| 76 | + const delegateResult = await resetSession.mock.results[0]!.value; |
| 77 | + expect(result).toBe(delegateResult); |
| 78 | + }); |
| 79 | + |
| 80 | + it("getMemory returns the delegate's Memory instance", async () => { |
| 81 | + const { createCoachEngine, memorySentinel } = await setup(); |
| 82 | + const engine = createCoachEngine({} as never, {} as never); |
| 83 | + expect(engine.getMemory()).toBe(memorySentinel); |
| 84 | + }); |
| 85 | + |
| 86 | + it("CoachAgent members exactly equal the seam members (compile-time)", () => { |
| 87 | + expectTypeOf<CoachAgent["chat"]>().toEqualTypeOf<CoachEngineSeam["chat"]>(); |
| 88 | + expectTypeOf<CoachAgent["hasSession"]>().toEqualTypeOf<CoachEngineSeam["hasSession"]>(); |
| 89 | + expectTypeOf<CoachAgent["resetSession"]>().toEqualTypeOf<CoachEngineSeam["resetSession"]>(); |
| 90 | + const toSeam = (a: CoachAgent): CoachEngineSeam => a; |
| 91 | + expect(typeof toSeam).toBe("function"); |
| 92 | + }); |
| 93 | +}); |
0 commit comments