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