From 526437926a026c3ed9c7d58268501955007ae991 Mon Sep 17 00:00:00 2001 From: Isaias Tavares Date: Mon, 2 Nov 2026 17:15:39 -0300 Subject: [PATCH] test(db): run the suites that boot the app on Postgres in their own lane npm run test:pg --workspace=server (vitest.pg.config.ts) runs every suite that boots the app through buildApp() on the real database module against Postgres, each test file on its own copy of one template database the engine's bootstrap migrates once per run. TREK_TEST_DB_URL names the server; without it the lane fails instead of passing on nothing. The CI job 'Server: Postgres Tests' runs it on a postgres:17 service and is part of ci-ok. tests/postgres/ holds what only Postgres needs proving: - PARITY-PG: MikroORM's comparator finds nothing between the bootstrapped database and the entities. What it reports that changes nothing (key columns it would make nullable, defaults and checks Postgres spells its own way, the hand-ported triggers) is proven so: the spelling statements run in a rolled-back transaction and every default and check must read the same after, and every ported trigger must exist. - the write lock: concurrent read-then-insert units of work never see the same count, and counts are numbers; a search ignores case. tours-hike and tours-plan-types stay on SQLite only: they move a faked setTimeout on until a tool call ends, which a database over the network does not finish within. --- .github/workflows/test.yml | 53 +++++++- CLAUDE.md | 3 +- server/CLAUDE.md | 3 +- server/package.json | 1 + server/tests/postgres/build-template.ts | 36 ++++++ server/tests/postgres/databases.ts | 37 ++++++ server/tests/postgres/global-setup.ts | 60 +++++++++ server/tests/postgres/schema-parity.test.ts | 132 ++++++++++++++++++++ server/tests/postgres/semantics.test.ts | 66 ++++++++++ server/tests/postgres/setup.ts | 44 +++++++ server/vitest.config.ts | 4 +- server/vitest.pg.config.ts | 62 +++++++++ 12 files changed, 495 insertions(+), 6 deletions(-) create mode 100644 server/tests/postgres/build-template.ts create mode 100644 server/tests/postgres/databases.ts create mode 100644 server/tests/postgres/global-setup.ts create mode 100644 server/tests/postgres/schema-parity.test.ts create mode 100644 server/tests/postgres/semantics.test.ts create mode 100644 server/tests/postgres/setup.ts create mode 100644 server/vitest.pg.config.ts diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index b0118bf59..ff3b12459 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -32,6 +32,8 @@ permissions: # Server: S3 Contract (MinIO) # the storage driver against MinIO # Server: Postgres Probe the repository SQL against Postgres +# Server: Postgres Tests the suites that boot the app, plus PARITY-PG and +# the write lock, on Postgres (vitest.pg.config.ts) # Client: Types & Checks typecheck and the strict ratchet; then the page # pattern and the source ratchets (RTL, size, # Prettier, layers, offline path, duplication, @@ -65,9 +67,9 @@ permissions: # test jobs feeds the scan. # - Job count: GitHub runs at most 20 jobs of the organisation at once and # does not say in which order it starts queued ones. A code change starts -# the 14 shards and about a dozen other jobs; seven of those (parity, the -# SDK, the bundle, S3, Postgres, shared coverage, the chart) are done in -# a minute or two, so what queues at the start waits about that long. The +# the 14 shards and about a dozen other jobs; eight of those (parity, the +# SDK, the bundle, S3, the Postgres probe and tests, shared coverage, the +# chart) are done in a few minutes, so what queues at the start waits about that long. The # jobs that run for minutes (shards, gates, e2e, the image) must not # outnumber the 20 slots, or one of them waits for another to finish and # the run grows by a whole job: a layout with six gate jobs did exactly @@ -815,6 +817,50 @@ jobs: if-no-files-found: ignore retention-days: 14 + # The Postgres lane: every suite that boots the app through buildApp() on the + # real database module, each file on its own copy of one migrated template, + # plus the Postgres-only suites (PARITY-PG, the write lock). The lane fails, + # never skips, without TREK_TEST_DB_URL. + server-postgres-tests: + name: 'Server: Postgres Tests' + runs-on: ubuntu-24.04 + timeout-minutes: 15 + needs: changes + if: needs.changes.outputs.code == 'true' + services: + postgres: + image: postgres:17 + env: + POSTGRES_PASSWORD: lane + ports: + - 5432:5432 + options: >- + --health-cmd "pg_isready -U postgres" + --health-interval 5s + --health-timeout 5s + --health-retries 20 + steps: + - uses: actions/checkout@v6 + with: + persist-credentials: false + + - uses: actions/setup-node@v6 + with: + node-version-file: .nvmrc + cache: npm + cache-dependency-path: package-lock.json + + - name: Install dependencies + run: npm ci + + - name: Build shared + run: npm run build --workspace=shared + + - name: Run the Postgres lane + env: + TREK_TEST_DB_URL: postgres://postgres:lane@127.0.0.1:5432/postgres + run: npm run test:pg --workspace=server + # The server suite in seven slices. vitest.shard.config.ts is vitest.config.ts # without the thresholds and the coverage reports: a slice's blob carries # its coverage to server-coverage, which checks the whole. fail-fast is off, @@ -1398,6 +1444,7 @@ jobs: - client-e2e - s3-contract - postgres-probe + - server-postgres-tests - server-tests - server-coverage - client-tests diff --git a/CLAUDE.md b/CLAUDE.md index 6563d96ae..41cb44163 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -31,6 +31,7 @@ Tests: npm run test # all workspaces npm run test --workspace=server # vitest run (server); also test:unit / test:integration / test:ws npm run test:e2e # server e2e (boots Nest against a temp SQLite) +npm run test:pg --workspace=server # the Postgres lane (vitest.pg.config.ts; needs TREK_TEST_DB_URL, e.g. postgres://postgres:trek@127.0.0.1:55432/postgres) npm run test:cov # coverage (lcov) for all four packages incl. plugin-sdk cd client && npm run test # client vitest run cd client && npm run e2e # Playwright (CI runs the public and app projects; the screenshot and help-media ones are local) @@ -136,7 +137,7 @@ These principles come out of a full-repo audit and shape all new code: ## Quality gates in CI -`.github/workflows/test.yml` runs, per package: typecheck (server also `typecheck:tests`, client also the strict-mode ratchet `lint:strict`), lint (client and server through `lint:warnings`, which also holds the warning count per rule), `lint:pages`, `lint:rtl`, `lint:size` (client and server), the server's `lint:strict`, `lint:boundaries`, `lint:tx`, `lint:dialect`, `lint:engine-dispatch`, `lint:test-sql`, `lint:test-mocks`, `lint:test-new-service`, `lint:query-api`, `lint:mcp-zod`, `lint:service-http` and `lint:response-contracts`, the client's and server's `lint:format`, `lint:layers`, `lint:offline`, `lint:dup`, `lint:pairs`, `lint:skips`, `lint:i18n-keys` and `theme:lint`, shared's `contracts:open`, the repo-wide `lint:knip` (in server-lint-checks), `check:plugin-facts`, `i18n:parity:strict` (with the untranslated-strings ratchet), the S3 contract suite, the Postgres probe (`postgres-probe`: every dialect helper on SQLite and Postgres, and the statements every repository method reaches with sample arguments against a Postgres service; the SQL errors per method and the list of methods it cannot measure are held to a baseline that only shrinks and fails while it is still unmeasured; see `server/CLAUDE.md`), the browser end-to-end flows (Chromium and WebKit), a client build with `check:gl-split`, coverage for all four packages, then a SonarCloud scan. It also builds the Docker image and boots it until `/api/health/ready` answers (`docker-image`), and lints and renders the Helm chart against the Kubernetes schemas and runs `docker compose config` on every compose file (`deploy-manifests`). `lint-prettier.yml` additionally runs `lint` + `lint:format` on `shared/`, both check-only. `plugin-sdk` has no lint script and is outside the eslint gates. **Not in CI**: the screenshot and help-media Playwright projects; run them locally. There are no git hooks; nothing runs before a commit except you. Other workflows build Docker images, scan them, and publish `trek-plugin-sdk` on `plugin-sdk-v*` tags. +`.github/workflows/test.yml` runs, per package: typecheck (server also `typecheck:tests`, client also the strict-mode ratchet `lint:strict`), lint (client and server through `lint:warnings`, which also holds the warning count per rule), `lint:pages`, `lint:rtl`, `lint:size` (client and server), the server's `lint:strict`, `lint:boundaries`, `lint:tx`, `lint:dialect`, `lint:engine-dispatch`, `lint:test-sql`, `lint:test-mocks`, `lint:test-new-service`, `lint:query-api`, `lint:mcp-zod`, `lint:service-http` and `lint:response-contracts`, the client's and server's `lint:format`, `lint:layers`, `lint:offline`, `lint:dup`, `lint:pairs`, `lint:skips`, `lint:i18n-keys` and `theme:lint`, shared's `contracts:open`, the repo-wide `lint:knip` (in server-lint-checks), `check:plugin-facts`, `i18n:parity:strict` (with the untranslated-strings ratchet), the S3 contract suite, the Postgres probe (`postgres-probe`: every dialect helper on SQLite and Postgres, and the statements every repository method reaches with sample arguments against a Postgres service; the SQL errors per method and the list of methods it cannot measure are held to a baseline that only shrinks and fails while it is still unmeasured; see `server/CLAUDE.md`), the Postgres lane (`server-postgres-tests`: the suites that boot the app, PARITY-PG and the write lock on a Postgres service), the browser end-to-end flows (Chromium and WebKit), a client build with `check:gl-split`, coverage for all four packages, then a SonarCloud scan. It also builds the Docker image and boots it until `/api/health/ready` answers (`docker-image`), and lints and renders the Helm chart against the Kubernetes schemas and runs `docker compose config` on every compose file (`deploy-manifests`). `lint-prettier.yml` additionally runs `lint` + `lint:format` on `shared/`, both check-only. `plugin-sdk` has no lint script and is outside the eslint gates. **Not in CI**: the screenshot and help-media Playwright projects; run them locally. There are no git hooks; nothing runs before a commit except you. Other workflows build Docker images, scan them, and publish `trek-plugin-sdk` on `plugin-sdk-v*` tags. - **Job names read "package: what"** (`Server: Types`, `Client: Tests 3/7`), and the list at the top of `test.yml` says what each job holds. A new job follows the scheme and joins that list, and the job-count note there says when a long job needs a slot freed first. - **Tests run beside the gates, in shards.** The client and server suites run in slices on parallel runners (`client-tests`, `server-tests`, through each package's `vitest.shard.config.ts`), and `client-coverage`/`server-coverage` merge their blob reports and check the thresholds against the whole. A red slice is usually a failing test, a red merge job usually a threshold. diff --git a/server/CLAUDE.md b/server/CLAUDE.md index caaa146d4..22f06324c 100644 --- a/server/CLAUDE.md +++ b/server/CLAUDE.md @@ -29,6 +29,7 @@ npm run lint:boundaries # import cycles and cross-domain reach-ins may only sh npm run lint:dialect # SQLite-only SQL spellings under src/ may only shrink (CI gate) npm run lint:engine-dispatch # isSqlite(/isPostgres(/platform instanceof outside src/db/dialect/ and src/db/engine/ may only shrink (CI gate) npm run probe:pg # dialect helpers + the repository statements it reaches against Postgres (CI job postgres-probe; needs TREK_PG_PROBE_URL) +npm run test:pg # the Postgres lane (CI job server-postgres-tests; needs TREK_TEST_DB_URL) npm run lint:tx # methods writing more than once outside one transaction may only shrink (CI gate) npm run lint:test-sql # raw better-sqlite3 statements in tests/ may only shrink (CI gate) npm run lint:test-mocks # vi.mock of src/config, src/nest/realtime and src/db in tests/ may only shrink (CI gate) @@ -79,7 +80,7 @@ Nest owns everything. Every domain is a DI module under `src/nest//` (`c - **Plugins** (`src/nest/plugins/`): the child-process runtime (`supervisor/`, `runtime/plugin-host-entry.ts`, `protocol/envelope.ts`) is **intentionally not Nest**; it is the security boundary. `host/rpc-host.ts` registers an RPC method at spawn only if the plugin holds the unlocking permission (registration = authorization). Install-time gates live in `install/`; the manifest's `trek` version range fails closed, and `TREK_PLUGINS_IGNORE_TREK_RANGE` downgrades that to a warning. The protocol tables in `protocol/envelope.ts` (+ `src/plugin-event-sink.ts`) are **generated** into `plugin-sdk/` and `shared/`: run `gen:plugin-facts` after touching them and commit the output. What a plugin gets back is `protocol/output-contract.ts`: every wire method has an entry, and an entity result (`trips.getById`, `todos.list`, `vacay.mine`, ...) is cut down to its published fields in `rpc-host.ts` on the way out, together with every child row it carries whose field is listed in `PLUGIN_ENTITY_NESTED` (a day's `notes_items`, a reservation's `endpoints`). A new column on any whole-row table behind those entities, child tables included, fails `tests/unit/plugins/output-contract.schema.test.ts` until it is listed as published or withheld (the SDK's `PLUGIN_ENTITY_FIELDS` and `PLUGIN_ENTITY_NESTED` are generated from it); a method left as `readModel` must return a value built field by field from named columns. A plugin's own SQL (`host/plugin-data.service.ts`) is held to a row cap, and `query`, `tx` and an `exec` script to a time budget checked between rows and statements (`migrate` is deliberately not timed). `runtime/egress-policy.ts` is a hand-kept mirror guarded by a parity test in `plugin-sdk/`; change both sides together. - **Storage**: `src/nest/storage/` is the upload driver abstraction (local / S3 / mirror behind one interface, with an admin surface and a usage job). Domain services never touch the filesystem or the S3 SDK directly. Where the data lives (`data/`, `uploads/`, backups, logs, key files, the DB file) is resolved in one place, `src/app-config/data-paths.ts`; a provider that reads one injects `DataPathsService`, plain helper modules shared with code outside the container call `resolveDataPaths()` (the list is in `src/app-config/README.md`), and nothing anchors a data path on its own `__dirname` or on `process.cwd()`. - **DB** (`src/db/`): `better-sqlite3`, WAL, FK on. `database.ts` only holds the connection and applies pragmas. It does not build a schema, and importing it opens nothing (the first reader does). The connection's lifecycle belongs to the `DatabaseLifecycle` provider (`nest/database/database-lifecycle.service.ts`): `buildApp()` opens it through `open()` (which binds the ORM to later swaps and runs the schema bootstrap below), the shutdown closes it through `close()`, and a restore or the demo reset swaps the file only through the database backup port, `DATABASE_BACKUP` (`nest/database/database-backup.interface.ts`; the SQLite implementation is `nest/backup/sqlite-database-backup.ts`: `VACUUM INTO` snapshot, close, atomic file swap, `reopen()`). The port works on `DatabaseLifecycle.file`, the one resolved path (`TREK_DB_FILE` or `data/travel.db`), and a snapshot that fails fails the backup rather than archiving the live file. `MaintenanceRepository` is provided only by `MaintenanceModule`'s factory and injected everywhere else; no consumer constructs it. A provider that has to follow every reopen (the demo reset job saves the first demo baseline after a restore's re-bootstrap seeded the example trips) registers with `DatabaseLifecycle.onReopened()`, which runs after the reopen's schema bootstrap; a listener contains its own failures, since one that throws fails the reopen. MikroORM's migrations (`db/migrations/`) are the only schema source, **including for tests**: the hand-kept `schema.ts`/`migrations.ts`/`seeds.ts` builders and the parity test that guarded them are gone. Seeding is the MikroORM seeders in `db/seeders/`, applied by `db/orm.ts` from `buildApp()` before anything reads. An install the retired positional runner migrated (a `schema_version` row, no `mikro_orm_migrations` rows) is baselined by `db/legacy-baseline.ts`: the migrations whose `Legacy migration step N` docstring is ≤ its version are recorded as executed (never replayed) in the same transaction as the migrator run, while the baseline migration itself still runs first, as the legacy runner's `createTables()` did on every boot. It refuses the boot when `schema_version` is not exactly one row holding an integer ≥ 1, or is above the last mapped step (258); a value from 1 to 258 is read as dev's steps of those numbers, so a pre-ORM feature-branch database whose steps differ is not detected (the Tours branch is the one case it cuts back). Recorded `mikro_orm_migrations` names this build does not ship refuse it too (`refuseNewerDatabase`), whether a newer release or a development build wrote them. 4.3.x and earlier have no such check and start on a database 5.x migrated. Before a boot applies pending migrations to a file database that holds anything, `db/pre-migrate-snapshot.ts` writes a `VACUUM INTO` copy next to it (`pre-migrate--