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

19.3 KB
Raw Blame
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { pinPlatform, posixPath } from '../../lib/testHelper.js';

import { mkdir, writeFile, rm, readFile } from 'fs/promises';
import { existsSync } from 'fs';
import { basename, join } from 'path';
import { tmpdir } from 'os';
import { EventEmitter } from 'events';
import { ChildProcess } from '../../lib/childProcess.js';

// Spawn mock — capture calls, drive stdout/stderr, capture the stdin-delivered
// prompt (grok reads it via --prompt-file /dev/stdin).
const spawnCalls = [];
const makeFakeChild = () => {
  const child = new EventEmitter();
  // killProcessTree tells a spawned child from a node-pty session by
  // `instanceof ChildProcess` (a pty takes a different kill shape), so the fake
  // has to carry the prototype the way a real spawn() result does.
  Object.setPrototypeOf(child, ChildProcess.prototype);
  child.stdout = new EventEmitter();
  child.stderr = new EventEmitter();
  child.stdin = { written: '', on: vi.fn(), write: vi.fn(function (s) { child.stdin.written += s; }), end: vi.fn() };
  child.kill = vi.fn();
  child.exitCode = null;
  child.signalCode = null;
  return child;
};
vi.mock('../../lib/childProcess.js', async (importOriginal) => {
  const actual = await importOriginal();
  const { readFileSync } = await import('fs');
  return {
    ...actual,
    spawn: vi.fn((bin, args, options) => {
      const child = makeFakeChild();
      // On Windows grok cannot read /dev/stdin, so prepareGrokPromptFile writes
      // the prompt to a temp file and rewrites --prompt-file to point at it —
      // then unlinks that file once the run closes. Capture its contents HERE,
      // between the write and the cleanup, so promptOf() can report the prompt
      // on both platforms instead of reading an always-empty stdin buffer.
      // (Same capture as imageGen/grok.test.js.)
      let promptFromFile = null;
      const pfIdx = args?.indexOf?.('--prompt-file');
      const pfPath = pfIdx >= 0 ? args[pfIdx + 1] : null;
      if (pfPath && pfPath !== '/dev/stdin') {
        try { promptFromFile = readFileSync(pfPath, 'utf8'); } catch { promptFromFile = null; }
      }
      const jobId = options?.cwd ? basename(options.cwd).replace(/^portos-grok-video-/, '') : null;
      spawnCalls.push({ bin, args, child, promptFromFile, jobId });
      return child;
    }),
  };
});

// Test-root PATHS so the harvest move + history file land somewhere we can
// read back (mirrors imageGen/grok.test.js).
const TEST_ROOT = join(tmpdir(), `portos-grok-video-test-${process.pid}-${Date.now()}`);
const FAKE_VIDEOS_DIR = join(TEST_ROOT, 'data-videos');
const FAKE_DATA_DIR = join(TEST_ROOT, 'data');
const fixture = vi.hoisted(() => ({ admission: null }));
vi.mock('../../lib/maintenanceAdmission.js', async importOriginal => ({
  ...await importOriginal(),
  maintenance: new Proxy({}, { get: (_target, property) => fixture.admission[property] }),
}));
vi.mock('../../lib/fileUtils.js', async () => {
  const actual = await vi.importActual('../../lib/fileUtils.js');
  actual.PATHS.videos = FAKE_VIDEOS_DIR;
  actual.PATHS.data = FAKE_DATA_DIR;
  actual.PATHS.videoThumbnails = join(TEST_ROOT, 'thumbnails');
  return {
    ...actual,
    ensureDir: vi.fn(async (dir) => mkdir(dir, { recursive: true })),
    rmGuarded: vi.fn(actual.rmGuarded),
    unlinkGuarded: vi.fn(actual.unlinkGuarded),
  };
});

// finalizeGeneratedVideo shells out to ffmpeg (faststart + thumbnail) — stub
// the ffmpeg layer so the shared finalizer's history/SSE/event flow still
// runs for real against fake MP4 bytes.
vi.mock('../../lib/ffmpeg.js', async () => {
  const actual = await vi.importActual('../../lib/ffmpeg.js');
  return {
    ...actual,
    optimizeForStreaming: vi.fn(async () => {}),
    generateThumbnail: vi.fn(async (_p, jobId) => `${jobId}.jpg`),
    probeVideoDuration: vi.fn(async () => null),
  };
});

