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

3.2 KB
Raw Blame
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';

const attachNodeVideo = vi.fn(async (loomId, episodeId, nodeId, { videoHistoryId }) => ({
  id: nodeId, videoHistoryId,
}));
const attachNodePlaybackAsset = vi.fn(async (loomId, episodeId, nodeId, fields) => ({
  id: nodeId, ...fields,
}));
vi.mock('./fableLoom/records.js', () => ({ attachNodePlaybackAsset, attachNodeVideo }));

const { mediaJobEvents } = await import('./mediaJobQueue/index.js');
const hook = await import('./fableLoomSceneVideoHook.js');

async function waitFor(predicate, { timeoutMs = 1000, intervalMs = 5 } = {}) {
  const deadline = Date.now() + timeoutMs;
  while (Date.now() < deadline) {
    if (predicate()) return;
    await new Promise((resolve) => setTimeout(resolve, intervalMs));
  }
  throw new Error('waitFor: predicate never became true');
}

const tag = (over = {}) => ({ loomId: 'loom-1', episodeId: 'ep-1', nodeId: 'node-1', ...over });
const completedVideoJob = ({ params = {}, id = 'video-1', generationId, queuedAt } = {}) => ({
  kind: 'video', id, params,
  result: { generationId: generationId ?? id, filename: `${id}.mp4` },
  ...(queuedAt ? { queuedAt } : {}),
});

describe('fableLoomSceneVideoHook', () => {
  beforeEach(() => {
    hook.__testing.reset();
    hook.initFableLoomSceneVideoHook();
    attachNodeVideo.mockClear();
    attachNodePlaybackAsset.mockClear();
  });

  afterEach(() => hook.__testing.reset());

  it('files a completed fableLoom-tagged video onto its node', async () => {
    mediaJobEvents.emit('completed', completedVideoJob({ params: { fableLoom: tag() } }));
    await waitFor(() => attachNodeVideo.mock.calls.length > 0);
    expect(attachNodeVideo).toHaveBeenCalledWith('loom-1', 'ep-1', 'node-1', { videoHistoryId: 'video-1' });
  });

  it('falls back to the queue job id when generationId is absent', async () => {
    mediaJobEvents.emit('completed', {
      kind: 'video', id: 'video-2', params: { fableLoom: tag() }, result: { filename: 'video-2.mp4' },
    });
    await waitFor(() => attachNodeVideo.mock.calls.length > 0);
    expect(attachNodeVideo.mock.calls[0][3]).toEqual({ videoHistoryId: 'video-2' });
  });

  it('files typed playback assets with their transition identity and conditioning', async () => {
    mediaJobEvents.emit('completed', completedVideoJob({
      id: 'video-exit',
      params: {
        fableLoom: tag({ role: 'exit', transitionId: 'tr-1' }),
        visualConditioning: { version: 1, status: 'locked' },
      },
    }));
    await waitFor(() => attachNodePlaybackAsset.mock.calls.length > 0);
    expect(attachNodePlaybackAsset).toHaveBeenCalledWith('loom-1', 'ep-1', 'node-1', {
      role: 'exit',
      videoHistoryId: 'video-exit',
      transitionId: 'tr-1',
      visualConditioning: { version: 1, status: 'locked' },
    });
  });

  it('ignores image jobs and videos without a complete destination tag', async () => {
    mediaJobEvents.emit('completed', { kind: 'image', id: 'image-1', params: { fableLoom: tag() }, result: { filename: 'image-1.png' } });
    mediaJobEvents.emit('completed', completedVideoJob({ params: { fableLoom: { loomId: 'loom-1' } } }));
    await new Promise((resolve) => setTimeout(resolve, 25));
    expect(attachNodeVideo).not.toHaveBeenCalled();
  });
});