|
| 1 | +import { describe, test, expect } from "bun:test" |
| 2 | +import fs from "fs" |
| 3 | +import path from "path" |
| 4 | + |
| 5 | +const REPO_ROOT = path.resolve(import.meta.dir, "../../../..") |
| 6 | +const PUBLISH_SCRIPT = path.join(REPO_ROOT, "packages/opencode/script/publish.ts") |
| 7 | +const DBT_TOOLS_DIR = path.join(REPO_ROOT, "packages/dbt-tools") |
| 8 | + |
| 9 | +// --------------------------------------------------------------------------- |
| 10 | +// 1. Source dbt-tools uses ESM — if this changes, publish.ts must adapt |
| 11 | +// --------------------------------------------------------------------------- |
| 12 | + |
| 13 | +describe("dbt-tools ESM contract", () => { |
| 14 | + test('source package.json declares "type": "module"', () => { |
| 15 | + const pkg = JSON.parse(fs.readFileSync(path.join(DBT_TOOLS_DIR, "package.json"), "utf-8")) |
| 16 | + expect(pkg.type).toBe("module") |
| 17 | + }) |
| 18 | + |
| 19 | + test("bin/altimate-dbt uses node shebang (not bun)", () => { |
| 20 | + const bin = fs.readFileSync(path.join(DBT_TOOLS_DIR, "bin/altimate-dbt"), "utf-8") |
| 21 | + expect(bin).toContain("#!/usr/bin/env node") |
| 22 | + }) |
| 23 | + |
| 24 | + test("bin/altimate-dbt uses ESM import() to load dist/index.js", () => { |
| 25 | + const bin = fs.readFileSync(path.join(DBT_TOOLS_DIR, "bin/altimate-dbt"), "utf-8") |
| 26 | + expect(bin).toContain('import("../dist/index.js")') |
| 27 | + }) |
| 28 | + |
| 29 | + test("build outputs ESM format (--format esm in build script)", () => { |
| 30 | + const pkg = JSON.parse(fs.readFileSync(path.join(DBT_TOOLS_DIR, "package.json"), "utf-8")) |
| 31 | + const buildScript = pkg.scripts?.build || "" |
| 32 | + expect(buildScript).toContain("--format esm") |
| 33 | + }) |
| 34 | +}) |
| 35 | + |
| 36 | +// --------------------------------------------------------------------------- |
| 37 | +// 2. publish.ts must bundle a package.json with "type": "module" |
| 38 | +// --------------------------------------------------------------------------- |
| 39 | + |
| 40 | +describe("publish.ts dbt-tools ESM bundling", () => { |
| 41 | + const publishSource = fs.readFileSync(PUBLISH_SCRIPT, "utf-8") |
| 42 | + |
| 43 | + test("copyAssets writes a dbt-tools/package.json", () => { |
| 44 | + // The publish script must create a package.json in the bundled dbt-tools dir. |
| 45 | + // Without it, Node defaults to CJS and `import` statements in dist/index.js fail. |
| 46 | + expect(publishSource).toContain("dbt-tools/package.json") |
| 47 | + }) |
| 48 | + |
| 49 | + test('bundled package.json includes "type": "module"', () => { |
| 50 | + // The publish script must write `{ "type": "module" }` (or equivalent) |
| 51 | + // so that Node treats .js files in dbt-tools/ as ESM. |
| 52 | + expect(publishSource).toContain('"type": "module"') |
| 53 | + }) |
| 54 | + |
| 55 | + test("copyAssets creates dbt-tools/bin and dbt-tools/dist directories", () => { |
| 56 | + expect(publishSource).toContain("dbt-tools/bin") |
| 57 | + expect(publishSource).toContain("dbt-tools/dist") |
| 58 | + }) |
| 59 | + |
| 60 | + test("copyAssets copies the altimate-dbt bin wrapper", () => { |
| 61 | + expect(publishSource).toContain("dbt-tools/bin/altimate-dbt") |
| 62 | + }) |
| 63 | + |
| 64 | + test("copyAssets copies dist/index.js (not the entire dist/ tree)", () => { |
| 65 | + // Copying all of dist/ would include ~220MB of .node native binaries |
| 66 | + expect(publishSource).toContain("dist/index.js") |
| 67 | + }) |
| 68 | +}) |
| 69 | + |
| 70 | +// --------------------------------------------------------------------------- |
| 71 | +// 3. Structural: if dbt-tools ever switches away from ESM, tests should catch it |
| 72 | +// --------------------------------------------------------------------------- |
| 73 | + |
| 74 | +describe("dbt-tools + Node compatibility", () => { |
| 75 | + test("bin wrapper import path matches dist output location", () => { |
| 76 | + // The bin wrapper does `import("../dist/index.js")`. |
| 77 | + // If the build output location changes, this test forces an update. |
| 78 | + const bin = fs.readFileSync(path.join(DBT_TOOLS_DIR, "bin/altimate-dbt"), "utf-8") |
| 79 | + const match = bin.match(/import\(["']([^"']+)["']\)/) |
| 80 | + expect(match).not.toBeNull() |
| 81 | + const importPath = match![1] |
| 82 | + expect(importPath).toBe("../dist/index.js") |
| 83 | + }) |
| 84 | + |
| 85 | + test("Node would fail without package.json type:module (regression guard)", () => { |
| 86 | + // This is the exact scenario that caused the bug: |
| 87 | + // - bin/altimate-dbt has `#!/usr/bin/env node` |
| 88 | + // - It uses `import("../dist/index.js")` |
| 89 | + // - dist/index.js starts with `import { createRequire } from "node:module"` |
| 90 | + // - Without "type": "module" in package.json, Node treats .js as CJS |
| 91 | + // - CJS cannot use top-level `import` → SyntaxError |
| 92 | + // |
| 93 | + // This test verifies the chain of conditions that require package.json: |
| 94 | + const bin = fs.readFileSync(path.join(DBT_TOOLS_DIR, "bin/altimate-dbt"), "utf-8") |
| 95 | + |
| 96 | + // Condition 1: Uses node (not bun) as the runtime |
| 97 | + const usesNode = bin.includes("#!/usr/bin/env node") |
| 98 | + // Condition 2: Uses ESM import syntax |
| 99 | + const usesESMImport = bin.includes("import(") |
| 100 | + |
| 101 | + if (usesNode && usesESMImport) { |
| 102 | + // Then package.json MUST have "type": "module" |
| 103 | + const pkg = JSON.parse(fs.readFileSync(path.join(DBT_TOOLS_DIR, "package.json"), "utf-8")) |
| 104 | + expect(pkg.type).toBe("module") |
| 105 | + |
| 106 | + // AND publish.ts MUST bundle that information |
| 107 | + const publishSource = fs.readFileSync(PUBLISH_SCRIPT, "utf-8") |
| 108 | + expect(publishSource).toContain('"type": "module"') |
| 109 | + } |
| 110 | + }) |
| 111 | + |
| 112 | + test("all dbt-tools bin entries use node shebang (consistency check)", () => { |
| 113 | + const pkg = JSON.parse(fs.readFileSync(path.join(DBT_TOOLS_DIR, "package.json"), "utf-8")) |
| 114 | + const binEntries: Record<string, string> = pkg.bin || {} |
| 115 | + |
| 116 | + for (const [name, relPath] of Object.entries(binEntries)) { |
| 117 | + const binPath = path.join(DBT_TOOLS_DIR, relPath) |
| 118 | + expect(fs.existsSync(binPath)).toBe(true) |
| 119 | + |
| 120 | + const content = fs.readFileSync(binPath, "utf-8") |
| 121 | + // All bin entries should use node (not bun) since end users may not have bun |
| 122 | + expect(content).toContain("#!/usr/bin/env node") |
| 123 | + } |
| 124 | + }) |
| 125 | +}) |
0 commit comments