const { recordGrokVideoUsage } = vi.hoisted(() => ({ recordGrokVideoUsage: vi.fn(async () => {}) }));
vi.mock('../grokMediaUsage.js', () => ({ recordGrokVideoUsage }));

const grok = await import('./grok.js');
const { videoGenEvents } = await import('./events.js');
const { loadHistory } = await import('./history.js');
const { createMaintenanceAdmission } = await import('../../lib/maintenanceAdmission.js');
const { rmGuarded, unlinkGuarded } = await import('../../lib/fileUtils.js');
const realFiles = await vi.importActual('../../lib/fileUtils.js');
const { probeVideoDuration } = await import('../../lib/ffmpeg.js');

const flush = () => new Promise((r) => setImmediate(r));
const scratchDirFor = (jobId) => join(tmpdir(), `portos-grok-video-${jobId}`);
const stagingPathFor = (jobId) => join(scratchDirFor(jobId), 'output.mp4');
// Prefer the --prompt-file temp file (Windows) and fall back to stdin (POSIX).
const promptOf = (i = 0) => spawnCalls[i].promptFromFile ?? spawnCalls[i].child.stdin.written;
const closeChild = async (i = 0, code = 1) => {
  const terminal = new Promise(resolve => {
    const finish = payload => {
      if (payload.generationId !== spawnCalls[i].jobId) return;
      videoGenEvents.off('failed', finish);
      videoGenEvents.off('completed', finish);
      resolve();
    };
    videoGenEvents.on('failed', finish);
    videoGenEvents.on('completed', finish);
  });
  spawnCalls[i].child.exitCode = code;
  spawnCalls[i].child.emit('close', code, null);
  await terminal;
};
// Minimal valid MP4 header: size box + `ftyp` at offset 4.
const MP4_BYTES = Buffer.concat([
  Buffer.from([0x00, 0x00, 0x00, 0x18, 0x66, 0x74, 0x79, 0x70]),
  Buffer.from('isommp42-fake-payload'),
]);

beforeEach(async () => {
  spawnCalls.length = 0;
  videoGenEvents.removeAllListeners();
  grok._internals.setHarvestTimeoutForTests(10);
  await rm(TEST_ROOT, { recursive: true, force: true }).catch(() => {});
  await mkdir(FAKE_DATA_DIR, { recursive: true });
  fixture.admission = createMaintenanceAdmission(FAKE_DATA_DIR);
  rmGuarded.mockReset().mockImplementation(realFiles.rmGuarded);
  unlinkGuarded.mockReset().mockImplementation(realFiles.unlinkGuarded);
});

afterEach(async () => {
  vi.useRealTimers();
  grok._internals.setHarvestTimeoutForTests();
  await rm(TEST_ROOT, { recursive: true, force: true }).catch(() => {});
});

describe('videoGen/grok — generateVideo', () => {
  it('spawns grok headlessly and prompts the image-first flow for text mode', async () => {
    const job = await grok.generateVideo({ prompt: 'a fox running' });
    expect(job.mode).toBe('grok');
    expect(job.filename).toMatch(/^[0-9a-f-]{36}\.mp4$/);
    expect(job.status).toBe('running');
    const { bin, args } = spawnCalls[0];
    expect(bin).toBe('grok');
    expect(args).toEqual(expect.arrayContaining(['--permission-mode', 'bypassPermissions']));
    const prompt = promptOf();
    // Image-first: base image via image_gen, then image_to_video.
    expect(prompt).toContain('image_gen');
    expect(prompt).toContain('image_to_video');
    expect(prompt).toContain('a fox running');
    expect(prompt).toContain(stagingPathFor(job.jobId));
    // Default duration is the shortest supported clip.
    expect(prompt).toContain('6 seconds');
    await closeChild();
  });

  it('skips the base-image step when a source image is supplied', async () => {
    await grok.generateVideo({ prompt: 'gentle pan', sourceImagePath: '/abs/frame.png', duration: 10 });
    const prompt = promptOf();
    expect(prompt).not.toContain('image_gen');
    expect(prompt).toContain('image_to_video');
    expect(posixPath(prompt)).toContain('/abs/frame.png');
    expect(prompt).toContain('10 seconds');
    await closeChild();
  });

  it('falls back to 6s for an unsupported duration', async () => {
    await grok.generateVideo({ prompt: 'a fox', duration: 42 });
    expect(promptOf()).toContain('6 seconds');
    await closeChild();
  });

  it('derives the base-image aspect ratio from width/height', async () => {
    await grok.generateVideo({ prompt: 'a fox', width: 1920, height: 1080 });
    expect(promptOf()).toContain('aspect_ratio "16:9"');
    await closeChild();
  });

  it('rejects an empty prompt', async () => {
    await expect(grok.generateVideo({ prompt: '  ' })).rejects.toThrow(/Prompt is required/);
  });

  it('emits started on videoGenEvents with the job meta', async () => {
    const started = vi.fn();
    videoGenEvents.on('started', started);
    const job = await grok.generateVideo({ prompt: 'a fox' });
    expect(started).toHaveBeenCalledWith(expect.objectContaining({ generationId: job.jobId, modelId: 'grok' }));
    await closeChild();
  });

  it('emits activity on stdout so the queue idle watchdog resets', async () => {
    const activity = vi.fn();
    videoGenEvents.on('activity', activity);
    const job = await grok.generateVideo({ prompt: 'a fox' });
    spawnCalls[0].child.stdout.emit('data', Buffer.from('rendering…\n'));
    expect(activity).toHaveBeenCalledWith({ generationId: job.jobId });
    await closeChild();
  });
});

