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

13.8 KB
Raw Blame
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { readFile } from 'fs/promises';
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';
import { applyTemplate } from '../lib/promptTemplate.js';

// Resolve the templates from disk so the tests exercise the real Prompts
// Manager templates — if a future edit breaks an unwrapped {{var}} or a
// mistyped section name, the test catches it.
//
// data/ is gitignored (populated on first boot via `npm run setup:data`),
// so on a fresh CI checkout only data.reference/ exists. Prefer the runtime
// copy (catches drift from local edits) and fall back to the committed
// seed so CI works without running setup first.
const __HERE = dirname(fileURLToPath(import.meta.url));
const RUNTIME_STAGES_DIR = join(__HERE, '..', '..', 'data', 'prompts', 'stages');
const SAMPLE_STAGES_DIR = join(__HERE, '..', '..', 'data.reference', 'prompts', 'stages');
const loadStage = async (stageName) => {
  const runtimePath = join(RUNTIME_STAGES_DIR, `${stageName}.md`);
  return readFile(runtimePath, 'utf-8').catch((err) => {
    if (err.code !== 'ENOENT') throw err;
    return readFile(join(SAMPLE_STAGES_DIR, `${stageName}.md`), 'utf-8');
  });
};

// Mock the prompt-service buildPrompt to render the on-disk template through
// the same engine production uses, without needing a live aiToolkit instance.
vi.mock('./promptService.js', () => ({
  buildPrompt: vi.fn(async (stageName, view) => {
    const template = await loadStage(stageName);
    return applyTemplate(template, view);
  }),
}));

const { buildTreatmentPrompt, buildEvaluatePrompt, buildPlanPrompt } = await import('./creativeDirectorPrompts.js');

const baseProject = {
  id: 'cd-1',
  name: 'Test Project',
  aspectRatio: '16:9',
  quality: 'standard',
  modelId: 'ltx-video',
  targetDurationSeconds: 30,
  styleSpec: 'neon noir',
  collectionId: 'col-1',
  startingImageFile: null,
  userStory: null,
  treatment: { scenes: [{}, {}, {}] },
};

const baseScene = {
  sceneId: 'scene-2',
  order: 1,
  intent: 'archway opens to reveal city',
  prompt: 'long shot, archway opens',
  durationSeconds: 5,
  useContinuationFromPrior: false,
  retryCount: 0,
  renderedJobId: 'job-abc-123',
};

beforeEach(() => {
  // Templates are loaded fresh from disk — no state to reset.
});

describe('buildPlanPrompt — locked render settings', () => {
  const planProject = { ...baseProject, aspectRatio: '9:16', quality: 'high', directive: { goal: 'make a surreal clip', deliverables: [], constraints: {} } };
  const toolSpecs = [{ type: 'function', function: { name: 'media_enqueueVideoJob', description: 'render', parameters: { type: 'object' } } }];

  it('surfaces the project preset (9:16 → 432×768) so the planner stops guessing dimensions', async () => {
    const out = await buildPlanPrompt(planProject, { toolSpecs });
    expect(out).toContain('## Locked render settings');
    expect(out).toContain('**9:16** (432×768)');
    expect(out).toContain('**high** quality');
    // Instructs the planner not to author the enforced params.
    expect(out).toContain('Do not set or override backend, model, aspect ratio, width, height, FPS, frame count, steps');
  });
});

// The agent's PATCH target must stay a token: buildAgentPrompt expands it at
// spawn time to the loopback origin that actually speaks HTTP on this install
// (the mirror when :5555 is TLS-only), which a load-time URL cannot know (#12405).
describe('Creative Director stage prompts — API origin', () => {
  it.each([
    ['treatment', () => buildTreatmentPrompt(baseProject), '/api/creative-director/cd-1/treatment'],
    ['plan', () => buildPlanPrompt(baseProject, { toolSpecs: [] }), '/api/creative-director/cd-1/plan'],
    ['evaluate', () => buildEvaluatePrompt(baseProject, baseScene), '/api/creative-director/cd-1/scene/scene-2'],
  ])('%s sends its write-back to the {portosApiUrl} token, not a hardcoded origin', async (_stage, render, path) => {
    const out = await render();
    expect(out).toContain(`PATCH {portosApiUrl}${path}`);
    expect(out).not.toMatch(/https?:\/\/localhost:5555/);
  });
});

