Files
accounted/tests/tool-pg/setup.ts
T
Jakob Wennberg 325c827322 test(mcp): run the tools against a real PostgREST, not a fake supabase (#1983)
All 100 files in extensions/general/mcp-server/__tests__ fake supabase.
query-journal.test.ts says out loud that its query chain is "exercised by the
live MCP smoke test", and no such test exists in CI. So the PostgREST grammar
of 157 tools, every .select() column string, every resource embed, every
or=(...) form, is gated by nothing and fails first in production.

pg-real cannot cover this: it holds a pg Pool and writes SQL, and none of that
grammar is resolved by Postgres. It is resolved by PostgREST at request time.

Adds a tool-pg vitest project, a docker-compose stack, a reset script that
replays every migration the way the pg-real CI job does, and a CI job.

The first sweep covers 74 read tools and finds no malformed query, across 87
real requests. That number is honest rather than impressive: with an empty
argument set many tools bail before querying. Per-tool fixtures are what
deepen it, and this harness is what makes writing them worth the effort.

Includes a self-test that injects a bad column and asserts the harness detects
it. That is not ceremony. It caught this file passing green while exercising
nothing, twice: once locally where supabase-js prefixes /rest/v1 onto a bare
PostgREST that does not serve it, and once on CI where Node 20 has no native
WebSocket, so every client construction threw and was swallowed by the
per-tool catch as a domain refusal. The client is now built once outside that
catch, the proof-of-life assertion counts real requests instead of being
trivially satisfiable, and realtime gets an inert transport.

Also excludes .next from all three vitest projects. These projects override
vitest's default excludes, so a local `npm run build` leaves a traced copy of
the repo that gets collected as a second set of test files.

Co-authored-by: Jakob Wennberg <311770904+jakobwennberg-oss@users.noreply.github.com>
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-27 18:11:41 +02:00

36 lines
1.3 KiB
TypeScript

/**
* Vitest setup for the MCP tool integration project.
*
* Fails loudly and early if the stack is not up, because the alternative is a
* suite that silently passes against nothing. `scripts/tool-pg/reset.sh` brings
* it up and replays every migration.
*/
import { beforeAll } from 'vitest'
import { TOOL_PG_REST_URL, TOOL_PG_DATABASE_URL, signServiceRoleJwt } from './client'
// tests/pg/fixtures.ts builds its rows through a `pg` Pool keyed on
// DATABASE_URL. Pointing that at the same database lets this project reuse
// seedCompany / insertCompany / insertPostedJournalEntry verbatim instead of
// growing a second, divergent set of fixtures.
process.env.DATABASE_URL ??= TOOL_PG_DATABASE_URL
beforeAll(async () => {
const jwt = signServiceRoleJwt()
let response: Response
try {
response = await fetch(`${TOOL_PG_REST_URL}/companies?select=id&limit=1`, {
headers: { apikey: jwt, Authorization: `Bearer ${jwt}` },
})
} catch (err) {
throw new Error(
`tool-pg: PostgREST unreachable at ${TOOL_PG_REST_URL}. Run: npm run tools:pg:reset\n${String(err)}`,
)
}
if (!response.ok) {
throw new Error(
`tool-pg: PostgREST answered ${response.status} for a trivial read: ${await response.text()}\n` +
'The schema is probably missing or stale. Run: npm run tools:pg:reset',
)
}
}, 30_000)