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

openPreviewSession.test.ts

142 lines4.8 KB

1import {
2 DEFAULT_BROWSER_PROFILE_ID,
3 DEFAULT_CLIENT_SETTINGS,
4 FILL_PREVIEW_VIEWPORT,
5 type PreviewOpenInput,
6 type PreviewSessionSnapshot,
7 type ScopedThreadRef,
8} from "@t3tools/contracts";
9import * as Cause from "effect/Cause";
10import { AsyncResult } from "effect/unstable/reactivity";
11import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
12​
13import * as browserDefaults from "~/browser/browserDefaults";
14import { BrowserSettingsReadError, openUrlInPreview } from "~/browser/openFileInPreview";
15import { __setClientSettingsForTests } from "~/hooks/useSettings";
16import { readThreadPreviewState, resetPreviewStateForTests } from "~/previewStateStore";
17​
18import { openPreviewSession } from "./openPreviewSession";
19​
20const threadRef = {
21 environmentId: "local" as ScopedThreadRef["environmentId"],
22 threadId: "thread-1" as ScopedThreadRef["threadId"],
23};
24​
25const snapshot: PreviewSessionSnapshot = {
26 threadId: threadRef.threadId,
27 tabId: "tab-1",
28 navStatus: {
29 _tag: "Loading",
30 url: "https://t3.chat/",
31 title: "",
32 },
33 canGoBack: false,
34 canGoForward: false,
35 updatedAt: "2026-06-11T23:00:00.000Z",
36};
37​
38beforeEach(() => {
39 resetPreviewStateForTests();
40 __setClientSettingsForTests(DEFAULT_CLIENT_SETTINGS);
41});
42​
43afterEach(() => {
44 vi.restoreAllMocks();
45});
46​
47describe("openPreviewSession", () => {
48 it("creates an idle tab without recording a recently visited URL", async () => {
49 const idleSnapshot: PreviewSessionSnapshot = {
50 ...snapshot,
51 tabId: "tab-blank",
52 navStatus: { _tag: "Idle" },
53 };
54 const open = vi.fn(async (_input: PreviewOpenInput) => AsyncResult.success(idleSnapshot));
55​
56 await openPreviewSession({
57 openPreview: ({ input }) => open(input),
58 threadRef,
59 });
60​
61 expect(open).toHaveBeenCalledWith({
62 threadId: "thread-1",
63 viewport: FILL_PREVIEW_VIEWPORT,
64 profileId: DEFAULT_BROWSER_PROFILE_ID,
65 });
66 expect(readThreadPreviewState(threadRef).snapshot).toEqual(idleSnapshot);
67 expect(readThreadPreviewState(threadRef).recentlySeenUrls).toEqual([]);
68 });
69​
70 it("applies the RPC response without waiting for a preview event", async () => {
71 const open = vi.fn(async (_input: PreviewOpenInput) => AsyncResult.success(snapshot));
72​
73 await openPreviewSession({
74 openPreview: ({ input }) => open(input),
75 threadRef,
76 url: "t3.chat",
77 });
78​
79 expect(open).toHaveBeenCalledWith({
80 threadId: "thread-1",
81 url: "t3.chat",
82 viewport: FILL_PREVIEW_VIEWPORT,
83 profileId: DEFAULT_BROWSER_PROFILE_ID,
84 });
85 expect(readThreadPreviewState(threadRef).snapshot).toEqual(snapshot);
86 expect(readThreadPreviewState(threadRef).recentlySeenUrls).toEqual(["https://t3.chat/"]);
87 });
88​
89 it("returns failures without mutating preview state", async () => {
90 const failure = new Error("preview unavailable");
91​
92 const result = await openPreviewSession({
93 openPreview: async () => AsyncResult.failure(Cause.fail(failure)),
94 threadRef,
95 url: "t3.chat",
96 });
97​
98 expect(result._tag).toBe("Failure");
99 expect(readThreadPreviewState(threadRef).snapshot).toBeNull();
100 expect(readThreadPreviewState(threadRef).recentlySeenUrls).toEqual([]);
101 });
102​
103 it.each(["session", "link"] as const)(
104 "does not open a %s with unread settings and uses the saved profile on retry",
105 async (entryPoint) => {
106 const failure = new Error("Settings read failed");
107 vi.spyOn(browserDefaults, "resolveBrowserDefaults").mockRejectedValueOnce(failure);
108 const viewport = { _tag: "freeform", width: 1280, height: 720 } as const;
109 __setClientSettingsForTests({
110 ...DEFAULT_CLIENT_SETTINGS,
111 browserDefaultViewport: viewport,
112 browserDefaultProfileId: "work",
113 browserProfiles: [{ id: "work", name: "Work", kind: "persistent" }],
114 });
115 const openPreview = vi.fn(async () => AsyncResult.success(snapshot));
116 const input = { openPreview, threadRef, url: "https://t3.chat/" };
117 const open = entryPoint === "session" ? openPreviewSession : openUrlInPreview;
118​
119 const result = await open(input);
120​
121 expect(result._tag).toBe("Failure");
122 if (result._tag === "Failure") {
123 expect(Cause.squash(result.cause)).toBeInstanceOf(BrowserSettingsReadError);
124 expect(Cause.squash(result.cause)).toMatchObject({ cause: failure });
125 }
126 expect(openPreview).not.toHaveBeenCalled();
127 expect(readThreadPreviewState(threadRef).snapshot).toBeNull();
128 expect(readThreadPreviewState(threadRef).recentlySeenUrls).toEqual([]);
129​
130 await expect(open(input)).resolves.toMatchObject({ _tag: "Success" });
131 expect(openPreview).toHaveBeenCalledExactlyOnceWith({
132 environmentId: threadRef.environmentId,
133 input: {
134 threadId: threadRef.threadId,
135 url: input.url,
136 viewport,
137 profileId: "work",
138 },
139 });
140 },
141 );
142});