import { readFileSync, readdirSync } from "node:fs"; import { join, resolve } from "node:path"; import { describe, expect, it } from "vitest"; import { withRootImportMap } from "./rootImportMap.js"; const BASE = "/api/projects/demo/preview/"; const maps = (html: string) => [...html.matchAll(/`, BASE, ); expect(maps(html)).toEqual([ { imports: { "/": `${BASE}lib/`, [BASE]: BASE, three: `${BASE}vendor/three.module.js`, cdn: "https://cdn.example/x.js", }, scopes: { [`${BASE}vendor/`]: { util: `${BASE}vendor/util.js` } }, }, ]); expect(html.indexOf("importmap")).toBeLessThan(html.indexOf('type="module"')); }); it("leaves a map the browser would refuse, and adds its own beside a template's", () => { const broken = ``; expect(withRootImportMap(broken, BASE)).toBe(broken); const inTemplate = withRootImportMap( ``, BASE, ); expect(maps(inTemplate)).toEqual([{ imports: { "/": BASE, [BASE]: BASE } }, { imports: {} }]); }); it("has no dynamic import in the preview runtime, which the map would send under the project", () => { const runtime = resolve(import.meta.dirname, "../../../core/src/runtime"); const sources = readdirSync(runtime, { recursive: true, encoding: "utf8" }).filter( (file) => file.endsWith(".ts") && !file.endsWith(".test.ts"), ); expect(sources.length).toBeGreaterThan(20); const importing = sources.filter((file) => /(?