mirror of
https://github.com/bitbaum/substrata.git
synced 2026-12-04 15:58:14 +01:00
/data returned 500 in production: `lastRunAt.slice is not a function`.
`db.query<{ finished_at: string }>` tells TypeScript what the driver returns; it
does not make the driver return it. node-postgres parses timestamptz into a JS
Date, so the assertion compiled, passed 137 tests, built, deployed — and threw
on the first request that found a row.
It could not have been caught by any gate as they stand, and that is the real
lesson: no build has a database, so `freshness()` threw at prerender, hit its
catch, and every gate only ever exercised the fallback. Production was the first
place a row existed.
A scan for the same shape found a second, worse instance. `runScheduledSweep`
read `last_swept` as string and sorted with `.localeCompare`. The first
scheduled run survived it only because research_sweep_state was empty; it now
has four rows, so the 20:18 run would have thrown and the timer would have been
dead again — with the queue quietly never growing, which is precisely the
failure the freshness panel exists to make visible.
So the fix is a lint, not two edits: test/db-types.test.ts fails the build on any
`query<T>` that declares a column ending in _at, _time or _swept as a string.
It caught both live instances before they were fixed, which is its proof.
Claude-Session: https://claude.ai/code/session_01Y6ecRd91ZsPPcjuNkKTFV2
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
65 lines
2.6 KiB
TypeScript
65 lines
2.6 KiB
TypeScript
/**
|
|
* A `query<T>` generic is an assertion, not a validation.
|
|
*
|
|
* `db.query<{ finished_at: string }>(...)` tells TypeScript what the driver
|
|
* returns; it does not make the driver return it. node-postgres parses
|
|
* `timestamptz` into a JS `Date`, so declaring such a column `string` compiles,
|
|
* passes every test, builds — and then throws `.slice is not a function` the
|
|
* first time a row actually exists.
|
|
*
|
|
* That "first time a row exists" is why this needs a lint rather than a test of
|
|
* behaviour: no build has a database, so the fallback path is the only one any
|
|
* gate ever took. Both live instances shipped green. One of them would have
|
|
* killed the scheduled sweep on its second run, because run one read an empty
|
|
* table and run two would have called `.localeCompare` on a Date.
|
|
*/
|
|
|
|
import { test } from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { readdirSync, readFileSync, statSync } from 'node:fs';
|
|
import path from 'node:path';
|
|
|
|
const ROOT = path.resolve(new URL('..', import.meta.url).pathname);
|
|
const SKIP = new Set(['node_modules', '.next', '.git', '.claude', 'dist']);
|
|
|
|
/** Column names that are timestamps in this schema, by convention. */
|
|
const TIMESTAMP_COLUMN = /(_at|_time|_swept)$/;
|
|
|
|
function sourceFiles(dir: string, out: string[] = []): string[] {
|
|
for (const entry of readdirSync(dir)) {
|
|
if (SKIP.has(entry)) continue;
|
|
const full = path.join(dir, entry);
|
|
if (statSync(full).isDirectory()) sourceFiles(full, out);
|
|
else if (/\.tsx?$/.test(entry)) out.push(full);
|
|
}
|
|
return out;
|
|
}
|
|
|
|
test('a timestamp column is never asserted to be a string', () => {
|
|
const offenders: string[] = [];
|
|
|
|
for (const file of sourceFiles(ROOT)) {
|
|
// This file documents the mistake in prose, so it contains the shape it
|
|
// hunts. Scanning itself would make the lint permanently red.
|
|
if (file.endsWith('db-types.test.ts')) continue;
|
|
const source = readFileSync(file, 'utf8');
|
|
for (const match of source.matchAll(/query<\{(.*?)\}>/gs)) {
|
|
const body = match[1];
|
|
const line = source.slice(0, match.index).split('\n').length;
|
|
for (const field of body.matchAll(/(\w+)\s*:\s*([^;,\n]+)/g)) {
|
|
const [, name, type] = field;
|
|
if (!TIMESTAMP_COLUMN.test(name)) continue;
|
|
if (type.includes('Date')) continue;
|
|
if (!type.includes('string')) continue;
|
|
offenders.push(`${path.relative(ROOT, file)}:${line} — ${name}: ${type.trim()}`);
|
|
}
|
|
}
|
|
}
|
|
|
|
assert.deepEqual(
|
|
offenders,
|
|
[],
|
|
`These columns are Dates at runtime; declaring them string compiles and then throws:\n ${offenders.join('\n ')}`,
|
|
);
|
|
});
|