|
| 1 | +import { afterAll, beforeAll, describe, expect, test } from "bun:test" |
| 2 | +import { writeFileSync } from "node:fs" |
| 3 | +import path from "node:path" |
| 4 | +import { runReview, DEFAULT_RUBRIC, DEFAULT_REVIEW_CONFIG, type ChangedFile } from "../../src/altimate/review" |
| 5 | +import { createDispatcherRunner } from "../../src/altimate/review/runner" |
| 6 | +import { registerAll } from "../../src/altimate/native/altimate-core" |
| 7 | +import { tmpdir } from "../fixture/fixture" |
| 8 | + |
| 9 | +// --------------------------------------------------------------------------- |
| 10 | +// Full-stack E2E: dbt PR review pipeline → real Dispatcher → real altimate-core |
| 11 | +// 0.5.1 engine. NO mocks. Exercises the complete chain that the dialect wiring |
| 12 | +// (core 0.5.1 `dialect` arg) and `decidable` handling run through: |
| 13 | +// |
| 14 | +// runReview → semanticChangeLane → runner.equivalence(old, new, dialect) |
| 15 | +// → Dispatcher → altimate_core.equivalence handler |
| 16 | +// → core.checkEquivalence(sqlA, sqlB, schema, dialect) |
| 17 | +// |
| 18 | +// A real dbt manifest supplies the schema so the engine can resolve columns and |
| 19 | +// actually DECIDE equivalence rather than abstain. |
| 20 | +// --------------------------------------------------------------------------- |
| 21 | + |
| 22 | +describe("E2E: review pipeline + real engine equivalence (core 0.5.1)", () => { |
| 23 | + beforeAll(async () => { |
| 24 | + process.env.ALTIMATE_TELEMETRY_DISABLED = "true" |
| 25 | + // The DispatcherRunner calls the REAL native handlers; register them in case |
| 26 | + // another file reset the Dispatcher. |
| 27 | + registerAll() |
| 28 | + const { registerAllSql } = await import("../../src/altimate/native/sql/register") |
| 29 | + registerAllSql() |
| 30 | + }) |
| 31 | + afterAll(() => { |
| 32 | + delete process.env.ALTIMATE_TELEMETRY_DISABLED |
| 33 | + }) |
| 34 | + |
| 35 | + // A real manifest with typed columns so resolveSchema() yields a usable schema. |
| 36 | + async function reviewerWithManifest() { |
| 37 | + const tmp = await tmpdir() |
| 38 | + const manifestPath = path.join(tmp.path, "manifest.json") |
| 39 | + writeFileSync( |
| 40 | + manifestPath, |
| 41 | + JSON.stringify({ |
| 42 | + metadata: { adapter_type: "snowflake" }, |
| 43 | + nodes: { |
| 44 | + "model.demo.orders": { |
| 45 | + resource_type: "model", |
| 46 | + name: "orders", |
| 47 | + original_file_path: "models/marts/orders.sql", |
| 48 | + config: { materialized: "table" }, |
| 49 | + depends_on: { nodes: [] }, |
| 50 | + columns: { |
| 51 | + id: { name: "id", data_type: "integer" }, |
| 52 | + amount: { name: "amount", data_type: "integer" }, |
| 53 | + status: { name: "status", data_type: "varchar" }, |
| 54 | + }, |
| 55 | + }, |
| 56 | + }, |
| 57 | + sources: {}, |
| 58 | + }), |
| 59 | + ) |
| 60 | + return { tmp, runner: createDispatcherRunner({ manifestPath }) } |
| 61 | + } |
| 62 | + |
| 63 | + async function review(oldSql: string, newSql: string, dialect = "snowflake") { |
| 64 | + const { tmp, runner } = await reviewerWithManifest() |
| 65 | + try { |
| 66 | + const files: ChangedFile[] = [{ path: "models/marts/orders.sql", status: "modified", diff: "+changed" }] |
| 67 | + const env = await runReview({ |
| 68 | + changedFiles: files, |
| 69 | + config: { ...DEFAULT_REVIEW_CONFIG, reviewers: ["semantic_change"], dialect, ai: false }, |
| 70 | + rubric: DEFAULT_RUBRIC, |
| 71 | + mode: "comment", |
| 72 | + runner, |
| 73 | + getContent: async (_f, side) => (side === "new" ? newSql : oldSql), |
| 74 | + getCompiled: async (_f, side) => (side === "new" ? newSql : oldSql), |
| 75 | + generatedAt: "2026-06-10T00:00:00Z", |
| 76 | + }) |
| 77 | + return env |
| 78 | + } finally { |
| 79 | + await tmp[Symbol.asyncDispose]?.() |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + const eqFindings = (env: Awaited<ReturnType<typeof review>>) => |
| 84 | + env.findings.filter((f) => f.evidence?.tool === "altimate_core.equivalence") |
| 85 | + |
| 86 | + test("real engine DECIDES a non-equivalent rewrite through the full pipeline", async () => { |
| 87 | + // `amount > 5` vs `amount > 6` is a genuine row-changing predicate change. |
| 88 | + const env = await review( |
| 89 | + "select id from orders where amount > 5", |
| 90 | + "select id from orders where amount > 6", |
| 91 | + ) |
| 92 | + const findings = eqFindings(env) |
| 93 | + expect(findings.length).toBeGreaterThan(0) |
| 94 | + const f = findings[0] |
| 95 | + expect(f.category).toBe("semantic_change") |
| 96 | + expect((f.evidence?.result as any)?.equivalent).toBe(false) |
| 97 | + // The engine names the concrete predicate difference (not a vague abstention). |
| 98 | + const diffs = (f.evidence?.result as any)?.differences ?? [] |
| 99 | + expect(diffs.length).toBeGreaterThan(0) |
| 100 | + }) |
| 101 | + |
| 102 | + test("real engine PROVES an equivalent refactor → lane stays silent (no false positive)", async () => { |
| 103 | + // AND-conjunct reorder is provably equivalent; the reviewer must not nitpick it. |
| 104 | + const env = await review( |
| 105 | + "select id from orders where amount > 5 and status = 'x'", |
| 106 | + "select id from orders where status = 'x' and amount > 5", |
| 107 | + ) |
| 108 | + expect(eqFindings(env)).toEqual([]) |
| 109 | + expect(env.verdict).toBe("APPROVE") |
| 110 | + }) |
| 111 | + |
| 112 | + test("identical compiled SQL → equivalence lane is skipped entirely", async () => { |
| 113 | + const sql = "select id from orders where amount > 5" |
| 114 | + const env = await review(sql, sql) |
| 115 | + expect(eqFindings(env)).toEqual([]) |
| 116 | + expect(env.verdict).toBe("APPROVE") |
| 117 | + }) |
| 118 | + |
| 119 | + test("column projection change (drop a column) is decided NOT equivalent", async () => { |
| 120 | + const env = await review( |
| 121 | + "select id, amount from orders", |
| 122 | + "select id from orders", |
| 123 | + ) |
| 124 | + const findings = eqFindings(env) |
| 125 | + expect(findings.length).toBeGreaterThan(0) |
| 126 | + expect((findings[0].evidence?.result as any)?.equivalent).toBe(false) |
| 127 | + }) |
| 128 | + |
| 129 | + test("DEFAULT config dialect (empty string) still DECIDES — no engine throw", async () => { |
| 130 | + // ReviewConfig.dialect defaults to "". The engine throws on an unknown dialect |
| 131 | + // "", so without coercion the lane would abstain on EVERY change under the |
| 132 | + // default config. This drives the full pipeline with dialect="" and asserts |
| 133 | + // the non-equivalent change is still caught (decided), proving the coercion. |
| 134 | + const env = await review( |
| 135 | + "select id from orders where amount > 5", |
| 136 | + "select id from orders where amount > 6", |
| 137 | + "", // <- default ReviewConfig.dialect |
| 138 | + ) |
| 139 | + const findings = eqFindings(env) |
| 140 | + expect(findings.length).toBeGreaterThan(0) |
| 141 | + expect((findings[0].evidence?.result as any)?.equivalent).toBe(false) |
| 142 | + }) |
| 143 | +}) |
0 commit comments