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

openPreviewSession.test.ts

91 lines2.8 KB

1import {
2 DEFAULT_BROWSER_PROFILE_ID,
3 FILL_PREVIEW_VIEWPORT,
4 type PreviewOpenInput,
5 type PreviewSessionSnapshot,
6 type ScopedThreadRef,
7} from "@t3tools/contracts";
8import * as Cause from "effect/Cause";
9import { AsyncResult } from "effect/unstable/reactivity";
10import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
11​
12import { readThreadPreviewState, resetPreviewStateForTests } from "~/previewStateStore";
13​
14import { openPreviewSession } from "./openPreviewSession";
15​
16const threadRef = {
17 environmentId: "local" as ScopedThreadRef["environmentId"],
18 threadId: "thread-1" as ScopedThreadRef["threadId"],
19};
20​
21const snapshot: PreviewSessionSnapshot = {
22 threadId: threadRef.threadId,
23 tabId: "tab-1",
24 navStatus: {
25 _tag: "Loading",
26 url: "https://t3.chat/",
27 title: "",
28 },
29 canGoBack: false,
30 canGoForward: false,
31 updatedAt: "2026-06-11T23:00:00.000Z",
32};
33​
34beforeEach(resetPreviewStateForTests);
35​
36describe("openPreviewSession", () => {
37 it("creates an idle tab without recording a recently visited URL", async () => {
38 const idleSnapshot: PreviewSessionSnapshot = {
39 ...snapshot,
40 tabId: "tab-blank",
41 navStatus: { _tag: "Idle" },
42 };
43 const open = vi.fn(async (_input: PreviewOpenInput) => AsyncResult.success(idleSnapshot));
44​
45 await openPreviewSession({
46 openPreview: ({ input }) => open(input),
47 threadRef,
48 });
49​
50 expect(open).toHaveBeenCalledWith({
51 threadId: "thread-1",
52 viewport: FILL_PREVIEW_VIEWPORT,
53 profileId: DEFAULT_BROWSER_PROFILE_ID,
54 });
55 expect(readThreadPreviewState(threadRef).snapshot).toEqual(idleSnapshot);
56 expect(readThreadPreviewState(threadRef).recentlySeenUrls).toEqual([]);
57 });
58​
59 it("applies the RPC response without waiting for a preview event", async () => {
60 const open = vi.fn(async (_input: PreviewOpenInput) => AsyncResult.success(snapshot));
61​
62 await openPreviewSession({
63 openPreview: ({ input }) => open(input),
64 threadRef,
65 url: "t3.chat",
66 });
67​
68 expect(open).toHaveBeenCalledWith({
69 threadId: "thread-1",
70 url: "t3.chat",
71 viewport: FILL_PREVIEW_VIEWPORT,
72 profileId: DEFAULT_BROWSER_PROFILE_ID,
73 });
74 expect(readThreadPreviewState(threadRef).snapshot).toEqual(snapshot);
75 expect(readThreadPreviewState(threadRef).recentlySeenUrls).toEqual(["https://t3.chat/"]);
76 });
77​
78 it("returns failures without mutating preview state", async () => {
79 const failure = new Error("preview unavailable");
80​
81 const result = await openPreviewSession({
82 openPreview: async () => AsyncResult.failure(Cause.fail(failure)),
83 threadRef,
84 url: "t3.chat",
85 });
86​
87 expect(result._tag).toBe("Failure");
88 expect(readThreadPreviewState(threadRef).snapshot).toBeNull();
89 expect(readThreadPreviewState(threadRef).recentlySeenUrls).toEqual([]);
90 });
91});