describe('videoGen/grok — harvest and finalize', () => {
  it('moves a staged MP4 into PATHS.videos, writes history, and emits completed', async () => {
    const completed = vi.fn();
    videoGenEvents.on('completed', completed);

    const job = await grok.generateVideo({ prompt: 'a fox running' });
    await mkdir(scratchDirFor(job.jobId), { recursive: true });
    await writeFile(stagingPathFor(job.jobId), MP4_BYTES);
    await closeChild(0, 0);

    const deadline = Date.now() + 5000;
    while (Date.now() < deadline && completed.mock.calls.length === 0) {
      await new Promise((r) => setTimeout(r, 50));
    }
    expect(completed).toHaveBeenCalledTimes(1);
    expect(completed.mock.calls[0][0]).toEqual(expect.objectContaining({
      generationId: job.jobId,
      path: `/data/videos/${job.filename}`,
      thumbnail: `${job.jobId}.jpg`,
    }));
    const written = await readFile(join(FAKE_VIDEOS_DIR, job.filename));
    expect(Buffer.compare(written, MP4_BYTES)).toBe(0);
    // Scratch dir cleaned up; history entry recorded with the grok engine tag.
    expect(existsSync(scratchDirFor(job.jobId))).toBe(false);
    const history = await loadHistory();
    expect(history[0]).toEqual(expect.objectContaining({ id: job.jobId, modelId: 'grok', mode: 'text', duration: 6 }));
    expect(recordGrokVideoUsage).toHaveBeenCalledWith(expect.objectContaining({
      jobId: job.jobId, videoSeconds: 6, images: 1, inputImages: 1,
    }));
  });

  it('records the delivered length and warns when a 15 s request comes back short', async () => {
    probeVideoDuration.mockResolvedValueOnce(10.04);
    const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
    const completed = vi.fn();
    videoGenEvents.on('completed', completed);

    const job = await grok.generateVideo({ prompt: 'a fox running', duration: 15 });
    expect(promptOf()).toContain('for 15 seconds');
    await mkdir(scratchDirFor(job.jobId), { recursive: true });
    await writeFile(stagingPathFor(job.jobId), MP4_BYTES);
    await closeChild(0, 0);

    const deadline = Date.now() + 5000;
    while (Date.now() < deadline && completed.mock.calls.length === 0) {
      await new Promise((r) => setTimeout(r, 50));
    }
    expect(completed).toHaveBeenCalledTimes(1);
    expect(warn).toHaveBeenCalledWith(expect.stringContaining('grok delivered 10.04s for a 15s request'));
    const history = await loadHistory();
    expect(history.find((h) => h.id === job.jobId)).toEqual(expect.objectContaining({ duration: 15, deliveredSec: 10.04 }));
    warn.mockRestore();
  });

  it('rejects a staged file that is not MP4 (signature sniff)', async () => {
    const failed = vi.fn();
    videoGenEvents.on('failed', failed);

    const job = await grok.generateVideo({ prompt: 'a fox' });
    await mkdir(scratchDirFor(job.jobId), { recursive: true });
    await writeFile(stagingPathFor(job.jobId), 'Error: render failed');
    spawnCalls[0].child.exitCode = 0;
    spawnCalls[0].child.emit('close', 0, null);
    await vi.waitFor(() => expect(failed).toHaveBeenCalledTimes(1));
    expect(failed.mock.calls[0][0].error).toMatch(/non-MP4 file/);
    expect(existsSync(join(FAKE_VIDEOS_DIR, job.filename))).toBe(false);
    expect(existsSync(scratchDirFor(job.jobId))).toBe(false);
  });

  it('fails with the narration tail when grok exits 0 with no file', async () => {
    const failed = vi.fn();
    videoGenEvents.on('failed', failed);

    await grok.generateVideo({ prompt: 'a fox' });
    spawnCalls[0].child.stdout.emit('data', Buffer.from('I cannot animate that.\n'));
    spawnCalls[0].child.exitCode = 0;
    spawnCalls[0].child.emit('close', 0, null);
    await vi.waitFor(() => expect(failed).toHaveBeenCalledTimes(1));
    expect(failed.mock.calls[0][0].error).toMatch(/I cannot animate that/);
    // Video-flavored wording, not the image provider's.
    expect(failed.mock.calls[0][0].error).toMatch(/video/i);
  });

  it('fails with the stderr tail on non-zero exit', async () => {
    const failed = vi.fn();
    videoGenEvents.on('failed', failed);
    await grok.generateVideo({ prompt: 'a fox' });
    spawnCalls[0].child.stderr.emit('data', Buffer.from('boom: session expired\n'));
    await closeChild(0, 1);
    expect(failed).toHaveBeenCalledTimes(1);
    expect(failed.mock.calls[0][0].error).toMatch(/Exit code 1/);
    expect(failed.mock.calls[0][0].error).toMatch(/session expired/);
  });
});

