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 gallery upload fixtures and report stage timings (#13238)

Adam Eivy06417b295e41
diff --git a/server/services/imageGen/saveUploadedGalleryImage.test.js b/server/services/imageGen/saveUploadedGalleryImage.test.js
index f821d353ed..9725e64848 100644
--- a/server/services/imageGen/saveUploadedGalleryImage.test.js
+++ b/server/services/imageGen/saveUploadedGalleryImage.test.js
@@ -1,10 +1,97 @@
-import { describe, it, expect, vi, beforeAll, afterAll } from 'vitest';
-import { mkdtempSync, rmSync, existsSync, readFileSync, writeFileSync } from 'fs';
+import { describe, it, expect, vi, beforeAll, afterAll, afterEach } from 'vitest';
+import { mkdtempSync, rmSync, existsSync, readFileSync, writeFileSync, statSync } from 'fs';
import { join } from 'path';
import { tmpdir } from 'os';
import sharp from 'sharp';
+import { AsyncLocalStorage } from 'node:async_hooks';
import { makePathsProxy } from '../../lib/mockPathsDataRoot.js';
+const uploadTrace = new AsyncLocalStorage();
+
+// Log starts as well as settlements: a timed-out worker may never reach finally.
+// Only stage names/timings leave the fixture, never its paths or image metadata.
+function beginStage(stage) {
+ if (!uploadTrace.getStore()) return () => {};
+ const start = performance.now();
+ const event = { stage, state: 'running' };
+ console.info(`📥 Gallery upload stage ${JSON.stringify(event)}`);
+ return (state = 'fulfilled', code) => {
+ if (event.state !== 'running') return;
+ event.state = state;
+ if (code) event.code = code;
+ event.elapsedMs = Math.round(performance.now() - start);
+ console.info(`📥 Gallery upload stage ${JSON.stringify(event)}`);
+ };
+}
+
+async function measureStage(stage, work) {
+ const finish = beginStage(stage);
+ try {
+ const value = await work();
+ finish();
+ return value;
+ } catch (error) {
+ finish('rejected', error.code);
+ throw error;
+ }
+}
+
+// Own the entire test continuation, including setup and assertions. Vitest's
+// timeout abandons its waiter; it does not cancel native Sharp or filesystem work.
+function ownFixture(root) {
+ const pending = new Set();
+ let closing = false;
+ const settle = () => Promise.all([...pending]);
+ return {
+ run(work, trace = false) {
+ if (closing) throw new Error('Gallery fixture is closing');
+ const operation = Promise.resolve().then(() => uploadTrace.run(trace, work));
+ const terminal = operation.then(() => {}, () => {});
+ pending.add(terminal);
+ void terminal.then(() => pending.delete(terminal));
+ return operation;
+ },
+ settle,
+ async cleanup() {
+ closing = true;
+ await settle();
+ rmSync(root, { recursive: true, force: true });
+ expect(existsSync(root)).toBe(false);
+ },
+ };
+}
+
+vi.mock('../../lib/pngMetadata.js', async original => {
+ const actual = await original();
+ return { ...actual, extractPngGenerationMetadataFile: (...args) =>
+ measureStage('metadata-read', () => actual.extractPngGenerationMetadataFile(...args)) };
+});
+
+vi.mock('../../lib/backupSnapshotBoundary.js', async original => {
+ const actual = await original();
+ return { ...actual, withBackupAssetPublication: async (work, ...args) => {
+ const admitted = beginStage('backup-admission');
+ try {
+ return await actual.withBackupAssetPublication(() => {
+ admitted();
+ return measureStage('publication', work);
+ }, ...args);
+ } finally {
+ admitted('rejected');
+ }
+ } };
+});
+
+vi.mock('fs/promises', async original => {
+ const actual = await original();
+ return { ...actual, rename: (...args) => measureStage('publish-rename', () => actual.rename(...args)) };
+});
+
+vi.mock('../mediaAssetIndex/index.js', async original => {
+ const actual = await original();
+ return { ...actual, indexImage: (...args) => measureStage('index-refresh', () => actual.indexImage(...args)) };
+});
+
// The PNG 8-byte signature — used to assert a written gallery file is a PNG
// without importing from the (mocked) fileUtils module.
const PNG_SIGNATURE = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]);
@@ -44,6 +131,11 @@ vi.mock('../../lib/fileUtils.js', async () => {
return makePathsProxy(actual, {
dataRoot: () => imagesDir, // unused root; images override below is what matters
extraOverrides: () => ({ images: imagesDir }),
+ overrides: {
+ ensureDir: (...args) => measureStage('gallery-directory', () => actual.ensureDir(...args)),
+ atomicWrite: (...args) => measureStage('sidecar-write', () => actual.atomicWrite(...args)),
+ unlinkGuarded: (...args) => measureStage('staging-cleanup', () => actual.unlinkGuarded(...args)),
+ },
});
});
@@ -55,6 +147,7 @@ vi.mock('../../lib/pythonSetup.js', () => ({
}));
let tmpRoot;
+let fixture;
let priorRegistryEnv;
let saveUploadedGalleryImage;
@@ -66,39 +159,51 @@ let realJpeg;
beforeAll(async () => {
tmpRoot = mkdtempSync(join(tmpdir(), 'portos-gallery-upload-test-'));
imagesDir = join(tmpRoot, 'images');
- priorRegistryEnv = process.env.PORTOS_MEDIA_MODELS_FILE;
- process.env.PORTOS_MEDIA_MODELS_FILE = join(tmpRoot, 'media-models.json');
- realPng = await sharp({ create: { width: 2, height: 2, channels: 3, background: { r: 10, g: 20, b: 30 } } }).png().toBuffer();
- realJpeg = await sharp({ create: { width: 2, height: 2, channels: 3, background: { r: 40, g: 50, b: 60 } } }).jpeg().toBuffer();
- vi.resetModules();
- ({ saveUploadedGalleryImage } = await import('./local.js'));
+ fixture = ownFixture(tmpRoot);
+ return fixture.run(async () => {
+ const toFile = sharp.prototype.toFile;
+ vi.spyOn(sharp.prototype, 'toFile').mockImplementation(function (...args) {
+ return measureStage('sharp-encode', () => toFile.apply(this, args));
+ });
+ priorRegistryEnv = process.env.PORTOS_MEDIA_MODELS_FILE;
+ process.env.PORTOS_MEDIA_MODELS_FILE = join(tmpRoot, 'media-models.json');
+ realPng = await sharp({ create: { width: 2, height: 2, channels: 3, background: { r: 10, g: 20, b: 30 } } }).png().toBuffer();
+ realJpeg = await sharp({ create: { width: 2, height: 2, channels: 3, background: { r: 40, g: 50, b: 60 } } }).jpeg().toBuffer();
+ vi.resetModules();
+ ({ saveUploadedGalleryImage } = await import('./local.js'));
+ });
});
-afterAll(() => {
+afterEach(async () => {
+ await fixture?.settle();
+});
+
+afterAll(async () => {
+ await fixture?.cleanup();
+ vi.restoreAllMocks();
if (priorRegistryEnv === undefined) delete process.env.PORTOS_MEDIA_MODELS_FILE;
else process.env.PORTOS_MEDIA_MODELS_FILE = priorRegistryEnv;
- rmSync(tmpRoot, { recursive: true, force: true });
});
describe('saveUploadedGalleryImage', () => {
- it('writes a PNG into the gallery and returns a /data/images path', async () => {
+ it('writes a PNG into the gallery and returns a /data/images path', () => fixture.run(async () => {
const { filename, path } = await saveUploadedGalleryImage(realPng.toString('base64'));
expect(filename).toMatch(/^upload-[0-9a-f]{8}\.png$/);
expect(path).toBe(`/data/images/${filename}`);
expect(existsSync(join(imagesDir, filename))).toBe(true);
// Written file is a valid PNG (so the gallery's PNG-only list/delete manage it).
expect(isPng(readFileSync(join(imagesDir, filename)))).toBe(true);
- });
+ }));
- it('normalizes a non-PNG upload (GIF/JPEG) to a managed .png gallery file', async () => {
+ it('normalizes a non-PNG upload (GIF/JPEG) to a managed .png gallery file', () => fixture.run(async () => {
for (const src of [GIF, realJpeg]) {
const { filename } = await saveUploadedGalleryImage(src.toString('base64'));
expect(filename.endsWith('.png')).toBe(true);
expect(isPng(readFileSync(join(imagesDir, filename)))).toBe(true);
}
- });
+ }));
- it('extracts Stable Diffusion parameters before normalization and writes a sidecar', async () => {
+ it('extracts Stable Diffusion parameters before normalization and writes a sidecar', () => fixture.run(async () => {
const source = addPngText(realPng, 'parameters', [
'a paper boat on a moonlit lake',
'Negative prompt: blurry, text',
@@ -112,26 +217,62 @@ describe('saveUploadedGalleryImage', () => {
});
const sidecar = join(imagesDir, filename.replace('.png', '.metadata.json'));
expect(JSON.parse(readFileSync(sidecar, 'utf8'))).toMatchObject(metadata);
- });
+ }));
- it('rejects an empty upload with a 400', async () => {
+ it('rejects an empty upload with a 400', () => fixture.run(async () => {
await expect(saveUploadedGalleryImage('')).rejects.toMatchObject({ status: 400 });
- });
+ }));
- it('rejects non-image bytes with a 400', async () => {
+ it('rejects non-image bytes with a 400', () => fixture.run(async () => {
await expect(
saveUploadedGalleryImage(Buffer.from('definitely not an image').toString('base64')),
).rejects.toMatchObject({ status: 400, code: 'UNSUPPORTED_IMAGE' });
- });
+ }));
- it('accepts a file above the former JSON ceiling and preserves PNG metadata', async () => {
- const png = await sharp({ create: { width: 2, height: 2, channels: 3, background: 'red' } }).png().toBuffer();
+ it('accepts a file above the former JSON ceiling and preserves PNG metadata', () => fixture.run(async () => {
+ const png = await measureStage('fixture-encode', () =>
+ sharp({ create: { width: 2, height: 2, channels: 3, background: 'red' } }).png().toBuffer());
const source = addPngText(png, 'parameters', 'A test image\nSteps: 20, Seed: 42');
const tempPath = join(tmpRoot, 'large.png');
// Trailing padding is legal input for the decoder, without a huge pixel allocation.
- writeFileSync(tempPath, Buffer.concat([source, Buffer.alloc(56 * 1024 * 1024)]));
- const saved = await saveUploadedGalleryImage(null, tempPath);
- expect(saved.metadata.prompt).toBe('A test image');
+ await measureStage('fixture-write', () =>
+ writeFileSync(tempPath, Buffer.concat([source, Buffer.alloc(56 * 1024 * 1024)])));
+ expect(statSync(tempPath).size).toBeGreaterThan(56 * 1024 * 1024);
+ const saved = await measureStage('upload', () => saveUploadedGalleryImage(null, tempPath));
+ expect(saved.metadata).toMatchObject({ prompt: 'A test image', steps: 20, seed: 42 });
+ expect(JSON.parse(readFileSync(join(imagesDir, saved.filename.replace('.png', '.metadata.json')), 'utf8')))
+ .toMatchObject(saved.metadata);
expect(isPng(readFileSync(join(imagesDir, saved.filename)))).toBe(true);
+ }, true));
+
+ it('keeps the fixture until an abandoned writer rejects and settles', async () => {
+ const root = mkdtempSync(join(tmpdir(), 'portos-gallery-owned-work-'));
+ const owner = ownFixture(root);
+ let release;
+ const gate = new Promise(resolve => { release = resolve; });
+ const failure = new Error('controlled writer failure');
+ const operation = owner.run(async () => {
+ await gate;
+ writeFileSync(join(root, 'late-write'), 'fixture');
+ throw failure;
+ });
+ // Deterministically abandon the waiter, as a test deadline would. No sleep
+ // or real timeout is needed to prove that the underlying writer stays owned.
+ await expect(Promise.race([operation, Promise.reject(new Error('controlled timeout'))]))
+ .rejects.toThrow('controlled timeout');
+ let removed = false;
+ const cleanup = owner.cleanup().then(() => { removed = true; });
+ try {
+ await Promise.resolve();
+ expect(removed).toBe(false);
+ expect(existsSync(root)).toBe(true);
+ expect(() => owner.run(() => {})).toThrow('Gallery fixture is closing');
+ } finally {
+ release();
+ await expect(operation).rejects.toBe(failure);
+ await cleanup;
+ }
+ expect(removed).toBe(true);
+ expect(existsSync(root)).toBe(false);
});
});