File size: 3,106 Bytes
4e7644b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import { beforeEach, describe, expect, it, vi } from "vitest";

const cancelApi = vi.fn();
const getOutputApi = vi.fn();
const messageError = vi.fn();
const messageSuccess = vi.fn();

vi.mock("../api/modules/toolCalls", () => ({
  toolCallsApi: {
    cancel: (...args: unknown[]) => cancelApi(...args),
    getOutput: (...args: unknown[]) => getOutputApi(...args),
  },
  subscribeToolCallStream: () => () => {},
  extractOutputText: (output: { content?: Array<{ text?: string }> }) =>
    output.content?.[0]?.text || "",
}));

vi.mock("antd", () => ({
  message: {
    error: (...args: unknown[]) => messageError(...args),
    info: vi.fn(),
    success: (...args: unknown[]) => messageSuccess(...args),
  },
}));

vi.mock("../i18n", () => ({
  default: { t: (_k: string, fallback: string) => fallback },
}));

vi.mock("../utils/resolveBackendSessionId", () => ({
  resolveBackendSessionId: (preferred?: string | null) => preferred || "",
}));

import { useBackgroundTasksStore } from "../stores/backgroundTasksStore";
import {
  cancelBackgroundTask,
  registerBackgroundTask,
  stopBackgroundWatchersNotInSession,
} from "./useBackgroundTaskWatcher";

describe("useBackgroundTaskWatcher session isolation", () => {
  beforeEach(() => {
    cancelApi.mockReset();
    getOutputApi.mockReset();
    messageError.mockReset();
    messageSuccess.mockReset();
    useBackgroundTasksStore.setState({ tasks: [] });
  });

  it("removes empty-session tasks when switching to another session", () => {
    const store = useBackgroundTasksStore.getState();
    store.addTask({
      toolCallId: "tc-a",
      toolName: "a",
      sessionId: "sid-a",
      startTime: 1,
    });
    store.addTask({
      toolCallId: "tc-empty",
      toolName: "e",
      sessionId: "",
      startTime: 2,
    });
    store.addTask({
      toolCallId: "tc-b",
      toolName: "b",
      sessionId: "sid-b",
      startTime: 3,
    });

    stopBackgroundWatchersNotInSession("sid-b");

    const left = useBackgroundTasksStore
      .getState()
      .tasks.map((t) => t.toolCallId)
      .sort();
    expect(left).toEqual(["tc-b"]);
  });

  it("refuses cancel when sessionId is empty", async () => {
    await expect(cancelBackgroundTask("", "tc-1")).rejects.toThrow(
      /Missing backend session id/,
    );
    expect(cancelApi).not.toHaveBeenCalled();
    expect(messageError).toHaveBeenCalled();
  });

  it("hydrates /output immediately when alreadyCompleted", async () => {
    getOutputApi.mockResolvedValue({
      tool_call_id: "tc-fast",
      is_closed: true,
      final_state: "success",
      content: [{ text: "fast-done" }],
    });

    registerBackgroundTask({
      sessionId: "sid-fast",
      toolCallId: "tc-fast",
      toolName: "shell",
      alreadyCompleted: true,
    });

    await vi.waitFor(() => {
      const task = useBackgroundTasksStore
        .getState()
        .tasks.find((t) => t.toolCallId === "tc-fast");
      expect(task?.status).toBe("done");
      expect(task?.result).toBe("fast-done");
    });
    expect(getOutputApi).toHaveBeenCalledWith("sid-fast", "tc-fast");
  });
});