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.2 KB
Raw Blame
import { describe, it, expect } from 'vitest';
import {
  ABILITY_ADAPTERS, getAbilityAdapter, buildCommissionDirective, buildRenderBackendPin,
} from './abilityAdapters.js';
import { CREATIVE_COMMISSION_ABILITIES, ABILITY_GENERATION_SPEC } from '../../lib/creativeCommissionValidation.js';
import {
  COMMISSION_INTENT_MAX, COMMISSION_STYLE_SPEC_MAX, COMMISSION_BRIEF_TAG_MAX,
} from '../../lib/creativeBriefLimits.js';
import { MAX_DIRECTIVE_GOAL_LEN, MAX_STYLE_SOURCE_LEN } from './directive.js';
import { buildVideoPromptGuidance, isMiniMaxVideoModel } from './videoPromptGuidance.js';

const MAXED_INTENT = 'x'.repeat(COMMISSION_INTENT_MAX);

describe('ability adapter registry', () => {
  it('has an adapter for every supported ability (and no extras)', () => {
    expect(Object.keys(ABILITY_ADAPTERS).sort()).toEqual([...CREATIVE_COMMISSION_ABILITIES].sort());
  });

  it('returns null for an unknown ability', () => {
    expect(getAbilityAdapter('hologram')).toBeNull();
    expect(getAbilityAdapter(undefined)).toBeNull();
  });
});

