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

openPreviewSession.test.ts

128 lines4.4 KB

1import {
2 DEFAULT_CLIENT_SETTINGS,
3 type PreviewSessionSnapshot,
4 type ScopedThreadRef,
5} from "@t3tools/contracts";
6import * as Cause from "effect/Cause";
7import { AsyncResult } from "effect/reactivity";
8import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
9​
10import * as browserDefaults from "~/browser/browserDefaults";
11import { BrowserSettingsReadError, openUrlInPreview } from "~/browser/openFileInPreview";
12import { __setClientSettingsForTests } from "~/hooks/useSettings";
13import { useRightPanelStore } from "~/rightPanelStore";
14import {
15 applyPreviewServerSnapshot,
16 readThreadPreviewState,
17 resetPreviewStateForTests,
18 setActivePreviewTab,
19} from "~/previewStateStore";
20​
21import { openPreviewSession } from "./openPreviewSession";
22​
23const threadRef = {
24 environmentId: "local" as ScopedThreadRef["environmentId"],
25 threadId: "thread-1" as ScopedThreadRef["threadId"],
26};
27​
28const snapshot: PreviewSessionSnapshot = {
29 threadId: threadRef.threadId,
30 tabId: "tab-1",
31 navStatus: {
32 _tag: "Loading",
33 url: "https://t3.chat/",
34 title: "",
35 },
36 canGoBack: false,
37 canGoForward: false,
38 updatedAt: "2026-06-11T23:00:00.000Z",
39};
40​
41beforeEach(() => {
42 resetPreviewStateForTests();
43 useRightPanelStore.setState({ byThreadKey: {}, threadPanelVisibilityByThreadKey: {} });
44 __setClientSettingsForTests(DEFAULT_CLIENT_SETTINGS);
45});
46​
47afterEach(() => {
48 vi.restoreAllMocks();
49});
50​
51describe("openPreviewSession", () => {
52 it.each(["session", "link"] as const)(
53 "does not open a %s with unread settings and uses the saved profile on retry",
54 async (entryPoint) => {
55 const failure = new Error("Settings read failed");
56 vi.spyOn(browserDefaults, "resolveBrowserDefaults").mockRejectedValueOnce(failure);
57 const viewport = { _tag: "freeform", width: 1280, height: 720 } as const;
58 __setClientSettingsForTests({
59 ...DEFAULT_CLIENT_SETTINGS,
60 browserDefaultViewport: viewport,
61 browserDefaultProfileId: "work",
62 browserProfiles: [{ id: "work", name: "Work", kind: "persistent" }],
63 });
64 const openPreview = vi.fn(async () => AsyncResult.success(snapshot));
65 const input = { openPreview, threadRef, url: "https://t3.chat/" };
66 const open = entryPoint === "session" ? openPreviewSession : openUrlInPreview;
67​
68 const result = await open(input);
69​
70 expect(result._tag).toBe("Failure");
71 if (result._tag === "Failure") {
72 expect(Cause.squash(result.cause)).toBeInstanceOf(BrowserSettingsReadError);
73 expect(Cause.squash(result.cause)).toMatchObject({ cause: failure });
74 }
75 expect(openPreview).not.toHaveBeenCalled();
76 expect(readThreadPreviewState(threadRef).snapshot).toBeNull();
77 expect(readThreadPreviewState(threadRef).recentlySeenUrls).toEqual([]);
78​
79 await expect(open(input)).resolves.toMatchObject({ _tag: "Success" });
80 expect(openPreview).toHaveBeenCalledExactlyOnceWith({
81 environmentId: threadRef.environmentId,
82 input: {
83 threadId: threadRef.threadId,
84 url: input.url,
85 viewport,
86 profileId: "work",
87 },
88 });
89 },
90 );
91});
92​
93describe("openUrlInPreview from a link", () => {
94 // regression: #16961 a background link open switched the active preview tab
95 it("keeps the current tab active for a background open", async () => {
96 applyPreviewServerSnapshot(threadRef, { ...snapshot, tabId: "tab-current" });
97​
98 await openUrlInPreview({
99 openPreview: async () => AsyncResult.success(snapshot),
100 threadRef,
101 url: "https://t3.chat/",
102 background: true,
103 });
104​
105 const state = readThreadPreviewState(threadRef);
106 expect(state.activeTabId).toBe("tab-current");
107 expect(Object.keys(state.sessions).toSorted()).toEqual(["tab-1", "tab-current"]);
108 });
109​
110 it("keeps a tab the user picked while a background open was in flight", async () => {
111 applyPreviewServerSnapshot(threadRef, { ...snapshot, tabId: "tab-current" });
112 applyPreviewServerSnapshot(threadRef, { ...snapshot, tabId: "tab-other" });
113 setActivePreviewTab(threadRef, "tab-current");
114​
115 await openUrlInPreview({
116 openPreview: async () => {
117 applyPreviewServerSnapshot(threadRef, snapshot);
118 setActivePreviewTab(threadRef, "tab-other");
119 return AsyncResult.success(snapshot);
120 },
121 threadRef,
122 url: "https://t3.chat/",
123 background: true,
124 });
125​
126 expect(readThreadPreviewState(threadRef).activeTabId).toBe("tab-other");
127 });
128});