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: drain document-authoring bodies before fail-fast data-root cleanup (#12254)

[._.]/ Adam Eivy0df883b1d0bd
diff --git a/scripts/documentGenerationCleanup.test.js b/scripts/documentGenerationCleanup.test.js
new file mode 100644
index 0000000000..14192c8133
--- /dev/null
+++ b/scripts/documentGenerationCleanup.test.js
@@ -0,0 +1,71 @@
+import { expect, it } from 'vitest';
+import { spawnSync } from 'node:child_process';
+import { mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join, dirname, resolve } from 'node:path';
+import { fileURLToPath } from 'node:url';
+import { createVitestTempFixture, fixtureReadyFailureSource } from './lib/vitestTempRootFixture.js';
+
+// A sibling fails only once document authoring is inside its provider call.
+// The cancelled body must finish staging its candidate before teardown removes
+// the lazy data root; otherwise that staging write recreates the root (#12254).
+it('settles document authoring before fail-fast data-root cleanup', () => {
+ const host = mkdtempSync(join(tmpdir(), 'mv-document-cancel-'));
+ try {
+ const fixture = join(host, 'fixture');
+ const ready = join(fixture, 'ready');
+ const events = join(fixture, 'events');
+ const finished = join(fixture, 'finished');
+ const { args, options } = createVitestTempFixture(host, 'server', '');
+ const sourcePath = fileURLToPath(new URL('../server/services/musicVideo/documentGeneration.test.js', import.meta.url));
+ const header = "it('preflights selected media before a provider call and stages a reviewable 30-second document', async () => {";
+ const generate = "const { document: candidate } = await generateMixedMediaDocument(id, { providerId: 'stub-provider', model: 'fixture-model' });";
+ const end = "expect(await readFile(join(PATHS.data, candidate.directory, 'generated.js'), 'utf8')).toContain('function render(ctx, env)');";
+ const source = readFileSync(sourcePath, 'utf8')
+ // Only module/URL specifiers: the suite also quotes `from './x.js'` inside
+ // runtime-source rewrite strings, which must stay verbatim.
+ .replace(/^(import [^\n]*?from\s+|[^\n]*?(?:import|mock|URL)\()(['"])(\.\.?\/[^'"]+)\2/gm, (_, lead, quote, specifier) => lead + JSON.stringify(resolve(dirname(sourcePath), specifier)))
+ // Registered before the suite's own teardown, so under stack-ordered
+ // after-hooks it observes settlement AFTER that teardown has run, even
+ // in the broken control where teardown does not wait for the body.
+ .replace("const { PATHS } = await import(", `afterAll(async () => {
+ const { existsSync } = await import('node:fs');
+ await vi.waitFor(() => expect(existsSync(${JSON.stringify(finished)})).toBe(true), { timeout: 1000 });
+ });
+ const { PATHS } = await import(`)
+ .replace(header, `${header.replace('async () =>', 'async ({ signal }) =>')}
+ const { appendFileSync, existsSync, writeFileSync } = await import('node:fs');
+ const record = event => appendFileSync(${JSON.stringify(events)}, event + '\\n');
+ const cancelled = new Promise(resolve => signal.addEventListener('abort', () => setImmediate(resolve), { once: true }));
+ try {`)
+ .replace(generate, `h.onSubmit = async () => {
+ record('provider-ready');
+ writeFileSync(${JSON.stringify(ready)}, 'ready');
+ await cancelled;
+ record('root-at-resume:' + existsSync(PATHS.data));
+ };
+ ${generate}`)
+ .replace(end, `${end}
+ record('authoring-assertions-complete');
+ } finally {
+ record('body-settled');
+ writeFileSync(${JSON.stringify(finished)}, 'finished');
+ }`);
+ expect(source).toContain('await cancelled;');
+ expect(source).toContain("record('authoring-assertions-complete');");
+ expect(source).toContain(`existsSync(${JSON.stringify(finished)})`);
+ writeFileSync(join(fixture, 'lifecycle.test.js'), source);
+ writeFileSync(join(fixture, 'failure.test.js'), fixtureReadyFailureSource(ready));
+ args[args.indexOf('--maxWorkers') + 1] = '2';
+ args.push('--bail=1', '--testNamePattern', 'preflights selected media before a provider call|controlled sibling failure');
+ const result = spawnSync(process.execPath, args, options);
+ const output = `${result.stdout}\n${result.stderr}`;
+ expect(result.error).toBeUndefined();
+ expect(result.status, output).toBe(1);
+ expect(output).toContain('controlled sibling failure');
+ expect(readFileSync(events, 'utf8'), output).toBe('provider-ready\nroot-at-resume:true\nauthoring-assertions-complete\nbody-settled\n');
+ expect(output).not.toMatch(/ENOTEMPTY|ENOENT|Unhandled|test temp leak:/);
+ } finally {
+ rmSync(host, { recursive: true, force: true });
+ }
+}, 25000);
diff --git a/server/services/musicVideo/documentGeneration.test.js b/server/services/musicVideo/documentGeneration.test.js
index e341c45291..a86f1e28d1 100644
--- a/server/services/musicVideo/documentGeneration.test.js
+++ b/server/services/musicVideo/documentGeneration.test.js
@@ -1,12 +1,16 @@
// Creative approval behavior is covered through production review routes and orchestration.
vi.mock('./productionReview.js', async (load) => ({ ...await load(), assertProductionApproval: vi.fn() }));
-import { afterAll, beforeEach, describe, expect, it, vi } from 'vitest';
+import { afterAll, beforeEach, describe, expect, it as vitestIt, vi } from 'vitest';
import { mkdir, readFile, writeFile, unlink } from 'node:fs/promises';
import { join } from 'node:path';
import { createContext, runInContext } from 'node:vm';
import { bindAuthoredPreviewPlayback } from './authoredPreviewPlayback.js';
import { titleCardRuntimeSource } from '../../lib/musicVideoTitleCard.js';
-import { cleanupTempDataRoots, lazyTempDataRoot, makePathsProxy } from '../../lib/mockPathsDataRoot.js';
+import { cleanupTempDataRoots, lazyTempDataRoot, makePathsProxy, ownTestBodies } from '../../lib/mockPathsDataRoot.js';
+
+// Fail-fast cancellation stops Vitest waiting, not an authoring body's I/O: drain
+// owned bodies before the lazy data root is removed (#12254).
+const { it, drain } = ownTestBodies(vitestIt);
const h = vi.hoisted(() => ({ calls: 0, prompt: '', response: '', onSubmit: null, onPrepare: null, provider: { id: 'stub-provider' }, args: null }));
vi.mock('../../lib/paths.js', async (importOriginal) => makePathsProxy(await importOriginal(), {
@@ -34,7 +38,9 @@ const source = (color) => `function render(ctx, env) { ctx.fillStyle = '${color}
const response = (colors) => JSON.stringify({ sections: Object.entries(colors).map(([id, color]) => ({ id, source: source(color) })) });
const manifestAt = async (document) => JSON.parse(await readFile(join(PATHS.data, document.directory, 'manifest.json'), 'utf8'));
-afterAll(() => cleanupTempDataRoots());
+afterAll(async () => {
+ try { await drain(); } finally { cleanupTempDataRoots(); }
+});
beforeEach(() => { h.provider = { id: 'stub-provider' }; h.args = null; h.onSubmit = null; h.onPrepare = null; h.calls = 0; h.prompt = ''; h.response = response({ intro: '#112233', still: '#445566', clip: '#778899' }); });
async function fixture() {
@@ -540,32 +546,34 @@ describe('treatment-driven mixed-media document authoring', () => {
await expect(readFile(join(PATHS.data, 'cache', 'music-video-authoring', `${id}.json`))).rejects.toMatchObject({ code: 'ENOENT' });
});
- it.each(['treatment', 'media', 'storyboard'])('discards staged local sections after a %s basis edit (#10528)', async (change) => {
- const id = await fixture();
- await generateMixedMediaDocument(id, { providerId: 'stub-provider' });
- const budget = h.prompt.length - 200;
- h.provider = { id: 'ollama', type: 'api' };
- let calls = 0;
- h.onSubmit = async () => {
- if (++calls === 2) throw new Error('Later batch failed');
- h.response = response({ intro: '#010203', still: '#010203', clip: '#010203' });
- };
- await expect(generateMixedMediaDocument(id, { providerId: 'ollama', promptBudgetChars: budget })).rejects.toThrow('Later batch failed');
- await projects.mutateProjectRecord(id, (current) => ({ project: {
- ...current,
- ...(change === 'treatment' ? { treatment: { ...current.treatment, revision: 2 } } : {}),
- ...(change === 'media' ? { scenes: current.scenes.map((scene) => scene.sceneId === 's-clip' ? { ...scene, takes: [{ ...scene.takes[0], shotInstruction: { shotMode: 'performance', edit: { inSec: 2, outSec: 8 } } }] } : scene) } : {}),
- ...(change === 'storyboard' ? { productionReview: { draft: { storyboard: [{ sceneId: 's-intro', action: 'Revised movement' }] } } } : {}),
- } }));
- const asked = [];
- h.onSubmit = async () => {
- asked.push(...['intro', 'still', 'clip'].filter((section) => h.prompt.includes(`"id":"${section}"`)));
- h.response = response({ intro: '#aabbcc', still: '#aabbcc', clip: '#aabbcc' });
- };
- const { document } = await generateMixedMediaDocument(id, { providerId: 'ollama', promptBudgetChars: budget });
- expect(asked).toEqual(['intro', 'still', 'clip']);
- expect((await manifestAt(document)).sections.map((section) => section.source)).toEqual(['intro', 'still', 'clip'].map(() => source('#aabbcc')));
- });
+ for (const change of ['treatment', 'media', 'storyboard']) {
+ it(`discards staged local sections after a ${change} basis edit (#10528)`, async () => {
+ const id = await fixture();
+ await generateMixedMediaDocument(id, { providerId: 'stub-provider' });
+ const budget = h.prompt.length - 200;
+ h.provider = { id: 'ollama', type: 'api' };
+ let calls = 0;
+ h.onSubmit = async () => {
+ if (++calls === 2) throw new Error('Later batch failed');
+ h.response = response({ intro: '#010203', still: '#010203', clip: '#010203' });
+ };
+ await expect(generateMixedMediaDocument(id, { providerId: 'ollama', promptBudgetChars: budget })).rejects.toThrow('Later batch failed');
+ await projects.mutateProjectRecord(id, (current) => ({ project: {
+ ...current,
+ ...(change === 'treatment' ? { treatment: { ...current.treatment, revision: 2 } } : {}),
+ ...(change === 'media' ? { scenes: current.scenes.map((scene) => scene.sceneId === 's-clip' ? { ...scene, takes: [{ ...scene.takes[0], shotInstruction: { shotMode: 'performance', edit: { inSec: 2, outSec: 8 } } }] } : scene) } : {}),
+ ...(change === 'storyboard' ? { productionReview: { draft: { storyboard: [{ sceneId: 's-intro', action: 'Revised movement' }] } } } : {}),
+ } }));
+ const asked = [];
+ h.onSubmit = async () => {
+ asked.push(...['intro', 'still', 'clip'].filter((section) => h.prompt.includes(`"id":"${section}"`)));
+ h.response = response({ intro: '#aabbcc', still: '#aabbcc', clip: '#aabbcc' });
+ };
+ const { document } = await generateMixedMediaDocument(id, { providerId: 'ollama', promptBudgetChars: budget });
+ expect(asked).toEqual(['intro', 'still', 'clip']);
+ expect((await manifestAt(document)).sections.map((section) => section.source)).toEqual(['intro', 'still', 'clip'].map(() => source('#aabbcc')));
+ });
+ }
it('rechecks the production reservation when all local sections were staged before publication failed', async () => {
const id = await fixture();