Skip to content

Commit 0aa4ee9

Browse files
authored
fix(server): reject unresolved explicit preview tabs (pingdotgg#19737)
1 parent 499c9ee commit 0aa4ee9

2 files changed

Lines changed: 122 additions & 0 deletions

File tree

‎apps/server/src/preview/ServerBrowser.test.ts‎

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,114 @@ beforeEach(() => {
306306
desktopConnections.length = 0;
307307
});
308308

309+
it.live.each(["desktop" as const, undefined])(
310+
"explicit desktop targets ($0 runtime) fail without creating a server tab",
311+
(runtime) =>
312+
Effect.scoped(
313+
Effect.gen(function* () {
314+
const { broker, tabId } = yield* ready;
315+
const manager = yield* Manager.PreviewManager;
316+
const desktop = yield* manager.open({
317+
threadId: scope.thread.threadId,
318+
url: "https://example.com/original",
319+
...(runtime === undefined ? {} : { runtime }),
320+
});
321+
const before = yield* manager.list({ threadId: scope.thread.threadId });
322+
const error = yield* broker
323+
.invoke<PreviewAutomationStatus>({
324+
scope,
325+
tabId: PreviewTabId.make(desktop.tabId),
326+
operation: "open",
327+
input: { url: "https://example.com/replacement", open: true },
328+
})
329+
.pipe(Effect.flip);
330+
expect(error).toMatchObject({ _tag: "PreviewAutomationExecutionError" });
331+
expect(error.message).toContain("desktop runtime");
332+
expect(error.message).toContain("cannot control");
333+
expect(yield* manager.list({ threadId: scope.thread.threadId })).toEqual(before);
334+
expect(contexts).toHaveLength(1);
335+
// A rejected explicit target must not replace this session's working tab.
336+
const reused = yield* broker.invoke<PreviewAutomationStatus>({
337+
scope,
338+
operation: "open",
339+
input: { open: false },
340+
});
341+
expect(reused.tabId).toBe(tabId);
342+
}),
343+
).pipe(Effect.provide(layer)),
344+
);
345+
346+
it.live("an unknown explicit target fails without creating a server tab", () =>
347+
Effect.scoped(
348+
Effect.gen(function* () {
349+
const { broker } = yield* ready;
350+
const manager = yield* Manager.PreviewManager;
351+
const before = yield* manager.list({ threadId: scope.thread.threadId });
352+
const error = yield* broker
353+
.invoke<PreviewAutomationStatus>({
354+
scope,
355+
tabId: PreviewTabId.make("missing-tab"),
356+
operation: "open",
357+
input: { url: "https://example.com/replacement", open: true },
358+
})
359+
.pipe(Effect.flip);
360+
expect(error).toMatchObject({
361+
_tag: "PreviewAutomationTabNotFoundError",
362+
tabId: "missing-tab",
363+
});
364+
expect(yield* manager.list({ threadId: scope.thread.threadId })).toEqual(before);
365+
expect(contexts).toHaveLength(1);
366+
}),
367+
).pipe(Effect.provide(layer)),
368+
);
369+
370+
it.live("an explicit server target reuses its ID and navigates without creating a tab", () =>
371+
Effect.scoped(
372+
Effect.gen(function* () {
373+
const { broker, tabId } = yield* ready;
374+
const manager = yield* Manager.PreviewManager;
375+
const before = yield* manager.list({ threadId: scope.thread.threadId });
376+
const opened = yield* broker.invoke<PreviewAutomationStatus>({
377+
scope,
378+
tabId,
379+
operation: "open",
380+
input: { url: "https://example.com/reused", open: false },
381+
});
382+
expect(opened).toMatchObject({ tabId, url: "https://example.com/reused" });
383+
const { sessions } = yield* manager.list({ threadId: scope.thread.threadId });
384+
expect(sessions.map((session) => session.tabId)).toEqual(
385+
before.sessions.map((session) => session.tabId),
386+
);
387+
expect(contexts).toHaveLength(1);
388+
expect(contexts[0]!.page.goto).toHaveBeenCalledWith(
389+
"https://example.com/reused",
390+
expect.anything(),
391+
);
392+
}),
393+
).pipe(Effect.provide(layer)),
394+
);
395+
396+
it.live("an unresolved implicit target still falls back to a new server tab", () =>
397+
Effect.scoped(
398+
Effect.gen(function* () {
399+
const { broker, tabId } = yield* ready;
400+
const manager = yield* Manager.PreviewManager;
401+
yield* manager.close({ threadId: scope.thread.threadId, tabId });
402+
const opened = yield* broker.invoke<PreviewAutomationStatus>({
403+
scope,
404+
operation: "open",
405+
input: { url: "https://example.com/fallback", open: false },
406+
});
407+
expect(opened.tabId).not.toBe(tabId);
408+
expect(opened).toMatchObject({ available: true, url: "https://example.com/fallback" });
409+
const { sessions } = yield* manager.list({ threadId: scope.thread.threadId });
410+
expect(sessions).toEqual([
411+
expect.objectContaining({ tabId: opened.tabId, runtime: "server" }),
412+
]);
413+
}),
414+
).pipe(Effect.provide(layer)),
415+
);
416+
309417
it.live("readiness none responds immediately but takeover input waits for navigation commit", () =>
310418
Effect.scoped(
311419
Effect.gen(function* () {

‎apps/server/src/preview/ServerBrowser.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1711,6 +1711,20 @@ const make = Effect.gen(function* () {
17111711
),
17121712
)
17131713
: undefined;
1714+
if (reuse && request.tabIdExplicit && !found) {
1715+
const { sessions } = await Effect.runPromise(
1716+
manager.list({ threadId: ThreadId.make(request.threadId) }),
1717+
);
1718+
const desktop = sessions.some(
1719+
(session) => session.tabId === request.tabId && session.runtime !== "server",
1720+
);
1721+
throw new ServerBrowserPage.ServerBrowserOperationError(
1722+
desktop ? "PreviewAutomationExecutionError" : "PreviewAutomationTabNotFoundError",
1723+
desktop
1724+
? "This tab belongs to the desktop runtime. This environment's server browser cannot control desktop tabs. Omit tabId to open a server tab instead."
1725+
: "The requested server preview tab does not exist.",
1726+
);
1727+
}
17141728
// Only an explicit tabId reuses a tab this session did not open; a tab
17151729
// it last read (such as the user's) is not taken over implicitly.
17161730
const existing =

0 commit comments

Comments
 (0)