import { spawnSync } from "node:child_process"; import { mkdtemp, rm } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import { afterAll, describe, expect, it } from "vitest"; import { generateRuntimeConfig } from "../../scripts/generate-runtime-config.ts"; import { getBuildConfig } from "../../src/contracts/env.ts"; import { childProcessTimeout } from "../helpers/child-process-timeout.ts"; /** * 13-07. A build served from a sub-path must fetch its runtime config and * release manifest from that sub-path; dist is served under the base, so a * root URL is a 404 at boot. */ const roots: string[] = []; afterAll(async () => { await Promise.all(roots.map((root) => rm(root, { recursive: true, force: true }))); }); describe("sub-path deployment URLs (13-07)", () => { it("derives the runtime config URL from the base path", () => { expect(getBuildConfig({ VITE_ROUTER_BASE_PATH: "/app/" }).runtimeConfigUrl).toBe( "/app/config.json", ); expect(getBuildConfig({}).runtimeConfigUrl).toBe("/config.json"); expect( getBuildConfig({ VITE_ROUTER_BASE_PATH: "/app/", VITE_RUNTIME_CONFIG_URL: "/app/runtime.json", }).runtimeConfigUrl, ).toBe("/app/runtime.json"); }); it("rebases the profile's root release manifest URL onto the base path", async () => { const rebased = await generateRuntimeConfig("local", { VITE_ROUTER_BASE_PATH: "/app/" }); expect(rebased["RELEASE_MANIFEST_URL"]).toBe("/app/release-manifest.json"); const root = await generateRuntimeConfig("local", {}); expect(root["RELEASE_MANIFEST_URL"]).toBe("/release-manifest.json"); }); it("fails the build when an explicit runtime config URL leaves the base path", async () => { const outDir = await mkdtemp(path.join(tmpdir(), "sub-path-build-")); roots.push(outDir); const environment = Object.fromEntries( Object.entries(process.env).filter(([name]) => !name.startsWith("VITE_")), ); const run = spawnSync( path.resolve("node_modules/.bin/vite"), ["build", "--outDir", outDir, "--emptyOutDir", "--logLevel", "error"], { encoding: "utf8", env: { ...environment, VITE_ROUTER_BASE_PATH: "/app/", VITE_RUNTIME_CONFIG_URL: "/config.json", }, }, ); expect(run.status).not.toBe(0); expect(run.stderr + run.stdout).toMatch( /VITE_RUNTIME_CONFIG_URL \/config\.json is outside VITE_ROUTER_BASE_PATH \/app\//u, ); }, childProcessTimeout(700)); });