Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

test: anchor the seed-stage drift guard to the migration's own stage list

[._.]/ Adam Eivy77fe3d062ff5
diff --git a/.changelog/NEXT.md b/.changelog/NEXT.md
index 452f26c804..f203b951ac 100644
--- a/.changelog/NEXT.md
+++ b/.changelog/NEXT.md
@@ -2,7 +2,7 @@
## Internal
-- **[issue-2706] Stage-seeding migrations now guard their own `data.reference` assets.** Migrations 189 (model-personality), 094 (object-attachment checks), and 182 (CWQE quality) each seed stage prompts on upgrade but shipped without a colocated test, so a rename or removal of a reference template would have surfaced only at runtime as "Stage not found" on a fresh install. Each now has a `.test.js` asserting it seeds when absent, no-ops on re-run, never clobbers a customized template or hand-tuned config entry, and — the point — that every `data.reference/prompts/stages/*.md` template it names and its `stage-config.json` entry actually ship. The three suites collapse to one `runSeedStageMigrationTests({ migration, stages, prefix })` call apiece via a new shared helper — the seed-family analogue of `_testHelpers.js`'s `runPromptMigrationTests`. (`scripts/migrations/_seedStageTestHelpers.js`)
+- **[issue-2706] Stage-seeding migrations now guard their own `data.reference` assets.** Migrations 189 (model-personality), 094 (object-attachment checks), and 182 (CWQE quality) each seed stage prompts on upgrade but shipped without a colocated test, so a rename or removal of a reference template would have surfaced only at runtime as "Stage not found" on a fresh install. Each now has a `.test.js` asserting it seeds when absent, no-ops on re-run, never clobbers a customized template or hand-tuned config entry, and — the point — that every `data.reference/prompts/stages/*.md` template it names and its `stage-config.json` entry actually ship. The guard is anchored to the migration's own stage list (now exposed on the object the seed helper returns), so adding a stage without shipping its template fails the suite instead of leaving it green against a stale hand-copied list. The three suites collapse to one `runSeedStageMigrationTests({ migration, stages, prefix })` call apiece via a new shared helper — the seed-family analogue of `_testHelpers.js`'s `runPromptMigrationTests`. (`scripts/migrations/_seedStageTestHelpers.js`)
## Fixed
diff --git a/scripts/migrations/_seedStageHelpers.js b/scripts/migrations/_seedStageHelpers.js
index b787daf242..d884d68fd0 100644
--- a/scripts/migrations/_seedStageHelpers.js
+++ b/scripts/migrations/_seedStageHelpers.js
@@ -65,12 +65,19 @@ export function normalizeStageSpec(spec) {
* with an `N added` summary. Behaviour-identical to the hand-rolled
* `for (const stageKey of STAGES)` loops it replaces.
*
+ * The returned object also carries the normalized `stages` list. The runner only
+ * ever looks for `up()` (`scripts/run-migrations.js`) and ignores everything else,
+ * so exposing it costs nothing at runtime — it lets `_seedStageTestHelpers.js`
+ * assert against the stage list the migration *actually* seeds rather than a list
+ * hand-copied into the test (which would go stale silently when a stage is added).
+ *
* @param {Array<string | { stageKey: string, filename?: string }>} stageSpecs
- * @returns {{ up: (ctx: { rootDir: string }) => Promise<void> }}
+ * @returns {{ up: (ctx: { rootDir: string }) => Promise<void>, stages: Array<{ stageKey: string, filename: string }> }}
*/
export function makeSeedMigrations(stageSpecs) {
const stages = stageSpecs.map(normalizeStageSpec);
return {
+ stages,
async up({ rootDir }) {
const stagesDir = join(rootDir, 'data', 'prompts', 'stages');
await mkdir(stagesDir, { recursive: true });
diff --git a/scripts/migrations/_seedStageTestHelpers.js b/scripts/migrations/_seedStageTestHelpers.js
index 2b706398a7..e5bee5069e 100644
--- a/scripts/migrations/_seedStageTestHelpers.js
+++ b/scripts/migrations/_seedStageTestHelpers.js
@@ -27,17 +27,14 @@ import { mkdtempSync, rmSync, mkdirSync, writeFileSync, readFileSync, existsSync
import { tmpdir } from 'os';
import { join } from 'path';
-import { repoRoot } from './_testHelpers.js';
+import { repoRoot, sampleBody } from './_testHelpers.js';
import { normalizeStageSpec } from './_seedStageHelpers.js';
const refStagePath = (filename) => join(repoRoot, 'data.reference', 'prompts', 'stages', filename);
const refConfigPath = () => join(repoRoot, 'data.reference', 'prompts', 'stage-config.json');
/** Live shipped template body, or `null` when the template no longer ships. */
-const shippedBody = (filename) => {
- const path = refStagePath(filename);
- return existsSync(path) ? readFileSync(path, 'utf-8') : null;
-};
+const shippedBody = (filename) => (existsSync(refStagePath(filename)) ? sampleBody(filename) : null);
/** Live shipped stage-config, read fresh (never cached across suites). */
const shippedConfig = () => JSON.parse(readFileSync(refConfigPath(), 'utf-8'));
@@ -47,8 +44,12 @@ const shippedConfig = () => JSON.parse(readFileSync(refConfigPath(), 'utf-8'));
* and a data.reference drift catch.
*
* @param {object} opts
- * @param {{ up: (ctx: { rootDir: string }) => Promise<void> }} opts.migration - the migration's default export
- * @param {Array<string | { stageKey: string, filename?: string }>} opts.stages - the exact stage specs the migration seeds
+ * @param {{ up: (ctx: { rootDir: string }) => Promise<void>, stages: Array<object> }} opts.migration - the migration's default export
+ * @param {Array<string | { stageKey: string, filename?: string }>} opts.stages - the stage specs the caller
+ * expects the migration to seed. Spelled out in the test for readability, then pinned to
+ * `migration.stages` by the first case below — so adding a stage to the migration without
+ * updating the test (or vice versa) fails loudly instead of leaving the suite asserting
+ * about a stale list.
* @param {string} opts.prefix - `mkdtempSync` dir name; keep migration-specific (`'migration-189-'`)
* so a debugger leaves a recognizable sandbox in the temp dir
*/
@@ -87,6 +88,13 @@ export function runSeedStageMigrationTests({ migration, stages, prefix }) {
rmSync(rootDir, { recursive: true, force: true });
});
+ it('seeds exactly the stages this suite covers (no drift between test and migration)', () => {
+ // Anchors every case below to the migration's REAL list. Without this, adding
+ // a stage to the migration and forgetting to ship its template would leave the
+ // whole suite green — it would still be asserting about the stale list here.
+ expect(migration.stages).toEqual(specs);
+ });
+
it('seeds every template and stage-config entry on an install missing them', async () => {
await expect(migration.up({ rootDir })).resolves.not.toThrow();
@@ -94,6 +102,9 @@ export function runSeedStageMigrationTests({ migration, stages, prefix }) {
const installed = JSON.parse(readFileSync(installedConfigPath, 'utf-8'));
for (const { stageKey, filename } of specs) {
expect(readFileSync(join(stagesDir, filename), 'utf-8')).toBe(shippedBody(filename));
+ // toBeTruthy first: under partial drift both sides are `undefined` and a bare
+ // toEqual would pass vacuously, reading like it checked the seeding.
+ expect(config.stages[stageKey], `data.reference stage-config missing entry: ${stageKey}`).toBeTruthy();
expect(installed.stages[stageKey]).toEqual(config.stages[stageKey]);
}
});
@@ -133,7 +144,11 @@ export function runSeedStageMigrationTests({ migration, stages, prefix }) {
const installed = JSON.parse(readFileSync(installedConfigPath, 'utf-8'));
// Tuned entry preserved verbatim; the remaining entries still get added.
expect(installed.stages[specs[0].stageKey]).toEqual(tuned);
+ // Empty for a single-stage migration — the tuned-entry assertion above is the
+ // whole point there. Most of the ~59 makeSeedMigration(s) migrations this
+ // helper could cover are single-stage, so don't read this loop as coverage.
for (const { stageKey } of specs.slice(1)) {
+ expect(config.stages[stageKey], `data.reference stage-config missing entry: ${stageKey}`).toBeTruthy();
expect(installed.stages[stageKey]).toEqual(config.stages[stageKey]);
}
});