Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

browserSurfaceStore.test.ts

175 lines4.9 KB

1import { beforeEach, describe, expect, it } from "vite-plus/test";
2​
3import {
4 acquireBrowserSurface,
5 resolveBrowserSurfacePanelRect,
6 useBrowserSurfaceStore,
7} from "./browserSurfaceStore";
8​
9describe("browserSurfaceStore", () => {
10 beforeEach(() => {
11 useBrowserSurfaceStore.setState({ byTabId: {} });
12 });
13​
14 it("freezes the source content dimensions for a fitted presentation", () => {
15 const tabId = "fitted-browser-surface";
16 const sourceOwner = Symbol("source");
17 const sourceContent = {
18 x: 10,
19 y: 20,
20 width: 1_280,
21 height: 720,
22 scale: 1,
23 scrollLeft: 0,
24 scrollTop: 0,
25 };
26 useBrowserSurfaceStore.getState().claim(tabId, sourceOwner, false);
27 useBrowserSurfaceStore.getState().presentContent(tabId, sourceContent);
28​
29 const fittedLease = acquireBrowserSurface(tabId, true);
30 useBrowserSurfaceStore.getState().presentContent(tabId, {
31 ...sourceContent,
32 width: 360,
33 height: 203,
34 scale: 0.28125,
35 });
36​
37 expect(useBrowserSurfaceStore.getState().byTabId[tabId]?.fittedSourceContent).toEqual(
38 sourceContent,
39 );
40 fittedLease.release();
41 });
42​
43 it("freezes the first content dimensions when fitting starts before the browser is measured", () => {
44 const tabId = "pending-fitted-browser-surface";
45 const fittedLease = acquireBrowserSurface(tabId, true);
46 const sourceContent = {
47 x: 0,
48 y: 0,
49 width: 1_280,
50 height: 720,
51 scale: 1,
52 scrollLeft: 0,
53 scrollTop: 0,
54 };
55​
56 useBrowserSurfaceStore.getState().presentContent(tabId, sourceContent);
57 useBrowserSurfaceStore.getState().presentContent(tabId, {
58 ...sourceContent,
59 width: 320,
60 height: 180,
61 scale: 0.25,
62 });
63​
64 expect(useBrowserSurfaceStore.getState().byTabId[tabId]?.fittedSourceContent).toEqual(
65 sourceContent,
66 );
67 fittedLease.release();
68 });
69​
70 it("tracks content dimensions for a browser that has never been visible", () => {
71 const tabId = "hidden-browser-surface-content-test";
72 useBrowserSurfaceStore.getState().presentContent(tabId, {
73 x: 0,
74 y: 0,
75 width: 393,
76 height: 852,
77 scale: 1,
78 scrollLeft: 0,
79 scrollTop: 0,
80 });
81​
82 expect(useBrowserSurfaceStore.getState().byTabId[tabId]).toMatchObject({
83 rect: null,
84 visible: false,
85 content: { width: 393, height: 852 },
86 });
87 });
88​
89 it("keeps a hidden background tab on its own last rect", () => {
90 const staleRect = { x: 0, y: 0, width: 500, height: 700 };
91 const liveRect = { x: 10, y: 20, width: 900, height: 640 };
92 expect(
93 resolveBrowserSurfacePanelRect(
94 {
95 hidden: {
96 rect: staleRect,
97 visible: false,
98 content: null,
99 fittedSourceContent: null,
100 fitSourceContent: false,
101 cornerRadius: 0,
102 updatedAt: 1,
103 owner: null,
104 },
105 active: {
106 rect: liveRect,
107 visible: true,
108 content: null,
109 fittedSourceContent: null,
110 fitSourceContent: false,
111 cornerRadius: 0,
112 updatedAt: 2,
113 owner: null,
114 },
115 },
116 "hidden",
117 ),
118 ).toEqual(staleRect);
119 });
120​
121 it("ignores updates and releases from a stale surface lease", () => {
122 const tabId = "leased-browser-surface";
123 const staleRect = { x: 0, y: 0, width: 500, height: 700 };
124 const liveRect = { x: 10, y: 20, width: 900, height: 640 };
125 const staleLease = acquireBrowserSurface(tabId);
126 staleLease.present(staleRect, true);
127​
128 const liveLease = acquireBrowserSurface(tabId);
129 liveLease.present(liveRect, true);
130 expect(staleLease.present(staleRect, true)).toBe(false);
131 staleLease.release();
132​
133 expect(useBrowserSurfaceStore.getState().byTabId[tabId]).toMatchObject({
134 rect: liveRect,
135 visible: true,
136 });
137 });
138​
139 it("hides a surface when its current lease is released", () => {
140 const tabId = "released-browser-surface";
141 const lease = acquireBrowserSurface(tabId);
142 lease.present({ x: 10, y: 20, width: 900, height: 640 }, true);
143​
144 lease.release();
145 lease.present({ x: 0, y: 0, width: 1, height: 1 }, true);
146​
147 expect(useBrowserSurfaceStore.getState().byTabId[tabId]).toMatchObject({
148 visible: false,
149 owner: null,
150 });
151 });
152​
153 it("clears fitted presentation state when its lease is released", () => {
154 const tabId = "released-fitted-browser-surface";
155 const fittedLease = acquireBrowserSurface(tabId, true);
156 useBrowserSurfaceStore.getState().presentContent(tabId, {
157 x: 0,
158 y: 0,
159 width: 1_280,
160 height: 800,
161 scale: 1,
162 scrollLeft: 0,
163 scrollTop: 0,
164 });
165​
166 fittedLease.release();
167​
168 expect(useBrowserSurfaceStore.getState().byTabId[tabId]).toMatchObject({
169 fittedSourceContent: null,
170 fitSourceContent: false,
171 owner: null,
172 visible: false,
173 });
174 });
175});