reply-language-reader.test.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { expect, it, vi } from "vitest";
  2. // 大型工厂模块在准备阶段加载,避免转换开销占用端口行为用例的超时预算。
  3. import { createWorkspaceZCodeApp } from "../src/zcode-protocol/workspace-model-runtime.js";
  4. import {
  5. ProtocolRequestError,
  6. type ZCodeProtocolAgentServerContext,
  7. } from "../src/zcode-protocol/server-types.js";
  8. import type { ZCodeAppOptions } from "../src/app/types.js";
  9. import { ZCODE_REPLY_LANGUAGE_REQUEST_TIMEOUT_MS, zcodeProtocolMethods } from "@zcode/shared";
  10. const workspace = {
  11. workspacePath: "/work",
  12. workspaceIdentity: "local:/work",
  13. workspaceKey: "local:/work",
  14. };
  15. const input = { sessionId: "session", traceContext: { traceId: "test-trace" } };
  16. async function readerWith(requestClient: ReturnType<typeof vi.fn>) {
  17. const createZCodeApp = vi.fn(async (_options: Omit<ZCodeAppOptions, "providerRegistry">) => ({}));
  18. await createWorkspaceZCodeApp(
  19. {
  20. deps: { createZCodeApp },
  21. requestClient,
  22. notify: vi.fn(),
  23. } as unknown as ZCodeProtocolAgentServerContext,
  24. workspace,
  25. {},
  26. );
  27. return createZCodeApp.mock.calls[0]![0].readReplyLanguage!;
  28. }
  29. it("reads the Host reply language with a dedicated bounded request", async () => {
  30. const requestClient = vi.fn(async () => ({ language: "Português (Brasil)" }));
  31. const read = await readerWith(requestClient);
  32. const controller = new AbortController();
  33. await expect(read({ ...input, signal: controller.signal })).resolves.toBe("Português (Brasil)");
  34. expect(requestClient).toHaveBeenCalledWith(
  35. zcodeProtocolMethods.sessionReadReplyLanguage,
  36. { sessionId: "session" },
  37. expect.anything(),
  38. expect.objectContaining({
  39. signal: controller.signal,
  40. timeoutMs: ZCODE_REPLY_LANGUAGE_REQUEST_TIMEOUT_MS,
  41. }),
  42. );
  43. });
  44. it("passes an unset reply language through as null", async () => {
  45. const read = await readerWith(vi.fn(async () => ({ language: null })));
  46. await expect(read(input)).resolves.toBeNull();
  47. });
  48. it("leaves request failures to the runtime, which skips the announcement", async () => {
  49. const failure = new ProtocolRequestError(-32601, "Method not found");
  50. const read = await readerWith(
  51. vi.fn(async () => {
  52. throw failure;
  53. }),
  54. );
  55. await expect(read(input)).rejects.toBe(failure);
  56. });