Files
eden/workers/check-reference.ts
T

38 lines
2.7 KiB
TypeScript

import assert from 'node:assert/strict';
import { resolve, join } from 'node:path';
import { Database } from 'bun:sqlite';
import { SqliteReferenceRepository } from '../src/lib/server/reference/repository';
const directory = resolve(process.env.REFERENCE_DIR ?? 'data/reference-live');
const manifest = await Bun.file(join(directory, 'manifest.json')).json();
assert.equal(manifest.active?.profile, 'production', 'live catalog must have a production profile');
const repository = new SqliteReferenceRepository(directory, 'production');
const db = new Database(repository.databasePath, { readonly: true, strict: true });
try {
const names = new Map((await repository.types([34, 587])).map(type => [type.id, type.name]));
assert.equal(names.get(34), 'Tritanium');
assert.equal(names.get(587), 'Rifter');
const counts: Record<string, number> = {};
for (const table of ['types', 'blueprints', 'systems', 'gates', 'stations', 'unresolved_references']) {
const row = db.query<{ n: number }, []>(`SELECT COUNT(*) AS n FROM ${table}`).get();
counts[table] = Number(row?.n);
assert.equal(counts[table], manifest.active.counts[table], `${table} must match the published manifest`);
}
assert(counts.types > 50_000);
assert(counts.systems > 5_000);
assert(counts.blueprints > 0);
assert(counts.gates > 0);
const verifiedStations = Number((db.query<{ n: number }, []>('SELECT COUNT(*) AS n FROM stations WHERE source_verified = 1').get() as { n: number }).n);
assert.equal(verifiedStations, manifest.active.counts.verified_stations, 'verified station count must match the published manifest');
assert(verifiedStations > 0);
const joinedStations = Number((db.query<{ n: number }, []>('SELECT COUNT(*) AS n FROM stations st JOIN systems s ON s.system_id = st.system_id JOIN constellations c ON c.constellation_id = s.constellation_id JOIN regions r ON r.region_id = c.region_id').get() as { n: number }).n);
assert.equal(joinedStations, counts.stations, 'every station must join through system, constellation, and region');
const verifiedStation = (await repository.stations({ limit: 1 })).find((station) => station.sourceVerified);
assert(verifiedStation, 'reader must expose a verified station row');
assert.deepEqual(await repository.stations([Number.MAX_SAFE_INTEGER]), [], 'unknown station IDs must remain absent rather than fabricated');
const integrity = db.query<{ integrity_check: string }, []>('PRAGMA integrity_check').all();
assert.deepEqual(integrity.map(row => row.integrity_check), ['ok']);
assert.deepEqual(db.query('PRAGMA foreign_key_check').all(), []);
console.log(JSON.stringify({ productionReference: 'passed', directory, version: await repository.version(), counts }));
} finally { db.close(); repository.close(); }