describe('buildCommissionDirective — video (unchanged brief/feedback fold)', () => {
  it('composes goal + deliverables + constraints from the brief', () => {
    const directive = buildCommissionDirective({
      name: 'Nightly Surreal',
      targetAbility: 'video',
      brief: {
        intent: 'something surreal, dreamlike, unsettlingly beautiful',
        genre: 'surrealism',
        styleSpec: 'flat color, Magritte',
        constraints: { universeId: 'u-123' },
      },
      feedbackWindow: 5,
    });
    expect(directive.goal).toContain('Create a short-form video piece.');
    expect(directive.goal).toContain('something surreal');
    expect(directive.goal).toContain('Genre: surrealism.');
    expect(directive.goal).toContain('Style: flat color, Magritte.');
    expect(directive.deliverables).toEqual(['One rendered video matching the brief']);
    expect(directive.constraints).toMatchObject({ universeId: 'u-123', targetAbility: 'video' });
    expect(directive.constraints.generation).toMatchObject({ quality: 'standard', videoMode: 'auto' });
  });

  it('folds recent feedback into the goal', () => {
    const directive = buildCommissionDirective({
      targetAbility: 'video',
      brief: { intent: 'surreal' },
      feedback: [{ rating: 'down', note: 'less horror' }, { rating: 'up', note: 'more Magritte' }],
      feedbackWindow: 5,
    });
    expect(directive.goal).toContain('Recent likes: more Magritte.');
    expect(directive.goal).toContain('Recent dislikes: less horror.');
  });

  it('always carries the selected output and sanitized generation starting point', () => {
    expect(buildCommissionDirective({ targetAbility: 'video', brief: { intent: 'x' } }).constraints)
      .toMatchObject({ targetAbility: 'video', generation: { quality: 'standard', videoMode: 'auto' } });
  });

  it('clamps the goal under the directive cap with a large feedback window + long notes', () => {
    const feedback = Array.from({ length: 50 }, (_, i) => ({ rating: i % 2 === 0 ? 'up' : 'down', note: 'z'.repeat(1000) }));
    const directive = buildCommissionDirective({ targetAbility: 'video', brief: { intent: 'surreal' }, feedback, feedbackWindow: 50 });
    expect(directive.goal.length).toBeLessThanOrEqual(MAX_DIRECTIVE_GOAL_LEN);
  });

  it('keeps the feedback digest even when the brief text is very long', () => {
    const directive = buildCommissionDirective({
      targetAbility: 'video',
      brief: { intent: 'x'.repeat(COMMISSION_INTENT_MAX * 2), styleSpec: 'y'.repeat(COMMISSION_STYLE_SPEC_MAX * 2) },
      feedback: [{ rating: 'down', note: 'less horror' }],
      feedbackWindow: 5,
    });
    expect(directive.goal.length).toBeLessThanOrEqual(MAX_DIRECTIVE_GOAL_LEN);
    expect(directive.goal).toContain('Recent dislikes: less horror.');
  });

  it('falls back to the video adapter for an unknown ability', () => {
    const directive = buildCommissionDirective({ targetAbility: 'hologram', brief: { intent: 'x' } });
    expect(directive.goal).toContain('Create a short-form video piece.');
  });

  it('adds the generic shot recipe and MiniMax guidance for the selected model', () => {
    const directive = buildCommissionDirective({
      targetAbility: 'video',
      brief: { intent: 'a courier crosses a rainy plaza' },
      generation: { videoModelId: 'minimax_h3_8bit' },
    });
    expect(directive.goal).toContain('beginning, middle, and end');
    expect(directive.goal).toContain('master timeline plus timestamped micro-beats');
    expect(directive.goal).toContain('7000 characters');
  });

  it('keeps model-specific guidance off the generic path while retaining the shot recipe', () => {
    expect(isMiniMaxVideoModel('ltx-2.5')).toBe(false);
    expect(buildVideoPromptGuidance('ltx-2.5')).toContain('production-ready prompt');
    expect(buildVideoPromptGuidance('ltx-2.5')).not.toContain('[Tracking shot]');
  });

  // A brief filled to the SCHEMA caps is the largest one a route or PATCH can
  // store, so the system prefix AND the user's own words must both survive it:
  // the clamp drops the tail, and the guidance is prepended, so an under-sized
  // MAX_DIRECTIVE_GOAL_LEN would silently eat the brief instead of erroring.
  it('carries a brief filled to the schema caps, a maxed style source, AND the MiniMax recipe', () => {
    const intent = 'x'.repeat(COMMISSION_INTENT_MAX);
    const styleSpec = 'y'.repeat(COMMISSION_STYLE_SPEC_MAX);
    const styleSource = { text: 's'.repeat(MAX_STYLE_SOURCE_LEN), moodBoardId: 'b1' };
    const directive = buildCommissionDirective({
      targetAbility: 'video',
      brief: { intent, styleSpec, genre: 'g'.repeat(COMMISSION_BRIEF_TAG_MAX), category: 'c'.repeat(COMMISSION_BRIEF_TAG_MAX) },
      feedback: Array.from({ length: 50 }, (_, i) => ({ rating: i % 2 === 0 ? 'up' : 'down', note: 'z'.repeat(1000) })),
      feedbackWindow: 50,
      generation: { videoModelId: 'minimax_h3_cuda' },
    }, { styleSource });
    expect(directive.goal).toContain('MiniMax H3 prompt template');
    expect(directive.goal).toContain(intent);
    expect(directive.goal).toContain(styleSpec);
    expect(directive.goal).toContain(styleSource.text);
    expect(directive.goal.length).toBeLessThanOrEqual(MAX_DIRECTIVE_GOAL_LEN);
  });

  // The universe / mood board base must reach EVERY output type's planner —
  // the plan stage never renders the project styleSpec — ahead of the user's
  // own style notes, with the resolved board id in the constraints.
  it.each(['video', 'image', 'music', 'music-video', 'series'])('folds the style source into the %s directive', (targetAbility) => {
    const directive = buildCommissionDirective(
      { targetAbility, brief: { intent: 'a quiet harbor', styleSpec: 'flat color', constraints: { universeId: 'u1' } } },
      { styleSource: { text: 'Art direction base: ink wash', moodBoardId: 'b1' } },
    );
    expect(directive.goal.indexOf('Art direction base: ink wash')).toBeLessThan(directive.goal.indexOf('Style: flat color'));
    expect(directive.constraints).toMatchObject({ universeId: 'u1', moodBoardId: 'b1' });
  });

  it('drops a deleted universe from the planner scope', () => {
    const directive = buildCommissionDirective(
      { targetAbility: 'series', brief: { intent: 'a harbor saga', constraints: { universeId: 'gone' } } },
      { styleSource: { text: '', universeMissing: true } },
    );
    expect(directive.constraints).not.toHaveProperty('universeId');
    expect(directive.goal).toContain('Invent a fitting universe context');
  });

  it('uses the install default model when choosing model-specific guidance', () => {
    const directive = buildCommissionDirective(
      { targetAbility: 'video', brief: { intent: 'a quiet harbor' } },
      { defaultVideoModelId: () => 'minimax_h3_8bit' },
    );
    expect(directive.goal).toContain('MiniMax H3 prompt template');
  });

  it('does not apply local-model guidance to a Grok-pinned commission', () => {
    const directive = buildCommissionDirective(
      { targetAbility: 'video', brief: { intent: 'a quiet harbor' }, generation: { videoMode: 'grok' } },
      { defaultVideoModelId: () => 'minimax_h3_8bit' },
    );
    expect(directive.goal).not.toContain('MiniMax H3 prompt template');
    expect(directive.goal).toContain('production-ready prompt');
  });

  it('honors the resolved settings-level cloud backend for auto mode', () => {
    const directive = buildCommissionDirective(
      { targetAbility: 'video', brief: { intent: 'a quiet harbor' } },
      { defaultVideoModelId: () => 'minimax_h3_8bit', effectiveVideoMode: 'grok' },
    );
    expect(directive.goal).not.toContain('MiniMax H3 prompt template');
  });

  it('uses the resolved creative-agent target model before the install default', () => {
    const directive = buildCommissionDirective(
      { targetAbility: 'video', brief: { intent: 'a quiet harbor' } },
      { defaultVideoModelId: () => 'ltx-2.5', effectiveVideoModelId: 'minimax_h3_8bit' },
    );
    expect(directive.goal).toContain('MiniMax H3 prompt template');
  });

  it('uses the resolved target model for auto mode even when the commission has a stale model', () => {
    const directive = buildCommissionDirective(
      {
        targetAbility: 'video',
        brief: { intent: 'a quiet harbor' },
        generation: { videoMode: 'auto', videoModelId: 'minimax_h3_8bit' },
      },
      { effectiveVideoMode: 'local', effectiveVideoModelId: 'ltx-2.5' },
    );
    expect(directive.goal).not.toContain('MiniMax H3 prompt template');
  });

  it('keeps an explicitly pinned local commission model ahead of the target default', () => {
    const directive = buildCommissionDirective(
      {
        targetAbility: 'video',
        brief: { intent: 'a quiet harbor' },
        generation: { videoMode: 'local', videoModelId: 'minimax_h3_8bit' },
      },
      { effectiveVideoMode: 'local', effectiveVideoModelId: 'ltx-2.5' },
    );
    expect(directive.goal).toContain('MiniMax H3 prompt template');
  });
});

