agent-interface.test.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import { describe, expect, test } from "bun:test"
  2. import { ACP } from "../../src/acp/agent"
  3. import type { Agent as ACPAgent } from "@agentclientprotocol/sdk"
  4. /**
  5. * Type-level test: This line will fail to compile if ACP.Agent
  6. * doesn't properly implement the ACPAgent interface.
  7. *
  8. * The SDK checks for methods like `agent.unstable_setSessionModel` at runtime
  9. * and throws "Method not found" if they're missing. TypeScript allows optional
  10. * interface methods to be omitted, but the SDK still expects them.
  11. *
  12. * @see https://github.com/agentclientprotocol/typescript-sdk/commit/7072d3f
  13. */
  14. type _AssertAgentImplementsACPAgent = ACP.Agent extends ACPAgent ? true : never
  15. const _typeCheck: _AssertAgentImplementsACPAgent = true
  16. /**
  17. * Runtime verification that optional methods the SDK expects are actually implemented.
  18. * The SDK's router checks `if (!agent.methodName)` and throws MethodNotFound if missing.
  19. */
  20. describe("acp.agent interface compliance", () => {
  21. // Extract method names from the ACPAgent interface type
  22. type ACPAgentMethods = keyof ACPAgent
  23. // Methods that the SDK's router explicitly checks for at runtime
  24. const sdkCheckedMethods: ACPAgentMethods[] = [
  25. // Required
  26. "initialize",
  27. "newSession",
  28. "prompt",
  29. "cancel",
  30. // Optional but checked by SDK router
  31. "loadSession",
  32. "setSessionMode",
  33. "authenticate",
  34. // Unstable - SDK checks these with unstable_ prefix
  35. "unstable_listSessions",
  36. "unstable_forkSession",
  37. "unstable_resumeSession",
  38. "unstable_setSessionModel",
  39. ]
  40. test("Agent implements all SDK-checked methods", () => {
  41. for (const method of sdkCheckedMethods) {
  42. expect(typeof ACP.Agent.prototype[method as keyof typeof ACP.Agent.prototype], `Missing method: ${method}`).toBe(
  43. "function",
  44. )
  45. }
  46. })
  47. })