Repository navigation
Expand file tree
/
Copy pathqa-ai-upload-ui.cjs
More file actions
248 lines (247 loc) · 10.2 KB
/
Copy pathqa-ai-upload-ui.cjs
File metadata and controls
248 lines (247 loc) · 10.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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
if (process.env.DOCA_QA_ISOLATED !== "1")
throw Error("Isolated upload QA only: set DOCA_QA_ISOLATED=1");
const { chromium } = require(process.env.PLAYWRIGHT_MODULE || "playwright");
const assert = require("node:assert/strict");
const fs = require("node:fs/promises"), path = require("node:path"), os = require("node:os");
const info = require("../.cache/qa-ai-upload-fixture.json");
assert.equal(info.origin, "http://127.0.0.1:39351");
(async () => {
const browser = await chromium.launch({ channel: "chrome", headless: true });
const context = await browser.newContext({
viewport: { width: 1400, height: 1050 },
});
const localFolder = await fs.mkdtemp(path.join(os.tmpdir(), "doca-local-folder-qa-"));
await fs.mkdir(path.join(localFolder, "人物参考"));
await fs.writeFile(path.join(localFolder, "book.txt"), "LOCAL_BOOK_MARKER");
await fs.writeFile(path.join(localFolder, "人物参考", "mom.txt"), "LOCAL_MOM_MARKER");
const res = await context.request.post(info.origin + "/api/v1/auth/login", {
headers: { origin: info.origin },
data: { login: "uploadqa", password: "isolated-upload-qa-2026" },
});
assert.equal(res.status(), 200, await res.text());
const page = await context.newPage();
const errors = [];
page.on("pageerror", (e) => errors.push(e.message));
const input = page.locator('.ai-composer [contenteditable="true"]');
const go = async (id) => {
await page.goto(info.origin + "/#/ai?session=" + id);
await input.waitFor();
};
const expectText = async (text) => {
await page.waitForFunction(
(text) =>
document.querySelector('.ai-composer [contenteditable="true"]')
?.textContent === text,
text,
);
};
try {
await go(info.sessions[0]);
await input.fill("草稿 A:妈妈讲故事,保留书页构图");
await page.reload();
await input.waitFor();
await expectText("草稿 A:妈妈讲故事,保留书页构图");
await go(info.sessions[1]);
await expectText("");
await input.fill("草稿 B:稍后继续");
await go(info.sessions[0]);
await expectText("草稿 A:妈妈讲故事,保留书页构图");
console.log("PASS drafts survive reload and session switching");
await page
.getByRole("button", { name: "上传文件或图片", exact: true })
.click();
assert.equal(await page.locator(".file-source-choice").count(), 2);
assert.equal(await page.locator(".file-source-extras button").count(), 1);
await page.screenshot({ path: "/tmp/doca-ai-upload-sources.png" });
await page
.locator(".file-source-choice")
.first()
.locator("button")
.first()
.click();
let picker = page.locator(".folder-file-picker");
await picker.waitFor();
await picker.getByRole("button", { name: /sample-one.txt/ }).click();
await picker.getByRole("button", { name: /sample-two.txt/ }).click();
await picker.locator("footer button.primary").click();
await page
.waitForFunction(() =>
document
.querySelector(".ai-upload-list")
?.textContent.includes("sample-two"),
)
.catch(async () => {
assert.ok(
(await page.locator(".ai-upload-list").innerText()).includes(
"sample-one",
),
);
assert.ok(
(await page.locator(".ai-upload-list").innerText()).includes(
"sample-two",
),
);
});
console.log("PASS two main sources and multiple stored file selection");
await page
.getByRole("button", { name: "上传文件或图片", exact: true })
.click();
await page
.locator(".file-source-choice")
.first()
.locator("button")
.first()
.click();
picker = page.locator(".folder-file-picker");
await picker.waitFor();
await picker.getByRole("button", { name: /故事书素材/ }).click();
await picker.locator("footer button.primary").click();
const folder = page.locator(".ai-folder-preview-trigger");
await folder.waitFor();
await folder.click();
await page
.locator(".ant-modal")
.getByText("book-pages.txt", { exact: true })
.waitFor();
await page
.locator(".ant-modal")
.getByRole("button", { name: "妈妈六视图", exact: true })
.click();
await page
.locator(".ant-modal")
.getByText("mom.txt", { exact: true })
.waitFor();
await page.locator(".ant-modal-close").click();
await page.reload();
await input.waitFor();
await expectText("草稿 A:妈妈讲故事,保留书页构图");
assert.ok(
(await page.locator(".ai-upload-list").innerText()).includes(
"sample-two",
),
);
assert.equal(await folder.innerText(), "故事书素材");
console.log(
"PASS folder preview, nested files and uploaded attachment/folder draft restoration",
);
await page.screenshot({ path: "/tmp/doca-ai-folder-draft.png" });
// Failed sends preserve the whole draft.
await page.route("**/api/v1/ai/sessions/*/messages", (route) =>
route.fulfill({
status: 503,
contentType: "application/json",
body: JSON.stringify({ message: "Injected send failure" }),
}),
);
await page.getByRole("button", { name: "发送", exact: true }).click();
await page.getByText("Injected send failure", { exact: true }).first().waitFor();
await page.reload();
await input.waitFor();
await expectText("草稿 A:妈妈讲故事,保留书页构图");
await page.unroute("**/api/v1/ai/sessions/*/messages");
assert.equal(await folder.count(), 1);
console.log("PASS failed send retains draft");
let sentResponse;
const responseHeld = new Promise(resolve => { sentResponse = resolve; });
await page.route("**/api/v1/ai/sessions/*/messages", async route => {
const response = await route.fetch();
await new Promise(resolve => setTimeout(resolve, 1500));
await route.fulfill({ response });
sentResponse();
});
await page.getByRole("button", { name: "发送", exact: true }).click();
await page.evaluate(id => { location.hash = "/ai?session=" + id; }, info.sessions[1]);
await expectText("草稿 B:稍后继续");
await responseHeld;
await expectText("草稿 B:稍后继续");
await page.unroute("**/api/v1/ai/sessions/*/messages");
await go(info.sessions[0]);
await expectText("");
console.log("PASS an in-flight send clears its own draft after switching sessions");
await page.locator(".ai-sent-target-folder").last().waitFor();
await page.locator(".ai-sent-target-folder").last().click();
await page
.locator(".ant-modal")
.getByText("book-pages.txt", { exact: true })
.waitFor();
await page.locator(".ant-modal-close").click();
console.log("PASS sent folder reference opens its contents");
await page.screenshot({ path: "/tmp/doca-ai-sent-folder.png" });
await go(info.sessions[1]);
await expectText("草稿 B:稍后继续");
await page
.getByRole("button", { name: "上传文件或图片", exact: true })
.click();
const chooseFiles = page.waitForEvent("filechooser");
await page
.locator(".file-source-choice")
.nth(1)
.locator("button")
.first()
.click();
await (
await chooseFiles
).setFiles(
Array.from({ length: 12 }, (_, i) => ({
name: `view-${i + 1}.txt`,
mimeType: "text/plain",
buffer: Buffer.from(`VIEW_${i + 1}`),
})),
);
await page.getByText("view-12.txt", {exact:true}).waitFor();
await page.evaluate(id => { location.hash = "/ai?session=" + id; }, info.sessions[0]);
await expectText("");
await page.evaluate(id => { location.hash = "/ai?session=" + id; }, info.sessions[1]);
await expectText("草稿 B:稍后继续");
await page.getByText("view-12.txt", {exact:true}).waitFor();
console.log("PASS selected local files stay in memory across session switching");
await page.reload();
await input.waitFor();
await expectText("草稿 B:稍后继续");
assert.equal(await page.getByText("view-12.txt", {exact:true}).count(), 0);
console.log(
"PASS reload retains text while local files are never uploaded before sending",
);
await page.getByRole("button", {name:"上传文件或图片",exact:true}).click();
const chooseFolder = page.waitForEvent("filechooser");
await page.locator(".file-source-choice").nth(1).locator(".file-source-subaction").click();
await (await chooseFolder).setFiles(localFolder);
const localReference = page.locator(".ai-folder-preview-trigger").filter({hasText:path.basename(localFolder)});
await localReference.waitFor({timeout:30000});
await localReference.click();
await page.locator(".ant-modal").getByText("book.txt",{exact:true}).waitFor();
await page.locator(".ant-modal").getByRole("button",{name:"人物参考",exact:true}).click();
await page.locator(".ant-modal").getByText("mom.txt",{exact:true}).waitFor();
await page.locator(".ant-modal-close").click();
await page.reload();await input.waitFor();await localReference.waitFor();
console.log("PASS local folder selection preserves hierarchy and a clickable draft reference after reload");
// Closing and reopening a page retains unsent text in the same browser profile.
await page.close();
const reopened = await context.newPage();
reopened.on("pageerror", (e) => errors.push(e.message));
await reopened.goto(info.origin + "/#/ai?session=" + info.sessions[1]);
await reopened.locator('.ai-composer [contenteditable="true"]').waitFor();
assert.equal(
await reopened
.locator('.ai-composer [contenteditable="true"]')
.textContent(),
"草稿 B:稍后继续",
);
assert.deepEqual(errors, []);
console.log(
"PASS successful send clears only its own draft; closing/reopening restores the other session",
);
} catch (e) {
if (!page.isClosed()) {
await page.screenshot({ path: "/tmp/doca-ai-ui-failure.png" });
console.log((await page.locator("body").innerText()).slice(-2000));
}
throw e;
} finally {
await browser.close();
await fs.rm(localFolder,{recursive:true,force:true});
}
})().catch((e) => {
console.error(e);
process.exitCode = 1;
});