describe('per-ability directives steer the planner to the right tools', () => {
  it('image: names still-image tools and counts the stills', () => {
    const d = buildCommissionDirective({ targetAbility: 'image', brief: { intent: 'a lighthouse' }, generation: { imageCount: 3 } });
    expect(d.goal).toContain('Produce 3 still images');
    expect(d.goal).toMatch(/do NOT plan a video/i);
    expect(d.deliverables).toEqual(['3 still images matching the brief']);
  });

  it('image: singular phrasing for a single still', () => {
    const d = buildCommissionDirective({ targetAbility: 'image', brief: { intent: 'x' }, generation: { imageCount: 1 } });
    expect(d.goal).toContain('a single still image');
    expect(d.deliverables).toEqual(['One still image matching the brief']);
  });

  it('music: names the music tools and the target length', () => {
    const d = buildCommissionDirective({ targetAbility: 'music', brief: { intent: 'ambient drone' }, generation: { lengthSeconds: 45 } });
    expect(d.goal).toContain('~45s music');
    expect(d.goal).toMatch(/music generation tools/i);
    expect(d.deliverables).toEqual(['One ~45s music track matching the brief']);
  });

  it('music: preserves taste anchors and original-work constraints under the goal cap', () => {
    const d = buildCommissionDirective({
      targetAbility: 'music',
      // A brief filled to the schema caps — the largest a route or PATCH can store.
      brief: { intent: MAXED_INTENT, styleSpec: 'y'.repeat(COMMISSION_STYLE_SPEC_MAX) },
      generation: { lengthSeconds: 45 },
    }, {
      tasteRecipe: {
        version: 1, source: 'digital-twin', window: 'month', anchorCount: 1,
        explorationPercent: 20, explorationCount: 0, explorationDirection: 'balanced',
        anchors: [{ kind: 'artist', name: 'Example Artist', count: 3, source: 'observed' }],
        sourceVersion: 'music-taste-v1:example', sourceHash: 'example-hash',
      },
    });
    expect(d.goal.length).toBeLessThanOrEqual(MAX_DIRECTIVE_GOAL_LEN);
    expect(d.goal).toContain(MAXED_INTENT);
    expect(d.goal).toContain('Example Artist');
    expect(d.goal).toContain('Create an original work');
    expect(d.goal).toContain('do not reproduce source tracks');
  });

  it('music-video: asks for both a music bed and a video scored to it', () => {
    const d = buildCommissionDirective({ targetAbility: 'music-video', brief: { intent: 'neon drift' } });
    expect(d.goal).toMatch(/music bed AND a matching video/i);
    expect(d.deliverables).toHaveLength(2);
  });

  it('series: scopes to the provided universe when constrained', () => {
    const withU = buildCommissionDirective({ targetAbility: 'series', brief: { intent: 'noir', constraints: { universeId: 'u-9' } }, generation: { episodeCount: 2 } });
    expect(withU.goal).toContain('Create the series within the provided universe');
    expect(withU.goal).toContain('first 2 issues/episodes');
    expect(withU.constraints).toMatchObject({ universeId: 'u-9', targetAbility: 'series', generation: { episodeCount: 2 } });

    const noU = buildCommissionDirective({ targetAbility: 'series', brief: { intent: 'noir' }, generation: { episodeCount: 1 } });
    expect(noU.goal).toContain('Invent a fitting universe');
  });
});

