Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

test(goldens): doctor JSON goldens use <latest> for the latest migration, with a registry guard

garrytan2cfad3bf4340
diff --git a/test/doctor-json-golden-latest.test.ts b/test/doctor-json-golden-latest.test.ts
new file mode 100644
index 0000000000..c9f2b1a7bd
--- /dev/null
+++ b/test/doctor-json-golden-latest.test.ts
@@ -0,0 +1,34 @@
+/**
+ * The doctor JSON goldens replace the latest schema migration number with
+ * `<latest>` (test/helpers/doctor-json-golden.ts) so a migration renumber does
+ * not churn them. These cases pin what the placeholder may hide: only the
+ * migration registry maximum is normalized, any other number still diffs, and
+ * the golden guard fails when the doctor's reported latest version is not the
+ * registry maximum.
+ */
+import { describe, expect, test } from 'bun:test';
+import { expectSchemaLatestMatchesRegistry, normalizeDoctorText, type GbrainRun } from './helpers/doctor-json-golden.ts';
+import { MIGRATIONS } from '../src/core/schema-migrations/registry.generated.ts';
+
+const LATEST = Math.max(...MIGRATIONS.map((m) => m.version));
+
+function run(message: string): GbrainRun {
+ return { args: [], exitCode: 0, stdout: '', stderr: '', roots: {}, json: { checks: [{ name: 'schema_version', message }] } };
+}
+
+describe('doctor golden <latest> placeholder', () => {
+ test('normalizes only the registry maximum in schema_version messages', () => {
+ expect(normalizeDoctorText(`Version ${LATEST} (latest: ${LATEST})`, {})).toBe('Version <latest> (latest: <latest>)');
+ expect(normalizeDoctorText(`Version ${LATEST - 1}, latest is ${LATEST}. Fix: gbrain apply-migrations --yes`, {}))
+ .toBe(`Version ${LATEST - 1}, latest is <latest>. Fix: gbrain apply-migrations --yes`);
+ expect(normalizeDoctorText(`Version ${LATEST + 1} (latest: ${LATEST + 1})`, {})).toBe(`Version ${LATEST + 1} (latest: ${LATEST + 1})`);
+ expect(normalizeDoctorText(`daily_limit ${LATEST}`, {})).toBe(`daily_limit ${LATEST}`);
+ });
+
+ test('golden guard accepts the registry maximum and rejects anything else', () => {
+ expect(() => expectSchemaLatestMatchesRegistry(run(`Version ${LATEST} (latest: ${LATEST})`))).not.toThrow();
+ expect(() => expectSchemaLatestMatchesRegistry(run(`Version ${LATEST - 1} (latest: ${LATEST - 1})`))).toThrow(/registry maximum/);
+ expect(() => expectSchemaLatestMatchesRegistry(run(`Version ${LATEST - 1}, latest is ${LATEST}.`))).toThrow(/Fix: {2}bun run build:schema-migrations/);
+ expect(() => expectSchemaLatestMatchesRegistry({ ...run(''), json: null })).toThrow(/no schema_version check/);
+ });
+});
diff --git a/test/doctor-json-golden.test.ts b/test/doctor-json-golden.test.ts
index 2026a4f686..101cf3aa69 100644
--- a/test/doctor-json-golden.test.ts
+++ b/test/doctor-json-golden.test.ts
@@ -23,6 +23,7 @@ import { afterAll, describe, expect, test } from 'bun:test';
import { expectGolden, expectNormalizerStable, defineNormalizer } from './helpers/golden.ts';
import {
doctorJsonNormalizer,
+ expectSchemaLatestMatchesRegistry,
makeDoctorHome,
networkAttempts,
patchConfig,
@@ -89,6 +90,8 @@ describe('gbrain doctor --json goldens (PGLite + early stops)', () => {
expect(c.fresh.json).not.toBeNull();
expect(c.fast.json).not.toBeNull();
expect(c.degraded.json).not.toBeNull();
+ expectSchemaLatestMatchesRegistry(c.fresh);
+ expectSchemaLatestMatchesRegistry(c.degraded);
expectGolden('doctor/json-pglite-fresh', c.fresh, DOCTOR_JSON);
expectGolden('doctor/json-pglite-fast', c.fast, DOCTOR_JSON);
expectGolden('doctor/json-pglite-degraded', c.degraded, DOCTOR_JSON);
diff --git a/test/e2e/doctor-json-golden.test.ts b/test/e2e/doctor-json-golden.test.ts
index 40dea6d329..e63e05846c 100644
--- a/test/e2e/doctor-json-golden.test.ts
+++ b/test/e2e/doctor-json-golden.test.ts
@@ -22,6 +22,7 @@ import { assertSafeE2eDatabaseUrl } from '../helpers/db-guard.ts';
import { expectGolden, expectNormalizerStable, defineNormalizer } from '../helpers/golden.ts';
import {
doctorJsonNormalizer,
+ expectSchemaLatestMatchesRegistry,
makeDoctorHome,
networkAttempts,
runGbrain,
@@ -92,6 +93,7 @@ describeE2E('gbrain doctor --json golden (Postgres)', () => {
const report = first.run.json as { engine?: string; checks?: Array<{ name: string; status: string }> } | null;
expect(report?.engine).toBe('postgres');
expect(report?.checks?.find((c) => c.name === 'connection')?.status).toBe('ok');
+ expectSchemaLatestMatchesRegistry(first.run);
expectGolden('doctor/json-postgres-fresh', first, normalizer);
}, 240_000);
});
diff --git a/test/fixtures/goldens/README.md b/test/fixtures/goldens/README.md
index 8ad2aedbfc..5be9d51ef5 100644
--- a/test/fixtures/goldens/README.md
+++ b/test/fixtures/goldens/README.md
@@ -48,7 +48,10 @@ reproduce byte for byte. Each file is written by `test/helpers/golden.ts` as
skills dir, reduced PATH, keyless, `fetch` refused and logged by
`test/helpers/no-network-preload.ts`). Variants: PGLite fresh / `--fast` / degraded
embedding config, no config, unreachable Postgres (+`--fast`), fresh Postgres in a
- scratch database.
+ scratch database. The `schema_version` message carries `<latest>` for the migration
+ registry maximum (as the package version is `<version>`), so a migration renumber does
+ not regenerate these files; `expectSchemaLatestMatchesRegistry` asserts the raw report
+ names the registry maximum (`test/doctor-json-golden-latest.test.ts` pins both).
- `doctor/early-stop-*.json` (`test/doctor-early-stop-golden.serial.test.ts`,
`doctor-checks-v1`): `buildChecks` null-engine, `--fast`, connect-error and
getStats-failure early stops.
diff --git a/test/fixtures/goldens/doctor/json-pglite-degraded.json b/test/fixtures/goldens/doctor/json-pglite-degraded.json
index 86194ddbaa..d92715502d 100644
--- a/test/fixtures/goldens/doctor/json-pglite-degraded.json
+++ b/test/fixtures/goldens/doctor/json-pglite-degraded.json
@@ -507,7 +507,7 @@
},
{
"category": "meta",
- "message": "Version 208 (latest: 208)",
+ "message": "Version <latest> (latest: <latest>)",
"name": "schema_version",
"status": "ok"
},
diff --git a/test/fixtures/goldens/doctor/json-pglite-fresh.json b/test/fixtures/goldens/doctor/json-pglite-fresh.json
index 618bf92713..0d23daa3b1 100644
--- a/test/fixtures/goldens/doctor/json-pglite-fresh.json
+++ b/test/fixtures/goldens/doctor/json-pglite-fresh.json
@@ -510,7 +510,7 @@
},
{
"category": "meta",
- "message": "Version 208 (latest: 208)",
+ "message": "Version <latest> (latest: <latest>)",
"name": "schema_version",
"status": "ok"
},
diff --git a/test/fixtures/goldens/doctor/json-postgres-fresh.json b/test/fixtures/goldens/doctor/json-postgres-fresh.json
index 0d2d457e9a..308276d714 100644
--- a/test/fixtures/goldens/doctor/json-postgres-fresh.json
+++ b/test/fixtures/goldens/doctor/json-postgres-fresh.json
@@ -503,7 +503,7 @@
},
{
"category": "meta",
- "message": "Version 208 (latest: 208)",
+ "message": "Version <latest> (latest: <latest>)",
"name": "schema_version",
"status": "ok"
},
diff --git a/test/helpers/doctor-json-golden.ts b/test/helpers/doctor-json-golden.ts
index 8eec2852c5..af71b375df 100644
--- a/test/helpers/doctor-json-golden.ts
+++ b/test/helpers/doctor-json-golden.ts
@@ -15,7 +15,9 @@
* details, issues) plus the report envelope, with volatile tokens replaced:
* temp home / repo / os tmpdir paths, UUIDs, ISO timestamps, the running
* package and Bun versions, the machine hostname, and `*_ms` / elapsed / pid-style
- * detail keys. Check order is emitted order (deterministic; no Map/Set
+ * detail keys, and the latest schema migration number in the `schema_version`
+ * message (`<latest>`, so a migration renumber does not churn the goldens;
+ * `expectSchemaLatestMatchesRegistry` keeps the hidden number honest). Check order is emitted order (deterministic; no Map/Set
* iteration is re-sorted). stderr lines are kept (normalized) because the
* early-stop paths announce themselves there.
*/
@@ -24,11 +26,14 @@ import { hostname, tmpdir } from 'node:os';
import { delimiter, join } from 'node:path';
import { defineNormalizer, mapStrings, scrubKeys, scrubPaths, scrubTimestamps } from './golden.ts';
import { PROVIDER_ENV_KEYS } from './provider-env.ts';
+import { MIGRATIONS } from '../../src/core/schema-migrations/registry.generated.ts';
const REPO_ROOT = join(import.meta.dir, '..', '..');
const CLI = join(REPO_ROOT, 'src', 'cli.ts');
const PRELOAD = join(import.meta.dir, 'no-network-preload.ts');
const PACKAGE_VERSION = (JSON.parse(readFileSync(join(REPO_ROOT, 'package.json'), 'utf-8')) as { version: string }).version;
+const REGISTRY_LATEST = Math.max(...MIGRATIONS.map((m) => m.version));
+const SCHEMA_LATEST = /\b(Version |latest: |latest is |latest known version \()(\d+)\b/g;
export interface DoctorHome {
home: string;
@@ -140,12 +145,33 @@ export function normalizeDoctorText(text: string, roots: Record<string, string>,
out = scrubTimestamps(out).replace(UUID, '<uuid>');
out = out.replace(/\(most recent caller: at [^()]*\([^()]*\)\)/g, '(most recent caller: <frame>)');
out = out.split(PACKAGE_VERSION).join('<version>');
+ out = out.replace(SCHEMA_LATEST, (match, prefix: string, n: string) => (Number(n) === REGISTRY_LATEST ? `${prefix}<latest>` : match));
out = out.split(`Bun ${Bun.version}`).join('Bun <bun-version>');
const host = hostname();
if (host) out = out.split(host).join('<hostname>');
return out;
}
+/**
+ * Golden guard for the `<latest>` placeholder: the raw (un-normalized) doctor
+ * report must name the migration registry maximum as both the brain's schema
+ * version and the latest version, so normalizing it away hides nothing.
+ */
+export function expectSchemaLatestMatchesRegistry(run: GbrainRun): void {
+ const checks = (run.json as { checks?: Array<{ name: string; message: string }> } | null)?.checks ?? [];
+ const message = checks.find((c) => c.name === 'schema_version')?.message ?? '<no schema_version check>';
+ const expected = `Version ${REGISTRY_LATEST} (latest: ${REGISTRY_LATEST})`;
+ if (message !== expected) {
+ throw new Error([
+ `FAIL: doctor schema_version says "${message}", expected "${expected}" (migration registry maximum ${REGISTRY_LATEST}).`,
+ 'Why: the doctor goldens replace the latest migration number with <latest>; this guard keeps the doctor\'s reported',
+ ' latest version equal to src/core/schema-migrations/registry.generated.ts, so the placeholder cannot hide a stale schema.',
+ 'Fix: bun run build:schema-migrations (stale registry), then rerun this test; a fresh brain must migrate to the registry maximum.',
+ 'See: docs/TESTING.md#refactor-wave-1-goldens',
+ ].join('\n'));
+ }
+}
+
/** `doctor-json-v1` with optional extra literal/regex replacements (e.g. a Postgres URL). */
export function doctorJsonNormalizer(extra: Array<[RegExp | string, string]> = [], name = 'doctor-json-v1') {
return defineNormalizer<GbrainRun>(name, (run) => {