import { afterAll, beforeAll, expect, test } from "bun:test";
import { GlobalRegistrator } from "@happy-dom/global-registrator";
import type { ReactNode } from "react";
// Which header the macOS window bar shows when a page opens in front of a
// page that stays mounted, hidden (the Capabilities catalog under Slack).
let ownsDom = false;
let previousActEnvironment: PropertyDescriptor | undefined;
let React: typeof import("react");
let ReactDomClient: typeof import("react-dom/client");
let titles: typeof import("./window-bar-title");
let windowBar: typeof import("../app/components/window-bar");
beforeAll(async () => {
ownsDom = !GlobalRegistrator.isRegistered;
if (ownsDom) GlobalRegistrator.register({ url: "https://example.test" });
previousActEnvironment = Object.getOwnPropertyDescriptor(globalThis, "IS_REACT_ACT_ENVIRONMENT");
(
globalThis as typeof globalThis & { IS_REACT_ACT_ENVIRONMENT: boolean }
).IS_REACT_ACT_ENVIRONMENT = true;
React = await import("react");
ReactDomClient = await import("react-dom/client");
titles = await import("./window-bar-title");
windowBar = await import("../app/components/window-bar");
});
afterAll(async () => {
if (ownsDom) await GlobalRegistrator.unregister();
if (previousActEnvironment)
Object.defineProperty(globalThis, "IS_REACT_ACT_ENVIRONMENT", previousActEnvironment);
else Reflect.deleteProperty(globalThis, "IS_REACT_ACT_ENVIRONMENT");
});
function Header({ title }: { title: string }) {
const inBar = titles.useWindowBarTitle(title);
return
{title}
;
}
/** The catalog stays mounted and hidden while a page is open in front of it. */
function Catalog({
pageOpen,
boundary,
page = "Slack",
}: {
pageOpen: boolean;
boundary: boolean;
/** The open page's header; null for a page without one (loading, errors). */
page?: string | null;
}) {
const header = ;
return (
{pageOpen && page ? : null}
{boundary ? (
{header}
) : (
header
)}
);
}
async function mount(node: ReactNode, check: (view: View) => Promise) {
const host = document.createElement("div");
document.body.append(host);
const root = ReactDomClient.createRoot(host);
try {
await React.act(async () => root.render(node));
await check({
bar: () => host.querySelector("[data-window-bar-title]")?.textContent ?? null,
render: (next: ReactNode) => React.act(async () => root.render(next)),
});
} finally {
await React.act(async () => root.unmount());
host.remove();
}
}
type View = { bar(): string | null; render(next: ReactNode): Promise };
test("a page opened in front of a hidden, mounted page takes the window bar", async () => {
await mount(, async (view) => {
expect(view.bar()).toBe("Capabilities");
await view.render();
expect(view.bar()).toBe("Slack");
// A page that replaces the open one (the Slack agent form) takes the bar too.
await view.render();
expect(view.bar()).toBe("New Slack agent");
await view.render();
expect(view.bar()).toBe("Capabilities");
await view.render();
expect(view.bar()).toBe("Slack");
});
});
test("a page without a header (loading, errors) leaves the hidden page's title", async () => {
await mount(, async (view) => {
await view.render();
expect(view.bar()).toBe("Capabilities");
// The page's header arrives once it has loaded.
await view.render();
expect(view.bar()).toBe("Slack");
});
});
test("claims go in mount order, so a hidden header mounted first keeps the bar", async () => {
// Documents the rule the boundary works around, not a desired outcome.
await mount(, async (view) => {
await view.render();
expect(view.bar()).toBe("Capabilities");
});
});
test("a boundary without `fallback` always keeps headers out of the bar", async () => {
await mount(
,
async (view) => {
expect(view.bar()).toBe("Fallback");
},
);
});
test("a page's own fallback claim yields to a page it opens in front of itself", async () => {
// The Site view names the bar itself (a hook, not a header) and stays
// mounted, hidden, behind its publish form; its claim mounted first.
function Site({ title, formOpen }: { title: string; formOpen: string | null }) {
titles.useWindowBarTitle(title, formOpen !== null);
return formOpen ? : null;
}
const tree = (formOpen: string | null) => (
);
await mount(tree(null), async (view) => {
expect(view.bar()).toBe("Opengeni usage");
await view.render(tree("Publish to web"));
expect(view.bar()).toBe("Publish to web");
await view.render(tree(null));
expect(view.bar()).toBe("Opengeni usage");
// While the form has no header yet (its code loading), the Site keeps the bar.
await view.render(tree(""));
expect(view.bar()).toBe("Opengeni usage");
});
});
test("inside the hidden page, dialogs never take the bar and pages outside the boundary do", async () => {
// Skill, plugin and bundle pages render inside the catalog's tree (portaled
// out), outside the header's boundary; its dialogs have a boundary of their
// own. The dialog mounts first, so a claim leaking out of it would win.
const Tree = ({ dialog, page }: { dialog: boolean; page: string | null }) => (
{dialog ? (
) : null}
{page ? : null}
);
await mount(, async (view) => {
expect(view.bar()).toBe("Capabilities");
await view.render();
expect(view.bar()).toBe("A skill");
await view.render();
expect(view.bar()).toBe("Capabilities");
});
});
function CopyableHeader({ title, fallback = false }: { title: string; fallback?: boolean }) {
titles.useWindowBarTitle(title, fallback, { copyable: true });
return null;
}
test("only a copyable title (a Site's) leaves the drag region for its Copy title menu", async () => {
const copyable = () =>
document
.querySelector("[data-window-bar-title-text]")
?.className.includes("[-webkit-app-region:no-drag]") ?? null;
await mount(
,
async (view) => {
expect(view.bar()).toBe("Settings");
expect(copyable()).toBe(false);
await view.render(
,
);
expect(view.bar()).toBe("Opengeni usage");
expect(copyable()).toBe(true);
// A page opened in front (the publish form) takes the bar, and drags it.
await view.render(
,
);
expect(view.bar()).toBe("Publish to web");
expect(copyable()).toBe(false);
},
);
});