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

2.5 KB
Raw Blame
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import { aiStatusEvents, startAIOp } from './aiStatusEvents.js';

// Capture every emitted status event so we can assert which fields cross the
// `ai:status` channel that the OpenWorld AI Core landmark consumes.
function captureEvents(fn) {
  const events = [];
  const handler = (e) => events.push(e);
  aiStatusEvents.on('status', handler);
  fn();
  aiStatusEvents.off('status', handler);
  return events;
}

describe('startAIOp building-association fields', () => {
  it('propagates appId + workspacePath onto every phase event', () => {
    const events = captureEvents(() => {
      const op = startAIOp({
        op: 'task-gen',
        label: 'Generating tasks',
        appId: 'app-42',
        workspacePath: '/repos/widget',
      });
      op.update('model:loading', 'Loading…');
      op.complete('Done');
    });

    expect(events).toHaveLength(3);
    for (const e of events) {
      expect(e.appId).toBe('app-42');
      expect(e.workspacePath).toBe('/repos/widget');
    }
    expect(events.map(e => e.phase)).toEqual(['start', 'model:loading', 'complete']);
  });

  it('propagates an operation id and local-only marker onto every phase event', () => {
    const events = captureEvents(() => {
      const op = startAIOp({
        op: 'fableloom-feedback-episode',
        label: 'Updating episode',
        operationId: 'op-42',
        localOnly: true,
      });
      op.update('running', 'Working…');
      op.complete('Ready');
    });

    expect(events).toHaveLength(3);
    expect(events.every((event) => event.operationId === 'op-42' && event.localOnly)).toBe(true);
  });

  it('leaves appId/workspacePath undefined for unassociated internal ops', () => {
    const [start] = captureEvents(() => {
      startAIOp({ op: 'taste-summary', label: 'Summarizing taste' });
    });
    expect(start.appId).toBeUndefined();
    expect(start.workspacePath).toBeUndefined();
  });

  it('carries token throughput on the completion event', () => {
    const events = captureEvents(() => {
      const op = startAIOp({ op: 'jira-report', label: 'Report', appId: 'app-1' });
      op.complete('Done', { tokens: 480, tokensPerSec: 120 });
    });
    const complete = events.find(e => e.phase === 'complete');
    expect(complete.tokens).toBe(480);
    expect(complete.tokensPerSec).toBe(120);
    // The start event has no token info yet.
    const start = events.find(e => e.phase === 'start');
    expect(start.tokens).toBeUndefined();
  });
});