app-client.test.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import { describe, expect, it, vi } from "vitest";
  2. import { appControlCancelMethod, appControlProtocol } from "@zcode/shared";
  3. import { createAppSessionControlBinding } from "../../src/session-control/index.js";
  4. import type { AppTrustedContext } from "@zcode/session-control/contracts";
  5. const rootSession = { kind: "app" as const, workspacePath: "/workspace", sessionId: "caller" };
  6. const ctx: AppTrustedContext = {
  7. callerId: "caller",
  8. rootSession,
  9. allowedScopes: [{ ...rootSession, scope: "workspace" }],
  10. traceId: "trace",
  11. turnId: "turn",
  12. toolCallId: "call",
  13. };
  14. describe("App session control reverse RPC", () => {
  15. it("uses the actual connection and bound root identity, with strict typed response", async () => {
  16. const searchScope = {
  17. scopes: [{ kind: "app", scope: "project", projectId: "novel", sourceServerId: "source" }],
  18. unavailableSources: [{ sourceServerId: "remote", reason: "disconnected" }],
  19. };
  20. const result = { sessions: [], searchScope };
  21. const requestClient = vi.fn(async () => ({ ok: true, result }));
  22. const binding = createAppSessionControlBinding({
  23. requestClient: requestClient as never,
  24. sessionId: "caller",
  25. workspacePath: "/workspace",
  26. });
  27. await expect(binding.control.listSessions(ctx, {})).resolves.toEqual(result);
  28. expect(requestClient).toHaveBeenCalledWith(
  29. appControlProtocol.listSessions.method,
  30. { callerSessionId: "caller", callId: "call", turnId: "turn", input: { archived: "exclude" } },
  31. appControlProtocol.listSessions.result,
  32. expect.objectContaining({ trace: { traceId: "trace" } }),
  33. );
  34. });
  35. it("forwards explicit discovery filters without authority and rejects list replies missing coverage", async () => {
  36. const searchScope = {
  37. scopes: [],
  38. unavailableSources: [{ sourceServerId: "source", reason: "disconnected" }],
  39. };
  40. const requestClient = vi.fn(async () => ({ ok: true, result: { sessions: [], searchScope } }));
  41. const binding = createAppSessionControlBinding({
  42. requestClient: requestClient as never,
  43. sessionId: "caller",
  44. workspacePath: "/workspace",
  45. });
  46. const project = { kind: "app" as const, projectId: "novel", sourceServerId: "source" };
  47. await binding.control.listSessions(ctx, { project, query: "小说" });
  48. expect(requestClient.mock.calls[0]?.[1]).toEqual({
  49. callerSessionId: "caller",
  50. callId: "call",
  51. turnId: "turn",
  52. input: { project, query: "小说", archived: "exclude" },
  53. });
  54. requestClient.mockResolvedValue({ ok: true, result: { projects: [], searchScope } } as never);
  55. await expect(binding.control.listProjects(ctx, {})).resolves.toEqual({
  56. projects: [],
  57. searchScope,
  58. });
  59. requestClient.mockResolvedValue({ ok: true, result: { sessions: [] } } as never);
  60. await expect(binding.control.listSessions(ctx, {})).rejects.toThrow();
  61. requestClient.mockResolvedValue({ ok: true, result: { projects: [] } } as never);
  62. await expect(binding.control.listProjects(ctx, {})).rejects.toThrow();
  63. });
  64. it("never sends a forged source root and does not fall back to a CLI runtime", async () => {
  65. const requestClient = vi.fn();
  66. const binding = createAppSessionControlBinding({
  67. requestClient: requestClient as never,
  68. sessionId: "caller",
  69. workspacePath: "/workspace",
  70. });
  71. await expect(
  72. binding.control.listProjects(
  73. { ...ctx, rootSession: { ...rootSession, sessionId: "forged" } },
  74. {},
  75. ),
  76. ).rejects.toMatchObject({ code: "forbidden" });
  77. expect(requestClient).not.toHaveBeenCalled();
  78. });
  79. it("maps explicit Host rejection and invalid output to failures", async () => {
  80. const requestClient = vi.fn(async () => ({
  81. ok: false,
  82. error: { code: "forbidden", message: "wrong source" },
  83. }));
  84. const binding = createAppSessionControlBinding({
  85. requestClient: requestClient as never,
  86. sessionId: "caller",
  87. workspacePath: "/workspace",
  88. });
  89. await expect(binding.control.listProjects(ctx, {})).rejects.toMatchObject({
  90. code: "forbidden",
  91. });
  92. requestClient.mockResolvedValue({ ok: true, result: { sessions: [] } } as never);
  93. await expect(binding.control.listProjects(ctx, {})).rejects.toThrow();
  94. });
  95. it("cancels only its own wait call and does not emit an interrupt", async () => {
  96. const controller = new AbortController();
  97. let pendingReject!: (reason: unknown) => void;
  98. const requestClient = vi.fn((method: string) =>
  99. method === appControlCancelMethod
  100. ? Promise.resolve({ cancelled: true })
  101. : new Promise((_resolve, reject) => {
  102. pendingReject = reject;
  103. }),
  104. );
  105. const binding = createAppSessionControlBinding({
  106. requestClient: requestClient as never,
  107. sessionId: "caller",
  108. workspacePath: "/workspace",
  109. });
  110. const waiting = binding.control.waitSessions(
  111. { ...ctx, signal: controller.signal },
  112. {
  113. operations: [{ session: { ...rootSession, sessionId: "target" }, commandId: "command" }],
  114. mode: "all",
  115. },
  116. );
  117. controller.abort();
  118. pendingReject(controller.signal.reason);
  119. await expect(waiting).rejects.toThrow();
  120. expect(requestClient.mock.calls.map(([method]) => method)).toEqual([
  121. appControlProtocol.waitSessions.method,
  122. appControlCancelMethod,
  123. ]);
  124. expect(requestClient.mock.calls[1]?.[1]).toEqual({ callerSessionId: "caller", callId: "call" });
  125. });
  126. });