|
| 1 | +import { describe, test, expect } from "bun:test" |
| 2 | +import { mock, beforeEach, afterEach } from "bun:test" |
| 3 | +import * as realChildProcess from "child_process" |
| 4 | +import { chmodSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "fs" |
| 5 | +import { join } from "path" |
| 6 | + |
| 7 | +const tmpRoot = join(import.meta.dir, ".tmp-extra-codex") |
| 8 | +const defaultDbtPath = join(tmpRoot, "bin", "dbt") |
| 9 | +const originalAltimateDbtPath = process.env.ALTIMATE_DBT_PATH |
| 10 | + |
| 11 | +const mockExecFile = mock((cmd: string, args: string[], opts: any, cb: Function) => { |
| 12 | + cb(null, "", "") |
| 13 | +}) |
| 14 | + |
| 15 | +mock.module("child_process", () => ({ |
| 16 | + ...realChildProcess, |
| 17 | + execFile: mockExecFile, |
| 18 | +})) |
| 19 | + |
| 20 | +const { configure, execDbtShow, execDbtCompileInline } = await import("../src/dbt-cli") |
| 21 | + |
| 22 | +function makeFakeExecutable(path: string) { |
| 23 | + mkdirSync(join(path, ".."), { recursive: true }) |
| 24 | + writeFileSync(path, "#!/bin/sh\nexit 0\n") |
| 25 | + chmodSync(path, 0o755) |
| 26 | +} |
| 27 | + |
| 28 | +function commandFailed(message: string, fields: Record<string, unknown> = {}) { |
| 29 | + const err: any = new Error(message) |
| 30 | + Object.assign(err, fields) |
| 31 | + return err |
| 32 | +} |
| 33 | + |
| 34 | +describe("dbt-cli extra regression coverage for PR #933", () => { |
| 35 | + beforeEach(() => { |
| 36 | + rmSync(tmpRoot, { recursive: true, force: true }) |
| 37 | + makeFakeExecutable(defaultDbtPath) |
| 38 | + process.env.ALTIMATE_DBT_PATH = defaultDbtPath |
| 39 | + configure({}) |
| 40 | + mockExecFile.mockReset() |
| 41 | + }) |
| 42 | + |
| 43 | + afterEach(() => { |
| 44 | + rmSync(tmpRoot, { recursive: true, force: true }) |
| 45 | + if (originalAltimateDbtPath === undefined) delete process.env.ALTIMATE_DBT_PATH |
| 46 | + else process.env.ALTIMATE_DBT_PATH = originalAltimateDbtPath |
| 47 | + configure({}) |
| 48 | + }) |
| 49 | + |
| 50 | + test("execDbtShow returns the complete query result shape from mixed JSON and garbage stdout", async () => { |
| 51 | + const rawSql = "select 1 as n" |
| 52 | + const compiledSql = "SELECT 1 AS n" |
| 53 | + const stdout = [ |
| 54 | + "", |
| 55 | + "not json at all", |
| 56 | + JSON.stringify({ info: { level: "info", msg: "Running with dbt=1.9.0" } }), |
| 57 | + "{truncated", |
| 58 | + JSON.stringify({ data: { compiled_sql: compiledSql } }), |
| 59 | + JSON.stringify({ data: { preview: JSON.stringify([{ n: 1, label: "one" }]) } }), |
| 60 | + ].join("\n") |
| 61 | + |
| 62 | + mockExecFile.mockImplementation((_cmd: string, _args: string[], _opts: any, cb: Function) => { |
| 63 | + cb(null, stdout, "") |
| 64 | + }) |
| 65 | + |
| 66 | + const result = await execDbtShow(rawSql) |
| 67 | + expect(result).toEqual({ |
| 68 | + columnNames: ["n", "label"], |
| 69 | + columnTypes: ["string", "string"], |
| 70 | + data: [{ n: 1, label: "one" }], |
| 71 | + rawSql, |
| 72 | + compiledSql, |
| 73 | + }) |
| 74 | + }) |
| 75 | + |
| 76 | + test("execDbtShow treats an empty preview array as a successful zero-row result without fallback", async () => { |
| 77 | + let calls = 0 |
| 78 | + mockExecFile.mockImplementation((_cmd: string, _args: string[], _opts: any, cb: Function) => { |
| 79 | + calls++ |
| 80 | + cb(null, JSON.stringify({ result: { preview: [], sql: "SELECT * FROM empty_table" } }), "") |
| 81 | + }) |
| 82 | + |
| 83 | + const result = await execDbtShow("select * from empty_table") |
| 84 | + expect(result.columnNames).toEqual([]) |
| 85 | + expect(result.columnTypes).toEqual([]) |
| 86 | + expect(result.data).toEqual([]) |
| 87 | + expect(result.compiledSql).toBe("SELECT * FROM empty_table") |
| 88 | + expect(calls).toBe(1) |
| 89 | + }) |
| 90 | + |
| 91 | + test("execDbtShow passes --limit 0 through to both JSON mode and plain-text fallback", async () => { |
| 92 | + const seenArgs: string[][] = [] |
| 93 | + mockExecFile.mockImplementation((_cmd: string, args: string[], _opts: any, cb: Function) => { |
| 94 | + seenArgs.push([...args]) |
| 95 | + if (seenArgs.length === 1) { |
| 96 | + cb(null, JSON.stringify({ info: { msg: "unrecognized successful shape" } }), "") |
| 97 | + } else { |
| 98 | + cb(null, ["| n |", "| - |", "| 1 |"].join("\n"), "") |
| 99 | + } |
| 100 | + }) |
| 101 | + |
| 102 | + const result = await execDbtShow("select 1 as n", 0) |
| 103 | + expect(result.data).toEqual([{ n: "1" }]) |
| 104 | + expect(seenArgs).toHaveLength(2) |
| 105 | + expect(seenArgs[0]).toContain("--limit") |
| 106 | + expect(seenArgs[0]?.[seenArgs[0].indexOf("--limit") + 1]).toBe("0") |
| 107 | + expect(seenArgs[1]).toEqual(["show", "--inline", "select 1 as n", "--limit", "0"]) |
| 108 | + }) |
| 109 | + |
| 110 | + test("execDbtShow omits --limit entirely when the limit is undefined", async () => { |
| 111 | + mockExecFile.mockImplementation((_cmd: string, args: string[], _opts: any, cb: Function) => { |
| 112 | + expect(args).not.toContain("--limit") |
| 113 | + cb(null, JSON.stringify({ data: { preview: '[{"n": 1}]' } }), "") |
| 114 | + }) |
| 115 | + |
| 116 | + await expect(execDbtShow("select 1")).resolves.toMatchObject({ data: [{ n: 1 }] }) |
| 117 | + }) |
| 118 | + |
| 119 | + test("execDbtShow uses the last error-level log event across top-level and nested dbt shapes", async () => { |
| 120 | + const sensitiveSql = "select 'do_not_log_this_show_secret' as token" |
| 121 | + const stdout = [ |
| 122 | + JSON.stringify({ level: "error", msg: "Encountered an error:" }), |
| 123 | + JSON.stringify({ info: { level: "error", msg: "Compilation Error: undefined macro final_macro" } }), |
| 124 | + ].join("\n") |
| 125 | + |
| 126 | + mockExecFile.mockImplementation((_cmd: string, _args: string[], _opts: any, cb: Function) => { |
| 127 | + cb(commandFailed(`Command failed: dbt show --inline ${sensitiveSql}`, { code: 1 }), stdout, "generic stderr") |
| 128 | + }) |
| 129 | + |
| 130 | + const caught = (await execDbtShow(sensitiveSql).catch((e) => e)) as Error |
| 131 | + expect(caught.message).toBe("Compilation Error: undefined macro final_macro") |
| 132 | + expect(caught.message).not.toContain("Encountered an error") |
| 133 | + expect(caught.message).not.toContain("do_not_log_this_show_secret") |
| 134 | + }) |
| 135 | + |
| 136 | + test("execDbtShow strips nested ANSI SGR sequences from structured error text", async () => { |
| 137 | + const stdout = JSON.stringify({ |
| 138 | + info: { |
| 139 | + level: "error", |
| 140 | + msg: "\u001b[1mCompilation \u001b[31mError\u001b[0m\u001b[22m: bad ref", |
| 141 | + }, |
| 142 | + }) |
| 143 | + |
| 144 | + mockExecFile.mockImplementation((_cmd: string, _args: string[], _opts: any, cb: Function) => { |
| 145 | + cb(commandFailed("Command failed", { code: 1 }), stdout, "") |
| 146 | + }) |
| 147 | + |
| 148 | + const caught = (await execDbtShow("select 1").catch((e) => e)) as Error |
| 149 | + expect(caught.message).toBe("Compilation Error: bad ref") |
| 150 | + expect(caught.message).not.toContain("\u001b[") |
| 151 | + }) |
| 152 | + |
| 153 | + test("execDbtShow redacts SQL when JSON parsing fails and only the plain-text fallback rejects", async () => { |
| 154 | + const sensitiveSql = "select 'plain_fallback_secret' as token" |
| 155 | + let calls = 0 |
| 156 | + mockExecFile.mockImplementation((_cmd: string, _args: string[], _opts: any, cb: Function) => { |
| 157 | + calls++ |
| 158 | + if (calls === 1) { |
| 159 | + cb(null, JSON.stringify({ info: { msg: "valid but unparseable success payload" } }), "") |
| 160 | + } else { |
| 161 | + cb( |
| 162 | + commandFailed(`Command failed: dbt show --inline ${sensitiveSql}`, { code: 3 }), |
| 163 | + "", |
| 164 | + "", |
| 165 | + ) |
| 166 | + } |
| 167 | + }) |
| 168 | + |
| 169 | + const caught = (await execDbtShow(sensitiveSql).catch((e) => e)) as Error |
| 170 | + expect(caught.message).toBe( |
| 171 | + "Could not parse dbt show JSON output, and plain-text fallback failed: dbt exited with status 3", |
| 172 | + ) |
| 173 | + expect(caught.message).not.toContain("plain_fallback_secret") |
| 174 | + expect(caught.message).not.toContain("--inline") |
| 175 | + }) |
| 176 | + |
| 177 | + test("execDbtCompileInline parses compiled SQL despite mixed garbage JSON-line output", async () => { |
| 178 | + const stdout = [ |
| 179 | + "dbt wrote a non-json warning", |
| 180 | + JSON.stringify({ info: { msg: "still running" } }), |
| 181 | + "{bad", |
| 182 | + JSON.stringify({ result: { compiled_code: "SELECT id FROM analytics.customers" } }), |
| 183 | + ].join("\n") |
| 184 | + |
| 185 | + mockExecFile.mockImplementation((_cmd: string, _args: string[], _opts: any, cb: Function) => { |
| 186 | + cb(null, stdout, "") |
| 187 | + }) |
| 188 | + |
| 189 | + await expect(execDbtCompileInline("select * from {{ ref('customers') }}")).resolves.toEqual({ |
| 190 | + sql: "SELECT id FROM analytics.customers", |
| 191 | + }) |
| 192 | + }) |
| 193 | + |
| 194 | + test("execDbtCompileInline falls back to trimmed plain text when JSON mode succeeds with no SQL", async () => { |
| 195 | + const seenArgs: string[][] = [] |
| 196 | + mockExecFile.mockImplementation((_cmd: string, args: string[], _opts: any, cb: Function) => { |
| 197 | + seenArgs.push([...args]) |
| 198 | + if (seenArgs.length === 1) cb(null, JSON.stringify({ info: { msg: "done" } }), "") |
| 199 | + else cb(null, "\n SELECT 2 AS n \n", "") |
| 200 | + }) |
| 201 | + |
| 202 | + const result = await execDbtCompileInline("select 2 as n") |
| 203 | + expect(result).toEqual({ sql: "SELECT 2 AS n" }) |
| 204 | + expect(seenArgs[0]).toEqual(["compile", "--inline", "select 2 as n", "--output", "json", "--log-format", "json"]) |
| 205 | + expect(seenArgs[1]).toEqual(["compile", "--inline", "select 2 as n"]) |
| 206 | + }) |
| 207 | + |
| 208 | + test("execDbtCompileInline does not use SQL-looking stdout from a failed JSON-mode run", async () => { |
| 209 | + let calls = 0 |
| 210 | + mockExecFile.mockImplementation((_cmd: string, _args: string[], _opts: any, cb: Function) => { |
| 211 | + calls++ |
| 212 | + if (calls === 1) { |
| 213 | + const trap = JSON.stringify({ payload: { compiled: "SELECT should_not_be_returned FROM failed_stdout" } }) |
| 214 | + cb(commandFailed("Command failed", { code: 1 }), trap, "Compilation Error: missing source") |
| 215 | + } else { |
| 216 | + cb(commandFailed("Command failed", { code: 1 }), "", "Compilation Error: missing source") |
| 217 | + } |
| 218 | + }) |
| 219 | + |
| 220 | + await expect(execDbtCompileInline("select * from missing_source")).rejects.toThrow( |
| 221 | + "Compilation Error: missing source", |
| 222 | + ) |
| 223 | + expect(calls).toBe(2) |
| 224 | + }) |
| 225 | + |
| 226 | + test("execDbtCompileInline chooses the last structured error and redacts command-line SQL fallback text", async () => { |
| 227 | + const sensitiveSql = "select 'compile_inline_last_error_secret' as token" |
| 228 | + const stdout = [ |
| 229 | + JSON.stringify({ info: { level: "error", msg: "Encountered an error:" } }), |
| 230 | + JSON.stringify({ level: "error", msg: "Runtime Error: final actionable inline failure" }), |
| 231 | + ].join("\n") |
| 232 | + |
| 233 | + mockExecFile.mockImplementation((_cmd: string, _args: string[], _opts: any, cb: Function) => { |
| 234 | + cb(commandFailed(`Command failed: dbt compile --inline ${sensitiveSql}`, { code: 2 }), stdout, "") |
| 235 | + }) |
| 236 | + |
| 237 | + const caught = (await execDbtCompileInline(sensitiveSql).catch((e) => e)) as Error |
| 238 | + expect(caught.message).toBe("Runtime Error: final actionable inline failure") |
| 239 | + expect(caught.message).not.toContain("Encountered an error") |
| 240 | + expect(caught.message).not.toContain("compile_inline_last_error_secret") |
| 241 | + }) |
| 242 | + |
| 243 | + test("execDbtCompileInline surfaces spawn-time failures without adding command-line SQL", async () => { |
| 244 | + mockExecFile.mockImplementation((_cmd: string, _args: string[], _opts: any, cb: Function) => { |
| 245 | + cb(commandFailed("spawn /missing/dbt ENOENT", { code: "ENOENT" }), "", "") |
| 246 | + }) |
| 247 | + |
| 248 | + const caught = (await execDbtCompileInline("select 'spawn_secret'").catch((e) => e)) as Error |
| 249 | + expect(caught.message).toBe("dbt compile inline failed: spawn /missing/dbt ENOENT") |
| 250 | + expect(caught.message).not.toContain("spawn_secret") |
| 251 | + expect(caught.message).not.toContain("--inline") |
| 252 | + }) |
| 253 | + |
| 254 | + test("configure resets dbt resolution and run options honor pythonPath/projectRoot", async () => { |
| 255 | + delete process.env.ALTIMATE_DBT_PATH |
| 256 | + const projectRoot = join(tmpRoot, "project") |
| 257 | + const binDir = join(projectRoot, ".venv", "bin") |
| 258 | + const pythonPath = join(binDir, "python") |
| 259 | + const dbtPath = join(binDir, "dbt") |
| 260 | + makeFakeExecutable(pythonPath) |
| 261 | + makeFakeExecutable(dbtPath) |
| 262 | + configure({ pythonPath, projectRoot }) |
| 263 | + |
| 264 | + mockExecFile.mockImplementation((cmd: string, _args: string[], opts: any, cb: Function) => { |
| 265 | + expect(cmd).toBe(dbtPath) |
| 266 | + expect(opts.cwd).toBe(projectRoot) |
| 267 | + expect(String(opts.env.PATH).startsWith(`${binDir}:`)).toBe(true) |
| 268 | + cb(null, JSON.stringify({ data: { preview: '[{"ok": true}]' } }), "") |
| 269 | + }) |
| 270 | + |
| 271 | + await expect(execDbtShow("select true as ok")).resolves.toMatchObject({ data: [{ ok: true }] }) |
| 272 | + }) |
| 273 | +}) |
| 274 | + |
| 275 | +describe("install.ps1 static safety checks", () => { |
| 276 | + test("Windows installer stays Bun-standalone and does not invoke npm/node install flows", () => { |
| 277 | + const text = readFileSync(join(import.meta.dir, "../../../install.ps1"), "utf-8") |
| 278 | + const executableLines = text |
| 279 | + .split(/\r?\n/) |
| 280 | + .map((line) => line.trim()) |
| 281 | + .filter((line) => line.length > 0 && !line.startsWith("#") && !line.startsWith("Write-")) |
| 282 | + .join("\n") |
| 283 | + |
| 284 | + expect(text).toContain("Bun-compiled standalone executable") |
| 285 | + expect(text).toContain("does NOT depend on npm/Node") |
| 286 | + expect(text).toContain("github.com/AltimateAI/altimate-code/releases") |
| 287 | + expect(executableLines).not.toMatch(/\bnpm\s+(install|i)\b/i) |
| 288 | + expect(executableLines).not.toMatch(/\bnode\s+.*install\b/i) |
| 289 | + }) |
| 290 | +}) |
0 commit comments