useBackgroundBashOutput.test.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // @vitest-environment jsdom
  2. import { act, renderHook, cleanup } from "@testing-library/react";
  3. import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
  4. import { useBackgroundBashOutput } from "@/hooks/useBackgroundBashOutput.js";
  5. import type { BackgroundBashOutputResult } from "@zcode/shared";
  6. const mocks = vi.hoisted(() => ({ query: vi.fn(), stop: vi.fn() }));
  7. vi.mock("@/hooks/useZCodeAgentService.js", () => ({ useZCodeAgentService: () => service }));
  8. vi.mock("@/v4/agentV4ConnectionHandshake.js", () => ({
  9. ensureAgentV4ConnectionHandshake: async () => ({}),
  10. }));
  11. vi.mock("@/logger.js", () => ({ logger: { debug: vi.fn() } }));
  12. const service = { backgroundBashOutputV4: mocks.query, sendConversationCommandV4: mocks.stop };
  13. const target = {
  14. workspacePath: "/project",
  15. workspaceIdentity: "ssh:a",
  16. remoteSessionId: "remote-a",
  17. sessionId: "session",
  18. workId: "work",
  19. };
  20. const output = (text: string, status = "running") => ({
  21. kind: "output",
  22. workId: "work",
  23. status,
  24. output: text,
  25. outputPath: "/output",
  26. truncated: false,
  27. });
  28. const tick = async (ms = 0) =>
  29. act(async () => {
  30. await vi.advanceTimersByTimeAsync(ms);
  31. });
  32. beforeEach(() => {
  33. vi.useFakeTimers();
  34. mocks.query.mockReset().mockResolvedValue(output("one"));
  35. mocks.stop.mockReset();
  36. });
  37. afterEach(() => {
  38. cleanup();
  39. vi.useRealTimers();
  40. });
  41. describe("background Bash preview polling", () => {
  42. it("polls only visible tabs, replaces the tail, freezes reading, and retains terminal output", async () => {
  43. const { result, rerender } = renderHook(
  44. ({ visible }) => useBackgroundBashOutput(target, visible),
  45. { initialProps: { visible: true } },
  46. );
  47. await tick();
  48. expect(result.current.display?.output).toBe("one");
  49. act(() => result.current.pause());
  50. mocks.query.mockResolvedValue(output("two"));
  51. await tick(1000);
  52. expect(result.current.display?.output).toBe("one");
  53. expect(result.current.latest?.output).toBe("two");
  54. act(() => result.current.resume());
  55. await tick();
  56. expect(result.current.display?.output).toBe("two");
  57. rerender({ visible: false });
  58. const count = mocks.query.mock.calls.length;
  59. await tick(5000);
  60. expect(mocks.query).toHaveBeenCalledTimes(count);
  61. mocks.query.mockResolvedValue(output("final", "completed"));
  62. rerender({ visible: true });
  63. await tick();
  64. expect(result.current.latest?.status).toBe("completed");
  65. expect(result.current.display?.output).toBe("final");
  66. const terminalCount = mocks.query.mock.calls.length;
  67. await tick(5000);
  68. expect(mocks.query).toHaveBeenCalledTimes(terminalCount);
  69. expect(mocks.stop).not.toHaveBeenCalled();
  70. expect(mocks.query).toHaveBeenLastCalledWith(target);
  71. });
  72. it("does not overlap a slow request or apply it after hiding", async () => {
  73. let resolve!: (value: BackgroundBashOutputResult) => void;
  74. mocks.query.mockReturnValueOnce(
  75. new Promise((done) => {
  76. resolve = done;
  77. }),
  78. );
  79. const { result, rerender } = renderHook(
  80. ({ visible }) => useBackgroundBashOutput(target, visible),
  81. { initialProps: { visible: true } },
  82. );
  83. await tick(5000);
  84. expect(mocks.query).toHaveBeenCalledTimes(1);
  85. rerender({ visible: false });
  86. rerender({ visible: true });
  87. await tick();
  88. expect(mocks.query).toHaveBeenCalledTimes(1);
  89. await act(async () => resolve(output("stale") as BackgroundBashOutputResult));
  90. await tick();
  91. expect(result.current.display?.output).toBe("one");
  92. expect(mocks.query).toHaveBeenCalledTimes(2);
  93. });
  94. it("retains successful output on failure and waits for retry", async () => {
  95. const { result } = renderHook(() => useBackgroundBashOutput(target, true));
  96. await tick();
  97. mocks.query.mockResolvedValue({ kind: "read_failed", workId: "work", code: "ENOENT" });
  98. await tick(1000);
  99. expect(result.current.error).toBe("read_failed");
  100. expect(result.current.display?.output).toBe("one");
  101. await tick(5000);
  102. expect(mocks.query).toHaveBeenCalledTimes(2);
  103. mocks.query.mockResolvedValue(output("recovered"));
  104. act(() => result.current.refresh());
  105. await tick();
  106. expect(result.current.display?.output).toBe("recovered");
  107. });
  108. it("retains a frozen window at completion and applies final output on resume", async () => {
  109. const { result } = renderHook(() => useBackgroundBashOutput(target, true));
  110. await tick();
  111. act(() => result.current.pause());
  112. mocks.query.mockResolvedValue(output("final", "completed"));
  113. await tick(1000);
  114. expect(result.current.latest?.status).toBe("completed");
  115. expect(result.current.display?.output).toBe("one");
  116. await tick(5000);
  117. expect(mocks.query).toHaveBeenCalledTimes(2);
  118. act(() => result.current.resume());
  119. await tick();
  120. expect(result.current.display?.output).toBe("final");
  121. });
  122. it("drops responses for closed tabs and does not cancel work", async () => {
  123. let resolve!: (value: BackgroundBashOutputResult) => void;
  124. mocks.query.mockReturnValueOnce(
  125. new Promise((done) => {
  126. resolve = done;
  127. }),
  128. );
  129. const { unmount } = renderHook(() => useBackgroundBashOutput(target, true));
  130. await tick();
  131. unmount();
  132. await act(async () => resolve(output("late") as BackgroundBashOutputResult));
  133. await tick(5000);
  134. expect(mocks.query).toHaveBeenCalledTimes(1);
  135. expect(mocks.stop).not.toHaveBeenCalled();
  136. });
  137. it.each(["unavailable", "unsupported"])(
  138. "stops polling on %s without a cancellation",
  139. async (kind) => {
  140. mocks.query.mockResolvedValue({ kind, workId: "work" });
  141. const { result } = renderHook(() => useBackgroundBashOutput(target, true));
  142. await tick(5000);
  143. expect(result.current.error).toBe(kind);
  144. expect(mocks.query).toHaveBeenCalledTimes(1);
  145. expect(mocks.stop).not.toHaveBeenCalled();
  146. },
  147. );
  148. });