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: settle video import fixtures and diagnose stalled stages (#13289)

[._.]/ Adam Eivy002f5699c617
diff --git a/server/services/videoUpload.test.js b/server/services/videoUpload.test.js
index a9033d4981..76bc38e408 100644
--- a/server/services/videoUpload.test.js
+++ b/server/services/videoUpload.test.js
@@ -6,10 +6,10 @@
* the route surface, not re-run here against real dirs.
*/
-import { mkdtempSync, writeFileSync, readFileSync, rmSync, existsSync } from 'fs';
+import { mkdtempSync, writeFileSync, readFileSync, rmSync, existsSync, readdirSync } from 'fs';
import { tmpdir } from 'os';
import { join } from 'path';
-import { describe, it, expect, vi, afterAll } from 'vitest';
+import { describe, it, expect, vi, afterAll, afterEach, beforeEach } from 'vitest';
import {
detectVideoContainer,
buildUploadHistoryEntry,
@@ -18,18 +18,105 @@ import {
saveUploadedGalleryVideoFile,
} from './videoUpload.js';
-const uploadState = vi.hoisted(() => ({ root: '', history: [], fail: false }));
+// Diagnostics retain only fixed stage names, state and elapsed time, never paths,
+// payloads or raw errors. The most recent 64 boundaries cover both large imports.
+const uploadState = vi.hoisted(() => ({ root: '', history: [], fail: false, stages: [] }));
+const traceStage = vi.hoisted(() => async (stage, work) => {
+ const record = { stage, state: 'pending', started: performance.now() };
+ uploadState.stages.push(record);
+ if (uploadState.stages.length > 64) uploadState.stages.shift();
+ try {
+ const result = await work();
+ record.state = 'fulfilled';
+ return result;
+ } catch (error) {
+ record.state = 'rejected';
+ throw error;
+ } finally {
+ record.elapsed = Math.round(performance.now() - record.started);
+ }
+});
+vi.mock('fs/promises', async (original) => {
+ const actual = await original();
+ return {
+ ...actual,
+ open: (...args) => traceStage('file-open', async () => {
+ const file = await actual.open(...args);
+ const read = file.read.bind(file);
+ const close = file.close.bind(file);
+ file.read = (...readArgs) => traceStage('file-read', () => read(...readArgs));
+ file.close = () => traceStage('file-close', close);
+ return file;
+ }),
+ stat: (...args) => traceStage('file-stat', () => actual.stat(...args)),
+ rename: (...args) => traceStage('file-rename', () => actual.rename(...args)),
+ };
+});
vi.mock('../lib/fileUtils.js', async (original) => {
const actual = await original();
- return { ...actual, PATHS: { ...actual.PATHS, get videos() { return uploadState.root; } } };
+ return {
+ ...actual,
+ PATHS: { ...actual.PATHS,
+ get videos() { return uploadState.root; },
+ get videoThumbnails() { return join(uploadState.root, 'thumbnails'); },
+ },
+ ...Object.fromEntries(['ensureDir', 'atomicWrite', 'copyFileGuarded', 'unlinkGuarded'].map(name =>
+ [name, (...args) => traceStage(name, () => actual[name](...args))])),
+ };
});
-vi.mock('../lib/ffmpeg.js', () => ({ generateThumbnail: vi.fn(async () => null), probeVideoDuration: vi.fn(async () => 2), probeMediaTags: vi.fn(async () => ({})) }));
-vi.mock('./videoGen/history.js', () => ({ mutateVideoHistory: vi.fn(async (mutate) => {
+vi.mock('../lib/backupSnapshotBoundary.js', async (original) => {
+ const actual = await original();
+ return { ...actual, withBackupAssetPublication: (work, options) =>
+ traceStage('publication-admission-and-release', () => actual.withBackupAssetPublication(
+ () => traceStage('admitted-work', work), options)) };
+});
+vi.mock('../lib/ffmpeg.js', () => ({
+ generateThumbnail: vi.fn(() => traceStage('thumbnail', async () => null)),
+ probeVideoDuration: vi.fn(() => traceStage('duration', async () => 2)),
+ probeMediaTags: vi.fn(() => traceStage('tags', async () => ({}))),
+}));
+vi.mock('./videoGen/history.js', () => ({ mutateVideoHistory: vi.fn((mutate) => traceStage('history', async () => {
if (uploadState.fail) throw new Error('history write failed');
uploadState.history = mutate(uploadState.history);
-}) }));
+})) }));
+
+// Vitest's deadline stops awaiting a test; it does not cancel its async body.
+// Own the whole body, including later imports/rollback, until terminal settlement.
+function ownFixtureOperations() {
+ const pending = new Set();
+ let closing = false;
+ return {
+ run(work) {
+ if (closing) throw new Error('Fixture is closing');
+ const operation = Promise.resolve().then(work);
+ pending.add(operation);
+ // Own rejection on the observer too; callers still receive the original error.
+ void operation.then(() => pending.delete(operation), () => pending.delete(operation));
+ return operation;
+ },
+ settle() { return Promise.allSettled([...pending]); },
+ async cleanup(remove) {
+ closing = true;
+ await Promise.allSettled([...pending]);
+ remove();
+ },
+ };
+}
+const fixture = ownFixtureOperations();
uploadState.root = mkdtempSync(join(tmpdir(), 'gallery-video-test-'));
-afterAll(() => rmSync(uploadState.root, { recursive: true, force: true }));
+function reportStages() {
+ const summary = uploadState.stages.map(({ stage, state, started, elapsed }) =>
+ `${stage}:${state}:${elapsed ?? Math.round(performance.now() - started)}ms`).join(', ');
+ console.error(`❌ Video upload fixture stages: ${summary}`);
+}
+beforeEach(({ onTestFailed }) => {
+ onTestFailed(reportStages);
+});
+// A timed-out body must also settle before the next test changes shared mocks.
+afterEach(() => fixture.settle());
+afterAll(async () => {
+ await fixture.cleanup(() => rmSync(uploadState.root, { recursive: true, force: true }));
+});
const ftyp = (brand) => Buffer.concat([
Buffer.from([0, 0, 0, 24]),
@@ -89,7 +176,7 @@ describe('saveUploadedGalleryVideo validation', () => {
});
-it('imports a large staged video without the JSON ceiling and removes failed imports', async () => {
+it('imports a large staged video without the JSON ceiling and removes failed imports', () => fixture.run(async () => {
const tempPath = join(uploadState.root, 'source.mp4');
const bytes = Buffer.concat([ftyp('isom'), Buffer.alloc(56 * 1024 * 1024)]);
writeFileSync(tempPath, bytes);
@@ -98,14 +185,13 @@ it('imports a large staged video without the JSON ceiling and removes failed imp
expect(uploadState.history[0]).toEqual(entry);
expect(readFileSync(join(uploadState.root, entry.filename)).equals(bytes)).toBe(true);
uploadState.fail = true;
- const { readdirSync } = await import('fs');
const before = readdirSync(uploadState.root).sort();
await expect(saveUploadedGalleryVideoFile(tempPath, 'failed.mp4')).rejects.toThrow('history write failed');
expect(readdirSync(uploadState.root).sort()).toEqual(before);
expect(existsSync(tempPath)).toBe(true);
-});
+}));
- it('projects embedded video tags with the PNG import parser and preserves original bytes', async () => {
+it('projects embedded video tags with the PNG import parser and preserves original bytes', () => fixture.run(async () => {
uploadState.fail = false;
const { probeMediaTags } = await import('../lib/ffmpeg.js');
const parameters = 'Paper boat\nNegative prompt: text\nSteps: 20, Seed: 42, Size: 512x512, CFG scale: 7, Model: example-model';
@@ -120,4 +206,39 @@ it('imports a large staged video without the JSON ceiling and removes failed imp
expect(await saveUploadedGalleryVideoBuffer(bytes)).not.toHaveProperty('prompt');
probeMediaTags.mockResolvedValueOnce(null);
expect(await saveUploadedGalleryVideoBuffer(bytes)).toMatchObject({ importWarnings: [expect.stringContaining('could not be inspected')] });
- });
+}));
+
+// A caller has already failed, but history is still pending: teardown must keep
+// the source alive through the import's rejection AND its real file rollback.
+it('settles an abandoned import before removing its fixture', () => fixture.run(async () => {
+ const { mutateVideoHistory } = await import('./videoGen/history.js');
+ const owner = ownFixtureOperations();
+ const source = join(uploadState.root, 'delayed.mp4');
+ writeFileSync(source, ftyp('isom'));
+ const before = readdirSync(uploadState.root).sort();
+ const entered = Promise.withResolvers();
+ const held = Promise.withResolvers();
+ mutateVideoHistory.mockImplementationOnce(() => traceStage('held-history', () => {
+ entered.resolve();
+ return held.promise;
+ }));
+ const operation = owner.run(() => saveUploadedGalleryVideoFile(source, 'delayed.mp4'));
+ const rejection = expect(operation).rejects.toThrow('held history failed');
+ await entered.promise;
+ const callerFailure = new Error('caller deadline');
+ await expect(Promise.race([operation, Promise.reject(callerFailure)])).rejects.toBe(callerFailure);
+ const remove = vi.fn(() => rmSync(source));
+ const cleanup = owner.cleanup(remove);
+ try {
+ await Promise.resolve();
+ expect(remove).not.toHaveBeenCalled();
+ expect(existsSync(source)).toBe(true);
+ expect(readdirSync(uploadState.root).length).toBe(before.length + 1);
+ expect(() => owner.run(() => {})).toThrow('Fixture is closing');
+ } finally {
+ held.reject(new Error('held history failed'));
+ try { await rejection; } finally { await cleanup; }
+ }
+ expect(remove).toHaveBeenCalledOnce();
+ expect(readdirSync(uploadState.root).sort()).toEqual(before.filter(name => name !== 'delayed.mp4'));
+}));