Open History to see the last commit
1import type { PreviewSessionSnapshot } from "@t3tools/contracts";
2import { describe, expect, it } from "vite-plus/test";
3
4import {
5 needsPreviewAutomationSessionSync,
6 resolvePreviewAutomationOpenTab,
7 resolvePreviewAutomationTarget,
8} from "./previewAutomationTarget";
9
10const snapshot = (tabId: string): PreviewSessionSnapshot => ({
11 threadId: "thread-1",
12 tabId,
13 navStatus: { _tag: "Idle" },
14 canGoBack: false,
15 canGoForward: false,
16 updatedAt: "2026-01-01T00:00:00.000Z",
17});
18
19describe("preview automation target selection", () => {
20 it("refreshes authoritative sessions whenever the caller relies on the active tab", () => {
21 const active = snapshot("tab-active");
22 expect(
23 needsPreviewAutomationSessionSync(
24 { snapshot: active, sessions: { [active.tabId]: active } },
25 undefined,
26 ),
27 ).toBe(true);
28 });
29
30 it("refreshes an explicit tab only when it is absent locally", () => {
31 const active = snapshot("tab-active");
32 const state = { snapshot: active, sessions: { [active.tabId]: active } };
33 expect(needsPreviewAutomationSessionSync(state, active.tabId)).toBe(false);
34 expect(needsPreviewAutomationSessionSync(state, "tab-missing")).toBe(true);
35 });
36
37 it("does not report the active tab under an unknown requested tab id", () => {
38 const active = snapshot("tab-active");
39 expect(
40 resolvePreviewAutomationTarget(
41 { snapshot: active, sessions: { [active.tabId]: active } },
42 "tab-missing",
43 ),
44 ).toEqual({ tabId: null, snapshot: null });
45 });
46
47 it("reuses the provider session's pinned tab instead of the mutable UI tab", () => {
48 const uiActive = snapshot("tab-ui-active");
49 const agentTab = snapshot("tab-opened-by-agent");
50 const state = {
51 snapshot: uiActive,
52 sessions: { [uiActive.tabId]: uiActive, [agentTab.tabId]: agentTab },
53 };
54
55 expect(resolvePreviewAutomationOpenTab(state, agentTab.tabId, true)).toBe(agentTab.tabId);
56 expect(resolvePreviewAutomationOpenTab(state, undefined, true)).toBe(uiActive.tabId);
57 expect(resolvePreviewAutomationOpenTab(state, agentTab.tabId, false)).toBeNull();
58 });
59});