Skip to content

Commit 0856a00

Browse files
committed
fix(server): reject unresolved explicit preview tabs
1 parent c814e51 commit 0856a00

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
@@ -313,6 +313,114 @@ beforeEach(() => {
313313
desktopConnections.length = 0;
314314
});
315315

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

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1803,6 +1803,20 @@ const make = Effect.gen(function* () {
18031803
),
18041804
)
18051805
: undefined;
1806+
if (reuse && request.tabIdExplicit && !found) {
1807+
const { sessions } = await Effect.runPromise(
1808+
manager.list({ threadId: ThreadId.make(request.threadId) }),
1809+
);
1810+
const desktop = sessions.some(
1811+
(session) => session.tabId === request.tabId && session.runtime !== "server",
1812+
);
1813+
throw new ServerBrowserPage.ServerBrowserOperationError(
1814+
desktop ? "PreviewAutomationExecutionError" : "PreviewAutomationTabNotFoundError",
1815+
desktop
1816+
? "This tab belongs to the desktop runtime. This environment's server browser cannot control desktop tabs. Omit tabId to open a server tab instead."
1817+
: "The requested server preview tab does not exist.",
1818+
);
1819+
}
18061820
// Only an explicit tabId reuses a tab this session did not open; a tab
18071821
// it last read (such as the user's) is not taken over implicitly.
18081822
const existing =

0 commit comments

Comments
 (0)