Open History to see the last commit
1import { scopeThreadRef } from "@t3tools/client-runtime/environment";
2import { type EnvironmentId, ThreadId } from "@t3tools/contracts";
3import { beforeEach, describe, expect, it } from "vite-plus/test";
4
5import {
6 browserMiniPlayerSource,
7 type PreviewMiniPlayerSource,
8 selectThreadPreviewMiniPlayer,
9 selectThreadPreviewMiniPlayerTabId,
10 usePreviewMiniPlayerStore,
11} from "./previewMiniPlayerStore";
12
13const refA = scopeThreadRef("env-1" as EnvironmentId, ThreadId.make("thread-A"));
14const refB = scopeThreadRef("env-1" as EnvironmentId, ThreadId.make("thread-B"));
15const tabA = browserMiniPlayerSource("tab-a");
16const tabB = browserMiniPlayerSource("tab-b");
17const pixel: PreviewMiniPlayerSource = {
18 kind: "device",
19 hostId: "nucbox",
20 deviceId: "emulator-5580",
21 platform: "android",
22 name: "Pixel",
23};
24
25beforeEach(() => {
26 usePreviewMiniPlayerStore.setState({ byThreadKey: {} });
27});
28
29describe("previewMiniPlayerStore", () => {
30 it("keeps floating previews scoped to their thread", () => {
31 usePreviewMiniPlayerStore.getState().open(refA, tabA);
32 usePreviewMiniPlayerStore.getState().open(refB, tabB);
33
34 expect(
35 selectThreadPreviewMiniPlayer(usePreviewMiniPlayerStore.getState().byThreadKey, refA),
36 ).toMatchObject({ source: tabA });
37 expect(
38 selectThreadPreviewMiniPlayer(usePreviewMiniPlayerStore.getState().byThreadKey, refB),
39 ).toMatchObject({ source: tabB });
40 });
41
42 it("preserves position when switching the floating tab within one thread", () => {
43 usePreviewMiniPlayerStore.getState().open(refA, tabA);
44 usePreviewMiniPlayerStore.getState().move(refA, "browser:tab-a", { x: 24, y: 48 });
45 usePreviewMiniPlayerStore.getState().open(refA, tabB);
46
47 expect(
48 selectThreadPreviewMiniPlayer(usePreviewMiniPlayerStore.getState().byThreadKey, refA),
49 ).toEqual({
50 source: tabB,
51 position: { x: 24, y: 48 },
52 width: null,
53 });
54 });
55
56 it("ignores stale drag updates after the floating tab changes", () => {
57 usePreviewMiniPlayerStore.getState().open(refA, tabA);
58 usePreviewMiniPlayerStore.getState().open(refA, tabB);
59 usePreviewMiniPlayerStore.getState().move(refA, "browser:tab-a", { x: 100, y: 100 });
60
61 expect(
62 selectThreadPreviewMiniPlayer(usePreviewMiniPlayerStore.getState().byThreadKey, refA),
63 ).toEqual({
64 source: tabB,
65 position: null,
66 width: null,
67 });
68 });
69
70 it("preserves a thread-bound width while switching tabs", () => {
71 usePreviewMiniPlayerStore.getState().open(refA, tabA);
72 usePreviewMiniPlayerStore.getState().resize(refA, "browser:tab-a", 480);
73 usePreviewMiniPlayerStore.getState().open(refA, tabB);
74
75 expect(
76 selectThreadPreviewMiniPlayer(usePreviewMiniPlayerStore.getState().byThreadKey, refA),
77 ).toMatchObject({ source: tabB, width: 480 });
78 });
79
80 it("floats one source per thread, so a device replaces the browser tab", () => {
81 usePreviewMiniPlayerStore.getState().open(refA, tabA);
82 usePreviewMiniPlayerStore.getState().open(refA, pixel);
83 const floating = selectThreadPreviewMiniPlayer(
84 usePreviewMiniPlayerStore.getState().byThreadKey,
85 refA,
86 );
87
88 expect(floating).toMatchObject({ source: pixel });
89 expect(
90 selectThreadPreviewMiniPlayerTabId(usePreviewMiniPlayerStore.getState().byThreadKey, refA),
91 ).toBeNull();
92 // The same device under a new label is still the same floating source.
93 usePreviewMiniPlayerStore.getState().open(refA, { ...pixel, name: "Renamed" });
94 expect(
95 selectThreadPreviewMiniPlayer(usePreviewMiniPlayerStore.getState().byThreadKey, refA),
96 ).toBe(floating);
97 });
98});