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
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
test: add unit tests for stage-seeding migrations 189/094/182 (#2706)
[._.]/ Adam Eivye280ebc57cbd
diff --git a/.changelog/NEXT.md b/.changelog/NEXT.mdindex 48e42138d1..452f26c804 100644--- a/.changelog/NEXT.md+++ b/.changelog/NEXT.md@@ -1,5 +1,9 @@# Unreleased Changes+## 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`)+## Fixed- [issue-2669] Sharing, Messages, and Calendar screens no longer show two stacked error toasts when a share/subscribe/export/save/sync action fails — the custom-catch callers now pass `{ silent: true }` so only their own toast fires, not the shared `request()` helper's default one too. Swept the Messages/calendar/sharing feature area (both ConfigTabs, InboxTab, ReviewTab, ShareToButton, and the Sharing page) and threaded a backward-compatible `options` arg into the affected API wrappers (`evaluateMessages`, `enableGmailApi`, `getGoogleAuthUrl`, `create/updateMessageAccount`, `create/updateCalendarAccount`, `updateSubcalendars`, `saveGoogleAuthCredentials`, `start/runGoogleAutoConfig`, `confirmDailyReviewEvent`).diff --git a/scripts/migrations/094-object-attachment-check-stages.test.js b/scripts/migrations/094-object-attachment-check-stages.test.jsnew file mode 100644index 0000000000..2b3128e6b8--- /dev/null+++ b/scripts/migrations/094-object-attachment-check-stages.test.js@@ -0,0 +1,14 @@+import { describe } from 'vitest';++import migration from './094-object-attachment-check-stages.js';+import { runSeedStageMigrationTests } from './_seedStageTestHelpers.js';++// The two object-attachment editorial-check stages (#1288) backing the+// `objects.unmotivated-interaction` / `objects.backstory-consistency` checks.+describe('migration 094 — seed object-attachment check stages', () => {+ runSeedStageMigrationTests({+ migration,+ stages: ['pipeline-editorial-object-motivation', 'pipeline-editorial-object-backstory'],+ prefix: 'migration-094-',+ });+});diff --git a/scripts/migrations/182-seed-cwqe-quality-stages.test.js b/scripts/migrations/182-seed-cwqe-quality-stages.test.jsnew file mode 100644index 0000000000..8e554a7ecc--- /dev/null+++ b/scripts/migrations/182-seed-cwqe-quality-stages.test.js@@ -0,0 +1,20 @@+import { describe } from 'vitest';++import migration from './182-seed-cwqe-quality-stages.js';+import { runSeedStageMigrationTests } from './_seedStageTestHelpers.js';++// The six Creative Writing Quality Engine judge/revision stages (v2.27.0).+describe('migration 182 — seed CWQE quality stages', () => {+ runSeedStageMigrationTests({+ migration,+ stages: [+ 'pipeline-judge-issue',+ 'pipeline-judge-foundation',+ 'pipeline-judge-compare',+ 'pipeline-editorial-adversarial-cuts',+ 'writers-room-cuts',+ 'writers-room-revise',+ ],+ prefix: 'migration-182-',+ });+});diff --git a/scripts/migrations/189-model-personality-stages.test.js b/scripts/migrations/189-model-personality-stages.test.jsnew file mode 100644index 0000000000..0fd98b4687--- /dev/null+++ b/scripts/migrations/189-model-personality-stages.test.js@@ -0,0 +1,15 @@+import { describe } from 'vitest';++import migration from './189-model-personality-stages.js';+import { runSeedStageMigrationTests } from './_seedStageTestHelpers.js';++// The two Digital Twin → Personality stages (#2610). The drift catch is the+// point: without it, renaming either reference template would surface only as+// POST /api/model-personality/run throwing "Stage not found" on a fresh install.+describe('migration 189 — seed model-personality stages', () => {+ runSeedStageMigrationTests({+ migration,+ stages: ['model-personality-profile', 'model-personality-alignment-scorer'],+ prefix: 'migration-189-',+ });+});diff --git a/scripts/migrations/_seedStageHelpers.js b/scripts/migrations/_seedStageHelpers.jsindex 79b9bf08e1..b787daf242 100644--- a/scripts/migrations/_seedStageHelpers.js+++ b/scripts/migrations/_seedStageHelpers.js@@ -47,10 +47,13 @@ import { atomicWrite } from '../../server/lib/fileUtils.js';/*** Normalize a stage spec (bare `stageKey` string or `{ stageKey, filename }`)* into `{ stageKey, filename }` with the universal `${stageKey}.md` default.+ * Exported so `_seedStageTestHelpers.js` derives fixture filenames through the+ * same rule the migration itself uses (the rule is covered directly by+ * `_seedStageHelpers.test.js`).* @param {string | { stageKey: string, filename?: string }} spec* @returns {{ stageKey: string, filename: string }}*/-function normalizeStageSpec(spec) {+export function normalizeStageSpec(spec) {if (typeof spec === 'string') return { stageKey: spec, filename: `${spec}.md` };const { stageKey, filename } = spec;return { stageKey, filename: filename || `${stageKey}.md` };diff --git a/scripts/migrations/_seedStageTestHelpers.js b/scripts/migrations/_seedStageTestHelpers.jsnew file mode 100644index 0000000000..2b706398a7--- /dev/null+++ b/scripts/migrations/_seedStageTestHelpers.js@@ -0,0 +1,151 @@+/**+ * Shared test scaffolding for `makeSeedMigrations`-based seed migrations (#2706).+ * Companion to `./_seedStageHelpers.js` — the seed-family analogue of+ * `./_testHelpers.js`'s `runPromptMigrationTests` (the hash-driven replace+ * family). The runner skips `_`-prefixed files, so this is never imported as a+ * migration.+ *+ * `_seedStageHelpers.test.js` already covers the seed *mechanics* against+ * synthetic stages (copy-if-missing, merge-if-absent, missing-sample tolerance,+ * single config write). What it cannot cover is each migration's own wiring:+ * that the specific stage ids it names seed correctly, and — most valuable —+ * that those ids' `data.reference/prompts/stages/*.md` templates and+ * `stage-config.json` entries actually ship. Without that guard, a future+ * reference-template rename/removal would surface only at runtime as+ * "Stage not found" on a fresh install, with no failing test.+ *+ * Per-migration `*.test.js` collapses to a `describe` + a single+ * `runSeedStageMigrationTests({ migration, stages, prefix })` call.+ *+ * Fixtures are built in an `mkdtempSync` sandbox seeded from copies of the live+ * `data.reference/` assets — the live tree is only ever READ, never written, and+ * the install's real `data/` tree is never touched.+ */++import { it, expect, beforeEach, afterEach } from 'vitest';+import { mkdtempSync, rmSync, mkdirSync, writeFileSync, readFileSync, existsSync } from 'fs';+import { tmpdir } from 'os';+import { join } from 'path';++import { repoRoot } 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;+};++/** Live shipped stage-config, read fresh (never cached across suites). */+const shippedConfig = () => JSON.parse(readFileSync(refConfigPath(), 'utf-8'));++/**+ * Standard seed-migration suite: seeds-when-absent, no-op re-run, no-clobber,+ * 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 {string} opts.prefix - `mkdtempSync` dir name; keep migration-specific (`'migration-189-'`)+ * so a debugger leaves a recognizable sandbox in the temp dir+ */+export function runSeedStageMigrationTests({ migration, stages, prefix }) {+ const specs = stages.map(normalizeStageSpec);+ let rootDir;+ let stagesDir;+ let installedConfigPath;++ beforeEach(() => {+ rootDir = mkdtempSync(join(tmpdir(), prefix));+ stagesDir = join(rootDir, 'data', 'prompts', 'stages');+ installedConfigPath = join(rootDir, 'data', 'prompts', 'stage-config.json');+ const refStagesDir = join(rootDir, 'data.reference', 'prompts', 'stages');+ mkdirSync(stagesDir, { recursive: true });+ mkdirSync(refStagesDir, { recursive: true });++ // Mirror the REAL shipped assets into the sandbox so the seed assertions+ // exercise what actually ships rather than a synthetic stand-in. A missing+ // asset is tolerated here (not thrown) so the drift test below reports it+ // precisely instead of every case dying in setup with an ENOENT.+ const config = shippedConfig();+ const fixtureStages = {};+ for (const { stageKey, filename } of specs) {+ const body = shippedBody(filename);+ if (body !== null) writeFileSync(join(refStagesDir, filename), body);+ if (config.stages[stageKey]) fixtureStages[stageKey] = config.stages[stageKey];+ }+ writeFileSync(+ join(rootDir, 'data.reference', 'prompts', 'stage-config.json'),+ `${JSON.stringify({ stages: fixtureStages }, null, 2)}\n`,+ );+ });++ afterEach(() => {+ rmSync(rootDir, { recursive: true, force: true });+ });++ it('seeds every template and stage-config entry on an install missing them', async () => {+ await expect(migration.up({ rootDir })).resolves.not.toThrow();++ const config = shippedConfig();+ const installed = JSON.parse(readFileSync(installedConfigPath, 'utf-8'));+ for (const { stageKey, filename } of specs) {+ expect(readFileSync(join(stagesDir, filename), 'utf-8')).toBe(shippedBody(filename));+ expect(installed.stages[stageKey]).toEqual(config.stages[stageKey]);+ }+ });++ it('is a no-op on re-run against an already-seeded install', async () => {+ await migration.up({ rootDir });+ const afterFirst = {+ config: readFileSync(installedConfigPath, 'utf-8'),+ bodies: specs.map(({ filename }) => readFileSync(join(stagesDir, filename), 'utf-8')),+ };++ await migration.up({ rootDir });++ expect(readFileSync(installedConfigPath, 'utf-8')).toBe(afterFirst.config);+ specs.forEach(({ filename }, i) => {+ expect(readFileSync(join(stagesDir, filename), 'utf-8')).toBe(afterFirst.bodies[i]);+ });+ });++ it('never clobbers a customized template or an existing stage-config entry', async () => {+ // Pre-existing customized install: every template hand-edited, and the+ // first stage's config entry hand-tuned.+ const customBody = '# CUSTOMIZED — do not overwrite\n';+ for (const { filename } of specs) writeFileSync(join(stagesDir, filename), customBody);+ const tuned = { name: 'user-tuned', model: 'custom' };+ writeFileSync(+ installedConfigPath,+ `${JSON.stringify({ stages: { [specs[0].stageKey]: tuned } }, null, 2)}\n`,+ );++ await migration.up({ rootDir });++ for (const { filename } of specs) {+ expect(readFileSync(join(stagesDir, filename), 'utf-8')).toBe(customBody);+ }+ const config = shippedConfig();+ 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);+ for (const { stageKey } of specs.slice(1)) {+ expect(installed.stages[stageKey]).toEqual(config.stages[stageKey]);+ }+ });++ it('ships every referenced template and stage-config entry in data.reference (drift catch)', () => {+ // Guards the migration against a future rename/removal of a reference+ // asset, which would otherwise only surface at runtime as "Stage not found"+ // on a fresh install.+ const config = shippedConfig();+ for (const { stageKey, filename } of specs) {+ expect(existsSync(refStagePath(filename)), `data.reference template missing: ${filename}`).toBe(true);+ expect(config.stages[stageKey], `data.reference stage-config missing entry: ${stageKey}`).toBeTruthy();+ }+ });+}