返回 DeepSeek-Reasonix
bridge-breadcrumbs.test.ts
根目录 / desktop / frontend / src / __tests__ / bridge-breadcrumbs.test.ts
1 // Run: tsx src/__tests__/bridge-breadcrumbs.test.ts
2
3 import { snapshotBreadcrumbs } from "../lib/breadcrumbs";
4 import { app } from "../lib/bridge";
5 import { installDesktopHostStub } from "./desktopHostStub";
6
7 let passed = 0;
8 let failed = 0;
9
10 function ok(value: boolean, label: string) {
11 if (value) {
12 process.stdout.write(` PASS ${label}\n`);
13 passed += 1;
14 } else {
15 process.stdout.write(` FAIL ${label}\n`);
16 failed += 1;
17 }
18 }
19
20 function ensureWindow() {
21 if (typeof window === "undefined") {
22 (globalThis as Record<string, unknown>).window = {} as Window & typeof globalThis;
23 }
24 }
25
26 function newBreadcrumbMessages(since: number, cat: string): string[] {
27 return snapshotBreadcrumbs()
28 .slice(since)
29 .filter((crumb) => crumb.cat === cat)
30 .map((crumb) => crumb.msg);
31 }
32
33 console.log("\nbridge breadcrumbs");
34
35 const previousPerformance = (globalThis as { performance?: Performance }).performance;
36 let now = 0;
37 (globalThis as Record<string, unknown>).performance = { now: () => now };
38
39 ensureWindow();
40 const desktopStub = installDesktopHostStub(({
41 main: {
42 App: {
43 async IsMainWindowMaximised() {
44 now = 42;
45 return true;
46 },
47 async CheckUpdate() {
48 now = 125;
49 throw new Error("offline");
50 },
51 } as never,
52 },
53 }).main.App);
54
55 const successStart = snapshotBreadcrumbs().length;
56 now = 10;
57 await app.IsMainWindowMaximised();
58 const successBridge = newBreadcrumbMessages(successStart, "bridge");
59 ok(
60 successBridge.includes("window IsMainWindowMaximised") &&
61 successBridge.includes("window IsMainWindowMaximised done ms=32"),
62 "records bridge start and completion timing breadcrumbs",
63 );
64
65 const errorStart = snapshotBreadcrumbs().length;
66 now = 100;
67 try {
68 await app.CheckUpdate("");
69 } catch {
70 // Expected: this branch verifies the breadcrumb emitted by the proxy rejection path.
71 }
72 const errorBridge = newBreadcrumbMessages(errorStart, "bridge");
73 const bridgeErrors = newBreadcrumbMessages(errorStart, "bridge.error");
74 ok(errorBridge.includes("update CheckUpdate"), "records bridge start breadcrumb before failed calls");
75 ok(bridgeErrors.includes("CheckUpdate ms=25"), "records bridge failure timing breadcrumbs");
76
77 desktopStub.uninstall();
78 if (previousPerformance) {
79 (globalThis as Record<string, unknown>).performance = previousPerformance;
80 } else {
81 delete (globalThis as { performance?: Performance }).performance;
82 }
83
84 console.log(`\n${passed} passed, ${failed} failed`);
85 if (failed > 0) process.exit(1);
86
86 lines TYPESCRIPT