background-work-owner.test.ts 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import { describe, expect, it, vi } from "vitest";
  2. import type { BackgroundBashOutputResult } from "@zcode/shared";
  3. import { readBackgroundBashOutputFromOwner as readOutput } from "../src/zcode-protocol/background-work-owner.js";
  4. import type { ZCodeProtocolAgentServerContext } from "../src/zcode-protocol/server-types.js";
  5. const output: BackgroundBashOutputResult = {
  6. kind: "output",
  7. workId: "work",
  8. status: "running",
  9. output: "child output",
  10. truncated: false,
  11. outputPath: "/output.log",
  12. };
  13. const unavailable: BackgroundBashOutputResult = { kind: "unavailable", workId: "work" };
  14. function setup(childResult?: BackgroundBashOutputResult) {
  15. const rootRead = vi.fn(async (_workId: string, sessionId: string) =>
  16. sessionId === "child" ? output : unavailable,
  17. );
  18. const childRead = vi.fn(async () => childResult ?? unavailable);
  19. const getSession = vi.fn(async (id: string) =>
  20. id === "child" || id === "sibling" ? { parentID: "root" } : undefined,
  21. );
  22. const sessions = new Map<string, unknown>([
  23. ["root", { app: { readBackgroundBashOutput: rootRead } }],
  24. ]);
  25. if (childResult) sessions.set("child", { app: { readBackgroundBashOutput: childRead } });
  26. const context = {
  27. sessions,
  28. deps: { sessionStore: { getSession } },
  29. } as unknown as ZCodeProtocolAgentServerContext;
  30. return { context, rootRead, childRead, getSession };
  31. }
  32. describe("background work owner", () => {
  33. it("queries the existing ancestor with the original child session without cold resume", async () => {
  34. const { context, rootRead, getSession } = setup();
  35. expect(await readOutput(context, "child", "work")).toEqual(output);
  36. expect(rootRead).toHaveBeenCalledExactlyOnceWith("work", "child");
  37. expect(getSession.mock.calls.map(([id]) => id)).toEqual(["child"]);
  38. });
  39. it("finds the old task in the ancestor after a resumed child gets a fresh adapter", async () => {
  40. const { context, rootRead, childRead } = setup(unavailable);
  41. expect(await readOutput(context, "child", "work")).toEqual(output);
  42. expect(childRead).toHaveBeenCalledExactlyOnceWith("work", "child");
  43. expect(rootRead).toHaveBeenCalledExactlyOnceWith("work", "child");
  44. });
  45. it("returns a task owned by the current child without consulting ancestors", async () => {
  46. const { context, rootRead, getSession } = setup(output);
  47. expect(await readOutput(context, "child", "work")).toEqual(output);
  48. expect(rootRead).not.toHaveBeenCalled();
  49. expect(getSession).not.toHaveBeenCalled();
  50. });
  51. it.each([
  52. { kind: "read_failed", workId: "work", code: "EACCES" },
  53. { kind: "unsupported", workId: "work" },
  54. ] as const)("preserves $kind without falling back to another adapter", async (result) => {
  55. const { context, rootRead, getSession } = setup(result);
  56. expect(await readOutput(context, "child", "work")).toEqual(result);
  57. expect(rootRead).not.toHaveBeenCalled();
  58. expect(getSession).not.toHaveBeenCalled();
  59. });
  60. it("does not read a sibling task through a shared ancestor", async () => {
  61. const { context, rootRead } = setup();
  62. expect(await readOutput(context, "sibling", "work")).toEqual(unavailable);
  63. expect(rootRead).toHaveBeenCalledExactlyOnceWith("work", "sibling");
  64. });
  65. it("returns unavailable for missing or cyclic ancestry", async () => {
  66. const { context, getSession } = setup();
  67. getSession.mockImplementation(async (id) => (id === "cycle" ? { parentID: id } : undefined));
  68. expect(await readOutput(context, "missing", "work")).toEqual(unavailable);
  69. expect(await readOutput(context, "cycle", "work")).toEqual(unavailable);
  70. expect(getSession.mock.calls.map(([id]) => id)).toEqual(["missing", "cycle"]);
  71. });
  72. });