diff --git a/.opencode/skills/dbt-analyze/references/altimate-dbt-commands.md b/.opencode/skills/dbt-analyze/references/altimate-dbt-commands.md index 8109ac84d2..6bdd9f694b 100644 --- a/.opencode/skills/dbt-analyze/references/altimate-dbt-commands.md +++ b/.opencode/skills/dbt-analyze/references/altimate-dbt-commands.md @@ -49,6 +49,8 @@ altimate-dbt children --model # downstream models altimate-dbt parents --model # upstream models ``` +When the project check fails (a broken profile, a missing env var) and an earlier parse left a manifest, `children` and `parents` answer `{ stale_manifest, models }` instead of a list: `models` comes from that last successful parse and may predate the current files, so check for it before relying on the answer for impact analysis. With no earlier manifest they answer an error. + ## Packages ```bash diff --git a/.opencode/skills/dbt-develop/references/altimate-dbt-commands.md b/.opencode/skills/dbt-develop/references/altimate-dbt-commands.md index 8109ac84d2..6bdd9f694b 100644 --- a/.opencode/skills/dbt-develop/references/altimate-dbt-commands.md +++ b/.opencode/skills/dbt-develop/references/altimate-dbt-commands.md @@ -49,6 +49,8 @@ altimate-dbt children --model # downstream models altimate-dbt parents --model # upstream models ``` +When the project check fails (a broken profile, a missing env var) and an earlier parse left a manifest, `children` and `parents` answer `{ stale_manifest, models }` instead of a list: `models` comes from that last successful parse and may predate the current files, so check for it before relying on the answer for impact analysis. With no earlier manifest they answer an error. + ## Packages ```bash diff --git a/.opencode/skills/dbt-docs/references/altimate-dbt-commands.md b/.opencode/skills/dbt-docs/references/altimate-dbt-commands.md index 8109ac84d2..6bdd9f694b 100644 --- a/.opencode/skills/dbt-docs/references/altimate-dbt-commands.md +++ b/.opencode/skills/dbt-docs/references/altimate-dbt-commands.md @@ -49,6 +49,8 @@ altimate-dbt children --model # downstream models altimate-dbt parents --model # upstream models ``` +When the project check fails (a broken profile, a missing env var) and an earlier parse left a manifest, `children` and `parents` answer `{ stale_manifest, models }` instead of a list: `models` comes from that last successful parse and may predate the current files, so check for it before relying on the answer for impact analysis. With no earlier manifest they answer an error. + ## Packages ```bash diff --git a/.opencode/skills/dbt-test/references/altimate-dbt-commands.md b/.opencode/skills/dbt-test/references/altimate-dbt-commands.md index 8109ac84d2..6bdd9f694b 100644 --- a/.opencode/skills/dbt-test/references/altimate-dbt-commands.md +++ b/.opencode/skills/dbt-test/references/altimate-dbt-commands.md @@ -49,6 +49,8 @@ altimate-dbt children --model # downstream models altimate-dbt parents --model # upstream models ``` +When the project check fails (a broken profile, a missing env var) and an earlier parse left a manifest, `children` and `parents` answer `{ stale_manifest, models }` instead of a list: `models` comes from that last successful parse and may predate the current files, so check for it before relying on the answer for impact analysis. With no earlier manifest they answer an error. + ## Packages ```bash diff --git a/.opencode/skills/dbt-troubleshoot/references/altimate-dbt-commands.md b/.opencode/skills/dbt-troubleshoot/references/altimate-dbt-commands.md index 8109ac84d2..6bdd9f694b 100644 --- a/.opencode/skills/dbt-troubleshoot/references/altimate-dbt-commands.md +++ b/.opencode/skills/dbt-troubleshoot/references/altimate-dbt-commands.md @@ -49,6 +49,8 @@ altimate-dbt children --model # downstream models altimate-dbt parents --model # upstream models ``` +When the project check fails (a broken profile, a missing env var) and an earlier parse left a manifest, `children` and `parents` answer `{ stale_manifest, models }` instead of a list: `models` comes from that last successful parse and may predate the current files, so check for it before relying on the answer for impact analysis. With no earlier manifest they answer an error. + ## Packages ```bash diff --git a/.opencode/skills/dbt-unit-tests/references/altimate-dbt-commands.md b/.opencode/skills/dbt-unit-tests/references/altimate-dbt-commands.md index 8109ac84d2..6bdd9f694b 100644 --- a/.opencode/skills/dbt-unit-tests/references/altimate-dbt-commands.md +++ b/.opencode/skills/dbt-unit-tests/references/altimate-dbt-commands.md @@ -49,6 +49,8 @@ altimate-dbt children --model # downstream models altimate-dbt parents --model # upstream models ``` +When the project check fails (a broken profile, a missing env var) and an earlier parse left a manifest, `children` and `parents` answer `{ stale_manifest, models }` instead of a list: `models` comes from that last successful parse and may predate the current files, so check for it before relying on the answer for impact analysis. With no earlier manifest they answer an error. + ## Packages ```bash diff --git a/bun.lock b/bun.lock index 268aeb726c..9d11e718fb 100644 --- a/bun.lock +++ b/bun.lock @@ -150,7 +150,7 @@ "altimate-dbt": "./bin/altimate-dbt", }, "dependencies": { - "@altimateai/dbt-integration": "^0.3.19", + "@altimateai/dbt-integration": "^0.3.20", }, "devDependencies": { "@tsconfig/bun": "catalog:", @@ -659,7 +659,7 @@ "@altimateai/altimate-core-win32-x64-msvc": ["@altimateai/altimate-core-win32-x64-msvc@0.9.0", "", { "os": "win32", "cpu": "x64" }, "sha512-MoELhLPzJGAxJMtL7C/g/gj464I9pbSmfJ6IamhFwsyz7nfUrKOkAnixVXuQrLEJ9I+sVZJuPMq2H1HUCkpOwA=="], - "@altimateai/dbt-integration": ["@altimateai/dbt-integration@0.3.19", "", { "dependencies": { "@altimateai/altimate-core": "0.5.0", "node-abort-controller": "^3.1.1", "node-fetch": "^3.3.2", "semver": "^7.6.3", "yaml": "^2.5.0" } }, "sha512-FsExJ+FHUzR6DEm9QNhPDBr2CscstkGgRZlU+FOIQghhPchJvnGGBSXBGz5hYiED2WPMeKUTOZk70uh5GQ8oUw=="], + "@altimateai/dbt-integration": ["@altimateai/dbt-integration@0.3.20", "", { "dependencies": { "@altimateai/altimate-core": "0.5.0", "node-abort-controller": "^3.1.1", "node-fetch": "^3.3.2", "semver": "^7.6.3", "yaml": "^2.5.0" } }, "sha512-K08nWtcTfmW9912ja3AR9vNCNjiWHrYDB5NaXbSeTN6f/MOnXbWMcLiKfYBiOm7kpLvaz5TZkbpsQDGeC4YCvg=="], "@altimateai/dbt-tools": ["@altimateai/dbt-tools@workspace:packages/dbt-tools"], diff --git a/packages/dbt-tools/package.json b/packages/dbt-tools/package.json index 44174b14b5..f063fb4683 100644 --- a/packages/dbt-tools/package.json +++ b/packages/dbt-tools/package.json @@ -9,11 +9,11 @@ "scripts": { "build": "bun build src/index.ts --outdir dist --target node --format esm && bun run script/copy-python.ts", "typecheck": "tsc --noEmit", - "test": "bun test test/cli.test.ts test/config.test.ts test/dbt-cli.test.ts test/dbt-resolve.test.ts test/adapter-bun.test.ts test/stale-manifest.test.ts test/build-integrity.test.ts test/adapter-target-path.test.ts --timeout 30000", + "test": "bun test test/check.test.ts test/cli.test.ts test/config.test.ts test/dbt-cli.test.ts test/dbt-resolve.test.ts test/adapter-bun.test.ts test/stale-manifest.test.ts test/build-integrity.test.ts test/adapter-target-path.test.ts --timeout 30000", "test:e2e": "bun test test/e2e/ --timeout 300000" }, "dependencies": { - "@altimateai/dbt-integration": "^0.3.19" + "@altimateai/dbt-integration": "^0.3.20" }, "devDependencies": { "@tsconfig/bun": "catalog:", diff --git a/packages/dbt-tools/src/adapter.ts b/packages/dbt-tools/src/adapter.ts index 4786164c1a..7a8c180c3f 100644 --- a/packages/dbt-tools/src/adapter.ts +++ b/packages/dbt-tools/src/adapter.ts @@ -98,7 +98,8 @@ function env(cfg: Config, options: CreateOptions): RuntimePythonEnvironment { const vars = { ...process.env } if (options.targetPath) vars.DBT_TARGET_PATH = options.targetPath return { - pythonPath: cfg.pythonPath, + // A Fusion config may have none: dbt-integration runs Fusion with an empty one. + pythonPath: cfg.pythonPath ?? "", getEnvironmentVariables: () => vars, } } diff --git a/packages/dbt-tools/src/check.ts b/packages/dbt-tools/src/check.ts index 4e5c4d2290..92953d5406 100644 --- a/packages/dbt-tools/src/check.ts +++ b/packages/dbt-tools/src/check.ts @@ -39,12 +39,49 @@ function project(root: string): Status { return { ok: true } } -export async function all(cfg: Config) { - const checks = { - python: await python(cfg.pythonPath), - dbt: cfg.pythonPath && existsSync(cfg.pythonPath) ? await dbt(cfg.pythonPath) : { ok: false as const, error: "Skipped (Python not found)", fix: "Fix Python first" }, - project: project(cfg.projectRoot), +type FusionLookup = { + pythonPath?: string + cwd?: string + env?: Record + timeoutMs?: number + skipScripts?: boolean +} + +/** How the Fusion check finds dbt: dbt-integration's own lookup, loaded only in Fusion mode. + * A field so tests can replace it. */ +export const fusionBinary = { + find: async (options: FusionLookup) => (await import("@altimateai/dbt-integration")).findDbtV2Binary(options), +} + +async function fusion(cfg: Config): Promise { + // Judge candidates as the adapter does: it runs any `dbt` whose `--version` reports 2.x, + // script launchers (asdf, nix and company wrappers) included, and allows 15 s for it. + const binary = await fusionBinary.find({ + pythonPath: cfg.pythonPath || undefined, + cwd: cfg.projectRoot || undefined, + env: process.env, + skipScripts: false, + timeoutMs: 15_000, + }) + if (binary) return { ok: true } + const nextTo = cfg.pythonPath ? `next to ${cfg.pythonPath}, ` : "" + return { + ok: false, + error: `dbt Fusion was not found: no dbt 2.x binary ${nextTo}in ~/.local/bin or on PATH`, + fix: "Install dbt Fusion so that `dbt --version` reports 2.x, or set dbtIntegration to core in the altimate-dbt config", } +} + +export async function all(cfg: Config) { + // dbt Fusion is a native binary: it needs no Python, and no dbt-core in one. + const checks: Record = + cfg.dbtIntegration === "fusion" + ? { fusion: await fusion(cfg), project: project(cfg.projectRoot) } + : { + python: await python(cfg.pythonPath), + dbt: cfg.pythonPath && existsSync(cfg.pythonPath) ? await dbt(cfg.pythonPath) : { ok: false as const, error: "Skipped (Python not found)", fix: "Fix Python first" }, + project: project(cfg.projectRoot), + } const passed = Object.values(checks).every((c) => c.ok) return { passed, checks } } diff --git a/packages/dbt-tools/test/check.test.ts b/packages/dbt-tools/test/check.test.ts new file mode 100644 index 0000000000..5024c7527e --- /dev/null +++ b/packages/dbt-tools/test/check.test.ts @@ -0,0 +1,187 @@ +import { afterEach, describe, expect, test } from "bun:test" +import { execFileSync } from "child_process" +import { existsSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from "fs" +import { homedir, tmpdir } from "os" +import { join } from "path" +import { all, fusionBinary, validate } from "../src/check" +import type { Config } from "../src/config" + +// dbt Fusion is a native binary, so a Fusion user may have no Python at all. The lookup for a +// dbt 2.x binary is replaced here; the real one is dbt-integration's `findDbtV2Binary`. +const realFind = fusionBinary.find +const dirs: string[] = [] + +afterEach(() => { + fusionBinary.find = realFind + for (const d of dirs.splice(0)) rmSync(d, { recursive: true, force: true }) +}) + +function project(): string { + const dir = mkdtempSync(join(tmpdir(), "dbt-check-")) + dirs.push(dir) + writeFileSync(join(dir, "dbt_project.yml"), "name: p\n") + return dir +} + +function config(overrides: Partial): Config { + return { projectRoot: project(), pythonPath: "", dbtIntegration: "fusion", queryLimit: 500, ...overrides } +} + +const FUSION = { path: "/home/u/.local/bin/dbt", version: [2, 0, 9], location: "installer" as const } + +describe("prerequisites in dbt Fusion mode", () => { + test("need no Python when a dbt 2.x binary resolves", async () => { + const asked: unknown[] = [] + fusionBinary.find = async (options) => { + asked.push(options) + return FUSION + } + const cfg = config({}) + + const result = await all(cfg) + + expect(result.passed).toBe(true) + expect(Object.keys(result.checks).sort()).toEqual(["fusion", "project"]) + expect(asked[0]).toMatchObject({ pythonPath: undefined, cwd: cfg.projectRoot }) + // As the adapter judges a candidate: script launchers count, and `--version` gets 15 s. + expect(asked[0]).toMatchObject({ skipScripts: false, timeoutMs: 15_000 }) + expect(await validate(cfg)).toBeNull() + }) + + test("need no Python path in the config at all", async () => { + fusionBinary.find = async () => FUSION + const cfg = config({}) + delete (cfg as Partial).pythonPath + + expect((await all(cfg)).passed).toBe(true) + }) + + test("look for dbt next to a given interpreter, which needn't exist", async () => { + const asked: unknown[] = [] + fusionBinary.find = async (options) => { + asked.push(options) + return FUSION + } + + const result = await all(config({ pythonPath: "/no/venv/bin/python" })) + + expect(result.passed).toBe(true) + expect(asked[0]).toMatchObject({ pythonPath: "/no/venv/bin/python" }) + }) + + test("fail, and say where they looked, when no dbt 2.x binary resolves", async () => { + fusionBinary.find = async () => undefined + + const issue = await validate(config({})) + + expect(issue).toContain("Prerequisites not met") + expect(issue).toContain("fusion: dbt Fusion was not found: no dbt 2.x binary in ~/.local/bin or on PATH") + }) + + test("name the interpreter they looked next to, when the config has one", async () => { + fusionBinary.find = async () => undefined + + const issue = await validate(config({ pythonPath: "/no/venv/bin/python" })) + + expect(issue).toContain( + "fusion: dbt Fusion was not found: no dbt 2.x binary next to /no/venv/bin/python, in ~/.local/bin or on PATH", + ) + }) + + test("still require the project", async () => { + fusionBinary.find = async () => FUSION + + const result = await all(config({ projectRoot: "/definitely/not/a/project" })) + + expect(result.passed).toBe(false) + expect(result.checks.project?.ok).toBe(false) + }) +}) + +// The real lookup, through the CLI in its own process: another test file here mocks +// `child_process`, and bun's module mocks reach every file of the run. A stand-in `dbt` is the +// only thing on PATH. The lookup always tries ~/.local/bin/dbt first (Bun ignores HOME for that), +// so the rejection test can't run where that is a real Fusion. +function realFusionInLocalBin(): boolean { + const dbt = join(homedir(), ".local", "bin", "dbt") + if (!existsSync(dbt)) return false + try { + return /^dbt(-fusion)? 2\./m.test(execFileSync(dbt, ["--version"], { encoding: "utf-8", timeout: 15_000 })) + } catch { + return false + } +} + +describe.skipIf(process.platform === "win32")("the Fusion lookup, run by `altimate-dbt doctor`", () => { + const entry = join(import.meta.dir, "../src/index.ts") + + /** `doctor` in Fusion mode with no Python path, and a `dbt` on PATH that prints `versionLine`. */ + function doctorWith(versionLine: string): { passed: boolean; checks: { check: string; status: string }[] } { + const home = mkdtempSync(join(tmpdir(), "dbt-check-home-")) + dirs.push(home) + const bin = join(home, "bin") + mkdirSync(bin) + writeFileSync(join(bin, "dbt"), `#!/bin/sh\nif [ "$1" = "--version" ]; then echo "${versionLine}"; fi\nexit 0\n`, { + mode: 0o755, + }) + mkdirSync(join(home, ".altimate-code")) + writeFileSync( + join(home, ".altimate-code", "dbt.json"), + JSON.stringify({ projectRoot: project(), dbtIntegration: "fusion", queryLimit: 500 }), + ) + const result = Bun.spawnSync([process.execPath, entry, "doctor"], { env: { HOME: home, PATH: bin } }) + return JSON.parse(result.stdout.toString()) + } + + test("accepts a Fusion behind a script launcher that reports 2.x", () => { + const report = doctorWith("dbt 2.0.9") + + expect(report.passed).toBe(true) + expect(report.checks).toContainEqual({ check: "fusion", status: "ok" }) + }) + + test.skipIf(realFusionInLocalBin())("still rejects dbt Core", () => { + const report = doctorWith("Core: installed: 1.10.23") + + expect(report.passed).toBe(false) + expect(report.checks).toContainEqual(expect.objectContaining({ check: "fusion", status: "fail" })) + }) +}) + +describe("prerequisites in other modes", () => { + const noFusion = async (): Promise => { + throw new Error("the Fusion lookup must not run outside Fusion mode") + } + + test("still require a Python path, and never look for Fusion", async () => { + fusionBinary.find = noFusion + + for (const dbtIntegration of ["core", "corecommand", "cloud"]) { + const result = await all(config({ dbtIntegration })) + expect(result.passed).toBe(false) + expect(Object.keys(result.checks).sort()).toEqual(["dbt", "project", "python"]) + expect(result.checks).toMatchObject({ python: { ok: false, error: "No Python path configured" } }) + } + }) + + // A stand-in interpreter that runs but has no dbt-core: `--version` works, every other run fails. + test.skipIf(process.platform === "win32")("still require dbt-core in a working Python", async () => { + fusionBinary.find = noFusion + const dir = mkdtempSync(join(tmpdir(), "dbt-check-py-")) + dirs.push(dir) + const python = join(dir, "python") + writeFileSync(python, '#!/bin/sh\nif [ "$1" = "--version" ]; then echo "Python 3.11.0"; exit 0; fi\nexit 1\n', { + mode: 0o755, + }) + + for (const dbtIntegration of ["core", "corecommand", "cloud"]) { + const result = await all(config({ dbtIntegration, pythonPath: python })) + expect(result.passed).toBe(false) + expect(result.checks.python?.ok).toBe(true) + expect(result.checks).toMatchObject({ + dbt: { ok: false, error: "dbt-core is not installed in the configured Python environment" }, + }) + expect(result.checks.project?.ok).toBe(true) + } + }) +}) diff --git a/packages/dbt-tools/test/e2e/dbt-versions.test.ts b/packages/dbt-tools/test/e2e/dbt-versions.test.ts index a90586f872..716c91746f 100644 --- a/packages/dbt-tools/test/e2e/dbt-versions.test.ts +++ b/packages/dbt-tools/test/e2e/dbt-versions.test.ts @@ -20,7 +20,7 @@ import { describe, test, expect, beforeAll, afterAll } from "bun:test" import { execFileSync, execSync } from "child_process" -import { existsSync, mkdtempSync, cpSync, rmSync, readdirSync } from "fs" +import { existsSync, mkdtempSync, cpSync, rmSync, readdirSync, readFileSync, writeFileSync } from "fs" import { join, resolve } from "path" import { tmpdir } from "os" @@ -400,6 +400,28 @@ describe.skipIf(!HAS_DBT)("altimate-dbt e2e", () => { // ----- dbt ls / children / parents ----- describe("graph (children/parents)", () => { + test( + "on a stale manifest, children answer from the last successful parse and mark no model external", + () => { + // Rename the profile so the project check fails. The build left target/manifest.json, + // so the answer comes from it: { stale_manifest, models }. Its nodes belong to this + // project, so none may be marked as an external project. + const profiles = join(workDir, "profiles.yml") + const original = readFileSync(profiles, "utf-8") + writeFileSync(profiles, original.replace(/^test_jaffle_shop:/m, "test_jaffle_shop_renamed:")) + try { + const out = altDbt(version.pythonPath, workDir, ["children", "--model", "stg_customers"]) + expect(out.stale_manifest).toBeDefined() + const labels = (out.models ?? []).map((m: { label: string }) => m.label) + expect(labels).toContain("customers") + expect(out.models.every((m: { isExternalProject: boolean }) => m.isExternalProject === false)).toBe(true) + } finally { + writeFileSync(profiles, original) + } + }, + TEST_TIMEOUT, + ) + test( "dbt ls lists models", () => { diff --git a/packages/opencode/src/altimate/validators/dbt-schema-verify.ts b/packages/opencode/src/altimate/validators/dbt-schema-verify.ts index df629694df..aae25dfa80 100644 --- a/packages/opencode/src/altimate/validators/dbt-schema-verify.ts +++ b/packages/opencode/src/altimate/validators/dbt-schema-verify.ts @@ -149,7 +149,10 @@ function formatFixHint(mismatches: SchemaVerifyOutput[]): string { function leadWithStaleManifestNote(results: SchemaVerifyOutput[], hint: string): string { const stale = results.find((r) => r.stale_manifest)?.stale_manifest if (!stale) return hint - const written = typeof stale.written_at === "string" ? ` (written ${sanitizeForPrompt(stale.written_at, 40)})` : "" + const written = + typeof stale.written_at === "string" && stale.written_at.trim() + ? ` (written ${sanitizeForPrompt(stale.written_at, 40)})` + : "" return `The dbt project check failed, so schema-verify read the manifest of the last successful parse${written}. Its spec may not match schema.yml as it is now.\n\n${hint}` } diff --git a/packages/opencode/test/altimate/validators/dbt-schema-verify-stale.test.ts b/packages/opencode/test/altimate/validators/dbt-schema-verify-stale.test.ts index efbcf47aeb..f2c3dbda38 100644 --- a/packages/opencode/test/altimate/validators/dbt-schema-verify-stale.test.ts +++ b/packages/opencode/test/altimate/validators/dbt-schema-verify-stale.test.ts @@ -24,11 +24,17 @@ const MISMATCH = { columns_reordered: [], type_mismatches: [], } +// The validator's message for MISMATCH on a current manifest, as before the stale note existed. +const CURRENT_REASON = + "1 of 1 models you edited have a column-shape mismatch against schema.yml: orders. The build may be green, but equality tests will fail." +const CURRENT_FIX_HINT = + "Model `orders`:\n • Columns in your model NOT in spec — REMOVE: note\n\nFix the model SQL to match the schema.yml spec (do not edit the spec), rebuild, and the harness will re-check before declaring done." const NOTE_START = "The dbt project check failed, so schema-verify read the manifest of the last successful parse" let root: string let project: string let answerFile: string +let answersDir: string let originalPath: string beforeAll(async () => { @@ -36,7 +42,14 @@ beforeAll(async () => { const bin = join(root, "bin") await fs.mkdir(bin) answerFile = join(root, "answer.json") - await fs.writeFile(join(bin, "altimate-dbt"), `#!/bin/sh\ncat "${answerFile}"\n`, { mode: 0o755 }) + answersDir = join(root, "answers") + await fs.mkdir(answersDir) + // Called as `altimate-dbt schema-verify --model `: answers/.json when there is one. + await fs.writeFile( + join(bin, "altimate-dbt"), + `#!/bin/sh\nif [ -f "${answersDir}/$3.json" ]; then cat "${answersDir}/$3.json"; else cat "${answerFile}"; fi\n`, + { mode: 0o755 }, + ) originalPath = process.env.PATH ?? "" process.env.PATH = `${bin}:${originalPath}` project = join(root, "project") @@ -51,6 +64,9 @@ afterAll(async () => { afterEach(async () => { await fs.rm(answerFile, { force: true }) + await fs.rm(answersDir, { recursive: true, force: true }) + await fs.mkdir(answersDir) + await fs.rm(join(project, "models", "customers.sql"), { force: true }) }) /** Runs the validator over one model edited this session, with `answer` as schema-verify's answer. */ @@ -82,10 +98,42 @@ describe("dbt-schema-verify on a stale manifest", () => { expect(result.fixHint).toContain("altimate-dbt schema-verify ") }) - test("a current manifest's message has no note", async () => { + test("a current manifest's message is the message from before the note", async () => { const result = await check(MISMATCH) expect(result.ok).toBe(false) - expect(result.fixHint).not.toContain(NOTE_START) + expect(result.reason).toBe(CURRENT_REASON) + expect(result.fixHint).toBe(CURRENT_FIX_HINT) + }) + + test("the note only leads the message, which is otherwise the same", async () => { + const stale = await check({ stale_manifest: STALE, ...MISMATCH }) + expect(stale.reason).toBe(CURRENT_REASON) + expect(stale.fixHint).toBe( + `${NOTE_START} (written «${WRITTEN_AT}»). Its spec may not match schema.yml as it is now.\n\n${CURRENT_FIX_HINT}`, + ) + }) + + test("a parse time that isn't a non-empty string is left out", async () => { + for (const written_at of ["", " ", 1731744000, null, { at: WRITTEN_AT }, [WRITTEN_AT]]) { + const result = await check({ stale_manifest: { written_at }, ...MISMATCH }) + expect(result.fixHint?.split("\n\n")[0]).toBe( + `${NOTE_START}. Its spec may not match schema.yml as it is now.`, + ) + } + }) + + test("one stale answer among current ones still leads with the note, once", async () => { + await fs.writeFile(join(answersDir, "orders.json"), JSON.stringify({ stale_manifest: STALE, ...MISMATCH })) + await fs.writeFile( + join(answersDir, "customers.json"), + JSON.stringify({ ...MISMATCH, model: "customers", columns_extra: ["tier"] }), + ) + await fs.writeFile(join(project, "models", "customers.sql"), "select 1 as id, 2 as tier\n") + const result = await check({}) + expect(result.fixHint?.startsWith(`${NOTE_START} (written «${WRITTEN_AT}»).`)).toBe(true) + expect(result.fixHint?.split(NOTE_START).length).toBe(2) + expect(result.fixHint).toContain("REMOVE: note") + expect(result.fixHint).toContain("REMOVE: tier") }) test("the parse time is quoted on one line, whatever the answer carries", async () => {