Open History to see the last commit
1import { ThreadId, type DesktopPreviewTabState } from "@t3tools/contracts";
2import { describe, expect, it } from "vite-plus/test";
3
4import { useBrowserSurfaceStore } from "~/browser/browserSurfaceStore";
5
6import { buildReportInput, createCaptureActivity, projectDesktopState } from "./usePreviewBridge";
7
8const favicon = {
9 dataUrl: "data:image/png;base64,AAAA",
10 pageUrl: "http://localhost:3000/app",
11 capturedAt: 1,
12};
13
14function state(navStatus: DesktopPreviewTabState["navStatus"]): DesktopPreviewTabState {
15 return {
16 tabId: "tab-1",
17 webContentsId: 1,
18 navStatus,
19 canGoBack: false,
20 canGoForward: false,
21 zoomFactor: 1,
22 pictureInPicture: false,
23 colorScheme: "system",
24 audioMuted: false,
25 audible: false,
26 controller: "none",
27 capturing: false,
28 favicon,
29 updatedAt: "2026-08-09T00:00:00.000Z",
30 };
31}
32
33describe("buildReportInput", () => {
34 it("reports title changes at the same URL without repeating unrelated desktop updates", () => {
35 const threadId = ThreadId.make("thread-1");
36 const tabId = "tab-1";
37 const url = "http://localhost:3000/app";
38 let lastReport = {
39 lastReportedUrl: null as string | null,
40 lastReportedKind: null as DesktopPreviewTabState["navStatus"]["kind"] | null,
41 lastReportedTitle: null as string | null,
42 };
43 const report = (nextState: DesktopPreviewTabState) => {
44 const result = buildReportInput({ threadId, tabId, state: nextState, ...lastReport });
45 if (result) {
46 lastReport = result;
47 }
48 return result?.input.navStatus ?? null;
49 };
50
51 const loaded = state({ kind: "Success", url, title: "Original title" });
52 expect(report(loaded)).toEqual({ _tag: "Success", url, title: "Original title" });
53 expect(report(loaded)).toBeNull();
54
55 const retitled = state({ kind: "Success", url, title: "Updated title" });
56 expect(report(retitled)).toEqual({ _tag: "Success", url, title: "Updated title" });
57 expect(
58 report({
59 ...retitled,
60 audible: true,
61 controller: "human",
62 updatedAt: "2026-08-09T00:00:01.000Z",
63 }),
64 ).toBeNull();
65 });
66});
67
68describe("projectDesktopState", () => {
69 it("shows a retained icon only while the current document has the captured origin", () => {
70 expect(
71 projectDesktopState(
72 state({ kind: "Loading", url: "http://localhost:3000/reload", title: "" }),
73 ).favicon,
74 ).toEqual(favicon);
75 expect(
76 projectDesktopState(
77 state({
78 kind: "LoadFailed",
79 url: "https://example.com/",
80 title: "",
81 code: -105,
82 description: "failed",
83 }),
84 ).favicon,
85 ).toBeNull();
86 expect(projectDesktopState(state({ kind: "Idle" })).favicon).toBeNull();
87 });
88});
89
90describe("createCaptureActivity", () => {
91 it("keeps the guest painting while the host captures it, alongside other activity", () => {
92 const activity = () => useBrowserSurfaceStore.getState().activityByTabId["tab-1"];
93 const recording = useBrowserSurfaceStore.getState().acquireActivity("tab-1");
94 const capture = createCaptureActivity("tab-1");
95
96 capture.update(true);
97 capture.update(true);
98 expect(activity()).toBe(2);
99 capture.update(false);
100 expect(activity()).toBe(1);
101 capture.update(true);
102 capture.release();
103 capture.release();
104 expect(activity()).toBe(1);
105 recording();
106 expect(activity()).toBeUndefined();
107 });
108});