Repository navigation
Expand file tree
/
Copy pathclosedViewStore.test.ts
More file actions
135 lines (128 loc) · 5.04 KB
/
Copy pathclosedViewStore.test.ts
File metadata and controls
135 lines (128 loc) · 5.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import { scopeThreadRef } from "@t3tools/client-runtime/environment";
import {
type EnvironmentId,
INCOGNITO_BROWSER_PROFILE_ID,
type PreviewSessionSnapshot,
ThreadId,
} from "@t3tools/contracts";
import { beforeEach, describe, expect, it } from "vite-plus/test";
import { type ClosedViewEntry, useClosedViewStore } from "./closedViewStore";
const refA = scopeThreadRef("env-1" as EnvironmentId, ThreadId.make("thread-A"));
const refB = scopeThreadRef("env-2" as EnvironmentId, ThreadId.make("thread-A"));
const diff = (threadRef: typeof refA) =>
({ kind: "panel-tab", threadRef, surface: { kind: "diff", id: "diff" } }) as const;
beforeEach(() => {
useClosedViewStore.setState({ entries: [] });
});
const snapshot: PreviewSessionSnapshot = {
threadId: refA.threadId,
tabId: "private-tab",
profileId: INCOGNITO_BROWSER_PROFILE_ID,
navStatus: { _tag: "Success", url: "https://private.example", title: "Private page" },
canGoBack: false,
canGoForward: false,
updatedAt: "2026-10-06T17:00:00.000Z",
};
describe("closedViewStore", () => {
it.each([
{ id: "bad", kind: "browser", threadRef: refA, snapshot: {} },
{
id: "bad",
kind: "browser",
threadRef: refA,
snapshot: { ...snapshot, profileId: "default", navStatus: null },
},
{ id: "bad", kind: "panel-tab", surface: { kind: "diff", id: "diff" } },
{ id: "bad", kind: "panel-tab", threadRef: refA },
{ id: "bad", kind: "panel-tab", threadRef: {}, surface: { kind: "diff", id: "diff" } },
{ id: "bad", kind: "panel-tab", threadRef: refA, surface: {} },
{ id: "bad", kind: "panel-tab", threadRef: refA, surface: { kind: "file", id: "file:x" } },
{
id: "bad",
kind: "panel-tab",
threadRef: refA,
surface: { kind: "device", id: "device:x", target: {} },
},
{
id: "bad",
kind: "panel-tab",
threadRef: refA,
surface: { kind: "pull-request", id: "pull-request:x", repository: "owner/repo", number: 1 },
},
{
id: "bad",
kind: "panel-tab",
threadRef: refA,
surface: { kind: "side-chat", id: "side-chat:x" },
},
])("discards incomplete saved views %j while retaining valid history", async (entry) => {
const id = useClosedViewStore.getState().remember(diff(refA));
const { storage, name } = useClosedViewStore.persist.getOptions();
await storage!.setItem(name!, {
state: { entries: [entry as ClosedViewEntry, ...useClosedViewStore.getState().entries] },
version: 1,
});
await useClosedViewStore.persist.rehydrate();
expect(useClosedViewStore.persist.hasHydrated()).toBe(true);
expect(useClosedViewStore.getState().entries).toEqual([{ ...diff(refA), id }]);
expect(() => useClosedViewStore.getState().remember(diff(refB))).not.toThrow();
});
it("keeps private tabs available in memory without saving their metadata", async () => {
const store = useClosedViewStore.getState();
const publicId = store.remember(diff(refA));
const privateId = store.remember({ kind: "browser", threadRef: refA, snapshot });
expect(useClosedViewStore.getState().entries.map((entry) => entry.id)).toEqual([
privateId,
publicId,
]);
const { storage, name } = useClosedViewStore.persist.getOptions();
const saved = JSON.stringify(await storage!.getItem(name!));
expect(saved).not.toContain("private.example");
expect(saved).not.toContain("Private page");
await useClosedViewStore.persist.rehydrate();
expect(useClosedViewStore.getState().entries.map((entry) => entry.id)).toEqual([publicId]);
});
it("removes private metadata and malformed entries from history saved by earlier versions", async () => {
const store = useClosedViewStore.getState();
const sideChatId = store.remember({
kind: "panel-tab",
threadRef: refA,
surface: { id: "side-chat:side", kind: "side-chat", threadId: ThreadId.make("side") },
});
const publicId = store.remember(diff(refA));
const browserId = store.remember({
kind: "browser",
threadRef: refA,
snapshot: {
...snapshot,
tabId: "public-tab",
profileId: "default",
navStatus: { _tag: "Success", url: "https://public.example", title: "Public page" },
},
});
store.remember({ kind: "browser", threadRef: refA, snapshot });
const { storage, name } = useClosedViewStore.persist.getOptions();
await storage!.setItem(name!, {
state: {
entries: [
null,
{},
{ kind: "browser" },
{ kind: "browser", snapshot: null },
...useClosedViewStore.getState().entries,
] as ClosedViewEntry[],
},
version: 0,
});
await useClosedViewStore.persist.rehydrate();
expect(useClosedViewStore.persist.hasHydrated()).toBe(true);
expect(useClosedViewStore.getState().entries.map((entry) => entry.id)).toEqual([
browserId,
publicId,
sideChatId,
]);
const saved = JSON.stringify(await storage!.getItem(name!));
expect(saved).not.toContain("private.example");
expect(saved).not.toContain("Private page");
});
});