import { expect, test } from "bun:test"; import { fileURLToPath } from "node:url"; import { mergePluginAssets, runtimeIdsFromMarkup, runtimeScriptsForMarkup, servePluginAsset, } from "../src/plugin-assets.ts"; import { resolvePackageMigrations } from "../src/plugin-migrations.ts"; test("injects only runtimes referenced by rendered markup", () => { const runtimes = [ { id: "captcha", source: "window.captcha = true" }, { id: "editor", source: "window.editor = true" }, ]; const body = '
'; expect([...runtimeIdsFromMarkup(body)]).toEqual(["captcha"]); const scripts = runtimeScriptsForMarkup(body, runtimes); expect(scripts).toHaveLength(1); expect(scripts[0]).toMatchObject({ src: "/__wrnexus/assets/captcha.js", type: "module", defer: true, }); }); test("serves package assets with a safe content type", async () => { const assets = mergePluginAssets( [{ id: "captcha", source: "window.captcha = true", type: "script" }], [], ); const response = await servePluginAsset(assets, "/__wrnexus/assets/captcha.js", "development"); expect(response?.status).toBe(200); expect(response?.headers.get("content-type")).toBe("text/javascript; charset=utf-8"); expect(response?.headers.get("x-content-type-options")).toBe("nosniff"); expect(await response?.text()).toContain("window.captcha"); }); test("published JavaScript runtimes bundle relative chunks by default", async () => { const entry = fileURLToPath(new URL("./fixtures/plugin-runtime/entry.js", import.meta.url)); const assets = mergePluginAssets([{ id: "published", entry }], []); const response = await servePluginAsset(assets, "/__wrnexus/assets/published.js", "development"); expect(response?.status).toBe(200); const source = await response!.text(); expect(source).toContain("published-runtime-chunk-loaded"); expect(source).not.toMatch(/from\s+["']\.\/chunk\.js["']/); }); test("explicit bundle false preserves prebuilt runtime modules", async () => { const entry = fileURLToPath(new URL("./fixtures/plugin-runtime/entry.js", import.meta.url)); const assets = mergePluginAssets([{ id: "raw", entry, bundle: false }], []); const response = await servePluginAsset(assets, "/__wrnexus/assets/raw.js", "development"); expect(await response!.text()).toContain('from "./chunk.js"'); }); test("resolves inline package migrations for the requested database", () => { const migrations = resolvePackageMigrations( [ { id: "default-schema", source: "-- +up\nCREATE TABLE one(id INTEGER);" }, { id: "analytics-schema", database: "analytics", source: "-- +up\nCREATE TABLE events(id INTEGER);", }, ], "analytics", ); expect(migrations).toHaveLength(1); expect(migrations[0]?.name).toBe("analytics-schema"); expect(migrations[0]?.up).toContain("CREATE TABLE events"); }); import { collectScripts } from "../src/runtime.ts"; test("collectScripts automatically adds a referenced package runtime once", () => { const scripts = collectScripts( '
', [{ id: "captcha", source: "window.captcha = true", type: "script" }], ); expect( scripts.filter( (script) => typeof script !== "string" && script.src === "/__wrnexus/assets/captcha.js", ), ).toHaveLength(1); }); test("collectScripts omits client navigation in document mode", () => { const scripts = collectScripts("
Server rendered
", [], { mode: "document", }); expect(scripts).not.toContain("/__wrnexus/nav.js"); }); test("collectScripts ships zero framework JavaScript for static markup by default", () => { expect(collectScripts('
About
')).toEqual([]); }); test("collectScripts adds navigation in explicit client mode", () => { expect(collectScripts("
Server rendered
", [], { mode: "client" })).toEqual([ expect.stringMatching(/^\/__wrnexus\/nav\.js\?v=[a-f0-9]{16}$/), ]); }); test("collectScripts auto-adds navigation when a page already needs a browser runtime", () => { expect(collectScripts('')).toEqual([ expect.stringMatching(/^\/__wrnexus\/nav\.js\?v=[a-f0-9]{16}$/), "/__wrnexus/reactive.js", ]); });