|
| 1 | +// Adversarial / regression tests for the v0.8.2 release. |
| 2 | +// |
| 3 | +// v0.8.2 rolls up: |
| 4 | +// #882 — the native altimate-core lineage tools (`column_lineage`, |
| 5 | +// `track_lineage`) run FULLY OFFLINE via the bundled napi binary. |
| 6 | +// Two tool descriptions used to falsely claim "Requires |
| 7 | +// altimate_core.init() with API key" — a stale Python-bridge leftover |
| 8 | +// that made the reviewer agent demand a key and degrade dbt_pr_review |
| 9 | +// to lint-only. The claim was corrected to "no API key required". |
| 10 | +// #884 — hardened the regression guard: loosened the false-claim regex and |
| 11 | +// added a source sweep across every altimate-core-* tool. |
| 12 | +// |
| 13 | +// These tests attack the corrected invariant adversarially: with EVERY altimate |
| 14 | +// auth environment variable stripped, can the engine still resolve lineage |
| 15 | +// (proving the "no API key" claim at runtime, not just in description text)? |
| 16 | +// Does the offline path stay crash-safe on hostile input? And can the stale |
| 17 | +// `altimate_core.init()` marker sneak back into any tool source? |
| 18 | +import { describe, test, expect, beforeAll, afterAll } from "bun:test" |
| 19 | +import { readdirSync, readFileSync } from "node:fs" |
| 20 | +import { join } from "node:path" |
| 21 | +import * as Dispatcher from "../../src/altimate/native/dispatcher" |
| 22 | +import { registerAll } from "../../src/altimate/native/altimate-core" |
| 23 | +import { AltimateCoreColumnLineageTool } from "../../src/altimate/tools/altimate-core-column-lineage" |
| 24 | +import { AltimateCoreTrackLineageTool } from "../../src/altimate/tools/altimate-core-track-lineage" |
| 25 | + |
| 26 | +// Plausible altimate auth/account env vars. The native engine reads NONE of |
| 27 | +// these — stripping them must not change lineage behavior. That is the whole |
| 28 | +// point of #882: lineage is offline. |
| 29 | +const ALTIMATE_AUTH_ENV = [ |
| 30 | + "ALTIMATE_API_KEY", |
| 31 | + "ALTIMATE_AI_API_KEY", |
| 32 | + "ALTIMATE_INSTANCE", |
| 33 | + "ALTIMATE_URL", |
| 34 | + "ALTIMATE_TENANT", |
| 35 | +] |
| 36 | + |
| 37 | +const saved: Record<string, string | undefined> = {} |
| 38 | + |
| 39 | +beforeAll(() => { |
| 40 | + for (const k of ALTIMATE_AUTH_ENV) { |
| 41 | + saved[k] = process.env[k] |
| 42 | + delete process.env[k] |
| 43 | + } |
| 44 | + // Re-register in case Dispatcher.reset() ran in another test file. |
| 45 | + registerAll() |
| 46 | +}) |
| 47 | + |
| 48 | +afterAll(() => { |
| 49 | + for (const k of ALTIMATE_AUTH_ENV) { |
| 50 | + if (saved[k] === undefined) delete process.env[k] |
| 51 | + else process.env[k] = saved[k] |
| 52 | + } |
| 53 | +}) |
| 54 | + |
| 55 | +// --------------------------------------------------------------------------- |
| 56 | +// #882 — the "no API key" claim must hold at RUNTIME, with auth env stripped |
| 57 | +// --------------------------------------------------------------------------- |
| 58 | +describe("v0.8.2 — altimate-core lineage runs offline (no API key)", () => { |
| 59 | + test("column_lineage resolves with every altimate auth env var unset", async () => { |
| 60 | + for (const k of ALTIMATE_AUTH_ENV) expect(process.env[k]).toBeUndefined() |
| 61 | + const result = await Dispatcher.call("altimate_core.column_lineage", { |
| 62 | + sql: "SELECT id, name FROM users", |
| 63 | + }) |
| 64 | + // success=true means the handler completed offline — no auth gate fired. |
| 65 | + expect(result.success).toBe(true) |
| 66 | + expect(result.data).toBeDefined() |
| 67 | + }) |
| 68 | + |
| 69 | + test("track_lineage resolves with every altimate auth env var unset", async () => { |
| 70 | + const result = await Dispatcher.call("altimate_core.track_lineage", { |
| 71 | + queries: ["CREATE TABLE stg AS SELECT id FROM raw", "SELECT id FROM stg"], |
| 72 | + }) |
| 73 | + expect(result.success).toBe(true) |
| 74 | + expect(result.data).toBeDefined() |
| 75 | + }) |
| 76 | + |
| 77 | + // Text claim and runtime behavior must agree: a tool that SAYS "no API key" |
| 78 | + // must also WORK with no API key. Pin both together so neither can drift. |
| 79 | + test("description claim matches runtime: column_lineage", async () => { |
| 80 | + const { description } = await AltimateCoreColumnLineageTool.init() |
| 81 | + expect(description.toLowerCase()).toContain("no api key") |
| 82 | + expect(description.toLowerCase()).not.toContain("altimate_core.init") |
| 83 | + }) |
| 84 | + |
| 85 | + test("description claim matches runtime: track_lineage", async () => { |
| 86 | + const { description } = await AltimateCoreTrackLineageTool.init() |
| 87 | + expect(description.toLowerCase()).toContain("no api key") |
| 88 | + expect(description.toLowerCase()).not.toContain("altimate_core.init") |
| 89 | + }) |
| 90 | +}) |
| 91 | + |
| 92 | +// --------------------------------------------------------------------------- |
| 93 | +// #882 — the offline path must stay crash-safe on hostile input |
| 94 | +// (a review must never crash CI; bad SQL is a structured result, not a throw) |
| 95 | +// --------------------------------------------------------------------------- |
| 96 | +describe("v0.8.2 — offline lineage is crash-safe on hostile input", () => { |
| 97 | + const hostileSql = [ |
| 98 | + "", // empty |
| 99 | + " ", // whitespace |
| 100 | + "SELECT", // truncated |
| 101 | + "'; DROP TABLE users; --", // injection-shaped |
| 102 | + "SELECT __proto__, constructor FROM x", // prototype-pollution-shaped identifiers |
| 103 | + "SELECT * FROM " + "a,".repeat(500) + "b", // pathological width |
| 104 | + ] |
| 105 | + |
| 106 | + for (const sql of hostileSql) { |
| 107 | + test(`column_lineage returns a structured result (no throw) for ${JSON.stringify(sql.slice(0, 24))}`, async () => { |
| 108 | + const result = await Dispatcher.call("altimate_core.column_lineage", { sql }) |
| 109 | + // Contract: the handler returns an envelope with a boolean success flag — |
| 110 | + // semantic failure lives in the data, it never throws. |
| 111 | + expect(typeof result.success).toBe("boolean") |
| 112 | + }) |
| 113 | + } |
| 114 | + |
| 115 | + test("track_lineage tolerates an empty query list without throwing", async () => { |
| 116 | + const result = await Dispatcher.call("altimate_core.track_lineage", { queries: [] }) |
| 117 | + expect(typeof result.success).toBe("boolean") |
| 118 | + }) |
| 119 | +}) |
| 120 | + |
| 121 | +// --------------------------------------------------------------------------- |
| 122 | +// #884 — the stale `altimate_core.init()` marker must never return to any tool |
| 123 | +// --------------------------------------------------------------------------- |
| 124 | +describe("v0.8.2 — no altimate-core tool reintroduces altimate_core.init()", () => { |
| 125 | + const toolsDir = join(import.meta.dir, "../../src/altimate/tools") |
| 126 | + const files = readdirSync(toolsDir).filter((f) => f.startsWith("altimate-core-") && f.endsWith(".ts")) |
| 127 | + |
| 128 | + test("there is at least one altimate-core tool to scan", () => { |
| 129 | + expect(files.length).toBeGreaterThan(0) |
| 130 | + }) |
| 131 | + |
| 132 | + for (const file of files) { |
| 133 | + test(`${file} is free of the stale altimate_core.init() marker`, () => { |
| 134 | + const src = readFileSync(join(toolsDir, file), "utf8") |
| 135 | + expect(src).not.toContain("altimate_core.init") |
| 136 | + }) |
| 137 | + } |
| 138 | +}) |
0 commit comments