Repository navigation
Expand file tree
/
Copy pathAgentPage.test.tsx
More file actions
157 lines (152 loc) · 5.2 KB
/
Copy pathAgentPage.test.tsx
File metadata and controls
157 lines (152 loc) · 5.2 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import { afterEach, expect, it, vi } from "vitest";
import {
cleanup,
fireEvent,
render,
screen,
waitFor,
act,
} from "@testing-library/react";
import App from "./App";
import type { DockState } from "./types";
import { clearModelCatalog } from "./modelCatalog";
const state: DockState = {
runtime: { enabled: true, version: "0.3.0" },
projects: [],
agents: ["Alpha", "Beta"].map((name, i) => ({
id: `a${i}`,
name,
provider: "codex",
project_id: null,
role: "",
})),
sessions: [
{ id: "s0", agent_id: "a0", title: "First conversation" },
{ id: "s1", agent_id: "a0", title: "Second conversation" },
{ id: "s2", agent_id: "a1", title: "Beta conversation" },
].map((s) => ({
...s,
project_id: null,
status: "idle",
created_at: "",
updated_at: "",
})),
runs: [],
messages: [],
events: [],
proposals: [],
approvals: [],
memories: [],
quotas: [],
subscriptions: [],
};
afterEach(() => {
cleanup();
clearModelCatalog();
vi.unstubAllGlobals();
delete window.__AGENTDOCK_DESKTOP_TOKEN__;
window.history.replaceState(null, "", "/");
});
async function setup(hash = "") {
window.history.replaceState(null, "", "/" + hash);
const fetchMock = vi.fn(async (path: string, _init?: RequestInit) => ({
ok: true,
json: async () =>
path === "/api/state"
? state
: path.includes("/events?")
? { events: [] }
: { models: [] },
}));
vi.stubGlobal("fetch", fetchMock);
window.__AGENTDOCK_DESKTOP_TOKEN__ = "page-fixture";
render(<App />);
await screen.findByRole("button", { name: "协作工作台" });
await waitFor(() => expect(screen.queryByText("连接本地工作台")).toBeNull());
return fetchMock;
}
const open = (name: string) =>
fireEvent.click(
screen.getByRole("button", { name: new RegExp(`^${name}.*进入会话`) }),
);
const back = () =>
fireEvent.click(screen.getByRole("button", { name: "返回 Agent 列表" }));
const draft = () =>
screen.getByLabelText("给 Agent 的任务") as HTMLTextAreaElement;
it("opens a dedicated page with only that agent's conversations, then returns to overview", async () => {
const fetchMock = await setup();
expect(screen.queryByRole("heading", { name: "会话" })).toBeNull();
expect(fetchMock.mock.calls.some(([path]) => path.includes("/events?"))).toBe(
false,
);
open("Alpha");
expect(window.location.hash).toBe("#/agents/a0");
expect(screen.getByRole("heading", { name: "Alpha", level: 1 })).toBeTruthy();
expect(
screen.getByRole("heading", { name: "First conversation" }),
).toBeTruthy();
expect(screen.queryByRole("button", { name: /^Beta/ })).toBeNull();
expect(screen.queryByRole("button", { name: "添加 Agent" })).toBeNull();
expect(screen.queryByText("TPS")).toBeNull();
await waitFor(() =>
expect(
fetchMock.mock.calls.some(
([path]) => path === "/api/sessions/s0/events?after=0",
),
).toBe(true),
);
back();
expect(window.location.hash).toBe("#/workspace");
expect(screen.queryByLabelText("给 Agent 的任务")).toBeNull();
expect(screen.getByRole("heading", { name: "协作工作台" })).toBeTruthy();
expect(
fetchMock.mock.calls.every(
([, init]) => (init as RequestInit | undefined)?.method !== "POST",
),
).toBe(true);
});
it("remembers each agent's selected conversation and keeps drafts separate across pages", async () => {
await setup();
open("Alpha");
fireEvent.change(draft(), { target: { value: "first draft" } });
fireEvent.click(screen.getByRole("button", { name: /^Second conversation/ }));
expect(draft().value).toBe("");
fireEvent.change(draft(), { target: { value: "second draft" } });
back();
open("Beta");
expect(draft().value).toBe("");
fireEvent.change(draft(), { target: { value: "beta draft" } });
back();
open("Alpha");
expect(
screen.getByRole("heading", { name: "Second conversation" }),
).toBeTruthy();
expect(draft().value).toBe("second draft");
fireEvent.click(screen.getByRole("button", { name: /^First conversation/ }));
expect(draft().value).toBe("first draft");
back();
open("Beta");
expect(draft().value).toBe("beta draft");
});
it("supports browser back and forward without losing the in-memory draft", async () => {
await setup();
open("Alpha");
fireEvent.change(draft(), { target: { value: "keep on back" } });
act(() => window.history.back());
await screen.findByRole("heading", { name: "协作工作台" });
act(() => window.history.forward());
await screen.findByRole("heading", { name: "Alpha", level: 1 });
expect(draft().value).toBe("keep on back");
});
it("restores a linked agent page and returns an unavailable ID to the list", async () => {
await setup("#/agents/a1");
expect(screen.getByRole("heading", { name: "Beta", level: 1 })).toBeTruthy();
fireEvent.click(screen.getByRole("button", { name: "返回工作台" }));
expect(screen.getByRole("heading", { name: "协作工作台" })).toBeTruthy();
act(() => {
window.history.pushState(null, "", "#/agents/deleted-agent");
window.dispatchEvent(new PopStateEvent("popstate"));
});
await waitFor(() => expect(window.location.hash).toBe("#/workspace"));
expect(screen.queryByLabelText("给 Agent 的任务")).toBeNull();
});