|
| 1 | +import { describe, test, expect, beforeAll } from "bun:test" |
| 2 | +import { readFileSync, writeFileSync, mkdtempSync, cpSync, existsSync } from "fs" |
| 3 | +import { join, resolve } from "path" |
| 4 | +import { tmpdir } from "os" |
| 5 | +import { $ } from "bun" |
| 6 | + |
| 7 | +const dist = join(import.meta.dir, "../dist") |
| 8 | +const scriptDir = join(import.meta.dir, "../script") |
| 9 | + |
| 10 | +// ─── Helpers ───────────────────────────────────────────────────────── |
| 11 | +// Simulate what copy-python.ts does against arbitrary bundle content, |
| 12 | +// without touching the real dist/index.js. |
| 13 | +function runPatchLogic(bundleContent: string): { patched: boolean; output: string } { |
| 14 | + const pattern = /var __dirname\s*=\s*"[^"]*python-bridge[^"]*"/ |
| 15 | + if (pattern.test(bundleContent)) { |
| 16 | + const replacement = `var __dirname = typeof import.meta.dirname === "string" ? import.meta.dirname : __require("path").dirname(__require("url").fileURLToPath(import.meta.url))` |
| 17 | + return { patched: true, output: bundleContent.replace(pattern, replacement) } |
| 18 | + } |
| 19 | + return { patched: false, output: bundleContent } |
| 20 | +} |
| 21 | + |
| 22 | +// ─── Adversarial: Regex edge cases ────────────────────────────────── |
| 23 | +describe("adversarial: patch regex", () => { |
| 24 | + test("matches typical CI runner path (Linux)", () => { |
| 25 | + const bundle = `var __dirname = "/home/runner/work/altimate-code/node_modules/.bun/python-bridge@1.1.0/node_modules/python-bridge"` |
| 26 | + const result = runPatchLogic(bundle) |
| 27 | + expect(result.patched).toBe(true) |
| 28 | + expect(result.output).toContain("import.meta.dirname") |
| 29 | + expect(result.output).not.toContain("/home/runner") |
| 30 | + }) |
| 31 | + |
| 32 | + test("matches macOS dev path", () => { |
| 33 | + const bundle = `var __dirname = "/Users/dev/code/node_modules/python-bridge"` |
| 34 | + const result = runPatchLogic(bundle) |
| 35 | + expect(result.patched).toBe(true) |
| 36 | + expect(result.output).not.toContain("/Users/dev") |
| 37 | + }) |
| 38 | + |
| 39 | + test("matches Windows CI path", () => { |
| 40 | + const bundle = `var __dirname = "C:\\Users\\runneradmin\\work\\node_modules\\python-bridge"` |
| 41 | + const result = runPatchLogic(bundle) |
| 42 | + expect(result.patched).toBe(true) |
| 43 | + expect(result.output).not.toContain("C:\\Users") |
| 44 | + }) |
| 45 | + |
| 46 | + test("matches path with @scope in node_modules", () => { |
| 47 | + const bundle = `var __dirname = "/opt/ci/node_modules/.bun/python-bridge@2.0.0/node_modules/python-bridge"` |
| 48 | + const result = runPatchLogic(bundle) |
| 49 | + expect(result.patched).toBe(true) |
| 50 | + }) |
| 51 | + |
| 52 | + test("does NOT match unrelated __dirname (no python-bridge)", () => { |
| 53 | + const bundle = `var __dirname = "/home/runner/work/some-other-module"` |
| 54 | + const result = runPatchLogic(bundle) |
| 55 | + expect(result.patched).toBe(false) |
| 56 | + // The original hardcoded path is preserved — this is CORRECT behavior. |
| 57 | + // The patch should only touch the python-bridge dirname. |
| 58 | + }) |
| 59 | + |
| 60 | + test("does NOT match __dirname that's already patched", () => { |
| 61 | + const bundle = `var __dirname = typeof import.meta.dirname === "string" ? import.meta.dirname : __require("path").dirname(__require("url").fileURLToPath(import.meta.url))` |
| 62 | + const result = runPatchLogic(bundle) |
| 63 | + expect(result.patched).toBe(false) |
| 64 | + }) |
| 65 | + |
| 66 | + test("handles extra whitespace in assignment", () => { |
| 67 | + const bundle = `var __dirname = "/some/path/to/python-bridge"` |
| 68 | + const result = runPatchLogic(bundle) |
| 69 | + expect(result.patched).toBe(true) |
| 70 | + }) |
| 71 | + |
| 72 | + test("only patches FIRST match (non-global)", () => { |
| 73 | + const bundle = [`var __dirname = "/first/path/python-bridge"`, `var __dirname = "/second/path/python-bridge"`].join( |
| 74 | + "\n", |
| 75 | + ) |
| 76 | + const result = runPatchLogic(bundle) |
| 77 | + expect(result.patched).toBe(true) |
| 78 | + // First one is patched |
| 79 | + expect(result.output).toContain("import.meta.dirname") |
| 80 | + // Second one survives — this is a known limitation |
| 81 | + expect(result.output).toContain("/second/path/python-bridge") |
| 82 | + }) |
| 83 | + |
| 84 | + test("does NOT match if quotes are single quotes", () => { |
| 85 | + const bundle = `var __dirname = '/home/runner/python-bridge'` |
| 86 | + const result = runPatchLogic(bundle) |
| 87 | + // Bun uses double quotes, so single-quote paths should not match |
| 88 | + expect(result.patched).toBe(false) |
| 89 | + }) |
| 90 | + |
| 91 | + test("does NOT match let or const declarations", () => { |
| 92 | + const bundle = `const __dirname = "/home/runner/python-bridge"` |
| 93 | + const result = runPatchLogic(bundle) |
| 94 | + // Pattern specifically matches `var __dirname` — const/let won't match |
| 95 | + expect(result.patched).toBe(false) |
| 96 | + }) |
| 97 | +}) |
| 98 | + |
| 99 | +// ─── Adversarial: Built output invariants ─────────────────────────── |
| 100 | +describe("adversarial: built bundle invariants", () => { |
| 101 | + beforeAll(async () => { |
| 102 | + // Ensure we have a fresh build |
| 103 | + if (!existsSync(join(dist, "index.js"))) { |
| 104 | + await $`bun run build`.cwd(join(import.meta.dir, "..")) |
| 105 | + } |
| 106 | + }) |
| 107 | + |
| 108 | + test("exactly ONE __dirname assignment exists in bundle", () => { |
| 109 | + const code = readFileSync(join(dist, "index.js"), "utf8") |
| 110 | + const matches = code.match(/var __dirname\s*=/g) |
| 111 | + expect(matches).not.toBeNull() |
| 112 | + expect(matches!.length).toBe(1) |
| 113 | + }) |
| 114 | + |
| 115 | + test("__dirname is used to resolve PYTHON_BRIDGE_SCRIPT", () => { |
| 116 | + const code = readFileSync(join(dist, "index.js"), "utf8") |
| 117 | + expect(code).toContain('path.join(__dirname, "node_python_bridge.py")') |
| 118 | + }) |
| 119 | + |
| 120 | + test("no CI runner paths leaked anywhere in bundle", () => { |
| 121 | + const code = readFileSync(join(dist, "index.js"), "utf8") |
| 122 | + // Common CI runner path prefixes |
| 123 | + expect(code).not.toContain("/home/runner/work/") |
| 124 | + expect(code).not.toContain("/github/workspace/") |
| 125 | + expect(code).not.toContain("D:\\a\\altimate-code\\") |
| 126 | + }) |
| 127 | + |
| 128 | + test("patched __dirname includes both primary and fallback paths", () => { |
| 129 | + const code = readFileSync(join(dist, "index.js"), "utf8") |
| 130 | + const line = code.split("\n").find((l) => l.includes("var __dirname")) |
| 131 | + expect(line).toBeDefined() |
| 132 | + // Primary: import.meta.dirname |
| 133 | + expect(line).toContain("import.meta.dirname") |
| 134 | + // Fallback: fileURLToPath |
| 135 | + expect(line).toContain("fileURLToPath") |
| 136 | + // Uses __require (Bun's bundled require) |
| 137 | + expect(line).toContain("__require") |
| 138 | + }) |
| 139 | + |
| 140 | + test("node_python_bridge.py in dist matches the source", () => { |
| 141 | + const resolved = require.resolve("@altimateai/dbt-integration") |
| 142 | + const sourcePy = join(require("path").dirname(resolved), "node_python_bridge.py") |
| 143 | + if (!existsSync(sourcePy)) return // skip if source not available |
| 144 | + |
| 145 | + const source = readFileSync(sourcePy, "utf8") |
| 146 | + const copied = readFileSync(join(dist, "node_python_bridge.py"), "utf8") |
| 147 | + expect(copied).toBe(source) |
| 148 | + }) |
| 149 | + |
| 150 | + test("altimate_python_packages directory was copied", () => { |
| 151 | + expect(existsSync(join(dist, "altimate_python_packages"))).toBe(true) |
| 152 | + }) |
| 153 | +}) |
| 154 | + |
| 155 | +// ─── Adversarial: Runtime resolution simulation ───────────────────── |
| 156 | +describe("adversarial: runtime resolution", () => { |
| 157 | + test("patched __dirname evaluates to a real directory at runtime", () => { |
| 158 | + // import.meta.dirname should resolve to THIS test file's directory |
| 159 | + // In the real bundle, it would resolve to dist/ |
| 160 | + const dirname = import.meta.dir |
| 161 | + expect(typeof dirname).toBe("string") |
| 162 | + expect(dirname.length).toBeGreaterThan(0) |
| 163 | + expect(dirname).not.toContain("runner") |
| 164 | + }) |
| 165 | + |
| 166 | + test("node_python_bridge.py is findable relative to dist/index.js", () => { |
| 167 | + // This is the critical runtime check: __dirname should be dist/, |
| 168 | + // and node_python_bridge.py should be in dist/ |
| 169 | + const bridgePath = join(dist, "node_python_bridge.py") |
| 170 | + expect(existsSync(bridgePath)).toBe(true) |
| 171 | + |
| 172 | + // Verify it's actually a Python file, not garbage |
| 173 | + const content = readFileSync(bridgePath, "utf8") |
| 174 | + expect(content).toContain("import") // Python imports |
| 175 | + expect(content.length).toBeGreaterThan(100) // not truncated |
| 176 | + }) |
| 177 | + |
| 178 | + test("bundle can be loaded from a DIFFERENT directory without path errors", async () => { |
| 179 | + // Simulate running the binary from /tmp — __dirname should still |
| 180 | + // resolve to where index.js lives, not the CWD |
| 181 | + const tmpDir = mkdtempSync(join(tmpdir(), "adversarial-")) |
| 182 | + const originalCwd = process.cwd() |
| 183 | + |
| 184 | + try { |
| 185 | + process.chdir(tmpDir) |
| 186 | + |
| 187 | + // The patched code uses import.meta.dirname which is compile-time |
| 188 | + // resolved to the file's actual location, not CWD. Verify this. |
| 189 | + const indexPath = join(dist, "index.js") |
| 190 | + expect(existsSync(indexPath)).toBe(true) |
| 191 | + |
| 192 | + // Read the bundle and verify the __dirname line doesn't reference CWD |
| 193 | + const code = readFileSync(indexPath, "utf8") |
| 194 | + expect(code).not.toContain(tmpDir) |
| 195 | + } finally { |
| 196 | + process.chdir(originalCwd) |
| 197 | + } |
| 198 | + }) |
| 199 | +}) |
| 200 | + |
| 201 | +// ─── Adversarial: Double-patch protection ─────────────────────────── |
| 202 | +describe("adversarial: idempotency", () => { |
| 203 | + test("running the patch twice does not corrupt the bundle", () => { |
| 204 | + const original = `var __dirname = "/ci/path/python-bridge"` |
| 205 | + |
| 206 | + // First patch |
| 207 | + const first = runPatchLogic(original) |
| 208 | + expect(first.patched).toBe(true) |
| 209 | + |
| 210 | + // Second patch on already-patched output — should be a no-op |
| 211 | + const second = runPatchLogic(first.output) |
| 212 | + expect(second.patched).toBe(false) |
| 213 | + expect(second.output).toBe(first.output) |
| 214 | + }) |
| 215 | + |
| 216 | + test("patch output is syntactically valid JS", () => { |
| 217 | + const bundle = `var __dirname = "/home/runner/python-bridge";` |
| 218 | + const result = runPatchLogic(bundle) |
| 219 | + |
| 220 | + // The patched code should not have unmatched quotes or parens |
| 221 | + const openParens = (result.output.match(/\(/g) || []).length |
| 222 | + const closeParens = (result.output.match(/\)/g) || []).length |
| 223 | + expect(openParens).toBe(closeParens) |
| 224 | + |
| 225 | + const openQuotes = (result.output.match(/"/g) || []).length |
| 226 | + expect(openQuotes % 2).toBe(0) // even number of quotes |
| 227 | + }) |
| 228 | +}) |
| 229 | + |
| 230 | +// ─── Adversarial: CI smoke test alignment ─────────────────────────── |
| 231 | +describe("adversarial: CI smoke test parity", () => { |
| 232 | + test("CI regex catches what build-integrity test catches", () => { |
| 233 | + // CI uses: grep -qE 'var __dirname\\s*=\\s*"(/|[A-Za-z]:\\\\)' (shell regex) |
| 234 | + // Test uses: /var __dirname\s*=\s*"(?:[A-Za-z]:\\\\|\/)/ |
| 235 | + // Both should flag the same hardcoded paths |
| 236 | + |
| 237 | + const linuxPath = `var __dirname = "/home/runner/python-bridge"` |
| 238 | + const windowsPath = `var __dirname = "C:\\\\Users\\\\runner\\\\python-bridge"` |
| 239 | + const patchedPath = `var __dirname = typeof import.meta.dirname === "string" ? import.meta.dirname : __require("path").dirname(__require("url").fileURLToPath(import.meta.url))` |
| 240 | + |
| 241 | + const ciRegex = /var __dirname\s*=\s*"(\/|[A-Za-z]:\\)/ |
| 242 | + const testRegex = /var __dirname\s*=\s*"(?:[A-Za-z]:\\\\|\/)/ |
| 243 | + |
| 244 | + // Both should flag hardcoded paths |
| 245 | + expect(ciRegex.test(linuxPath)).toBe(true) |
| 246 | + expect(testRegex.test(linuxPath)).toBe(true) |
| 247 | + |
| 248 | + expect(ciRegex.test(windowsPath)).toBe(true) |
| 249 | + expect(testRegex.test(windowsPath)).toBe(true) |
| 250 | + |
| 251 | + // Neither should flag the patched version |
| 252 | + expect(ciRegex.test(patchedPath)).toBe(false) |
| 253 | + expect(testRegex.test(patchedPath)).toBe(false) |
| 254 | + }) |
| 255 | +}) |
0 commit comments