import type { AgentEvent } from "../events/agent-events.js"; import type { Page } from "../events/requests.js"; import { settledRequests } from "../policy/request-status.js"; import { intoHead, mcpAppDataScript, PAGE_FALLBACK_THEMES, PAGE_THEME_VARIABLES, pageThemeCss, publishedPage, readMcpAppData, sealedPage, SEALED_PAGE_POLICY, sizedFirst, standalonePage, } from "./pages.js"; import { foldTurn, userRow } from "./transcript-fold.js"; // path-literals: content, a page's stored path as a transcript row carries it const PAGE: Page = { id: "a1b2c3d4e5", title: "Q3 revenue", path: ".intentic/records/artifacts/pages/c1/a1b2c3d4e5.r0.html" }; describe("intoHead", () => { it("puts markup first inside an existing head", () => { expect(intoHead(`
hi
`, ``)).toBe(`hi
`); expect(intoHead(`hi
`, ``)).toBe(`hi
`); }); it("never takes a head written inside a script or comment for the real one", () => { const html = ``; expect(intoHead(html, ``)).toBe(``); }); }); describe("sealedPage", () => { it("leads with the policy, then the theme and the bootstrap, before the page's own head", () => { const sealed = sealedPage(``, PAGE_FALLBACK_THEMES.dark); const policy = sealed.indexOf(SEALED_PAGE_POLICY); const theme = sealed.indexOf(`id="intentic-theme"`); const own = sealed.indexOf(`body{color:red}`); expect(policy).toBeGreaterThan(-1); expect(policy).toBeLessThan(theme); expect(theme).toBeLessThan(own); expect(sealed).toContain(`window.intentic=`); }); it("keeps the theme's values from ending the rule they sit in", () => { const hostile = { ...PAGE_FALLBACK_THEMES.light, variables: { ...PAGE_FALLBACK_THEMES.light.variables, "--foreground": `red;}" }, result: { content: [{ type: "text", text: "3" }] } }; const stored = standalonePage(intoHead(``, mcpAppDataScript(data))); expect(stored).not.toContain(""); expect(readMcpAppData(stored)).toEqual(data); expect(readMcpAppData(``)).toBeUndefined(); }); }); describe("publishedPage", () => { it("seals a stored page off the network and has it say its height to the page around it", () => { const published = publishedPage(standalonePage(`x`)); expect(published.indexOf(SEALED_PAGE_POLICY)).toBeLessThan(published.indexOf(`intentic-page-defaults`)); expect(published).toContain(`ui/notifications/size-changed`); }); }); describe("sizedFirst", () => { const page = `";x`; const shell = sizedFirst(page); it("carries the page whole inside one script the page cannot close", () => { expect(shell.startsWith(``)).toBe(true); // The only `` left is the shell's own closing tag: the page's markup cannot end the script early. expect(shell.split(``).length).toBe(2); }); it("writes back exactly the page it was given", () => { const literal = /var page=(".*?"),done=/.exec(shell)?.[1]; expect(literal).toBeDefined(); expect(JSON.parse(literal!)).toBe(page); }); });