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