Repository navigation
Expand file tree
/
Copy pathqa-browser-clipper-browser.mjs
More file actions
512 lines (511 loc) · 17 KB
/
Copy pathqa-browser-clipper-browser.mjs
File metadata and controls
512 lines (511 loc) · 17 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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
import assert from "node:assert/strict";
import http from "node:http";
import { writeFile } from "node:fs/promises";
import { chromium } from "/app/node_modules/playwright-core/index.mjs";
// Forward container localhost to the synthetic fixture on the host, preserving configured Host/Origin.
const proxies = [];
for (const port of [39474, 39475]) {
const proxy = http.createServer((req, res) => {
const upstream = http.request(
{
hostname: "host.docker.internal",
port,
method: req.method,
path: req.url,
headers: req.headers,
},
(response) => {
res.writeHead(response.statusCode, response.headers);
response.pipe(res);
},
);
upstream.on("error", (error) => {
res.writeHead(502);
res.end(error.message);
});
req.pipe(upstream);
});
proxy.on("upgrade", (req, socket, head) => {
const upstream = http.request({
hostname: "host.docker.internal",
port,
method: req.method,
path: req.url,
headers: req.headers,
});
upstream.on("upgrade", (response, remote, remoteHead) => {
socket.write(
`HTTP/1.1 ${response.statusCode} ${response.statusMessage}\r\n` +
Object.entries(response.headers)
.map(([key, value]) => `${key}: ${value}\r\n`)
.join("") +
"\r\n",
);
if (head.length) remote.write(head);
if (remoteHead.length) socket.write(remoteHead);
socket.pipe(remote);
remote.pipe(socket);
socket.on("error", () => remote.destroy());
remote.on("error", () => socket.destroy());
});
upstream.on("error", () => socket.destroy());
upstream.end();
});
await new Promise((resolve) => proxy.listen(port, "127.0.0.1", resolve));
proxies.push(proxy);
}
const context = await chromium.launchPersistentContext("/tmp/doca-browser-qa", {
executablePath: "/usr/bin/chromium",
headless: true,
args: [
"--no-sandbox",
"--disable-extensions-except=/tmp/doca-extension",
"--load-extension=/tmp/doca-extension",
],
});
const errors = [];
context.on("page", (page) =>
page.on("pageerror", (error) => errors.push(error.message)),
);
try {
const worker =
context.serviceWorkers()[0] ||
(await context.waitForEvent("serviceworker", { timeout: 15000 }));
const extensionId = worker.url().split("/")[2];
const source = await context.newPage();
await source.goto("http://localhost:39475/");
await source.waitForSelector("#article img");
await source.evaluate(() =>
Promise.all([...document.images].map((image) => image.decode())),
);
const ui = await context.newPage();
await ui.setViewportSize({ width: 370, height: 650 });
await ui.goto(`chrome-extension://${extensionId}/ui.html`);
async function loginPage(login = "clipperqa") {
let page = context
.pages()
.find((page) => page.url().includes("/#/browser-connect?"));
if (!page)
page = await context.waitForEvent("page", {
predicate: (page) => page.url().includes("/#/browser-connect?"),
timeout: 15000,
});
await page.locator('input[name="login"]').fill(login);
await page
.locator('input[name="password"]')
.fill("isolated-browser-qa-password");
await page.locator("form .primary").last().click();
await page.waitForEvent("close", { timeout: 15000 });
}
await loginPage();
try {
await ui.waitForSelector("#capture-pane:not([hidden])", { timeout: 15000 });
} catch (error) {
await ui.screenshot({
path: "/artifacts/login-failure.png",
fullPage: true,
});
console.log("LOGIN_BODY", await ui.locator("body").innerText());
throw error;
}
assert((await ui.locator("#identity").innerText()).includes("验收账户"));
await ui.screenshot({
path: "/artifacts/extension-capture.png",
fullPage: true,
});
const state = await ui.evaluate(
async () => await chrome.runtime.sendMessage({ type: "state" }),
);
assert(state.ok && state.value.connected);
const sourceTab = await ui.evaluate(
async () =>
(await chrome.tabs.query({ url: "http://localhost:39475/*" }))[0],
);
assert(sourceTab.id);
// A trusted mouse release exposes the actual closed-shadow selection toolbar.
await source.bringToFront();
const point = await source.locator("#article").boundingBox();
await source.evaluate(() => {
const range = document.createRange();
range.selectNodeContents(document.getElementById("article"));
const selection = getSelection();
selection.removeAllRanges();
selection.addRange(range);
});
await source.mouse.move(point.x + 30, point.y + 35);
await source.mouse.up();
await source.waitForSelector("[data-doca-capture-ui]");
const cdp = await context.newCDPSession(source);
async function toolbarTree() {
return (await cdp.send("DOM.getDocument", { depth: -1, pierce: true }))
.root;
}
function find(node, predicate) {
if (predicate(node)) return node;
for (const child of [
...(node.children || []),
...(node.shadowRoots || []),
]) {
const result = find(child, predicate);
if (result) return result;
}
return null;
}
const textOf = (node) =>
(node.nodeValue || "") +
[...(node.children || []), ...(node.shadowRoots || [])]
.map(textOf)
.join("");
const deadline = Date.now() + 12000;
let button;
while (Date.now() < deadline) {
const tree = await toolbarTree();
button = find(
tree,
(node) =>
node.nodeName === "BUTTON" && textOf(node).includes("收录到 Doca"),
);
if (button && !button.attributes.includes("disabled")) break;
await new Promise((resolve) => setTimeout(resolve, 100));
}
assert(button, "selection capture button");
const box = (await cdp.send("DOM.getBoxModel", { nodeId: button.nodeId }))
.model.border;
await source.mouse.click((box[0] + box[4]) / 2, (box[1] + box[5]) / 2);
let saved = false;
for (let attempt = 0; attempt < 100; attempt++) {
if (textOf(await toolbarTree()).includes("已保存,打开文档")) {
saved = true;
break;
}
await new Promise((resolve) => setTimeout(resolve, 150));
}
assert(saved, "selected content and image saved from the actual toolbar");
await source.screenshot({
path: "/artifacts/selection-toolbar.png",
fullPage: true,
});
const whole = await ui.evaluate(
async ({ tabId, profileId }) =>
await chrome.runtime.sendMessage({
type: "capture",
mode: "page",
tabId,
profileId,
profileRevision: 1,
}),
{ tabId: sourceTab.id, profileId: process.env.DOCA_QA_PROFILE },
);
assert(whole.ok, JSON.stringify(whole));
const settings = await context.newPage();
await settings.setViewportSize({ width: 1440, height: 1000 });
await settings.goto("http://localhost:39474/#/browser-assistant");
await settings
.getByRole("heading", { name: "浏览器助手", exact: true })
.waitFor();
await settings.getByText("故障案例", { exact: true }).waitFor();
await settings.evaluate(() =>
Promise.all(
document
.getAnimations()
.map((animation) => animation.finished.catch(() => {})),
),
);
await settings.evaluate(() =>
Promise.all(
document
.getAnimations()
.map((animation) => animation.finished.catch(() => {})),
),
);
await settings.screenshot({
path: "/artifacts/doca-profiles.png",
fullPage: true,
});
const firstReplica = await context.newPage(),
secondReplica = await context.newPage();
await firstReplica.goto(
`http://localhost:39474/#/r/${process.env.DOCA_QA_TARGET}`,
);
await secondReplica.goto(
`http://localhost:39474/#/r/${process.env.DOCA_QA_TARGET}`,
);
async function waitBody(page, text) {
try {
await page.waitForFunction(
(text) => document.body.innerText.includes(text),
text,
{ timeout: 15000 },
);
} catch (error) {
await page.screenshot({
path: "/artifacts/document-failure.png",
fullPage: true,
});
console.log(
"DOCUMENT_BODY",
(await page.locator("body").innerText()).slice(-10000),
);
throw error;
}
}
await waitBody(firstReplica, "原有排查内容");
await waitBody(secondReplica, "原有排查内容");
const appended = await ui.evaluate(
async ({ tabId, profileId }) =>
await chrome.runtime.sendMessage({
type: "capture",
mode: "page",
tabId,
profileId,
profileRevision: 1,
}),
{ tabId: sourceTab.id, profileId: process.env.DOCA_QA_APPEND },
);
assert(appended.ok, JSON.stringify(appended));
await waitBody(firstReplica, "接线端子松动");
await waitBody(secondReplica, "接线端子松动");
await firstReplica.screenshot({
path: "/artifacts/append-live-document.png",
fullPage: true,
});
await ui.goto(`chrome-extension://${extensionId}/ui.html#assistant`);
await ui.reload();
await ui.setViewportSize({ width: 410, height: 850 });
await ui.waitForSelector("#assistant-pane:not([hidden])");
await ui.selectOption("#ai-profile", process.env.DOCA_QA_PROFILE);
await ui.waitForFunction(
() => document.querySelector("#model").options.length > 0,
);
await ui.fill("#prompt", "请介绍故障排查的步骤,只在对话里回答。");
await ui.click("#send");
try {
await ui.waitForFunction(
() =>
document
.querySelector("#conversation")
.innerText.includes("这是模拟 AI 的回答,用于验证会话、页面和积分。"),
null,
{ timeout: 20000 },
);
} catch (error) {
await ui.screenshot({
path: "/artifacts/assistant-failure.png",
fullPage: true,
});
console.log("ASSISTANT_BODY", await ui.locator("body").innerText());
throw error;
}
await ui.screenshot({
path: "/artifacts/extension-assistant.png",
fullPage: true,
});
await settings.getByRole("tab", { name: "浏览器连接", exact: true }).click();
await settings.getByText("Doca Browser", { exact: true }).waitFor();
await settings.evaluate(() =>
Promise.all(
document
.getAnimations()
.map((animation) => animation.finished.catch(() => {})),
),
);
await settings.screenshot({
path: "/artifacts/doca-connections.png",
fullPage: true,
});
await settings.setViewportSize({ width: 720, height: 960 });
await settings.evaluate(() =>
Promise.all(
document
.getAnimations()
.map((animation) => animation.finished.catch(() => {})),
),
);
await settings.screenshot({
path: "/artifacts/doca-connections-narrow.png",
fullPage: true,
});
// The public site config contains no account credential. An invalid config keeps the site.
const replacement = JSON.stringify({
version: 2,
origin: "http://localhost:39474",
});
await ui.click("[data-tab=connection]");
await ui.locator("#advanced-config summary").click();
await ui.fill("#config", '{"version":1}');
await ui.locator("#configure button[type=submit]").click();
await ui
.getByText("配置格式无效或版本不支持,原站点配置已保留。", { exact: true })
.waitFor();
const retained = await ui.evaluate(
async () => await chrome.runtime.sendMessage({ type: "state" }),
);
assert(retained.value.connected);
await ui.fill("#config", replacement);
await ui.locator("#configure button[type=submit]").click();
await ui.getByText("已连接", { exact: true }).waitFor();
await worker.evaluate(async () => {
const auth = (await chrome.storage.session.get("authorization"))
.authorization;
globalThis.qaPreviousConnection = auth;
await chrome.storage.session.set({
authorization: { ...auth, expiresAt: "2000-01-01T00:00:00Z" },
});
});
const renewed = await ui.evaluate(
async () =>
await chrome.runtime.sendMessage({ type: "state", interactive: true }),
);
assert(
renewed.value.connected,
"Short token renews through the signed-in website without prompting",
);
const oldStatus = await worker.evaluate(async () => {
const previous = globalThis.qaPreviousConnection;
const response = await fetch(
previous.origin + "/api/v1/browser-clipper/me",
{
headers: { Authorization: "Bearer " + previous.token },
credentials: "omit",
},
);
delete globalThis.qaPreviousConnection;
return response.status;
});
assert.equal(oldStatus, 401);
const logout = await settings.evaluate(
async () =>
await fetch("/api/v1/auth/logout", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: "{}",
}).then((response) => response.status),
);
assert.equal(logout, 200);
const loggedOut = await ui.evaluate(
async () => await chrome.runtime.sendMessage({ type: "state" }),
);
assert(!loggedOut.value.connected);
assert.equal(loggedOut.value.error, "clipper_login_required");
const reconnect = ui.evaluate(
async () => await chrome.runtime.sendMessage({ type: "login" }),
);
await loginPage();
assert((await reconnect).value.connected);
const ownerState = (
await ui.evaluate(
async () => await chrome.runtime.sendMessage({ type: "state" }),
)
).value;
await worker.evaluate(
async (owner) =>
chrome.storage.session.set({
pending: {
origin: owner.origin,
connectionId: owner.connectionId,
requestId: "unsent-owner-material",
},
}),
ownerState,
);
await settings.evaluate(async () =>
fetch("/api/v1/auth/logout", { method: "POST" }),
);
const secondLogin = ui.evaluate(
async () => await chrome.runtime.sendMessage({ type: "login" }),
);
await loginPage("clipperqasecond");
const second = (await secondLogin).value;
assert(second.connected && second.user.name === "第二个验收账户");
assert(!second.pending);
assert.notEqual(second.connectionId, ownerState.connectionId);
await ui.reload();
await ui.getByText("第二个验收账户", { exact: true }).waitFor();
await settings.evaluate(async () =>
fetch("/api/v1/auth/logout", { method: "POST" }),
);
const ownerLogin = ui.evaluate(
async () => await chrome.runtime.sendMessage({ type: "login" }),
);
await loginPage();
const restored = (await ownerLogin).value;
assert(restored.connected && restored.pending);
assert.equal(restored.connectionId, ownerState.connectionId);
await worker.evaluate(async () => chrome.storage.session.remove("pending"));
await ui.reload();
await ui.click("[data-tab=connection]");
await ui.fill("#endpoint", "http://localhost:39474/api/v1");
await ui.locator("#endpoint-form button").click();
await ui
.getByText("请输入 HTTPS 的 Doca 根地址,本机开发可用 HTTP。", {
exact: true,
})
.waitFor();
await ui.fill("#endpoint", "http://localhost:39474/");
await ui.locator("#endpoint-form button").click();
await ui.getByText("已连接", { exact: true }).waitFor();
await ui.screenshot({
path: "/artifacts/extension-connection.png",
fullPage: true,
});
await settings.reload();
await settings.getByRole("tab", { name: "浏览器连接", exact: true }).click();
await settings
.getByRole("button", { name: "撤销全部连接", exact: true })
.click();
await settings.getByRole("button", { name: "撤销授权", exact: true }).click();
await settings.getByText("浏览器授权已撤销", { exact: true }).waitFor();
const revoked = await ui.evaluate(
async () => await chrome.runtime.sendMessage({ type: "state" }),
);
assert(revoked.ok && !revoked.value.connected);
assert.equal(revoked.value.error, "clipper_connection_revoked");
const rejected = await ui.evaluate(
async ({ tabId, profileId }) =>
await chrome.runtime.sendMessage({
type: "capture",
mode: "page",
tabId,
profileId,
profileRevision: 1,
}),
{ tabId: sourceTab.id, profileId: process.env.DOCA_QA_PROFILE },
);
assert(!rejected.ok);
assert.equal(rejected.error, "clipper_connection_revoked");
await firstReplica.reload();
await waitBody(firstReplica, "接线端子松动");
const report = {
status: "passed",
browserVersion: context.browser()?.version(),
followsWebsiteLogin: true,
loginPromptAndReturn: true,
websiteLogoutDisconnects: true,
accountSwitchIsolatesDrafts: true,
editableServerAddress: true,
automaticShortTokenRenewal: true,
selectionToolbar: true,
wholePage: true,
originalImageTransfer: true,
appendBroadcastToTwoReplicas: true,
assistantConversation: true,
publicSiteConfiguration: true,
invalidConfigPreservesConnection: true,
keyRotationRejectsOldToken: true,
revokeAllThroughSettings: true,
oldTokenRejected: true,
savedDocumentRetained: true,
pageErrors: errors,
};
await writeFile(
"/artifacts/browser-report.json",
JSON.stringify(report, null, 2),
);
assert.deepEqual(errors, []);
console.log(JSON.stringify(report));
} finally {
await context.close();
await Promise.all(
proxies.map((proxy) => new Promise((resolve) => proxy.close(resolve))),
);
}