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

browserSurfaceStore.test.ts

78 lines2.4 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("tracks content dimensions for a browser that has never been visible", () => {
15 const tabId = "hidden-browser-surface-content-test";
16 useBrowserSurfaceStore.getState().presentContent(tabId, {
17 x: 0,
18 y: 0,
19 width: 393,
20 height: 852,
21 scale: 1,
22 scrollLeft: 0,
23 scrollTop: 0,
24 });
25​
26 expect(useBrowserSurfaceStore.getState().byTabId[tabId]).toMatchObject({
27 rect: null,
28 visible: false,
29 content: { width: 393, height: 852 },
30 });
31 });
32​
33 it("uses the live panel rect for a hidden background tab", () => {
34 const staleRect = { x: 0, y: 0, width: 500, height: 700 };
35 const liveRect = { x: 10, y: 20, width: 900, height: 640 };
36 expect(
37 resolveBrowserSurfacePanelRect(
38 {
39 hidden: { rect: staleRect, visible: false, content: null, updatedAt: 1, owner: null },
40 active: { rect: liveRect, visible: true, content: null, updatedAt: 2, owner: null },
41 },
42 "hidden",
43 ),
44 ).toEqual(liveRect);
45 });
46​
47 it("ignores updates and releases from a stale surface lease", () => {
48 const tabId = "leased-browser-surface";
49 const staleRect = { x: 0, y: 0, width: 500, height: 700 };
50 const liveRect = { x: 10, y: 20, width: 900, height: 640 };
51 const staleLease = acquireBrowserSurface(tabId);
52 staleLease.present(staleRect, true);
53​
54 const liveLease = acquireBrowserSurface(tabId);
55 liveLease.present(liveRect, true);
56 staleLease.present(staleRect, true);
57 staleLease.release();
58​
59 expect(useBrowserSurfaceStore.getState().byTabId[tabId]).toMatchObject({
60 rect: liveRect,
61 visible: true,
62 });
63 });
64​
65 it("hides a surface when its current lease is released", () => {
66 const tabId = "released-browser-surface";
67 const lease = acquireBrowserSurface(tabId);
68 lease.present({ x: 10, y: 20, width: 900, height: 640 }, true);
69​
70 lease.release();
71 lease.present({ x: 0, y: 0, width: 1, height: 1 }, true);
72​
73 expect(useBrowserSurfaceStore.getState().byTabId[tabId]).toMatchObject({
74 visible: false,
75 owner: null,
76 });
77 });
78});