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

refactor([issue-1144]): extract shared MockEventSource test helper, normalize timeline cancel frame to 'canceled'

[._.]/ Adam Eivy1e18d581b4d0
diff --git a/client/src/hooks/useInstallStream.test.jsx b/client/src/hooks/useInstallStream.test.jsx
index ce1820efcc..4348d19546 100644
--- a/client/src/hooks/useInstallStream.test.jsx
+++ b/client/src/hooks/useInstallStream.test.jsx
@@ -1,36 +1,10 @@
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { renderHook, act } from '@testing-library/react';
import { useInstallStream } from './useInstallStream';
-
-// jsdom has no EventSource — stand up a minimal mock that records instances
-// and lets a test drive onmessage / onerror by hand.
-class MockEventSource {
- constructor(url) {
- this.url = url;
- this.onmessage = null;
- this.onerror = null;
- this.onopen = null;
- this.closed = false;
- this.readyState = MockEventSource.OPEN;
- MockEventSource.instances.push(this);
- }
-
- close() { this.closed = true; this.readyState = MockEventSource.CLOSED; }
-
- emit(payload) { this.onmessage?.({ data: JSON.stringify(payload) }); }
-
- emitRaw(data) { this.onmessage?.({ data }); }
-
- fail() { this.readyState = MockEventSource.CLOSED; this.onerror?.(); }
-}
-MockEventSource.CONNECTING = 0;
-MockEventSource.OPEN = 1;
-MockEventSource.CLOSED = 2;
-
-const last = () => MockEventSource.instances[MockEventSource.instances.length - 1];
+import { MockEventSource, lastEventSource as last } from '../test/mockEventSource';
beforeEach(() => {
- MockEventSource.instances = [];
+ MockEventSource.reset();
global.EventSource = MockEventSource;
// jsdom doesn't implement scrollIntoView; the auto-scroll effect calls it.
Element.prototype.scrollIntoView = vi.fn();
diff --git a/client/src/hooks/useMediaJobSse.test.jsx b/client/src/hooks/useMediaJobSse.test.jsx
index 5743c8f597..0ad50a2cee 100644
--- a/client/src/hooks/useMediaJobSse.test.jsx
+++ b/client/src/hooks/useMediaJobSse.test.jsx
@@ -1,38 +1,10 @@
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { renderHook } from '@testing-library/react';
import { useMediaJobSse } from './useMediaJobSse';
-
-// jsdom has no EventSource — stand up a minimal mock that records instances
-// and lets a test drive onmessage / onerror by hand.
-class MockEventSource {
- constructor(url) {
- this.url = url;
- this.onmessage = null;
- this.onerror = null;
- this.closed = false;
- this.readyState = MockEventSource.OPEN;
- MockEventSource.instances.push(this);
- }
-
- close() { this.closed = true; this.readyState = MockEventSource.CLOSED; }
-
- emit(payload) { this.onmessage?.({ data: JSON.stringify(payload) }); }
-
- emitRaw(data) { this.onmessage?.({ data }); }
-
- // A terminal failure (non-2xx / non-event-stream response): the browser sets
- // readyState CLOSED and will NOT auto-retry. A transient blip leaves
- // readyState CONNECTING so the browser's built-in reconnect can recover.
- fail(readyState = MockEventSource.CLOSED) { this.readyState = readyState; this.onerror?.(); }
-}
-MockEventSource.CONNECTING = 0;
-MockEventSource.OPEN = 1;
-MockEventSource.CLOSED = 2;
-
-const last = () => MockEventSource.instances[MockEventSource.instances.length - 1];
+import { MockEventSource, lastEventSource as last } from '../test/mockEventSource';
beforeEach(() => {
- MockEventSource.instances = [];
+ MockEventSource.reset();
global.EventSource = MockEventSource;
});
diff --git a/client/src/hooks/usePipelineProgress.test.jsx b/client/src/hooks/usePipelineProgress.test.jsx
index 29c9bd05e8..1aea46461e 100644
--- a/client/src/hooks/usePipelineProgress.test.jsx
+++ b/client/src/hooks/usePipelineProgress.test.jsx
@@ -1,25 +1,10 @@
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import { renderHook } from '@testing-library/react';
import { usePipelineProgress } from './usePipelineProgress';
-
-class MockEventSource {
- constructor(url) {
- this.url = url;
- this.onmessage = null;
- this.onerror = null;
- this.onopen = null;
- this.readyState = MockEventSource.OPEN;
- MockEventSource.instances.push(this);
- }
-
- close() { this.readyState = MockEventSource.CLOSED; }
-}
-MockEventSource.CONNECTING = 0;
-MockEventSource.OPEN = 1;
-MockEventSource.CLOSED = 2;
+import { MockEventSource } from '../test/mockEventSource';
beforeEach(() => {
- MockEventSource.instances = [];
+ MockEventSource.reset();
global.EventSource = MockEventSource;
});
diff --git a/client/src/hooks/useSseProgress.js b/client/src/hooks/useSseProgress.js
index d0e8ad4e15..d65f58ce8e 100644
--- a/client/src/hooks/useSseProgress.js
+++ b/client/src/hooks/useSseProgress.js
@@ -1,7 +1,8 @@
import { useEffect, useRef, useState } from 'react';
-// Both cancel spellings are terminal: pipeline runners emit `canceled`,
-// the video-timeline renderer emits `cancelled`.
+// Both cancel spellings are terminal. Every emitter normalizes on `canceled`
+// (single l); `cancelled` stays tolerated defensively so a stream from an
+// emitter that drifts back to the double-l spelling still terminates.
const TERMINAL_TYPES = new Set(['complete', 'canceled', 'cancelled', 'error']);
/**
diff --git a/client/src/hooks/useSseProgress.test.jsx b/client/src/hooks/useSseProgress.test.jsx
index bf5c2780ff..baeb207800 100644
--- a/client/src/hooks/useSseProgress.test.jsx
+++ b/client/src/hooks/useSseProgress.test.jsx
@@ -1,34 +1,10 @@
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import { renderHook, act } from '@testing-library/react';
import { useSseProgress } from './useSseProgress';
-
-// jsdom has no EventSource — minimal mock that records instances and lets a
-// test drive onmessage / onerror by hand (same shape as useInstallStream's).
-class MockEventSource {
- constructor(url) {
- this.url = url;
- this.onmessage = null;
- this.onerror = null;
- this.onopen = null;
- this.closed = false;
- this.readyState = MockEventSource.OPEN;
- MockEventSource.instances.push(this);
- }
-
- close() { this.closed = true; this.readyState = MockEventSource.CLOSED; }
-
- emit(payload) { this.onmessage?.({ data: JSON.stringify(payload) }); }
-
- fail() { this.readyState = MockEventSource.CLOSED; this.onerror?.(); }
-}
-MockEventSource.CONNECTING = 0;
-MockEventSource.OPEN = 1;
-MockEventSource.CLOSED = 2;
-
-const last = () => MockEventSource.instances[MockEventSource.instances.length - 1];
+import { MockEventSource, lastEventSource as last } from '../test/mockEventSource';
beforeEach(() => {
- MockEventSource.instances = [];
+ MockEventSource.reset();
global.EventSource = MockEventSource;
});
diff --git a/client/src/pages/PipelineManuscriptEditor.test.jsx b/client/src/pages/PipelineManuscriptEditor.test.jsx
index 3aedcea6d0..3b7fed7e6f 100644
--- a/client/src/pages/PipelineManuscriptEditor.test.jsx
+++ b/client/src/pages/PipelineManuscriptEditor.test.jsx
@@ -1,6 +1,7 @@
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { render, screen, waitFor, fireEvent, within } from '@testing-library/react';
import { MemoryRouter, Routes, Route } from 'react-router-dom';
+import { MockEventSource, lastEventSource as lastEs } from '../test/mockEventSource';
const api = vi.hoisted(() => ({
getPipelineSeries: vi.fn(),
@@ -355,31 +356,9 @@ describe('PipelineManuscriptEditor', () => {
});
});
-// jsdom has no EventSource — stand up a minimal mock the test drives by hand.
-class MockEventSource {
- constructor(url) {
- this.url = url;
- this.onmessage = null;
- this.onerror = null;
- this.onopen = null;
- this.readyState = MockEventSource.OPEN;
- MockEventSource.instances.push(this);
- }
- close() { this.readyState = MockEventSource.CLOSED; }
- emit(payload) { this.onmessage?.({ data: JSON.stringify(payload) }); }
- // Simulate a non-terminal stream death (attach 404, dropped connection): the
- // hook flips `closed` true via onerror when readyState is CLOSED, with no
- // terminal frame ever delivered.
- fail() { this.readyState = MockEventSource.CLOSED; this.onerror?.(); }
-}
-MockEventSource.CONNECTING = 0;
-MockEventSource.OPEN = 1;
-MockEventSource.CLOSED = 2;
-const lastEs = () => MockEventSource.instances[MockEventSource.instances.length - 1];
-
describe('PipelineManuscriptEditor — generate-edits streamed review', () => {
beforeEach(() => {
- MockEventSource.instances = [];
+ MockEventSource.reset();
// Stub for the whole describe — a deferred SSE-open effect can fire after the
// test body returns (during RTL cleanup), so don't delete it per-test or that
// late `new EventSource(url)` throws a ReferenceError.
diff --git a/client/src/pages/VideoTimelineEditor.jsx b/client/src/pages/VideoTimelineEditor.jsx
index 2cb90c5fbb..1fbae04c60 100644
--- a/client/src/pages/VideoTimelineEditor.jsx
+++ b/client/src/pages/VideoTimelineEditor.jsx
@@ -410,7 +410,7 @@ export default function VideoTimelineEditor() {
// the shared useSseProgress lifecycle), updates the progress bar, and on
// 'complete' navigates to Media History focused on the new clip. Frame
// shapes come from server/services/videoTimeline/local.js
- // (progress / complete / error / cancelled).
+ // (progress / complete / error / canceled).
const { latest: renderFrame, closed: renderStreamClosed } = useSseProgress(
renderJobId ? `/api/video-timeline/${renderJobId}/events` : null,
{ enabled: !!renderJobId },
@@ -425,7 +425,7 @@ export default function VideoTimelineEditor() {
} else if (renderFrame.type === 'error') {
toast.error(renderFrame.error || 'Render failed');
setRenderJobId(null);
- } else if (renderFrame.type === 'cancelled') {
+ } else if (renderFrame.type === 'canceled') {
toast('Render cancelled');
setRenderJobId(null);
setRenderProgress(0);
@@ -435,7 +435,7 @@ export default function VideoTimelineEditor() {
// Stream ended without a terminal frame — connection lost. Terminal frames
// are handled (and clear renderJobId) in the frame effect above.
if (!renderJobId || !renderStreamClosed) return;
- if (['complete', 'error', 'cancelled'].includes(renderFrame?.type)) return;
+ if (['complete', 'error', 'canceled'].includes(renderFrame?.type)) return;
toast.error('Lost connection to render — check Media History');
setRenderJobId(null);
setRenderProgress(0);
diff --git a/client/src/test/mockEventSource.js b/client/src/test/mockEventSource.js
new file mode 100644
index 0000000000..8a56f2b4f3
--- /dev/null
+++ b/client/src/test/mockEventSource.js
@@ -0,0 +1,41 @@
+/**
+ * Minimal EventSource stand-in for jsdom (which has none). Tests drive
+ * onmessage / onerror by hand:
+ *
+ * beforeEach(() => { MockEventSource.reset(); global.EventSource = MockEventSource; });
+ * afterEach(() => { delete global.EventSource; });
+ *
+ * `emit(payload)` delivers a JSON frame, `emitRaw(data)` delivers the string
+ * as-is, and `fail()` simulates a terminal connection failure (non-2xx /
+ * non-event-stream response: the browser sets readyState CLOSED and will NOT
+ * auto-retry). Pass `fail(MockEventSource.CONNECTING)` for a transient blip
+ * the browser's built-in reconnect would recover from.
+ */
+export class MockEventSource {
+ constructor(url) {
+ this.url = url;
+ this.onmessage = null;
+ this.onerror = null;
+ this.onopen = null;
+ this.closed = false;
+ this.readyState = MockEventSource.OPEN;
+ MockEventSource.instances.push(this);
+ }
+
+ close() { this.closed = true; this.readyState = MockEventSource.CLOSED; }
+
+ emit(payload) { this.onmessage?.({ data: JSON.stringify(payload) }); }
+
+ emitRaw(data) { this.onmessage?.({ data }); }
+
+ fail(readyState = MockEventSource.CLOSED) { this.readyState = readyState; this.onerror?.(); }
+
+ static reset() { MockEventSource.instances = []; }
+}
+MockEventSource.CONNECTING = 0;
+MockEventSource.OPEN = 1;
+MockEventSource.CLOSED = 2;
+MockEventSource.instances = [];
+
+export const lastEventSource = () =>
+ MockEventSource.instances[MockEventSource.instances.length - 1];
diff --git a/server/services/creativeDirector/stitchRunner.js b/server/services/creativeDirector/stitchRunner.js
index 21eafcaf29..2b93d5eb0e 100644
--- a/server/services/creativeDirector/stitchRunner.js
+++ b/server/services/creativeDirector/stitchRunner.js
@@ -89,7 +89,7 @@ export async function runStitch(projectId) {
// Fast-fail: if the render job itself has entered an error/cancelled
// state, there will never be a history entry — bail immediately.
const jobStatus = getRenderJobStatus(jobId);
- if (jobStatus && (jobStatus.status === 'error' || jobStatus.status === 'cancelled')) {
+ if (jobStatus && (jobStatus.status === 'error' || jobStatus.status === 'canceled')) {
const reason = jobStatus.error ?? `Render ${jobStatus.status}`;
console.log(`❌ CD stitch: timeline render ${jobStatus.status} for ${timeline.id}: ${reason}`);
await updateProject(projectId, { status: 'failed', failureReason: reason });
diff --git a/server/services/videoTimeline/local.js b/server/services/videoTimeline/local.js
index 73a48c823d..93fa2ebfad 100644
--- a/server/services/videoTimeline/local.js
+++ b/server/services/videoTimeline/local.js
@@ -418,15 +418,17 @@ export async function renderProject(projectId) {
proc.on('close', async (code, signal) => {
job.process = null;
if (code !== 0) {
- const cancelled = signal === 'SIGTERM' || signal === 'SIGKILL';
- job.status = cancelled ? 'cancelled' : 'error';
- const reason = cancelled
+ const canceled = signal === 'SIGTERM' || signal === 'SIGKILL';
+ // 'canceled' (single l) matches every other SSE emitter in the app —
+ // useSseProgress treats it as terminal.
+ job.status = canceled ? 'canceled' : 'error';
+ const reason = canceled
? 'Render cancelled'
: signal ? `Killed by signal ${signal}` : `ffmpeg exit ${code}`;
job.lastError = reason;
- console.log(`${cancelled ? '🛑' : '❌'} Timeline render ${cancelled ? 'cancelled' : 'failed'} [${jobId.slice(0, 8)}]: ${reason}`);
+ console.log(`${canceled ? '🛑' : '❌'} Timeline render ${canceled ? 'cancelled' : 'failed'} [${jobId.slice(0, 8)}]: ${reason}`);
await unlink(outputPath).catch(() => {});
- broadcastSse(job, { type: cancelled ? 'cancelled' : 'error', error: reason });
+ broadcastSse(job, { type: canceled ? 'canceled' : 'error', error: reason });
projectRenders.delete(projectId);
closeJobAfterDelay(jobs, jobId);
return;