Open History to see the last commit
1import {
2 PreviewReportStatusInput,
3 ThreadId,
4 type DesktopPreviewTabState,
5} from "@t3tools/contracts";
6import { Schema } from "effect";
7import { describe, expect, it } from "vite-plus/test";
8
9import { useBrowserSurfaceStore } from "~/browser/browserSurfaceStore";
10
11import { buildReportInput, createCaptureActivity, projectDesktopState } from "./usePreviewBridge";
12
13const isPreviewReportStatusInput = Schema.is(PreviewReportStatusInput);
14
15const favicon = {
16 dataUrl: "data:image/png;base64,AAAA",
17 pageUrl: "http://localhost:3000/app",
18 capturedAt: 1,
19};
20
21function state(navStatus: DesktopPreviewTabState["navStatus"]): DesktopPreviewTabState {
22 return {
23 tabId: "tab-1",
24 webContentsId: 1,
25 navStatus,
26 canGoBack: false,
27 canGoForward: false,
28 zoomFactor: 1,
29 pictureInPicture: false,
30 colorScheme: "system",
31 audioMuted: false,
32 audible: false,
33 controller: "none",
34 capturing: false,
35 viewerSize: null,
36 favicon,
37 updatedAt: "2026-08-09T00:00:00.000Z",
38 };
39}
40
41describe("buildReportInput", () => {
42 it("reports title changes at the same URL without repeating unrelated desktop updates", () => {
43 const threadId = ThreadId.make("thread-1");
44 const tabId = "tab-1";
45 const url = "http://localhost:3000/app";
46 let lastReport = {
47 lastReportedUrl: null as string | null,
48 lastReportedKind: null as DesktopPreviewTabState["navStatus"]["kind"] | null,
49 lastReportedTitle: null as string | null,
50 };
51 const report = (nextState: DesktopPreviewTabState) => {
52 const result = buildReportInput({ threadId, tabId, state: nextState, ...lastReport });
53 if (result) {
54 lastReport = result;
55 }
56 return result?.input.navStatus ?? null;
57 };
58
59 const loaded = state({ kind: "Success", url, title: "Original title" });
60 expect(report(loaded)).toEqual({ _tag: "Success", url, title: "Original title" });
61 expect(report(loaded)).toBeNull();
62
63 const retitled = state({ kind: "Success", url, title: "Updated title" });
64 expect(report(retitled)).toEqual({ _tag: "Success", url, title: "Updated title" });
65 expect(
66 report({
67 ...retitled,
68 audible: true,
69 controller: "human",
70 updatedAt: "2026-08-09T00:00:01.000Z",
71 }),
72 ).toBeNull();
73 });
74
75 it("reports a long emoji title as a valid status cut before a whole emoji", () => {
76 const longTitle = `a${"😀".repeat(300)}`;
77 const result = buildReportInput({
78 threadId: ThreadId.make("thread-1"),
79 tabId: "tab-1",
80 state: state({ kind: "Success", url: "http://localhost:3000/app", title: longTitle }),
81 lastReportedUrl: null,
82 lastReportedKind: null,
83 lastReportedTitle: null,
84 });
85
86 expect(isPreviewReportStatusInput(result!.input)).toBe(true);
87 expect(result!.input.navStatus).toMatchObject({ title: `a${"😀".repeat(255)}` });
88 });
89});
90
91describe("projectDesktopState", () => {
92 it("keeps the native load failure details for the panel", () => {
93 expect(
94 projectDesktopState(
95 state({
96 kind: "LoadFailed",
97 url: "https://example.com/",
98 title: "",
99 code: -105,
100 description: "failed",
101 }),
102 ).navStatus,
103 ).toEqual({
104 _tag: "LoadFailed",
105 url: "https://example.com/",
106 title: "",
107 code: -105,
108 description: "failed",
109 });
110 });
111
112 it("shows a retained icon only while the current document has the captured origin", () => {
113 expect(
114 projectDesktopState(
115 state({ kind: "Loading", url: "http://localhost:3000/reload", title: "" }),
116 ).favicon,
117 ).toEqual(favicon);
118 expect(
119 projectDesktopState(
120 state({
121 kind: "LoadFailed",
122 url: "https://example.com/",
123 title: "",
124 code: -105,
125 description: "failed",
126 }),
127 ).favicon,
128 ).toBeNull();
129 expect(projectDesktopState(state({ kind: "Idle" })).favicon).toBeNull();
130 });
131});
132
133describe("createCaptureActivity", () => {
134 it("keeps the guest painting while the host captures it, alongside other activity", () => {
135 const activity = () => useBrowserSurfaceStore.getState().activityByTabId["tab-1"];
136 const recording = useBrowserSurfaceStore.getState().acquireActivity("tab-1");
137 const capture = createCaptureActivity("tab-1");
138
139 capture.update(true);
140 capture.update(true);
141 expect(activity()).toBe(2);
142 capture.update(false);
143 expect(activity()).toBe(1);
144 capture.update(true);
145 capture.release();
146 capture.release();
147 expect(activity()).toBe(1);
148 recording();
149 expect(activity()).toBeUndefined();
150 });
151});