Repository navigation
Expand file tree
/
Copy pathqa-document-startup.mts
More file actions
482 lines (476 loc) · 15.7 KB
/
Copy pathqa-document-startup.mts
File metadata and controls
482 lines (476 loc) · 15.7 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
// Isolated accounts, in-memory database and temporary storage; never user documents.
import assert from "node:assert/strict";
import { randomBytes } from "node:crypto";
import { mkdtemp, mkdir, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { resolve, join } from "node:path";
import { createServer } from "vite";
import { chromium } from "playwright-core";
import { openDatabase } from "../packages/db/src/index.js";
import { createUser } from "../packages/core/src/modules/identity/passwords.js";
const root = await mkdtemp(join(tmpdir(), "doca-startup-qa-"));
process.env.DOCA_CREDENTIAL_MASTER_KEY = randomBytes(32).toString("hex");
process.env.DOCA_FILE_STORE_ID = "local";
process.env.DOCA_FILE_STORES_JSON = JSON.stringify({
version: 1,
stores: { local: { provider: "local", root } },
});
const { createApp } = await import("../apps/server/src/app/create-app.js");
const { storageRuntime } =
await import("../apps/server/src/adapters/storage.js");
const db = await openDatabase({ driver: "sqlite", path: ":memory:" });
const user = await createUser(
db,
{
login: "startupqa",
displayName: "Startup QA",
password: "isolated-startup-2026",
},
{ bootstrap: true },
);
const origin = "http://127.0.0.1:39379";
const app = await createApp(db, {
origin,
storage: { ...storageRuntime(), root },
ai: { memory: { driver: "sqlite", url: ":memory:" } },
});
await app.listen({ host: "127.0.0.1", port: 0 });
const apiAddress = app.server.address();
assert(apiAddress && typeof apiAddress !== "string");
const web = await createServer({
configFile: resolve("apps/web/vite.config.ts"),
server: {
host: "127.0.0.1",
port: 39379,
strictPort: true,
proxy: {
"/api": { target: `http://127.0.0.1:${apiAddress.port}`, ws: true },
},
},
});
await web.listen();
const address = web.httpServer!.address();
assert(address && typeof address !== "string");
const output = resolve("artifacts/verification/document-startup");
await mkdir(output, { recursive: true });
const browser = await chromium.launch({ channel: "chrome", headless: true });
const catalogContains = async ({
id,
expected,
}: {
id: string;
expected: boolean;
}) => {
const session = JSON.parse(
localStorage.getItem("doca.offline-session.v1") ?? "null",
);
if (!session) return !expected;
const db = await new Promise<IDBDatabase>((resolve) => {
const request = indexedDB.open("doca-offline-catalog-v1", 1);
request.onsuccess = () => resolve(request.result);
});
try {
if (!db.objectStoreNames.contains("documents")) return !expected;
return await new Promise<boolean>((resolve) => {
const request = db
.transaction("documents")
.objectStore("documents")
.get([session.bootstrap.user.id, id]);
request.onsuccess = () =>
resolve(
!!request.result?.document && request.result.scope === session.scope
? expected
: !expected,
);
});
} finally {
db.close();
}
};
let diagnosticPage: Awaited<ReturnType<typeof browser.newPage>> | undefined;
const checks: string[] = [];
const pass = (value: string) => {
checks.push(value);
console.log("PASS " + value);
};
try {
const context = await browser.newContext({
viewport: { width: 1440, height: 1000 },
});
const request = async (path: string, data?: unknown, method = "GET") => {
const response = await context.request.fetch(origin + "/api/v1" + path, {
method,
headers: { origin },
data,
});
assert.equal(response.status(), 200, await response.text());
return response.json();
};
await request(
"/auth/login",
{ login: "startupqa", password: "isolated-startup-2026" },
"POST",
);
const resource = await request(
"/resources",
{
title: "Cached startup regression",
kind: "document",
format: "rich_text",
},
"POST",
);
const page = await context.newPage();
diagnosticPage = page;
const duplicateKeyWarnings: string[] = [];
page.on("console", (message) => {
if (message.text().includes("Encountered two children with the same key"))
duplicateKeyWarnings.push(message.text());
});
const errors: string[] = [];
const updates: any[] = [];
let online = true,
denied = false,
bootstrapRequests = 0;
page.on("pageerror", (e) => {
errors.push(e.message);
console.error(e.stack);
});
page.on("request", (r) => {
if (new URL(r.url()).pathname === "/api/v1/bootstrap") bootstrapRequests++;
});
page.on("websocket", (ws) =>
ws.on("framesent", ({ payload }) => {
try {
const message = JSON.parse(String(payload));
if (message.type === "update") updates.push(message);
} catch {}
}),
);
await page.route("**/api/v1/**", (route) => {
if (!online) return route.abort();
if (
denied &&
new URL(route.request().url()).pathname ===
`/api/v1/resources/${resource.id}`
)
return route.fulfill({
status: 403,
contentType: "application/json",
body: JSON.stringify({ message: "Forbidden" }),
});
return route.continue();
});
await page.routeWebSocket("**/api/v1/ws", (ws) => {
if (online) ws.connectToServer();
else ws.close();
});
await page.goto(`${origin}/#/r/${resource.id}`);
const editor = () =>
page.locator('.document-editor-shell [contenteditable="true"]').first();
await editor().waitFor({ timeout: 60000 });
await editor().click();
await page.keyboard.press("ControlOrMeta+End");
await page.keyboard.press("Enter");
await page.keyboard.type("Original cached text.");
await page
.getByText("已保存到云端", { exact: true })
.waitFor({ state: "attached" });
await page.waitForFunction(catalogContains, {
id: resource.id,
expected: true,
});
assert.equal(
bootstrapRequests,
1,
"Plugin discovery and shell made duplicate bootstrap requests",
);
pass(
"Cold startup shares one bootstrap request and caches document metadata",
);
const replica = () =>
page.evaluate(
async ({ userId, resourceId }) => {
const db = await new Promise<IDBDatabase>((resolve, reject) => {
const request = indexedDB.open("doca-offline-v1", 1);
request.onsuccess = () => resolve(request.result);
request.onerror = () => reject(request.error);
});
const tx = db.transaction("replicas", "readonly");
const value = await new Promise<any>((resolve, reject) => {
const request = tx
.objectStore("replicas")
.get(`${userId}:${resourceId}:slate-kit:3`);
request.onsuccess = () => resolve(request.result);
request.onerror = () => reject(request.error);
});
db.close();
return {
pending: value.pending.map((p: any) => ({
id: p.id,
update: Array.from(p.update),
})),
checkpoint: Array.from(value.checkpoint),
};
},
{ userId: user.id, resourceId: resource.id },
);
// Keep HTTP requests pending, proving the cached editor appears before any response/timeout.
let release!: () => void;
const stalled = new Promise<void>((resolve) => {
release = resolve;
});
await page.route("**/api/v1/bootstrap", async (route) => {
await stalled;
await route.abort().catch(() => {});
});
await page.route(`**/api/v1/resources/${resource.id}`, async (route) => {
await stalled;
await route.abort().catch(() => {});
});
online = false;
const started = Date.now();
await page.reload();
await editor().waitFor({ timeout: 8000 });
assert((await editor().textContent())?.includes("Original cached text."));
const restoreMs = Date.now() - started;
await editor().click();
await page.keyboard.press("ControlOrMeta+End");
await page.keyboard.type(" Offline addition.");
await page.waitForFunction(() =>
document.body.textContent?.includes("已保存到本地"),
);
const pending = await replica();
assert(pending.pending.length > 0);
release();
await page.unroute("**/api/v1/bootstrap");
await page.unroute(`**/api/v1/resources/${resource.id}`);
pass(
`Cached content rendered while bootstrap/metadata hung (${restoreMs}ms), and offline edits persisted`,
);
await page.screenshot({ path: join(output, "cached-offline.png") });
await page.reload();
await editor().waitFor();
assert((await editor().textContent())?.includes("Offline addition."));
assert.deepEqual((await replica()).pending, pending.pending);
pass(
"Offline refresh retained text, original pending operation IDs and bytes",
);
const peer = await context.newPage();
await peer.goto(`${origin}/#/r/${resource.id}`);
const peerEditor = peer
.locator('.document-editor-shell [contenteditable="true"]')
.first();
await peerEditor.waitFor();
await peerEditor.click();
await peer.keyboard.press("ControlOrMeta+End");
await peer.keyboard.type(" Remote addition.");
await peer
.getByText("已保存到云端", { exact: true })
.waitFor({ state: "attached" });
await editor().click();
await page.evaluate(() => {
(window as any).startupEditor = document.querySelector(
'.document-editor-shell [contenteditable="true"]',
);
});
online = true;
await page
.getByText("已保存到云端", { exact: true })
.waitFor({ state: "attached" });
await page.waitForFunction(() =>
document
.querySelector('.document-editor-shell [contenteditable="true"]')
?.textContent?.includes("Remote addition."),
);
assert((await editor().textContent())?.includes("Offline addition."));
assert.equal((await replica()).pending.length, 0);
assert(
await page.evaluate(
() =>
(window as any).startupEditor ===
document.querySelector(
'.document-editor-shell [contenteditable="true"]',
),
),
);
assert(
await page.evaluate(() =>
document
.querySelector('.document-editor-shell [contenteditable="true"]')
?.contains(document.activeElement),
),
);
await peer.waitForFunction(() =>
document
.querySelector('.document-editor-shell [contenteditable="true"]')
?.textContent?.includes("Offline addition."),
);
pass(
"Reconnect merged local and remote edits, emptied only ACKed outbox and kept the same editor instance",
);
await page.screenshot({ path: join(output, "reconnected.png") });
const beforeIdle = updates.length;
const idleMs = process.env.DOCA_QA_STARTUP_QUICK === "1" ? 1000 : 60000;
await page.waitForTimeout(idleMs);
assert.equal(updates.length, beforeIdle);
pass(
`${idleMs / 1000} seconds of idle after reconnect produced zero content updates`,
);
const preserved = await replica();
denied = true;
await page.evaluate(() => window.dispatchEvent(new Event("online")));
await page.waitForFunction(catalogContains, {
id: resource.id,
expected: false,
});
await page.locator(".document-editor-shell").waitFor({ state: "detached" });
assert.deepEqual(await replica(), preserved);
pass(
"Explicit permission denial hid cached content and preserved the original durable replica",
);
for (const [format, selector] of [
["markdown", ".doca-markdown .exmd-editor"],
["spreadsheet", ".sheet-document .uos-office-toolbar"],
["canvas", ".doca-canvas canvas"],
["presentation", ".presentation-document .eppt-canvas-area"],
]) {
online = true;
const document = await request(
"/resources",
{
title: `Startup ${format}`,
kind: "document",
format,
...(format === "markdown"
? { markdown: "# Startup Markdown\n\nCached Markdown body." }
: {}),
},
"POST",
);
await page.goto(`${origin}/#/r/${document.id}`);
await page.locator(selector!).first().waitFor({ timeout: 60000 });
await page
.getByText(/已保存到云端/)
.first()
.waitFor({ state: "attached" });
await page.waitForFunction(catalogContains, {
id: document.id,
expected: true,
});
const hasReplica = await page.evaluate(
async ({ id, userId }) => {
const db = await new Promise<IDBDatabase>((resolve) => {
const request = indexedDB.open("doca-offline-v1", 1);
request.onsuccess = () => resolve(request.result);
});
const values = await new Promise<any[]>((resolve) => {
const tx = db.transaction("replicas", "readonly"),
store = tx.objectStore("replicas");
const keys = store.getAllKeys();
keys.onsuccess = () => {
const key = keys.result.find((key) =>
String(key).startsWith(`${userId}:${id}:`),
);
if (!key) {
resolve([]);
return;
}
const item = store.get(key);
item.onsuccess = () => resolve([item.result]);
};
});
db.close();
return !!values[0]?.epochId;
},
{ id: document.id, userId: user.id },
);
assert(hasReplica, `${format} did not persist its authoritative replica`);
const cachedTitle = await page
.locator(".document-name-value")
.textContent();
online = false;
await page.reload();
await page.locator(selector!).first().waitFor({ timeout: 8000 });
assert.equal(
await page.locator(".document-name-value").textContent(),
cachedTitle,
);
if (format === "markdown")
assert(
(
await page
.locator(".doca-markdown.markdown-sdk-container")
.textContent()
)?.includes("Cached Markdown body."),
);
await page.screenshot({ path: join(output, `offline-${format}.png`) });
pass(
`${format} restored its existing replica/baseline with the backend unavailable`,
);
}
online = true;
await request("/auth/logout", {}, "POST");
await peer.reload();
await page.waitForFunction(
() => !localStorage.getItem("doca.offline-session.v1"),
);
await page.locator(".eppt-canvas-area").waitFor({ state: "detached" });
assert.deepEqual(await replica(), preserved);
pass(
"Logout in another tab invalidated the cached identity without deleting document checkpoints",
);
assert.deepEqual(errors, []);
assert.deepEqual(duplicateKeyWarnings, []);
await writeFile(
join(output, "report.json"),
JSON.stringify({ checks, restoreMs, errors }, null, 2),
);
} catch (error) {
if (diagnosticPage) {
await diagnosticPage.screenshot({ path: join(output, "failure.png") });
await writeFile(
join(output, "failure.txt"),
await diagnosticPage.locator("body").innerText(),
);
await writeFile(
join(output, "failure-cache.json"),
await diagnosticPage.evaluate(async () => {
const db = await new Promise<IDBDatabase>((resolve) => {
const request = indexedDB.open("doca-offline-v1", 1);
request.onsuccess = () => resolve(request.result);
});
const values = await new Promise<any[]>((resolve) => {
const request = db
.transaction("replicas", "readonly")
.objectStore("replicas")
.getAll();
request.onsuccess = () => resolve(request.result);
});
db.close();
return JSON.stringify(
{
startup: JSON.parse(
localStorage.getItem("doca.offline-session.v1") ?? "null",
),
replicas: values.map((value) => ({
epochId: value.epochId,
baseline: value.baseline,
checkpointBytes: value.checkpoint.length,
pending: value.pending.length,
})),
},
null,
2,
);
}),
);
}
throw error;
} finally {
await browser.close();
await web.close();
await app.close();
await db.destroy();
await rm(root, { recursive: true, force: true });
}