Skip to content

feat(tests): FoodFiles v2 SQL diff validator#9

Open
stackbilt-admin wants to merge 1 commit intomainfrom
feat/foodfiles-sql-diff
Open

feat(tests): FoodFiles v2 SQL diff validator#9
stackbilt-admin wants to merge 1 commit intomainfrom
feat/foodfiles-sql-diff

Conversation

@stackbilt-admin
Copy link
Copy Markdown
Member

Summary

Closes #1. Opens the generator-accuracy feedback loop by diffing `generateSQL()` output against actual migration files for each FoodFiles v2 contract.

What this adds

  • `tests/foodfiles-sql-diff.test.ts` — vitest test that walks every `*.contract.ts` under `apps/api/src/contracts/`, dynamically imports it, runs `generateSQL()`, parses the result, and structurally diffs it against the CREATE TABLE from `apps/api/db/migrations/`.
  • `vitest.config.ts` — adds `@stackbilt/contracts` → `src/index.ts` alias so FoodFiles contract files resolve their package import without a published build.

What the diff checks

Dimension Covered
Column-name set parity
SQLite type parity (TEXT/INTEGER/REAL/BLOB)
NOT NULL parity
DEFAULT presence parity
Index shape ❌ (gate on #2/#3)
Foreign keys ❌ (generator v0.1 limit)
CHECK constraints ❌ (generator v0.1 limit)

Initial run results

7 contracts scanned, 6 real drifts caught on first execution — exactly the signal the issue asked for:

  • `MealPlan`: `servings`, `created_at`, `updated_at` — contract requires NOT NULL but migration allows NULL
  • `PantryItem`: `location`, `created_at`, `updated_at` — same NOT NULL mismatch

These are encoded in a `KNOWN_DRIFTS` allowlist so CI stays green today. Fixing a drift removes its entry → regressions re-fail the test.

CI behavior

Auto-skips when `FOODFILES_PATH` doesn't resolve to a directory containing `apps/api/src/contracts`. CI without FoodFiles checked out stays green; local runs opt in via env var.

Usage

```bash
FOODFILES_PATH=../foodfilesapi_v2 npx vitest run tests/foodfiles-sql-diff.test.ts
```

Test plan

  • 7/7 tests pass locally against foodfilesapi_v2
  • Suite auto-skips when FOODFILES_PATH missing
  • Existing `routes.test.ts` (36 tests) still passes
  • Follow-up: investigate whether NOT NULL drifts should be fixed in contracts or migrations

Closes #1

🤖 Generated with Claude Code

Closes #1. Opens the generator-accuracy feedback loop by diffing
generateSQL() output against the actual migration files for each
FoodFiles v2 contract.

What this adds
- tests/foodfiles-sql-diff.test.ts: vitest test that walks every
  *.contract.ts under apps/api/src/contracts/, dynamically imports
  it, runs generateSQL(), parses the result, and structurally diffs
  it against the CREATE TABLE statement from apps/api/db/migrations/.
- vitest.config.ts: adds @stackbilt/contracts → src/index.ts alias
  so FoodFiles contract files can resolve their import of this
  package at test time without a published build.

What the diff checks (per contract → migration)
- Column-name set parity
- SQLite type parity
- NOT NULL parity
- DEFAULT-value presence parity

What it intentionally skips (will gate contracts#2/#3)
- Index shape, foreign keys, CHECK constraints — generator v0.1
  doesn't emit these consistently yet.

Known drifts
Initial run surfaces 6 NOT NULL drifts across MealPlan and
PantryItem contracts. These are encoded in a KNOWN_DRIFTS allowlist
so CI is green today; fixes to contract or migration should remove
entries, so regressions re-fail the test. Each known drift is
logged at test runtime for visibility.

CI behavior
- Test suite auto-skips when FOODFILES_PATH doesn't resolve to a
  directory containing apps/api/src/contracts. CI without FoodFiles
  checked out stays green; local runs set FOODFILES_PATH to opt in.

Usage
  FOODFILES_PATH=../foodfilesapi_v2 npx vitest run tests/foodfiles-sql-diff.test.ts

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

validate: diff generated SQL against FoodFiles v2 existing migrations

1 participant