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

browserSurfaceStore.test.ts

187 lines5.4 KB

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