From 5a47a66c1db40f37d1ef1a5c1f8344c22bd608f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C4=9Bj=20Such=C3=A1nek?= <16311043+immakermatty@users.noreply.github.com> Date: Fri, 20 Nov 2026 03:16:45 +0000 Subject: [PATCH 1/3] feat(lazurio): Buddy's hidden window no longer marks his answers seen The host tells Buddy's frame whether its window is shown (lazurio.buddy.shown v1). While the window is hidden the chat view does not mount the visit watermark, so an answer that comes then stays unread and the sign by Buddy's avatar says answered. A frame that never hears the message (an older Lazurio) behaves as before. Co-Authored-By: Claude Opus 5.5 --- apps/web/src/components/ChatView.tsx | 4 ++- apps/web/src/lazurio/BuddySurface.tsx | 7 ++++ .../web/src/lazurio/buddySurfaceFrame.test.ts | 25 ++++++++++++-- apps/web/src/lazurio/buddySurfaceFrame.ts | 33 +++++++++++++++---- .../src/lazurio/buddySurfaceProtocol.test.ts | 25 ++++++++++++++ apps/web/src/lazurio/buddySurfaceProtocol.ts | 23 +++++++++++++ .../buddySurfaceTheme.browser.test.tsx | 1 + apps/web/src/lazurio/useBuddyFrame.ts | 3 ++ 8 files changed, 112 insertions(+), 9 deletions(-) diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsx index bcad4c43cee11..788a82b228253 100644 --- a/apps/web/src/components/ChatView.tsx +++ b/apps/web/src/components/ChatView.tsx @@ -191,6 +191,7 @@ import { useBuddyChatShortcuts, useBuddyPage, useBuddyQueuedRunCount, + useBuddyWindowInView, useBuddyHermesControls, } from "../lazurio/BuddySurface"; import { BuddyPageChip } from "../lazurio/BuddySurfacePageChip"; @@ -605,6 +606,7 @@ export default function ChatView(props: ChatViewProps) { const buddySurface = useBuddyHermesControls(environmentId); // The page Buddy is asked from travels with the person's next message (buddySurfacePage.ts). const buddyPage = useBuddyPage(environmentId); + const buddyInView = useBuddyWindowInView(environmentId); const environmentSettings = useEnvironmentSettings(environmentId); const settings = buddySurface ? { ...environmentSettings, followUpBehavior: "steer" as const } @@ -4157,7 +4159,7 @@ export default function ChatView(props: ChatViewProps) { onConfirm={onConfirmRevertStable} /> {embedded ? null : } - + {buddyInView ? : null} ); diff --git a/apps/web/src/lazurio/BuddySurface.tsx b/apps/web/src/lazurio/BuddySurface.tsx index 2daa87729693e..b27265a61daee 100644 --- a/apps/web/src/lazurio/BuddySurface.tsx +++ b/apps/web/src/lazurio/BuddySurface.tsx @@ -61,6 +61,7 @@ import { pageRecordOf } from "./buddySurfacePage"; import { useLinkedUnlistedProjectIds } from "./unlistedRows"; import { BUDDY_WORKING_ROW, buddyConversationRows, buddyFailureOf } from "./buddySurfaceTimeline"; import { prefersCzech } from "./threadTabs"; +import { buddyWindowInView } from "./buddySurfaceFrame"; import { useBuddyFrame } from "./useBuddyFrame"; type BuddyConfig = { readonly lazurioBuddy?: true; readonly lazurioBuddyOrigin?: string } | null; @@ -90,6 +91,12 @@ export function useBuddyHermesControls(environmentId?: string): boolean { return useBuddySurfaceMode(environmentId) === true && config?.lazurioBuddy === true; } +/** Whether the thread view counts as the person looking: not in Buddy's hidden window. */ +export function useBuddyWindowInView(environmentId: string): boolean { + const { framed, shown } = useBuddyFrame(useBuddySurfaceMode(environmentId) === true); + return buddyWindowInView(framed, shown); +} + /** Buddy's surface, read outside React when a command runs: false until the config is known. */ export const isBuddySurfaceNow = (): boolean => isBuddySurfaceConfig(appAtomRegistry.get(primaryServerConfigAtom)); diff --git a/apps/web/src/lazurio/buddySurfaceFrame.test.ts b/apps/web/src/lazurio/buddySurfaceFrame.test.ts index 330c461459ed1..02bac6894e7a3 100644 --- a/apps/web/src/lazurio/buddySurfaceFrame.test.ts +++ b/apps/web/src/lazurio/buddySurfaceFrame.test.ts @@ -1,6 +1,6 @@ import { describe, expect, it } from "vite-plus/test"; -import { startBuddyFrame } from "./buddySurfaceFrame"; +import { buddyWindowInView, startBuddyFrame } from "./buddySurfaceFrame"; const HOST = "https://launchpad.example-person.lazurio.io"; @@ -33,7 +33,9 @@ function frameWindow(options: { readonly host?: string; readonly openMenu?: bool shiftKey: false, ...overrides, }); - return { win: win as unknown as Window, posted, key }; + const receive = (data: unknown, origin = options.host ?? HOST, source: unknown = parent) => + listeners.get("message")?.({ data, origin, source }); + return { win: win as unknown as Window, posted, key, receive }; } const layouts = (posted: ReadonlyArray<{ readonly message: unknown }>) => @@ -69,6 +71,25 @@ describe("Buddy's frame", () => { expect(layouts(posted)).toEqual([]); }); + it("counts its thread as seen until the host says the window is hidden, and again once shown", () => { + const { win, receive } = frameWindow(); + const frame = startBuddyFrame(win); + const inView = () => buddyWindowInView(frame.hostOrigin !== null, frame.shown()); + // An older Lazurio never says: the frame behaves as before. + expect(frame.shown()).toBeNull(); + expect(inView()).toBe(true); + // The person closes the window; an answer that comes now stays unseen. + receive({ type: "lazurio.buddy.shown", v: 1, shown: false }); + expect(inView()).toBe(false); + // Only the parent on the host's origin says so. + receive({ type: "lazurio.buddy.shown", v: 1, shown: true }, "https://example.com"); + receive({ type: "lazurio.buddy.shown", v: 1, shown: true }, HOST, {}); + expect(inView()).toBe(false); + receive({ type: "lazurio.buddy.shown", v: 1, shown: true }); + expect(inView()).toBe(true); + expect(buddyWindowInView(false, false)).toBe(true); + }); + it("talks to nobody outside a host's frame", () => { const { win, posted, key } = frameWindow({ host: "https://example.com" }); const frame = startBuddyFrame(win); diff --git a/apps/web/src/lazurio/buddySurfaceFrame.ts b/apps/web/src/lazurio/buddySurfaceFrame.ts index c7f6027d28640..67bb283f3b9e1 100644 --- a/apps/web/src/lazurio/buddySurfaceFrame.ts +++ b/apps/web/src/lazurio/buddySurfaceFrame.ts @@ -6,6 +6,7 @@ */ import { acceptBuddyContext, + acceptBuddyShown, type BuddyContext, type BuddyLayout, buddyHostOriginOf, @@ -19,11 +20,21 @@ export interface BuddyFrame { readonly context: () => BuddyContext | null; /** The size the frame asked for last; the panel until it asks. */ readonly layout: () => Exclude; + /** Whether the host's window shows the frame; null until the host says (an older Lazurio never does). */ + readonly shown: () => boolean | null; readonly subscribe: (listener: () => void) => () => void; readonly requestLayout: (mode: BuddyLayout) => void; readonly stop: () => void; } +/** + * Whether the frame's thread counts as seen: always outside a frame, and in one until the host says + * its window is hidden. T3 Code marks a thread visited while it is mounted, so a hidden frame would + * read Buddy's answers before the person does. + */ +export const buddyWindowInView = (framed: boolean, shown: boolean | null): boolean => + !framed || shown !== false; + /** An open menu, dialog or list inside the surface takes Escape before the frame does. */ const ESCAPE_TAKERS = '[role="dialog"], [role="alertdialog"], [role="menu"], [role="listbox"]'; @@ -34,6 +45,7 @@ export function startBuddyFrame(win: Window): BuddyFrame { : buddyHostOriginOf(win.location.ancestorOrigins?.[0], win.document.referrer); let context: BuddyContext | null = null; let layout: Exclude = "panel"; + let shown: boolean | null = null; const listeners = new Set<() => void>(); const notify = () => { for (const listener of listeners) listener(); @@ -49,12 +61,20 @@ export function startBuddyFrame(win: Window): BuddyFrame { notify(); }; const onMessage = (event: MessageEvent) => { - const accepted = acceptBuddyContext( - { data: event.data, origin: event.origin, fromParent: event.source === win.parent }, - hostOrigin, - ); - if (accepted === null) return; - context = accepted; + const received = { + data: event.data, + origin: event.origin, + fromParent: event.source === win.parent, + }; + const accepted = acceptBuddyContext(received, hostOrigin); + if (accepted !== null) { + context = accepted; + notify(); + return; + } + const nowShown = acceptBuddyShown(received, hostOrigin); + if (nowShown === null || nowShown === shown) return; + shown = nowShown; notify(); }; const onKeyDown = (event: KeyboardEvent) => { @@ -79,6 +99,7 @@ export function startBuddyFrame(win: Window): BuddyFrame { hostOrigin, context: () => context, layout: () => layout, + shown: () => shown, subscribe: (listener) => { listeners.add(listener); return () => listeners.delete(listener); diff --git a/apps/web/src/lazurio/buddySurfaceProtocol.test.ts b/apps/web/src/lazurio/buddySurfaceProtocol.test.ts index f8be0f76985d7..a0e19de7a5ccc 100644 --- a/apps/web/src/lazurio/buddySurfaceProtocol.test.ts +++ b/apps/web/src/lazurio/buddySurfaceProtocol.test.ts @@ -2,6 +2,7 @@ import { describe, expect, it } from "vite-plus/test"; import { acceptBuddyContext, + acceptBuddyShown, buddyHostOriginOf, buddyLayoutRequest, buddyReady, @@ -117,6 +118,30 @@ describe("a context from the host", () => { }); }); +describe("whether the host's window shows the frame", () => { + const shown = (value: unknown, overrides: Record = {}) => ({ + type: "lazurio.buddy.shown", + v: 1, + shown: value, + ...overrides, + }); + + it("is taken from the parent on the host's origin, and dropped whole for any defect", () => { + expect(acceptBuddyShown(event(shown(false)), host)).toBe(false); + expect(acceptBuddyShown(event(shown(true)), host)).toBe(true); + for (const [data, origin, fromParent, hostOrigin] of [ + [shown(false), host, false, host], + [shown(false), "https://launchpad.vm-01.example.lazurio.io", true, host], + [shown(false), host, true, null], + [shown("false"), host, true, host], + [shown(false, { v: 2 }), host, true, host], + [shown(false, { extra: true }), host, true, host], + [context(), host, true, host], + ] as const) + expect(acceptBuddyShown(event(data, origin, fromParent), hostOrigin)).toBeNull(); + }); +}); + describe("what the frame sends", () => { it("is ready and a request for one of the three sizes", () => { expect(buddyReady()).toEqual({ type: "lazurio.buddy.ready", v: 1 }); diff --git a/apps/web/src/lazurio/buddySurfaceProtocol.ts b/apps/web/src/lazurio/buddySurfaceProtocol.ts index 08514e7c8a384..9772d3cc148a3 100644 --- a/apps/web/src/lazurio/buddySurfaceProtocol.ts +++ b/apps/web/src/lazurio/buddySurfaceProtocol.ts @@ -9,6 +9,7 @@ const BUDDY_MESSAGE = { ready: "lazurio.buddy.ready", layout: "lazurio.buddy.layout", context: "lazurio.buddy.context", + shown: "lazurio.buddy.shown", } as const; const BUDDY_PROTOCOL_VERSION = 1; @@ -270,6 +271,28 @@ export function acceptBuddyContext( return context; } +/** + * The frame's side of `lazurio.buddy.shown`: whether the host's window shows the frame, from the + * same parent and origin as a context; null to drop the message. + */ +export function acceptBuddyShown( + event: { readonly data: unknown; readonly origin: string; readonly fromParent: boolean }, + hostOrigin: string | null, +): boolean | null { + if (event.fromParent !== true) return null; + if (buddyHostEnvironment(hostOrigin) === null || event.origin !== hostOrigin) return null; + const data = event.data; + if ( + !isPlain(data) || + !hasOnly(data, ["type", "v", "shown"]) || + data.type !== BUDDY_MESSAGE.shown || + data.v !== BUDDY_PROTOCOL_VERSION || + typeof data.shown !== "boolean" + ) + return null; + return data.shown; +} + export const buddyReady = () => ({ type: BUDDY_MESSAGE.ready, v: BUDDY_PROTOCOL_VERSION }); /** A request for the window's size; null for a size there is none of. */ diff --git a/apps/web/src/lazurio/buddySurfaceTheme.browser.test.tsx b/apps/web/src/lazurio/buddySurfaceTheme.browser.test.tsx index f921303d0b001..cbf2a1692531d 100644 --- a/apps/web/src/lazurio/buddySurfaceTheme.browser.test.tsx +++ b/apps/web/src/lazurio/buddySurfaceTheme.browser.test.tsx @@ -20,6 +20,7 @@ vi.mock(import("./useBuddyFrame"), async (original) => ({ framed: frame.context !== null, context: frame.context, layout: "panel" as const, + shown: null, requestLayout: () => {}, }), })); diff --git a/apps/web/src/lazurio/useBuddyFrame.ts b/apps/web/src/lazurio/useBuddyFrame.ts index 289da0a174c25..f4806175352aa 100644 --- a/apps/web/src/lazurio/useBuddyFrame.ts +++ b/apps/web/src/lazurio/useBuddyFrame.ts @@ -20,6 +20,7 @@ const NO_FRAME: Omit = { hostOrigin: null, context: () => null, layout: () => "panel", + shown: () => null, subscribe: () => () => {}, requestLayout: () => {}, }; @@ -29,10 +30,12 @@ export function useBuddyFrame(buddy: boolean) { const current = buddy ? buddyFrame() : NO_FRAME; const context = useSyncExternalStore(current.subscribe, current.context, () => null); const layout = useSyncExternalStore(current.subscribe, current.layout, () => "panel" as const); + const shown = useSyncExternalStore(current.subscribe, current.shown, () => null); return { framed: current.hostOrigin !== null, context, layout, + shown, requestLayout: current.requestLayout, }; } From 5d321f0bff03d82f3ed7ddca8095e013eb08c3d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C4=9Bj=20Such=C3=A1nek?= <16311043+immakermatty@users.noreply.github.com> Date: Fri, 20 Nov 2026 11:59:15 +0000 Subject: [PATCH 2/3] fix(lazurio): a hidden Buddy frame loaded anew waits for the host before counting a visit In a host's frame the thread counts as seen only once the host says the window is shown. Before that, a frame loaded anew behind a closed window could record a visit, and a completed answer with it, before the host's shown: false arrived. A host that sends its context without shown first (an older Lazurio) still counts as before. Co-Authored-By: Claude Opus 5.5 --- apps/web/src/lazurio/BuddySurface.tsx | 4 +- .../web/src/lazurio/buddySurfaceFrame.test.ts | 40 +++++++++++++++---- apps/web/src/lazurio/buddySurfaceFrame.ts | 13 +++--- 3 files changed, 43 insertions(+), 14 deletions(-) diff --git a/apps/web/src/lazurio/BuddySurface.tsx b/apps/web/src/lazurio/BuddySurface.tsx index b27265a61daee..4e2fcf7baede5 100644 --- a/apps/web/src/lazurio/BuddySurface.tsx +++ b/apps/web/src/lazurio/BuddySurface.tsx @@ -93,8 +93,8 @@ export function useBuddyHermesControls(environmentId?: string): boolean { /** Whether the thread view counts as the person looking: not in Buddy's hidden window. */ export function useBuddyWindowInView(environmentId: string): boolean { - const { framed, shown } = useBuddyFrame(useBuddySurfaceMode(environmentId) === true); - return buddyWindowInView(framed, shown); + const { framed, shown, context } = useBuddyFrame(useBuddySurfaceMode(environmentId) === true); + return buddyWindowInView(framed, shown, context !== null); } /** Buddy's surface, read outside React when a command runs: false until the config is known. */ diff --git a/apps/web/src/lazurio/buddySurfaceFrame.test.ts b/apps/web/src/lazurio/buddySurfaceFrame.test.ts index 02bac6894e7a3..e828a31151af9 100644 --- a/apps/web/src/lazurio/buddySurfaceFrame.test.ts +++ b/apps/web/src/lazurio/buddySurfaceFrame.test.ts @@ -3,6 +3,18 @@ import { describe, expect, it } from "vite-plus/test"; import { buddyWindowInView, startBuddyFrame } from "./buddySurfaceFrame"; const HOST = "https://launchpad.example-person.lazurio.io"; +// What the host sends after the frame's ready, about its own Environment. +const personalContext = { + type: "lazurio.buddy.context", + v: 1, + lang: "cs", + theme: "light", + environment: { id: "example-person", kind: "personal", name: "Osobní" }, + organization: null, + app: null, + page: { path: "/" }, + guide: null, +}; /** A frame's window as far as the frame uses it: its parent, its ancestors and its listeners. */ function frameWindow(options: { readonly host?: string; readonly openMenu?: boolean } = {}) { @@ -71,23 +83,37 @@ describe("Buddy's frame", () => { expect(layouts(posted)).toEqual([]); }); - it("counts its thread as seen until the host says the window is hidden, and again once shown", () => { + it("counts its thread as seen only once the host shows the window, also when loaded anew while hidden", () => { const { win, receive } = frameWindow(); const frame = startBuddyFrame(win); - const inView = () => buddyWindowInView(frame.hostOrigin !== null, frame.shown()); - // An older Lazurio never says: the frame behaves as before. + const inView = () => + buddyWindowInView(frame.hostOrigin !== null, frame.shown(), frame.context() !== null); + // A frame loaded anew behind a closed window: until the host answers, an unread answer stays + // unread. expect(frame.shown()).toBeNull(); - expect(inView()).toBe(true); - // The person closes the window; an answer that comes now stays unseen. + expect(inView()).toBe(false); + // The host answers its ready: hidden first, then the context. receive({ type: "lazurio.buddy.shown", v: 1, shown: false }); + receive(personalContext); expect(inView()).toBe(false); - // Only the parent on the host's origin says so. + // Only the parent on the host's origin says it is shown. receive({ type: "lazurio.buddy.shown", v: 1, shown: true }, "https://example.com"); receive({ type: "lazurio.buddy.shown", v: 1, shown: true }, HOST, {}); expect(inView()).toBe(false); receive({ type: "lazurio.buddy.shown", v: 1, shown: true }); expect(inView()).toBe(true); - expect(buddyWindowInView(false, false)).toBe(true); + // Outside a host's frame the thread counts as seen, as before. + expect(buddyWindowInView(false, null, false)).toBe(true); + }); + + it("behaves as before with an older host, which sends its context and never says shown", () => { + const { win, receive } = frameWindow(); + const frame = startBuddyFrame(win); + receive(personalContext); + expect(frame.shown()).toBeNull(); + expect( + buddyWindowInView(frame.hostOrigin !== null, frame.shown(), frame.context() !== null), + ).toBe(true); }); it("talks to nobody outside a host's frame", () => { diff --git a/apps/web/src/lazurio/buddySurfaceFrame.ts b/apps/web/src/lazurio/buddySurfaceFrame.ts index 67bb283f3b9e1..6087b5472eb2f 100644 --- a/apps/web/src/lazurio/buddySurfaceFrame.ts +++ b/apps/web/src/lazurio/buddySurfaceFrame.ts @@ -28,12 +28,15 @@ export interface BuddyFrame { } /** - * Whether the frame's thread counts as seen: always outside a frame, and in one until the host says - * its window is hidden. T3 Code marks a thread visited while it is mounted, so a hidden frame would - * read Buddy's answers before the person does. + * Whether the frame's thread counts as seen. In a host's frame only once the host says it shows the + * window: a hidden frame, also one loaded anew, would otherwise read Buddy's answers first. A host + * that sends a context without `shown` first (an older Lazurio) never says, so then as before. */ -export const buddyWindowInView = (framed: boolean, shown: boolean | null): boolean => - !framed || shown !== false; +export const buddyWindowInView = ( + framed: boolean, + shown: boolean | null, + heardContext: boolean, +): boolean => !framed || shown === true || (shown === null && heardContext); /** An open menu, dialog or list inside the surface takes Escape before the frame does. */ const ESCAPE_TAKERS = '[role="dialog"], [role="alertdialog"], [role="menu"], [role="listbox"]'; From 41cc42f221bc5444474212037ab85236b6aef4d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C4=9Bj=20Such=C3=A1nek?= <16311043+immakermatty@users.noreply.github.com> Date: Fri, 20 Nov 2026 12:13:45 +0000 Subject: [PATCH 3/3] fix(lazurio): a page a Lazurio host frames waits for its word before the config is known Whether a host of the bubble frames the page is read from the window itself, without side effects, so the visit watermark stays unmounted in Buddy's hidden window also while the Buddy config is still loading. The frame starts there at once and hears the host's shown from the first render (Pablo's review of 5d321f0b). Co-Authored-By: Claude Opus 5.5 --- apps/web/src/lazurio/BuddySurface.tsx | 10 ++++++--- .../web/src/lazurio/buddySurfaceFrame.test.ts | 22 ++++++++++++++++++- apps/web/src/lazurio/buddySurfaceFrame.ts | 11 ++++++---- 3 files changed, 35 insertions(+), 8 deletions(-) diff --git a/apps/web/src/lazurio/BuddySurface.tsx b/apps/web/src/lazurio/BuddySurface.tsx index 4e2fcf7baede5..9d3e859f97dff 100644 --- a/apps/web/src/lazurio/BuddySurface.tsx +++ b/apps/web/src/lazurio/BuddySurface.tsx @@ -61,7 +61,7 @@ import { pageRecordOf } from "./buddySurfacePage"; import { useLinkedUnlistedProjectIds } from "./unlistedRows"; import { BUDDY_WORKING_ROW, buddyConversationRows, buddyFailureOf } from "./buddySurfaceTimeline"; import { prefersCzech } from "./threadTabs"; -import { buddyWindowInView } from "./buddySurfaceFrame"; +import { buddyHostOfWindow, buddyWindowInView } from "./buddySurfaceFrame"; import { useBuddyFrame } from "./useBuddyFrame"; type BuddyConfig = { readonly lazurioBuddy?: true; readonly lazurioBuddyOrigin?: string } | null; @@ -91,9 +91,13 @@ export function useBuddyHermesControls(environmentId?: string): boolean { return useBuddySurfaceMode(environmentId) === true && config?.lazurioBuddy === true; } -/** Whether the thread view counts as the person looking: not in Buddy's hidden window. */ +/** + * Whether the thread view counts as the person looking: not in Buddy's hidden window. A page a host + * of the bubble frames waits for the host's word from its first render, before the config is known. + */ export function useBuddyWindowInView(environmentId: string): boolean { - const { framed, shown, context } = useBuddyFrame(useBuddySurfaceMode(environmentId) === true); + const framed = buddyHostOfWindow(window) !== null; + const { shown, context } = useBuddyFrame(useBuddySurfaceMode(environmentId) === true || framed); return buddyWindowInView(framed, shown, context !== null); } diff --git a/apps/web/src/lazurio/buddySurfaceFrame.test.ts b/apps/web/src/lazurio/buddySurfaceFrame.test.ts index e828a31151af9..8c5d5d000e8c4 100644 --- a/apps/web/src/lazurio/buddySurfaceFrame.test.ts +++ b/apps/web/src/lazurio/buddySurfaceFrame.test.ts @@ -1,6 +1,6 @@ import { describe, expect, it } from "vite-plus/test"; -import { buddyWindowInView, startBuddyFrame } from "./buddySurfaceFrame"; +import { buddyHostOfWindow, buddyWindowInView, startBuddyFrame } from "./buddySurfaceFrame"; const HOST = "https://launchpad.example-person.lazurio.io"; // What the host sends after the frame's ready, about its own Environment. @@ -106,6 +106,26 @@ describe("Buddy's frame", () => { expect(buddyWindowInView(false, null, false)).toBe(true); }); + it("counts nothing in a host's frame before the config is known, then only once the host shows it", () => { + // Loaded anew at Buddy's thread behind a closed window; the config is still loading, so no + // frame has started yet. The window alone says a host frames it. + const { win, receive, posted } = frameWindow(); + const framed = buddyHostOfWindow(win) !== null; + expect(framed).toBe(true); + expect(posted).toEqual([]); + expect(buddyWindowInView(framed, null, false)).toBe(false); + // The config arrives and the frame starts; the host answers its ready, hidden. + const frame = startBuddyFrame(win); + const inView = () => buddyWindowInView(framed, frame.shown(), frame.context() !== null); + receive({ type: "lazurio.buddy.shown", v: 1, shown: false }); + receive(personalContext); + expect(inView()).toBe(false); + receive({ type: "lazurio.buddy.shown", v: 1, shown: true }); + expect(inView()).toBe(true); + // A page no host frames counts as before. + expect(buddyHostOfWindow(frameWindow({ host: "https://example.com" }).win)).toBeNull(); + }); + it("behaves as before with an older host, which sends its context and never says shown", () => { const { win, receive } = frameWindow(); const frame = startBuddyFrame(win); diff --git a/apps/web/src/lazurio/buddySurfaceFrame.ts b/apps/web/src/lazurio/buddySurfaceFrame.ts index 6087b5472eb2f..20c0ab79b8542 100644 --- a/apps/web/src/lazurio/buddySurfaceFrame.ts +++ b/apps/web/src/lazurio/buddySurfaceFrame.ts @@ -41,11 +41,14 @@ export const buddyWindowInView = ( /** An open menu, dialog or list inside the surface takes Escape before the frame does. */ const ESCAPE_TAKERS = '[role="dialog"], [role="alertdialog"], [role="menu"], [role="listbox"]'; +/** The host page around this one, if a host of the bubble frames it; read without side effects. */ +export const buddyHostOfWindow = (win: Window): string | null => + win.parent === win + ? null + : buddyHostOriginOf(win.location.ancestorOrigins?.[0], win.document.referrer); + export function startBuddyFrame(win: Window): BuddyFrame { - const hostOrigin = - win.parent === win - ? null - : buddyHostOriginOf(win.location.ancestorOrigins?.[0], win.document.referrer); + const hostOrigin = buddyHostOfWindow(win); let context: BuddyContext | null = null; let layout: Exclude = "panel"; let shown: boolean | null = null;