describe('buildTreatmentPrompt — template-rendered output', () => {
  it('passes resolved Video source descriptions and the matching revision in the treatment output contract', async () => {
    const revision = 'a'.repeat(32);
    const out = await buildTreatmentPrompt({ ...baseProject, workspace: 'video', videoPlanningContext: { revision }, resolvedVideoSources: [{ kind: 'universe', id: 'example-universe', summary: { canon: 'The traveler wears a silver cloak.' } }] });
    expect(out).toContain('The traveler wears a silver cloak.');
    expect(out).toContain(`"sourceContextRevision": "${revision}"`);
    expect(out).toContain('changed or deleted sources require planning again');
  });
  it('renders project header and resolves aspect/quality dimensions', async () => {
    const out = await buildTreatmentPrompt(baseProject);
    expect(out).toContain('# Creative Director — Treatment task');
    expect(out).toContain('"Test Project" (id: cd-1)');
    // Aspect dims come from ASPECT_PRESETS['16:9'] = 768×432.
    expect(out).toContain('Aspect ratio: 16:9 (768×432)');
    // Quality dims from QUALITY_PRESETS.standard = { steps: 20, guidance: 3, fps: 24 }.
    expect(out).toContain('Quality: standard (20 denoising steps, guidance 3, 24fps)');
    expect(out).toContain('Target episode duration: 30s (~1 min)');
  });

  it('uses the "Story" branch when no userStory provided', async () => {
    const out = await buildTreatmentPrompt(baseProject);
    expect(out).toContain('## Story');
    expect(out).toContain('The user did not supply a story');
    expect(out).not.toContain('## User-supplied story');
  });

  it('uses the "User-supplied story" branch when userStory is set', async () => {
    const out = await buildTreatmentPrompt({ ...baseProject, userStory: 'A heist on Mars.' });
    expect(out).toContain('## User-supplied story');
    expect(out).toContain('A heist on Mars.');
    expect(out).not.toContain('The user did not supply a story');
  });

  it('renders sourceImageFile literal as JSON null when no starting image', async () => {
    const out = await buildTreatmentPrompt(baseProject);
    expect(out).toContain('"sourceImageFile": null');
  });

  it('renders sourceImageFile literal as a quoted filename when starting image is set', async () => {
    const out = await buildTreatmentPrompt({ ...baseProject, startingImageFile: 'hero.png' });
    expect(out).toContain('"sourceImageFile": "hero.png"');
    expect(out).toContain('Starting image: /data/images/hero.png');
  });

  it('hides the Cast section + per-scene cast field when no cast is seeded (#1808)', async () => {
    const out = await buildTreatmentPrompt(baseProject);
    expect(out).not.toContain('## Cast & ingredients');
    expect(out).not.toContain('"cast"');
  });

  it('renders the Cast section + per-scene cast field when ingredients are seeded (#1808)', async () => {
    const out = await buildTreatmentPrompt({
      ...baseProject,
      cast: [
        { ingredientId: 'cat-c', name: 'Mara', type: 'character', role: 'cast', summary: 'A tall figure in a grey coat.' },
        { ingredientId: 'cat-p', name: 'The Spire', type: 'place', role: 'location' },
      ],
    });
    expect(out).toContain('## Cast & ingredients');
    // Member lines render name, type · role, id, and the optional summary.
    expect(out).toContain('**Mara** (character · cast, id `cat-c`): A tall figure in a grey coat.');
    // No summary → the `{{#summary}}` block stays empty (no trailing colon).
    expect(out).toContain('**The Spire** (place · location, id `cat-p`)');
    expect(out).not.toContain('**The Spire** (place · location, id `cat-p`):');
    // The header + intro render exactly once, not per member.
    expect(out.match(/## Cast & ingredients/g)).toHaveLength(1);
    // The per-scene cast field appears in the JSON output contract.
    expect(out).toContain('"cast": [{ "ingredientId":');
  });
});

describe('buildEvaluatePrompt — multi-frame sampling', () => {
  it('lists every sampled frame with timeline-position tags when frames are present', async () => {
    const scene = {
      ...baseScene,
      evaluationFrames: [
        'job-abc-123-f1.jpg',
        'job-abc-123-f2.jpg',
        'job-abc-123-f3.jpg',
        'job-abc-123-f4.jpg',
        'job-abc-123-f5.jpg',
      ],
    };
    const out = await buildEvaluatePrompt(baseProject, scene);
    for (const f of scene.evaluationFrames) {
      expect(out).toContain(`/data/video-thumbnails/${f}`);
    }
    expect(out).toContain('start (0%)');
    expect(out).toContain('end (~100%)');
    expect(out).toContain('~50% through');
    expect(out).toContain('Read EACH ONE');
    expect(out).toContain('Read every sampled frame');
    expect(out).toContain('Intent that arrives late still counts as delivered');
  });

  it('falls back to the single thumbnail line when no frames were extracted', async () => {
    const scene = { ...baseScene, evaluationFrames: [] };
    const out = await buildEvaluatePrompt(baseProject, scene);
    expect(out).toContain(`/data/video-thumbnails/${scene.renderedJobId}.jpg`);
    expect(out).toContain('Read the thumbnail using your vision capability');
    expect(out).not.toContain('Read EACH ONE');
  });

  it('falls back when evaluationFrames is missing entirely (legacy projects)', async () => {
    const scene = { ...baseScene };
    delete scene.evaluationFrames;
    const out = await buildEvaluatePrompt(baseProject, scene);
    expect(out).toContain(`/data/video-thumbnails/${scene.renderedJobId}.jpg`);
    expect(out).not.toContain('Sampled frames across the timeline');
  });
});

describe('buildEvaluatePrompt — scene metadata', () => {
  it('includes scene position label, retry budget, and quote-escaped prompt', async () => {
    const scene = {
      ...baseScene,
      evaluationFrames: [],
      retryCount: 1,
      prompt: 'a "smart" prompt with quotes',
    };
    const out = await buildEvaluatePrompt(baseProject, scene);
    expect(out).toContain('Scene id: `scene-2` (2/3)');
    expect(out).toContain('Retry count: 1 (max 3)');
    // promptJson view value is JSON.stringify so embedded quotes are escaped.
    expect(out).toContain('"a \\"smart\\" prompt with quotes"');
    expect(out).toContain('"retryCount": 2');
  });

  it('reports text-to-video strategy when no continuation and no source image', async () => {
    const out = await buildEvaluatePrompt(baseProject, { ...baseScene, evaluationFrames: [] });
    expect(out).toContain('Strategy: text-to-video');
  });

  it('reports continuation strategy when useContinuationFromPrior is true', async () => {
    const out = await buildEvaluatePrompt(baseProject, {
      ...baseScene,
      evaluationFrames: [],
      useContinuationFromPrior: true,
    });
    expect(out).toContain('Strategy: continued from prior scene last-frame');
  });

  it('reports seeded-image strategy when sourceImageFile is set', async () => {
    const out = await buildEvaluatePrompt(baseProject, {
      ...baseScene,
      evaluationFrames: [],
      sourceImageFile: 'hero.png',
    });
    expect(out).toContain('Strategy: seeded from image `hero.png`');
  });
});

describe('buildEvaluatePrompt — imageStrength surfacing', () => {
  it('shows the explicit imageStrength when the scene has one set', async () => {
    const out = await buildEvaluatePrompt(baseProject, {
      ...baseScene,
      evaluationFrames: [],
      imageStrength: 0.6,
    });
    expect(out).toContain('Image strength: 0.6');
    expect(out).not.toContain('Image strength: default');
  });

  it('falls back to "default" wording when imageStrength is unset', async () => {
    const out = await buildEvaluatePrompt(baseProject, {
      ...baseScene,
      evaluationFrames: [],
    });
    expect(out).toContain('Image strength: default');
  });

  it('treats null imageStrength like unset (use defaults)', async () => {
    const out = await buildEvaluatePrompt(baseProject, {
      ...baseScene,
      evaluationFrames: [],
      imageStrength: null,
    });
    expect(out).toContain('Image strength: default');
  });
});

describe('standalone Video treatment planning', () => {
  it('requires an exact timed script and preserves revision context without authorizing renders', async () => {
    const project = {
      ...baseProject, workspace: 'video', targetDurationSeconds: 120,
      renderBackend: { video: { mode: 'reactor' } },
      videoDraft: { durationRange: { min: 60, max: 180 }, sources: [{ kind: 'universe', id: 'example-universe', revision: 'rev-2' }] },
      treatment: { script: 'A visitor returns.', scenes: [{ sceneId: 'scene-retained', order: 0 }], artifact: { scriptId: 'script-example', revision: 2 } },
    };
    const out = await buildTreatmentPrompt(project);
    expect(out).toContain('exactly 120 seconds');
    expect(out).toContain('"script": "<complete production script>"');
    expect(out).toContain('"minSeconds":5.167');
    expect(out).toContain('"maxPromptCharacters":800');
    expect(out).toContain('"sceneId":"scene-retained"');
    expect(out).toContain('"scriptId":"script-example"');
    expect(out).toContain('"revision":"rev-2"');
    expect(out).toContain('Do not start production');
    expect(out).not.toContain('produce fewer scenes');
    expect(out).not.toContain('automatically begin rendering');
    expect(out).not.toMatch(/{{[#/^]?[a-zA-Z]/);
  });

  it('distinguishes pinned Grok durations from unresolved inherited capabilities', async () => {
    const project = { ...baseProject, workspace: 'video' };
    const grok = await buildTreatmentPrompt({ ...project, renderBackend: { video: { mode: 'grok' } } });
    expect(grok).toContain('"durationSeconds":[6,10,15]');
    const inherited = await buildTreatmentPrompt(project);
    expect(inherited).toContain('"backendCompatibility":"unresolved"');
    expect(inherited).toContain('source record IDs are not image filenames');
  });
});