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

agentOpenedPreviews.test.ts

244 lines8.7 KB

1import { scopedThreadKey, scopeThreadRef } from "@t3tools/client-runtime/environment";
2import { type EnvironmentId, type PreviewSessionSnapshot, ThreadId } from "@t3tools/contracts";
3import { describe, expect, it, vi } from "vite-plus/test";
4​
5import { browserMiniPlayerSource, usePreviewMiniPlayerStore } from "../../previewMiniPlayerStore";
6import {
7 __testing as previewStateTesting,
8 applyPreviewServerEvent,
9 applyPreviewServerSnapshot,
10 beginPreviewSessionClose,
11 cancelPreviewSessionClose,
12 reconcilePreviewServerSessions,
13 resetPreviewStateForTests,
14} from "../../previewStateStore";
15import { useRightPanelStore } from "../../rightPanelStore";
16import {
17 followPreviewSessions,
18 revealServerTabs,
19 startAgentOpenedPreviews,
20} from "./agentOpenedPreviews";
21​
22vi.mock("../../state/entities", () => ({
23 readEnvironmentSupportsServerBrowser: () => true,
24}));
25​
26const ref = scopeThreadRef("env-1" as EnvironmentId, ThreadId.make("thread-1"));
27const threadKey = scopedThreadKey(ref);
28​
29function setup() {
30 resetPreviewStateForTests();
31 usePreviewMiniPlayerStore.setState({ byThreadKey: {} });
32 useRightPanelStore.setState({ byThreadKey: {} });
34​
35const miniPlayerSource = () => usePreviewMiniPlayerStore.getState().byThreadKey[threadKey]?.source;
36​
37describe("revealServerTabs", () => {
38 const tab = (tabId: string, force?: boolean): PreviewSessionSnapshot => ({
39 threadId: "thread-1",
40 tabId,
41 navStatus: { _tag: "Loading", url: "http://localhost:5173/", title: "" },
42 canGoBack: false,
43 canGoForward: false,
44 updatedAt: "2026-01-01T00:00:00.000Z",
45 runtime: "server",
46 reveal: true,
47 ...(force === undefined ? {} : { revealRequest: { id: "r1", force } }),
48 });
49 const floatingTabId = () => {
50 const source = miniPlayerSource();
51 return source?.kind === "browser" ? source.tabId : null;
52 };
53​
54 it("floats revealed tabs, and only forced ones when floating previews are off", () => {
55 setup();
56 revealServerTabs(ref, [tab("tab_plain")], { autoShowFloatingPreview: false });
57 expect(floatingTabId()).toBeNull();
58 revealServerTabs(ref, [tab("tab_forced", true)], { autoShowFloatingPreview: false });
59 expect(floatingTabId()).toBe("tab_forced");
60 });
61​
62 it("focuses a tab already in the panel instead of floating it when forced", () => {
63 setup();
64 useRightPanelStore.getState().openBrowser(ref, "tab_a");
65 const browserSurfaceId = useRightPanelStore.getState().byThreadKey[threadKey]?.activeSurfaceId;
66 useRightPanelStore.getState().open(ref, "diff");
67​
68 revealServerTabs(ref, [tab("tab_a", false)], { autoShowFloatingPreview: true });
69 expect(useRightPanelStore.getState().byThreadKey[threadKey]?.activeSurfaceId).not.toBe(
70 browserSurfaceId,
71 );
72 revealServerTabs(ref, [tab("tab_a", true)], { autoShowFloatingPreview: true });
73 expect(useRightPanelStore.getState().byThreadKey[threadKey]?.activeSurfaceId).toBe(
74 browserSurfaceId,
75 );
76 expect(floatingTabId()).toBeNull();
77 });
78});
79​
80describe("followPreviewSessions", () => {
81 const snapshot = (tabId: string, reveal = true): PreviewSessionSnapshot => ({
82 threadId: "thread-1",
83 tabId,
84 navStatus: { _tag: "Loading", url: "http://localhost:5173/", title: "" },
85 canGoBack: false,
86 canGoForward: false,
87 updatedAt: "2026-01-01T00:00:00.000Z",
88 runtime: "server",
89 reveal,
90 });
91 const previewState = (sessions: ReadonlyArray<PreviewSessionSnapshot>, listLoaded = true) => ({
92 ...previewStateTesting.EMPTY_THREAD_PREVIEW_STATE,
93 listLoaded,
94 sessions: Object.fromEntries(sessions.map((entry) => [entry.tabId, entry])),
95 });
96 const browserTabs = () =>
97 useRightPanelStore
98 .getState()
99 .byThreadKey[threadKey]?.surfaces.filter((surface) => surface.kind === "preview")
100 .map((surface) => surface.id);
101​
102 it("adds visible sessions as tabs and drops tabs whose session ended", () => {
103 setup();
104 useRightPanelStore.getState().openBrowser(ref, "tab_gone");
105 followPreviewSessions(ref, previewState([snapshot("tab_a"), snapshot("tab_hidden", false)]));
106 expect(browserTabs()).toEqual(["browser:tab_a"]);
107 });
108​
109 it("waits for the first server list before treating missing sessions as closed", () => {
110 setup();
111 useRightPanelStore.getState().openBrowser(ref, "tab_a");
112 usePreviewMiniPlayerStore.getState().open(ref, browserMiniPlayerSource("tab_a"));
113 followPreviewSessions(ref, previewState([], false));
114 expect(browserTabs()).toEqual(["browser:tab_a"]);
115 expect(miniPlayerSource()).toMatchObject({ kind: "browser", tabId: "tab_a" });
116​
117 followPreviewSessions(ref, previewState([]));
118 expect(browserTabs()).toEqual([]);
119 expect(miniPlayerSource()).toBeUndefined();
120 });
121});
122​
123describe("startAgentOpenedPreviews", () => {
124 const serverEpoch = "server-a";
125 const serverTab = (overrides: Partial<PreviewSessionSnapshot> = {}): PreviewSessionSnapshot => ({
126 threadId: "thread-1",
127 tabId: "tab_a",
128 navStatus: { _tag: "Loading", url: "http://localhost:5173/", title: "" },
129 canGoBack: false,
130 canGoForward: false,
131 updatedAt: "2026-01-01T00:00:00.000Z",
132 runtime: "server",
133 reveal: true,
134 ...overrides,
135 });
136 const following = () => {
137 setup();
138 return { [Symbol.dispose]: startAgentOpenedPreviews() };
139 };
140 const navigated = (revision: number, snapshot: PreviewSessionSnapshot) =>
141 applyPreviewServerEvent(ref, {
142 type: "navigated",
143 threadId: "thread-1",
144 tabId: snapshot.tabId,
145 createdAt: snapshot.updatedAt,
146 snapshot,
147 serverEpoch,
148 revision,
149 });
150 const floatingTabId = () => {
151 const source = miniPlayerSource();
152 return source?.kind === "browser" ? source.tabId : null;
153 };
154 const browserTabs = (threadKeyToRead = threadKey) =>
155 useRightPanelStore
156 .getState()
157 .byThreadKey[threadKeyToRead]?.surfaces.filter((surface) => surface.kind === "preview")
158 .map((surface) => surface.id);
159​
160 it("treats the first list as a baseline and floats tabs opened later", () => {
161 using _following = following();
162 reconcilePreviewServerSessions(ref, {
163 sessions: [serverTab({ tabId: "tab_restored" })],
164 serverEpoch,
165 revision: 1,
166 });
167 expect(floatingTabId()).toBeNull();
168 expect(browserTabs()).toEqual(["browser:tab_restored"]);
169​
170 const opened = serverTab({ tabId: "tab_agent" });
171 applyPreviewServerEvent(ref, {
172 type: "opened",
173 threadId: "thread-1",
174 tabId: opened.tabId,
175 createdAt: opened.updatedAt,
176 snapshot: opened,
177 serverEpoch,
178 revision: 2,
179 });
180 expect(floatingTabId()).toBe("tab_agent");
181 });
182​
183 it("reveals an existing tab only for a new reveal request", () => {
184 using _following = following();
185 const tab = serverTab({ revealRequest: { id: "r1", force: false } });
186 reconcilePreviewServerSessions(ref, { sessions: [tab], serverEpoch, revision: 1 });
187​
188 navigated(2, { ...tab, updatedAt: "2026-01-01T00:00:01.000Z" });
189 expect(floatingTabId()).toBeNull();
190​
191 navigated(3, {
192 ...tab,
193 revealRequest: { id: "r2", force: true },
194 updatedAt: "2026-01-01T00:00:02.000Z",
195 });
196 expect(useRightPanelStore.getState().byThreadKey[threadKey]?.activeSurfaceId).toBe(
197 "browser:tab_a",
198 );
199 });
200​
201 it("never reveals tabs this client opened, desktop tabs, or a tab whose close failed", () => {
202 using _following = following();
203 const tab = serverTab({ tabId: "tab_closing" });
204 reconcilePreviewServerSessions(ref, { sessions: [tab], serverEpoch, revision: 1 });
205 applyPreviewServerSnapshot(ref, serverTab({ tabId: "tab_own", reveal: undefined }));
206 applyPreviewServerSnapshot(ref, { ...serverTab({ tabId: "tab_desktop" }), runtime: "desktop" });
207 beginPreviewSessionClose(ref, tab.tabId);
208 cancelPreviewSessionClose(ref, tab, tab.tabId);
209 expect(floatingTabId()).toBeNull();
210 });
211​
212 it("follows background threads and closes the floating browser when its session ends", () => {
213 using _following = following();
214 const other = scopeThreadRef("env-1" as EnvironmentId, ThreadId.make("thread-2"));
215 reconcilePreviewServerSessions(other, {
216 sessions: [serverTab({ threadId: "thread-2", tabId: "tab_b" })],
217 serverEpoch,
218 revision: 1,
219 });
220 expect(browserTabs(scopedThreadKey(other))).toEqual(["browser:tab_b"]);
221​
222 reconcilePreviewServerSessions(ref, { sessions: [], serverEpoch, revision: 1 });
223 const opened = serverTab();
224 applyPreviewServerEvent(ref, {
225 type: "opened",
226 threadId: "thread-1",
227 tabId: opened.tabId,
228 createdAt: opened.updatedAt,
229 snapshot: opened,
230 serverEpoch,
231 revision: 2,
232 });
233 expect(floatingTabId()).toBe("tab_a");
234 applyPreviewServerEvent(ref, {
235 type: "closed",
236 threadId: "thread-1",
237 tabId: opened.tabId,
238 createdAt: opened.updatedAt,
239 serverEpoch,
240 revision: 3,
241 });
242 expect(floatingTabId()).toBeNull();
243 });
244});