Repository navigation
Expand file tree
/
Copy pathProjects.test.tsx
More file actions
208 lines (201 loc) · 7.76 KB
/
Copy pathProjects.test.tsx
File metadata and controls
208 lines (201 loc) · 7.76 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import { afterEach, expect, it, vi } from "vitest";
import {
act,
cleanup,
fireEvent,
render,
screen,
within,
} from "@testing-library/react";
import App from "./App";
import type { DockState } from "./types";
const state: DockState = {
runtime: { enabled: false, version: "0.3.0" },
projects: ["Alpha", "Beta"].map((name) => ({
id: name.toLowerCase(),
name,
path: `/private/${name}`,
})),
agents: ["Alpha", "Beta", "Independent"].map((name) => ({
id: name,
name: `${name} agent`,
project_id: name === "Independent" ? null : name.toLowerCase(),
provider: "codex",
role: "",
})),
sessions: [
{
id: "s",
title: "Alpha conversation",
agent_id: "Alpha",
project_id: "alpha",
status: "idle",
created_at: "",
updated_at: "",
},
],
memories: ["alpha", "beta"].map((id) => ({
id,
project_id: id,
key: `${id}.rules`,
content: `${id} conventions`,
version: 1,
})),
proposals: [
{
id: "p",
project_id: "beta",
agent_id: "Beta",
key: "extra",
content: "Proposed beta rule",
expected_version: 0,
status: "pending",
created_at: "",
},
],
messages: [],
events: [],
approvals: [],
quotas: [],
subscriptions: [],
};
afterEach(() => {
cleanup();
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) => ({
ok: true,
json: async () =>
path === "/api/state"
? state
: path.includes("/events?")
? { events: [] }
: { models: [] },
}));
vi.stubGlobal("fetch", fetchMock);
window.__AGENTDOCK_DESKTOP_TOKEN__ = "projects-fixture";
render(<App />);
await screen.findByRole("navigation", { name: "主导航" });
return fetchMock;
}
const mainNav = () =>
within(screen.getByRole("navigation", { name: "主导航" }));
const projectNav = () =>
within(screen.getByRole("navigation", { name: "项目导航" }));
const openAlpha = () => {
fireEvent.click(mainNav().getByRole("button", { name: "项目" }));
fireEvent.click(screen.getByRole("button", { name: "打开项目 Alpha" }));
};
it("groups projects in one entry and keeps independent agents on the workspace", async () => {
const fetchMock = await setup();
expect(mainNav().queryByRole("button", { name: "任务派工" })).toBeNull();
expect(mainNav().queryByRole("button", { name: "共享记忆" })).toBeNull();
expect(screen.queryByLabelText("当前项目")).toBeNull();
expect(
screen.getByRole("button", { name: /^Independent agent/ }),
).toBeTruthy();
fireEvent.click(mainNav().getByRole("button", { name: "项目" }));
expect(screen.getAllByRole("button", { name: /^打开项目 / })).toHaveLength(2);
expect(screen.queryByText("/private/Alpha")).toBeNull();
expect(screen.queryByRole("navigation", { name: "项目导航" })).toBeNull();
fireEvent.click(screen.getByRole("button", { name: "打开项目 Alpha" }));
expect(screen.getByRole("heading", { level: 1, name: "Alpha" })).toBeTruthy();
expect(projectNav().getAllByRole("button")).toHaveLength(3);
expect(screen.queryByRole("button", { name: /^Beta agent/ })).toBeNull();
expect(
screen.queryByRole("button", { name: /^Independent agent/ }),
).toBeNull();
expect(fetchMock.mock.calls.some(([path]) => path.includes("/events?"))).toBe(
false,
);
fireEvent.click(mainNav().getByRole("button", { name: "协作工作台" }));
expect(window.location.hash).toBe("#/workspace");
expect(
screen.getByRole("button", { name: /^Independent agent/ }),
).toBeTruthy();
});
it("switches project memories, proposals and task recipients without crossing scopes", async () => {
await setup();
openAlpha();
fireEvent.click(projectNav().getByRole("button", { name: "记忆" }));
expect(screen.getByText("alpha conventions")).toBeTruthy();
expect(screen.queryByText("beta conventions")).toBeNull();
expect(screen.queryByText("Proposed beta rule")).toBeNull();
fireEvent.change(screen.getByLabelText("切换项目"), {
target: { value: "beta" },
});
expect(window.location.hash).toBe("#/projects?project=beta&view=memory");
expect(screen.getByText("beta conventions")).toBeTruthy();
expect(screen.getByText("Proposed beta rule")).toBeTruthy();
expect(screen.queryByText("alpha conventions")).toBeNull();
fireEvent.click(projectNav().getByRole("button", { name: "任务" }));
fireEvent.click(screen.getByRole("button", { name: "新建任务" }));
expect(screen.getByRole("option", { name: "Beta agent" })).toBeTruthy();
expect(screen.queryByRole("option", { name: "Alpha agent" })).toBeNull();
fireEvent.change(screen.getByLabelText("切换项目"), {
target: { value: "alpha" },
});
fireEvent.click(screen.getByRole("button", { name: "新建任务" }));
expect(screen.getByRole("option", { name: "Alpha agent" })).toBeTruthy();
expect(screen.queryByRole("option", { name: "Beta agent" })).toBeNull();
});
it("returns from an agent to its project and restores project tabs through browser history", async () => {
await setup();
openAlpha();
fireEvent.click(projectNav().getByRole("button", { name: "Agent" }));
fireEvent.click(
screen.getByRole("button", { name: /^Alpha agent.*进入会话/ }),
);
expect(window.location.hash).toBe("#/agents/Alpha?project=alpha");
fireEvent.change(screen.getByLabelText("给 Agent 的任务"), {
target: { value: "Project draft" },
});
fireEvent.click(screen.getByRole("button", { name: "返回 Agent 列表" }));
expect(window.location.hash).toBe("#/projects?project=alpha&view=agents");
fireEvent.click(
screen.getByRole("button", { name: /^Alpha agent.*进入会话/ }),
);
expect(
(screen.getByLabelText("给 Agent 的任务") as HTMLTextAreaElement).value,
).toBe("Project draft");
fireEvent.click(screen.getByRole("button", { name: "返回 Agent 列表" }));
fireEvent.click(projectNav().getByRole("button", { name: "任务" }));
fireEvent.click(projectNav().getByRole("button", { name: "记忆" }));
act(() => window.history.back());
await screen.findByRole("heading", { name: "任务" });
act(() => window.history.forward());
await screen.findByRole("heading", { name: "项目记忆" });
expect(screen.getByText("alpha conventions")).toBeTruthy();
});
it.each([
["#/projects?project=alpha&view=tasks", "任务"],
["#/projects?project=alpha&view=memory", "项目记忆"],
["#/messages?project=alpha", "任务"],
["#/memory?project=alpha", "项目记忆"],
["#/workspace?project=alpha", "Agent"],
["#/agents/Alpha?project=alpha", "Alpha agent"],
])("restores current and legacy project links: %s", async (hash, heading) => {
await setup(hash);
expect(screen.getByRole("heading", { name: heading })).toBeTruthy();
expect(
mainNav()
.getByRole("button", { name: "项目" })
.getAttribute("aria-current"),
).toBe("page");
});
it("returns a removed project to the list and translates project navigation", async () => {
await setup("#/projects?project=removed&view=memory");
await screen.findByRole("button", { name: "打开项目 Alpha" });
expect(screen.queryByRole("heading", { name: "项目记忆" })).toBeNull();
fireEvent.click(screen.getByRole("button", { name: "Switch to English" }));
expect(screen.getByRole("heading", { name: "Projects" })).toBeTruthy();
fireEvent.click(screen.getByRole("button", { name: "Open project Alpha" }));
fireEvent.click(screen.getByRole("button", { name: "Memory" }));
expect(screen.getByRole("heading", { name: "Project memory" })).toBeTruthy();
fireEvent.click(screen.getByRole("button", { name: "Back to projects" }));
expect(screen.getByRole("heading", { name: "Projects" })).toBeTruthy();
});