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

openPreviewSession.test.ts

88 lines2.7 KB

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