describe('videoGen/grok — cancel', () => {
  it('retains cancel escalation and maintenance ownership after a live error until close and all cleanup finish', async () => {
    vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout'] });
    const scratchCleanup = Promise.withResolvers();
    const uploadCleanup = Promise.withResolvers();
    const jobId = 'grok-maintenance-live-error';
    const upload = join(TEST_ROOT, 'source.png');
    await writeFile(upload, 'synthetic reference');
    rmGuarded.mockImplementationOnce(async (...args) => {
      await scratchCleanup.promise;
      return realFiles.rmGuarded(...args);
    });
    unlinkGuarded.mockImplementationOnce(async (...args) => {
      await uploadCleanup.promise;
      return realFiles.unlinkGuarded(...args);
    });
    const permit = fixture.admission.admit('media', jobId);
    let settlement;
    const failed = vi.fn(() => { settlement = permit.finish(); });
    videoGenEvents.on('failed', failed);
    let closing;
    try {
      await permit.run(() => grok.generateVideo({ jobId, prompt: 'synthetic motion', uploadedTempPath: upload }));
      const child = spawnCalls[0].child;
      child.pid = 101;
      fixture.admission.begin({ reason: 'Drain renderer', owner: 'Operator' });
      child.emit('error', new Error('Signal transport failed'));
      expect(failed).not.toHaveBeenCalled();
      expect(grok.getActiveJob()).toMatchObject({ generationId: jobId });
      // Exercise POSIX escalation without changing the journal's native
      // filesystem behavior (Windows cannot fsync an opened directory).
      const restorePlatform = pinPlatform('darwin');
      try { expect(grok.cancel(jobId)).toBe(true); }
      finally { restorePlatform(); }
      await vi.advanceTimersByTimeAsync(5000);
      expect(child.kill).toHaveBeenCalledWith('SIGKILL');
      expect(fixture.admission.status().state).toBe('draining');
      expect(rmGuarded).not.toHaveBeenCalled();

      closing = closeChild();
      await vi.waitFor(() => expect(unlinkGuarded).toHaveBeenCalledWith(upload));
      scratchCleanup.resolve();
      await vi.waitFor(() => expect(existsSync(scratchDirFor(jobId))).toBe(false));
      expect(failed).not.toHaveBeenCalled();
      expect(fixture.admission.status().state).toBe('draining');
      uploadCleanup.resolve();
      await closing;
      await settlement;
      expect(existsSync(upload)).toBe(false);
      expect(failed).toHaveBeenCalledTimes(1);
      expect(failed.mock.calls[0][0].error).toContain('Signal transport failed');
      expect(fixture.admission.status().state).toBe('ready');
      expect(grok.cancel(jobId)).toBe(false);
      child.emit('close', 1, null);
      expect(failed).toHaveBeenCalledTimes(1);
    } finally {
      scratchCleanup.resolve();
      uploadCleanup.resolve();
      if (!closing && spawnCalls[0] && failed.mock.calls.length === 0) closing = closeChild();
      await closing;
      await permit.finish();
      vi.useRealTimers();
    }
  });

  it('waits for cleanup once when a failed spawn emits both error and close', async () => {
    const cleanup = Promise.withResolvers();
    rmGuarded.mockImplementationOnce(async (...args) => {
      await cleanup.promise;
      return realFiles.rmGuarded(...args);
    });
    const failed = vi.fn();
    videoGenEvents.on('failed', failed);
    const job = await grok.generateVideo({ prompt: 'synthetic motion' });
    const child = spawnCalls[0].child;
    try {
      child.emit('error', new Error('spawn ENOENT'));
      child.emit('close', -2, null);
      await flush();
      expect(failed).not.toHaveBeenCalled();
      cleanup.resolve();
      await vi.waitFor(() => expect(failed).toHaveBeenCalledTimes(1));
      expect(existsSync(scratchDirFor(job.jobId))).toBe(false);
      expect(rmGuarded).toHaveBeenCalledTimes(1);
    } finally {
      cleanup.resolve();
      await vi.waitFor(() => expect(failed).toHaveBeenCalledTimes(1));
    }
  });

  it.each(['scratch', 'upload'])('retains a recovery blocker when %s cleanup fails', async target => {
    const jobId = `grok-maintenance-${target}-cleanup`;
    const upload = join(TEST_ROOT, 'source.png');
    await writeFile(upload, 'synthetic reference');
    (target === 'scratch' ? rmGuarded : unlinkGuarded).mockRejectedValueOnce(Object.assign(new Error('Cleanup denied'), { code: 'EACCES' }));
    const permit = fixture.admission.admit('media', jobId);
    await permit.run(() => grok.generateVideo({ jobId, prompt: 'synthetic motion', uploadedTempPath: upload }));
    fixture.admission.begin({ reason: 'Drain renderer', owner: 'Operator' });
    await closeChild();
    await permit.finish();
    expect(fixture.admission.status()).toMatchObject({
      state: 'draining', blockers: [expect.objectContaining({ resource: jobId, unsettled: true })],
    });
    await realFiles.rmGuarded(scratchDirFor(jobId), { recursive: true, force: true });
  });

  it('cancel() requires a jobId; cancel(jobId) SIGTERMs; cancelAll() sweeps', async () => {
    expect(() => grok.cancel()).toThrow(/requires a jobId/);
    const a = await grok.generateVideo({ prompt: 'one' });
    const b = await grok.generateVideo({ prompt: 'two' });
    expect(grok.cancel(a.jobId)).toBe(true);
    expect(spawnCalls[0].child.kill).toHaveBeenCalledWith('SIGTERM');
    expect(spawnCalls[1].child.kill).not.toHaveBeenCalled();
    expect(grok.cancelAll()).toBe(true);
    expect(spawnCalls[1].child.kill).toHaveBeenCalledWith('SIGTERM');
    await closeChild(0);
    await closeChild(1);
    expect(grok.cancel(b.jobId)).toBe(false);
    expect(grok.cancelAll()).toBe(false);
  });
});

it('cancels during preparation before starting a render', async () => {
  const { videoGenEvents: events } = await import('./events.js');
  const failed = vi.fn();
  const completed = vi.fn();
  events.on('failed', failed);
  events.on('completed', completed);
  const jobId = 'preamble-cancel-videoGen-grok';
  const pending = grok.generateVideo({ prompt: 'a quiet gate', jobId });
  expect(grok.cancel(jobId)).toBe('pending');
  await pending;
  await vi.waitFor(() => expect(failed).toHaveBeenCalledWith(expect.objectContaining({ generationId: jobId, error: 'Canceled' })));
  expect(completed).not.toHaveBeenCalled();
  expect(spawnCalls).toHaveLength(0);
  expect(grok.cancel(jobId)).toBe(false);
});