describe('sanitizeGeneration — fills defaults and preserves only the type keys', () => {
  it('video keeps its keys and drops off-type ones', () => {
    const g = getAbilityAdapter('video').sanitizeGeneration({ quality: 'high', aspectRatio: '9:16', targetDurationSeconds: 20, imageCount: 5, model: ' ltx ' });
    expect(g).toEqual({
      model: 'ltx', quality: 'high', aspectRatio: '9:16', targetDurationSeconds: 20,
      durationMode: 'manual', videoMode: 'auto', videoModelId: null,
    });
  });

  it('image fills defaults for missing keys and clamps an out-of-range count', () => {
    expect(getAbilityAdapter('image').sanitizeGeneration({})).toEqual({
      model: null, quality: 'standard', aspectRatio: '16:9', imageCount: 1,
      imageMode: 'auto', imageModelId: null,
    });
    expect(getAbilityAdapter('image').sanitizeGeneration({ imageCount: 99 }).imageCount).toBe(1);
    expect(getAbilityAdapter('image').sanitizeGeneration({ imageCount: 4 }).imageCount).toBe(4);
  });

  it('music keeps only model + lengthSeconds', () => {
    expect(getAbilityAdapter('music').sanitizeGeneration({ lengthSeconds: 60, aspectRatio: '16:9' })).toEqual({ model: null, lengthSeconds: 60 });
  });

  it('series keeps only model + episodeCount', () => {
    expect(getAbilityAdapter('series').sanitizeGeneration({ episodeCount: 3, quality: 'high' })).toEqual({ model: null, episodeCount: 3 });
  });

  it('every adapter default matches ABILITY_GENERATION_SPEC', () => {
    for (const [ability, spec] of Object.entries(ABILITY_GENERATION_SPEC)) {
      const sani = getAbilityAdapter(ability).sanitizeGeneration({});
      for (const [k, v] of Object.entries(spec.defaults)) expect(sani[k]).toBe(v);
    }
  });
});

