import { describe, expect, test } from "bun:test"; import { createMigrationsDataSource, createServerEntrySource, createStaticAssetsSource } from "../../scripts/build"; describe("build 版本注入", () => { test("生成的 server-entry 包含真实 bootstrap 参数", () => { const version = "0.1.0"; const serverEntryTs = createServerEntrySource(version); expect(serverEntryTs).toContain(`const APP_VERSION = "${version}"`); expect(serverEntryTs).toContain(`import { MIGRATIONS } from "./migrations-data";`); expect(serverEntryTs).toContain(`import { staticAssets } from "./static-assets";`); expect(serverEntryTs).toContain( `bootstrap({ configPath, migrations: MIGRATIONS, mode: "production", staticAssets, version: APP_VERSION })`, ); expect(serverEntryTs).toContain("createConsoleFallback().fatal"); }); test("生成的 static-assets 使用 indexHtml 与文件映射", () => { const staticAssetsTs = createStaticAssetsSource({ fileEntries: [` "/assets/app.js": Bun.file(f1),`], importLines: [ `import f0 from "./../dist/web/index.html" with { type: "file" };`, `import f1 from "./../dist/web/assets/app.js" with { type: "file" };`, ], indexHtmlVar: "f0", }); expect(staticAssetsTs).toContain(`export const staticAssets`); expect(staticAssetsTs).toContain(`"/assets/app.js": Bun.file(f1)`); expect(staticAssetsTs).toContain(`indexHtml: Bun.file(f0)`); }); test("生成的 migrations-data 嵌入真实记录形状", () => { const migrationsTs = createMigrationsDataSource([ { checksum: "abc123", id: "0000_initial", sql: "CREATE TABLE projects (id TEXT);" }, ]); expect(migrationsTs).toContain(`import type { MigrationRecord }`); expect(migrationsTs).toContain(`export const MIGRATIONS: MigrationRecord[]`); expect(migrationsTs).toContain(`id: "0000_initial"`); expect(migrationsTs).toContain(`checksum: "abc123"`); expect(migrationsTs).toContain(`CREATE TABLE projects`); }); test("构建步骤编号保持 1/4 到 4/4 一致", async () => { const source = await Bun.file(new URL("../../scripts/build.ts", import.meta.url)).text(); expect(source).toContain(`Step 1/4: Vite build`); expect(source).toContain(`Step 2/4: Code generation`); expect(source).toContain(`Step 3/4: Generating static assets`); expect(source).toContain(`Step 4/4: Bun compile`); expect(source).not.toContain(`Step 1/3`); }); });