import { createHtmlRenderer } from "@comark/html";
import { expect, type Page, test } from "@playwright/test";
import sources, { components } from "../packages/sources/src/html.ts";
import { componentViewCss } from "../packages/ui/src/editor/component-view.ts";
// The popovers CSS anchor positioning places: comark-sources' citation card and the editor's props panel. Chromium and
// Firefox (147 and later) both run them, each from its own file, since a file runs in one browser.
// Engines round a popover's edges differently, so a placement holds to the pixel.
const near = (actual: number, expected: number) =>
expect(Math.abs(actual - expected)).toBeLessThanOrEqual(1);
export function anchoredPopovers() {
test.use({ viewport: { width: 800, height: 600 } });
const render = createHtmlRenderer({ plugins: [sources()], components });
const markdown = [
"By the sun :cite[1], and by smell :cite[2].",
"::sources\n1. [Magnetoreception](https://en.wikipedia.org/wiki/Magnetoreception), on the sun\n2. [Homing](https://example.com/homing)\n::",
].join("\n\n");
const cite = (page: Page, n: number) => page.locator(`.cso-cite a[href="#cso-${n}"]`).first();
// The document with its top-left corner where `style` puts it.
const at = async (page: Page, style: string, before = "") =>
page.setContent(
`
${before}${await render(markdown)}
`,
);
const card = async (page: Page, n: number) => {
await cite(page, n).hover();
const shown = page.locator(".cso-card");
// It grows in, so it's measured once it has.
await expect(shown).toHaveCSS("opacity", "1");
const [link, box] = await Promise.all([cite(page, n).boundingBox(), shown.boundingBox()]);
if (!link || !box) throw new Error("Expected the citation and its card on screen");
return { link, box };
};
test.describe("a citation's card", () => {
test("uses measured placement with anchor-name but no position-area", async ({ page }) => {
const html = await render(markdown);
// Model both the CSS parser and CSS.supports of a partial implementation.
await page.setContent(
'' +
'