runtime.test.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import { afterEach, describe, expect, it } from "vitest";
  2. import { createSessionId } from "@zcode/contracts";
  3. import { createRegistryBackedTestApp } from "../helpers/registry-backed-test-app.js";
  4. import { cleanup, fixture, answer } from "./runtime-fixture.js";
  5. afterEach(async () => {
  6. for (const close of cleanup.splice(0).reverse()) await close();
  7. });
  8. describe("CLI session control runtime", () => {
  9. it("reads non-owned persisted history by ID but never restores or writes it", async () => {
  10. const f = await fixture(async () => answer);
  11. const external = await createRegistryBackedTestApp({
  12. sessionId: createSessionId(),
  13. sessionStore: f.store,
  14. skipUserConfig: true,
  15. env: { ZCODE_STORAGE_DIR: f.cwd },
  16. modelExecutor: { generateText: async () => answer },
  17. configuredDefaultModelSelection: { providerId: "zai", modelId: "glm-4.6" },
  18. runtimeConfig: { workingDirectory: f.cwd },
  19. });
  20. cleanup.push(async () => {
  21. await external.close?.();
  22. });
  23. await external.submitPrompt("external app or CLI owner");
  24. const ref = { ...f.ctx.rootSession, sessionId: external.sessionId };
  25. // 没有读范围的调用者只看到本进程托管的根。
  26. expect(
  27. (await f.manager.control.listSessions(f.ctx, {})).sessions.map(
  28. (item) => item.session.sessionId,
  29. ),
  30. ).not.toContain(external.sessionId);
  31. // 独立 CLI 装配声明可读本机会话库:其他进程的历史可列出、按 ID 读取正文,但只宣称可读。
  32. const reader = { ...f.ctx, readAnyLocalSession: true };
  33. const listed = (await f.manager.control.listSessions(reader, {})).sessions.find(
  34. (item) => item.session.sessionId === external.sessionId,
  35. );
  36. expect(listed).toMatchObject({ status: "unknown", availableActions: ["readSession"] });
  37. const read = await f.manager.control.readSession(reader, {
  38. sessionId: external.sessionId,
  39. view: "messages",
  40. });
  41. expect(read).toMatchObject({ view: "messages", session: { sessionId: external.sessionId } });
  42. expect(read.view === "messages" && read.messages.map((message) => message.text)).toContain(
  43. "external app or CLI owner",
  44. );
  45. for (const ctx of [f.ctx, reader])
  46. await expect(
  47. f.manager.control.sendToSession(ctx, {
  48. sessionId: external.sessionId,
  49. prompt: "do not restore",
  50. requestId: "external",
  51. }),
  52. ).rejects.toMatchObject({ code: "unavailable" });
  53. await expect(
  54. f.manager.control.sendToSession(reader, {
  55. session: ref,
  56. prompt: "do not restore",
  57. requestId: "external-ref",
  58. }),
  59. ).rejects.toMatchObject({ code: "unavailable" });
  60. // 读取不恢复 runtime,也不为目标创建 owner。
  61. expect(f.createRootApp).toHaveBeenCalledTimes(1);
  62. });
  63. });