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
// Engine mechanics are isolated from the review contract, covered in route/orchestration suites.
vi.mock('./productionReview.js', async (load) => ({ ...await load(), assertProductionApproval: vi.fn() }));
/**
* Render planning for the composition-document render style: a `document`
* project takes the seeked path with its document folder, a full render and a
* draft excerpt both refuse (and release their slot) while no document is
* attached, and the finished render is filed like any other.
*/
import { describe, expect, it, vi, afterAll } from 'vitest';
import { mkdir, writeFile } from 'node:fs/promises';
import { join } from 'node:path';
import { cleanupTempDataRoots, lazyTempDataRoot, makePathsProxy } from '../../lib/mockPathsDataRoot.js';
vi.mock('../../lib/paths.js', async (importOriginal) => makePathsProxy(await importOriginal(), {
dataRoot: () => lazyTempDataRoot('portos-mv-document-plan-'),
}));
vi.mock('../../lib/sseUtils.js', async load => ({ ...await load(), broadcastSse: vi.fn((...args) => broadcastOriginal(...args)) }));
const { broadcastSse: broadcastOriginal } = await vi.importActual('../../lib/sseUtils.js');
const { broadcastSse } = await import('../../lib/sseUtils.js');
vi.mock('../../lib/ffmpeg.js', async (importOriginal) => ({ ...(await importOriginal()), generateThumbnail: vi.fn(async () => 'thumb.jpg') }));
vi.mock('../instanceIdentity.js', () => ({ ensureInstanceId: vi.fn(async () => 'instance-test') }));
vi.mock('../videoGen/local.js', () => ({ loadHistory: vi.fn(async () => []), mutateVideoHistory: vi.fn(async () => {}) }));
vi.mock('./codeRender.js', async (importOriginal) => ({ ...(await importOriginal()), writeCodeProofSheet: vi.fn(async () => null) }));
vi.mock('./documentRender.js', async (importOriginal) => ({
...(await importOriginal()),
encodeDocumentComposition: vi.fn(async ({ plan }) => ({ width: plan.width, height: plan.height, fps: 24, durationSec: plan.durationSec, startSec: 0, boundaryTimes: [0] })),
}));
const { PATHS } = await import('../../lib/paths.js');
const projects = await import('./projects.js');
const { renderMusicVideo } = await import('./render.js');
const { startExcerptRender } = await import('./excerptRender.js');
const { importDocumentTemplate } = await import('./compositionDocument.js');
const { encodeDocumentComposition } = await import('./documentRender.js');
const { mutateVideoHistory } = await import('../videoGen/local.js');
const { musicVideoEvents } = await import('./events.js');
afterAll(() => cleanupTempDataRoots());
async function documentProject() {
await mkdir(PATHS.music, { recursive: true });
await writeFile(join(PATHS.music, 'song.wav'), Buffer.alloc(64));
const created = await projects.createProject({ name: 'Doc', uploadedAudioFilename: 'song.wav', composition: { mode: 'document' } });
await projects.mutateProjectRecord(created.id, (current) => ({ project: { ...current, audioAnalysis: { durationSec: 12.5, beats: [], downbeats: [], sections: [] } } }));
return created.id;
}
describe('composition-document render plan', () => {
it('checks owning production after preparation and again before encoding, releasing refused render slots', async () => {
const id = await documentProject();
await importDocumentTemplate(id);
encodeDocumentComposition.mockClear();
const refused = () => { throw Object.assign(new Error('Production stopped'), { code: 'PRODUCTION_NOT_RUNNING' }); };
await expect(renderMusicVideo(id, { verifyCurrent: refused })).rejects.toMatchObject({ code: 'PRODUCTION_NOT_RUNNING' });
expect(encodeDocumentComposition).not.toHaveBeenCalled();
let checks = 0;
await renderMusicVideo(id, { verifyCurrent: () => { if (++checks === 2) refused(); } });
await vi.waitFor(async () => expect((await projects.getProject(id)).status).toBe('failed'));
expect(checks).toBe(2);
expect(encodeDocumentComposition).not.toHaveBeenCalled();
// Both preparation and asynchronous encode refusal release the per-project slot.
await renderMusicVideo(id);
await vi.waitFor(() => expect(encodeDocumentComposition).toHaveBeenCalledOnce());
});
it('refuses a full render and an excerpt while no document is attached, and releases both slots', async () => {
const id = await documentProject();
for (let attempt = 0; attempt < 2; attempt++) {
await expect(renderMusicVideo(id)).rejects.toMatchObject({ status: 409, code: 'COMPOSITION_DOCUMENT_MISSING' });
await expect(startExcerptRender(id, { startSec: 1, endSec: 3 })).rejects.toMatchObject({ status: 409, code: 'COMPOSITION_DOCUMENT_MISSING' });
}
expect(encodeDocumentComposition).not.toHaveBeenCalled();
});
it('renders the attached document over the song and files the result', async () => {
const id = await documentProject();
const { document } = await importDocumentTemplate(id);
const { jobId } = await renderMusicVideo(id);
await vi.waitFor(() => expect(mutateVideoHistory).toHaveBeenCalled());
const call = encodeDocumentComposition.mock.calls.at(-1)[0];
expect(call).toMatchObject({
jobId,
audioPath: join(PATHS.music, 'song.wav'),
soundBed: null,
plan: { directory: document.directory, songDurationSec: 12.5, durationSec: 12.5, fps: 24, width: 1920, height: 1080 },
});
expect(call).not.toHaveProperty('windowStart');
await vi.waitFor(async () => expect((await projects.getProject(id)).renderHistoryId).toBe(jobId));
});
it('renders a faded 9:16 social cut of a 16:9 document project without re-framing the project (#9280)', async () => {
const id = await documentProject();
await importDocumentTemplate(id);
encodeDocumentComposition.mockClear();
const visibility = [{ sceneId: 'example-shot', atSec: 1, status: 'measured', visibleFraction: 0.2, hiddenFraction: 0.8, measuredFraction: 1 }];
encodeDocumentComposition.mockResolvedValueOnce({ width: 1080, height: 1920, fps: 24, boundaryTimes: [0], footageVisibility: visibility });
const { excerptId } = await startExcerptRender(id, { startSec: 1, endSec: 4, aspect: '9:16', fade: true });
await vi.waitFor(async () => expect((await projects.getProject(id)).excerpts.find((e) => e.id === excerptId).status).toBe('complete'));
const call = encodeDocumentComposition.mock.calls.at(-1)[0];
expect(call).toMatchObject({ fade: true, collectFootageVisibility: true, windowStart: 1, windowEnd: 4, plan: { width: 1080, height: 1920 } });
expect(call.project.treatment.brief.aspectRatio).toBe('9:16');
const stored = await projects.getProject(id);
expect(stored.treatment?.brief?.aspectRatio ?? '16:9').toBe('16:9');
expect(stored.excerpts.find((e) => e.id === excerptId)).toMatchObject({ aspect: '9:16', fade: true, width: 1080, height: 1920, footageVisibility: visibility });
});
it('refuses a social cut of a footage project, which has no frame of its own to re-lay-out (#9280)', async () => {
await mkdir(PATHS.music, { recursive: true });
await writeFile(join(PATHS.music, 'song.wav'), Buffer.alloc(64));
const { id } = await projects.createProject({ name: 'Footage', uploadedAudioFilename: 'song.wav' });
for (const body of [{ aspect: '9:16' }, { fade: true }]) {
await expect(startExcerptRender(id, { startSec: 1, endSec: 3, ...body })).rejects.toMatchObject({ status: 422, code: 'EXCERPT_ASPECT_UNSUPPORTED' });
}
});
// Uniquely catches a draft blocked by a stale lip-sync take, or a final render drawing one.
it('refuses a full render over a stale performance take (#9266), while a draft draws it as a cutaway', async () => {
const id = await documentProject();
await mkdir(PATHS.images, { recursive: true });
await writeFile(join(PATHS.images, 'still.png'), 'synthetic still');
await writeFile(join(PATHS.data, 'video-history.json'), JSON.stringify([{ id: 'clip-1', filename: 'clip-1.mp4' }]));
await importDocumentTemplate(id);
await projects.mutateProjectRecord(id, (current) => ({ project: {
...current,
scenes: [{ sceneId: 'mvs-1', order: 0, shotMode: 'performance', startSec: 1, endSec: 3, videoHistoryId: 'clip-1', referenceImageId: 'still.png', takes: [] }],
} }));
encodeDocumentComposition.mockClear();
for (let attempt = 0; attempt < 2; attempt++) {
await expect(renderMusicVideo(id)).rejects.toMatchObject({
status: 422, code: 'STALE_PERFORMANCE_TAKES', context: { stale: [{ sceneId: 'mvs-1', reason: 'not-lip-synced' }] },
});
}
expect(encodeDocumentComposition).not.toHaveBeenCalled();
await startExcerptRender(id, { startSec: 1, endSec: 3 });
await vi.waitFor(() => expect(encodeDocumentComposition).toHaveBeenCalledOnce());
expect(encodeDocumentComposition.mock.calls[0][0].project.scenes[0]).toMatchObject({ shotMode: 'cutaway', videoHistoryId: null, referenceImageId: 'still.png' });
expect((await projects.getProject(id)).scenes[0]).toMatchObject({ shotMode: 'performance', videoHistoryId: 'clip-1' });
});
});
it('rejects a changed film between preparing its reviewed plan and starting the encoder', async () => {
const id = await documentProject();
await importDocumentTemplate(id);
encodeDocumentComposition.mockClear();
let changed = false;
await renderMusicVideo(id, { verifyCurrent: async () => {
if (changed) return;
changed = true;
await projects.mutateProjectRecord(id, project => ({ project: { ...project,
composition: { ...project.composition, posterSec: 2 },
} }));
} });
await vi.waitFor(async () => expect((await projects.getProject(id)).status).toBe('failed'));
expect(encodeDocumentComposition).not.toHaveBeenCalled();
});
// The API client can leave before the SSE failure: keep the reason on the record.
it('persists a document failure for returning operators and clears it after a successful retry', async () => {
const id = await documentProject();
await importDocumentTemplate(id);
const renderAndWait = async () => {
let jobId;
const early = new Map();
const terminal = Promise.withResolvers();
const onRender = event => {
if (event.projectId !== id) return;
// A fast encoder can settle before dispatch returns its job ID.
if (!jobId) early.set(event.jobId, event);
else if (event.jobId === jobId) terminal.resolve(event);
};
musicVideoEvents.on('render', onRender);
try {
({ jobId } = await renderMusicVideo(id));
return early.get(jobId) || await terminal.promise;
} finally {
musicVideoEvents.off('render', onRender);
}
};
const message = 'Edit portosComposition.durationSec to a whole-frame duration and re-import the document.';
encodeDocumentComposition.mockRejectedValueOnce(new Error(message));
expect(await renderAndWait()).toMatchObject({ projectId: id, status: 'failed', error: `Composition document render failed: ${message}` });
expect(await projects.getProject(id)).toMatchObject({
status: 'failed', renderError: `Composition document render failed: ${message}`,
});
expect(await renderAndWait()).toMatchObject({ projectId: id, status: 'completed', error: null });
expect(await projects.getProject(id)).toMatchObject({ status: 'complete', renderError: null });
});
it.each(['error', 'canceled'])('persists document excerpt %s before notifying subscribers to refresh', async (status) => {
const id = await documentProject();
await importDocumentTemplate(id);
let rejectEncode;
encodeDocumentComposition.mockImplementationOnce(() => new Promise((resolve, reject) => { rejectEncode = reject; }));
const { excerptId } = await startExcerptRender(id, { startSec: 0, endSec: 2 });
await vi.waitFor(() => expect(rejectEncode).toBeTypeOf('function'));
const snapshots = [];
broadcastSse.mockImplementation((job, frame) => {
if (frame.type === status) snapshots.push(projects.getProject(id));
return broadcastOriginal(job, frame);
});
try {
rejectEncode(Object.assign(new Error('Synthetic encoder interruption'), status === 'canceled' ? { code: 'CANCELED' } : {}));
await vi.waitFor(() => expect(snapshots).toHaveLength(1));
const snapshot = await snapshots[0];
expect(snapshot.excerpts.find(e => e.id === excerptId)).toMatchObject({ status, jobId: null });
} finally {
broadcastSse.mockImplementation(broadcastOriginal);
}
});