Files
t3code/apps/web/src/components/preview/openPreviewSession.test.ts
T
Julius MarmingeandClaude Opus 5 cca9dc8330 test(web): update preview tests for the new open and createTab shapes
`preview.open` now carries the configured default viewport and `createTab`
carries the zoom/appearance defaults, so the call-shape assertions had to move
with them.

PreviewView's test stubs `browserDefaults` directly rather than deepening the
`state/session` mock: the component now reaches `useSettings`, which pulls in
`state/server`, and a test about browser chrome shouldn't drag the settings and
connection graph along with it.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
2026-08-15 16:26:16 +02:00

89 lines
2.7 KiB
TypeScript

import {
FILL_PREVIEW_VIEWPORT,
type PreviewOpenInput,
type PreviewSessionSnapshot,
type ScopedThreadRef,
} from "@t3tools/contracts";
import * as Cause from "effect/Cause";
import { AsyncResult } from "effect/unstable/reactivity";
import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
import { readThreadPreviewState, resetPreviewStateForTests } from "~/previewStateStore";
import { openPreviewSession } from "./openPreviewSession";
const threadRef = {
environmentId: "local" as ScopedThreadRef["environmentId"],
threadId: "thread-1" as ScopedThreadRef["threadId"],
};
const snapshot: PreviewSessionSnapshot = {
threadId: threadRef.threadId,
tabId: "tab-1",
navStatus: {
_tag: "Loading",
url: "https://t3.chat/",
title: "",
},
canGoBack: false,
canGoForward: false,
updatedAt: "2026-06-11T23:00:00.000Z",
};
beforeEach(resetPreviewStateForTests);
describe("openPreviewSession", () => {
it("creates an idle tab without recording a recently visited URL", async () => {
const idleSnapshot: PreviewSessionSnapshot = {
...snapshot,
tabId: "tab-blank",
navStatus: { _tag: "Idle" },
};
const open = vi.fn(async (_input: PreviewOpenInput) => AsyncResult.success(idleSnapshot));
await openPreviewSession({
openPreview: ({ input }) => open(input),
threadRef,
});
expect(open).toHaveBeenCalledWith({
threadId: "thread-1",
viewport: FILL_PREVIEW_VIEWPORT,
});
expect(readThreadPreviewState(threadRef).snapshot).toEqual(idleSnapshot);
expect(readThreadPreviewState(threadRef).recentlySeenUrls).toEqual([]);
});
it("applies the RPC response without waiting for a preview event", async () => {
const open = vi.fn(async (_input: PreviewOpenInput) => AsyncResult.success(snapshot));
await openPreviewSession({
openPreview: ({ input }) => open(input),
threadRef,
url: "t3.chat",
});
expect(open).toHaveBeenCalledWith({
threadId: "thread-1",
url: "t3.chat",
viewport: FILL_PREVIEW_VIEWPORT,
});
expect(readThreadPreviewState(threadRef).snapshot).toEqual(snapshot);
expect(readThreadPreviewState(threadRef).recentlySeenUrls).toEqual(["https://t3.chat/"]);
});
it("returns failures without mutating preview state", async () => {
const failure = new Error("preview unavailable");
const result = await openPreviewSession({
openPreview: async () => AsyncResult.failure(Cause.fail(failure)),
threadRef,
url: "t3.chat",
});
expect(result._tag).toBe("Failure");
expect(readThreadPreviewState(threadRef).snapshot).toBeNull();
expect(readThreadPreviewState(threadRef).recentlySeenUrls).toEqual([]);
});
});