import { afterAll, expect, test } from "bun:test";
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { join } from "node:path";
import { compile, generate, islandNamesFrom, resolveWrnImports } from "@wrnexus/compiler";
import { collectScripts } from "../src/script-selection.ts";
const created: string[] = [];
afterAll(() => {
for (const dir of created) rmSync(dir, { recursive: true, force: true });
});
function app() {
const root = mkdtempSync(join(process.cwd(), ".island-e2e-"));
created.push(root);
mkdirSync(join(root, "app"), { recursive: true });
writeFileSync(
join(root, "app", "Chart.tsx"),
`export default function Chart({ title }: { title: string }) { return
{title}
; }`,
);
writeFileSync(join(root, "app", "Card.wrn"), `component Card { view { card
} }`);
return root;
}
test("a .wrn importing a .tsx emits an island marker and requests the bootstrap", () => {
const root = app();
const page = join(root, "app", "page.wrn");
const source = [
'import Chart from "./Chart"',
'import Card from "./Card"',
"page Home {",
" view {",
' ',
" ",
" }",
"}",
].join("\n");
writeFileSync(page, source);
const ast = compile(source, page).ast;
const islands = islandNamesFrom(
resolveWrnImports(ast.structuredImports, page, { appRoot: root }),
);
// Only the .tsx import is an island; the .wrn component is not.
expect([...islands]).toEqual(["Chart"]);
const out = generate(ast, { islands });
expect(out).toContain('data-wrn-island="Chart"');
expect(out).toContain('data-wrn-island-strategy="visible"');
expect(out).toContain('data-component="Card"');
// Rendered island markup must pull in the island bootstrap.
expect(collectScripts('')).toContain("/__wrnexus/islands.js");
});
test("a page with no .tsx imports emits no island markup and no island script", () => {
const root = app();
const page = join(root, "app", "plain.wrn");
const source = ['import Card from "./Card"', "page Plain {", " view { }", "}"].join(
"\n",
);
writeFileSync(page, source);
const ast = compile(source, page).ast;
const islands = islandNamesFrom(
resolveWrnImports(ast.structuredImports, page, { appRoot: root }),
);
expect(islands.size).toBe(0);
const out = generate(ast, { islands });
expect(out).not.toContain("data-wrn-island");
expect(collectScripts(out)).not.toContain("/__wrnexus/islands.js");
});