describe('buildProjectParams — every type yields well-formed render settings', () => {
  const ctx = { defaultVideoModelId: () => 'ltx-default' };

  it('video maps its generation onto the render geometry', () => {
    const p = getAbilityAdapter('video').buildProjectParams({ generation: { aspectRatio: '1:1', quality: 'draft', targetDurationSeconds: 15 } }, ctx);
    expect(p).toEqual({ aspectRatio: '1:1', quality: 'draft', modelId: 'ltx-default', targetDurationSeconds: 15 });
  });

  it('lets the Creative Director choose the video length in auto mode', () => {
    const commission = { targetAbility: 'video', generation: { durationMode: 'auto' }, brief: { intent: 'a drifting city' } };
    const p = getAbilityAdapter('video').buildProjectParams(commission, ctx);
    expect(p.targetDurationSeconds).toBe(10);
    expect(buildCommissionDirective(commission).goal).toMatch(/choose an appropriate duration between 5 and 600 seconds/i);
  });

  it('the pinned local video model becomes the project model', () => {
    // `project.modelId` is what the CD planner prompt reports to the LLM and what
    // the teaser tool inherits, so a videoModelId that stopped at
    // `renderBackend.video.modelId` would leave both on the install default.
    const p = getAbilityAdapter('video').buildProjectParams({ generation: { videoMode: 'local', videoModelId: 'wan-2.2' } }, ctx);
    expect(p.modelId).toBe('wan-2.2');
  });

  it('falls back to the legacy universal model, then the install default', () => {
    expect(getAbilityAdapter('video').buildProjectParams({ generation: { model: 'ltx-13b' } }, ctx).modelId).toBe('ltx-13b');
    expect(getAbilityAdapter('video').buildProjectParams({ generation: {} }, ctx).modelId).toBe('ltx-default');
  });

  it('non-video types still carry harmless geometry defaults', () => {
    for (const ability of ['image', 'music', 'series']) {
      const p = getAbilityAdapter(ability).buildProjectParams({ generation: {} }, ctx);
      expect(p.aspectRatio).toBe('16:9');
      expect(p.quality).toBe('standard');
      expect(p.modelId).toBe('ltx-default');
      expect(typeof p.targetDurationSeconds).toBe('number');
    }
  });

  it('carries the music form length onto the owning project', () => {
    const p = getAbilityAdapter('music').buildProjectParams({ generation: { lengthSeconds: 75 } }, ctx);
    expect(p.targetDurationSeconds).toBe(75);
  });
});

describe('buildRenderBackendPin — per-commission render backend (#3135)', () => {
  it('returns null when nothing is pinned (auto = no pin)', () => {
    // The hard back-compat criterion: an auto/absent pin must never produce a
    // pin, so the enqueue-time forcing step is a strict no-op (see media.test.js)
    // and createProject is called with exactly the args it got before.
    expect(buildRenderBackendPin({ generation: { imageMode: 'auto', imageModelId: null } })).toBeNull();
    expect(buildRenderBackendPin({ generation: {} })).toBeNull();
    expect(buildRenderBackendPin({})).toBeNull();
    expect(buildRenderBackendPin(null)).toBeNull();
  });

  it('a model id alone is not a pin — the mode has to name a backend', () => {
    expect(buildRenderBackendPin({ generation: { imageMode: 'auto', imageModelId: 'example-model' } })).toBeNull();
  });

  it('carries a pinned image backend and its model id', () => {
    expect(buildRenderBackendPin({ generation: { imageMode: 'local', imageModelId: ' example-model ' } }))
      .toEqual({ image: { mode: 'local', modelId: 'example-model' } });
  });

  it('carries a cloud pin with a null model id (cloud CLIs pick their own model)', () => {
    expect(buildRenderBackendPin({ generation: { imageMode: 'grok', imageModelId: null } }))
      .toEqual({ image: { mode: 'grok', modelId: null } });
  });

  it('carries both kinds for a music-video commission', () => {
    expect(buildRenderBackendPin({
      generation: { videoMode: 'grok', videoModelId: null, imageMode: 'codex', imageModelId: null },
    })).toEqual({ video: { mode: 'grok', modelId: null }, image: { mode: 'codex', modelId: null } });
  });

  it('is reflected in buildProjectParams only when pinned', () => {
    const ctx = { defaultVideoModelId: () => 'ltx-default' };
    const unpinned = getAbilityAdapter('image').buildProjectParams({ generation: { imageMode: 'auto' } }, ctx);
    expect(unpinned).not.toHaveProperty('renderBackend');

    const pinned = getAbilityAdapter('image').buildProjectParams({ generation: { imageMode: 'grok' } }, ctx);
    expect(pinned.renderBackend).toEqual({ image: { mode: 'grok', modelId: null } });
  });
});