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([backport-pre-023-migrations-to-_lib]): direct unit tests for `createIfMissing` and `retireOnSampleMissing` opt-ins

[._.]/ Adam Eivya1717d1a97a5
diff --git a/scripts/migrations/_lib.test.js b/scripts/migrations/_lib.test.js
new file mode 100644
index 0000000000..1d5eb2d398
--- /dev/null
+++ b/scripts/migrations/_lib.test.js
@@ -0,0 +1,99 @@
+/**
+ * Direct unit tests for `applyPromptReplaceMigration` opt-ins that aren't
+ * exercised end-to-end by the per-migration `runPromptMigrationTests` helper:
+ *
+ * - `createIfMissing` — copy sample → data when data is absent (mig 005)
+ * - `retireOnSampleMissing` — soft-delete data when sample is absent (mig 003)
+ *
+ * Per-migration tests still rely on the underlying loop logic; these
+ * exercise the branches the helper guards behind opt-in flags.
+ */
+import { describe, it, expect, beforeEach, afterEach } from 'vitest';
+import { mkdtempSync, rmSync, writeFileSync, readFileSync, mkdirSync, existsSync } from 'fs';
+import { tmpdir } from 'os';
+import { join } from 'path';
+
+import { applyPromptReplaceMigration, md5 } from './_lib.js';
+
+const FILENAME = 'pipeline-fake.md';
+const BODY_OLD = '# OLD\n';
+const BODY_NEW = '# NEW\n';
+const BODY_CUSTOM = '# CUSTOMIZED\n';
+
+const baseOpts = {
+ accepted: { [FILENAME]: [md5(BODY_OLD)] },
+ current: { [FILENAME]: md5(BODY_NEW) },
+ label: 'fake',
+ customizedHint: () => '',
+};
+
+describe('applyPromptReplaceMigration opt-ins', () => {
+ let rootDir;
+ let stagesDir;
+ let sampleDir;
+
+ beforeEach(() => {
+ rootDir = mkdtempSync(join(tmpdir(), 'migration-lib-'));
+ stagesDir = join(rootDir, 'data', 'prompts', 'stages');
+ sampleDir = join(rootDir, 'data.sample', 'prompts', 'stages');
+ mkdirSync(stagesDir, { recursive: true });
+ mkdirSync(sampleDir, { recursive: true });
+ });
+
+ afterEach(() => {
+ rmSync(rootDir, { recursive: true, force: true });
+ });
+
+ describe('createIfMissing', () => {
+ it('copies the sample into data/ when data file is absent', async () => {
+ writeFileSync(join(sampleDir, FILENAME), BODY_NEW);
+ const result = await applyPromptReplaceMigration({ rootDir, ...baseOpts, createIfMissing: true });
+ expect(result).toMatchObject({ created: 1, updated: 0, skipped: 0 });
+ expect(readFileSync(join(stagesDir, FILENAME), 'utf-8')).toBe(BODY_NEW);
+ });
+
+ it('no-ops when both data and sample are absent', async () => {
+ const result = await applyPromptReplaceMigration({ rootDir, ...baseOpts, createIfMissing: true });
+ expect(result).toMatchObject({ created: 0, updated: 0, skipped: 0 });
+ expect(existsSync(join(stagesDir, FILENAME))).toBe(false);
+ });
+
+ it('default `createIfMissing: false` leaves data absent', async () => {
+ writeFileSync(join(sampleDir, FILENAME), BODY_NEW);
+ const result = await applyPromptReplaceMigration({ rootDir, ...baseOpts });
+ expect(result).toMatchObject({ created: 0, updated: 0, skipped: 0 });
+ expect(existsSync(join(stagesDir, FILENAME))).toBe(false);
+ });
+ });
+
+ describe('retireOnSampleMissing', () => {
+ it('soft-deletes an unmodified data file when the sample is gone', async () => {
+ writeFileSync(join(stagesDir, FILENAME), BODY_OLD);
+ const result = await applyPromptReplaceMigration({ rootDir, ...baseOpts, retireOnSampleMissing: true });
+ expect(result).toMatchObject({ retired: 1, updated: 0, skipped: 0 });
+ expect(existsSync(join(stagesDir, FILENAME))).toBe(false);
+ });
+
+ it('warns and skips when the data file was customized', async () => {
+ writeFileSync(join(stagesDir, FILENAME), BODY_CUSTOM);
+ const result = await applyPromptReplaceMigration({ rootDir, ...baseOpts, retireOnSampleMissing: true });
+ expect(result).toMatchObject({ retired: 0, updated: 0, skipped: 1 });
+ expect(readFileSync(join(stagesDir, FILENAME), 'utf-8')).toBe(BODY_CUSTOM);
+ });
+
+ it('still applies the normal upgrade when the sample is present', async () => {
+ writeFileSync(join(stagesDir, FILENAME), BODY_OLD);
+ writeFileSync(join(sampleDir, FILENAME), BODY_NEW);
+ const result = await applyPromptReplaceMigration({ rootDir, ...baseOpts, retireOnSampleMissing: true });
+ expect(result).toMatchObject({ updated: 1, retired: 0, skipped: 0 });
+ expect(readFileSync(join(stagesDir, FILENAME), 'utf-8')).toBe(BODY_NEW);
+ });
+
+ it('default `retireOnSampleMissing: false` raises on missing sample for an old-hash file', async () => {
+ writeFileSync(join(stagesDir, FILENAME), BODY_OLD);
+ await expect(
+ applyPromptReplaceMigration({ rootDir, ...baseOpts }),
+ ).rejects.toThrow(/ENOENT/);
+ });
+ });
+});