Skip to content

armgit / avibe

Public Imported from GitHub
0

Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.

2.9 KB
Raw Blame
import { readdirSync, readFileSync } from 'node:fs';
import { join } from 'node:path';
import { describe, expect, it, vi } from 'vitest';

import { createAgentCollectionReadAuthority } from './collectionReadAuthority';
import type { AgentSupply } from './types';

const agent = (mode: AgentSupply['mode']): AgentSupply => ({
  backend: 'claude',
  cli_present: true,
  mode,
  menu_kind: 'fixed',
  sources: mode === 'hub' ? { order: [], eligibility: [] } : null,
});

const deferred = <T,>() => {
  let resolve!: (value: T) => void;
  const promise = new Promise<T>((done) => { resolve = done; });
  return { promise, resolve };
};

const productFiles = (directory: string): string[] => readdirSync(directory, { withFileTypes: true })
  .flatMap((entry) => {
    const path = join(directory, entry.name);
    if (entry.isDirectory()) return productFiles(path);
    return /\.(?:ts|tsx)$/.test(entry.name) && !/\.test\.(?:ts|tsx)$/.test(entry.name) ? [path] : [];
  });

describe('collection read authority', () => {
  it('rejects an older Agent collection response after a newer generation lands', async () => {
    const older = deferred<AgentSupply[]>();
    const newer = deferred<AgentSupply[]>();
    const api = {
      listAgents: vi.fn()
        .mockReturnValueOnce(older.promise)
        .mockReturnValueOnce(newer.promise),
    };
    const authority = createAgentCollectionReadAuthority(api);

    const first = authority.read();
    const second = authority.read();
    newer.resolve([agent('hub')]);
    await expect(second).resolves.toEqual({ kind: 'current', value: [agent('hub')] });

    older.resolve([agent('direct')]);
    await expect(first).resolves.toEqual({ kind: 'stale' });
  });

  it('publishes deep CLI discovery after a concurrent fast read', async () => {
    const refresh = deferred<AgentSupply[]>();
    const api = {
      listAgents: vi.fn()
        .mockResolvedValueOnce([agent('direct')])
        .mockResolvedValueOnce([agent('hub')]),
      refreshAgentPresence: vi.fn().mockReturnValue(refresh.promise),
    };
    const authority = createAgentCollectionReadAuthority(api);

    const pending = authority.refresh();
    await expect(authority.read()).resolves.toEqual({ kind: 'current', value: [agent('direct')] });
    refresh.resolve([agent('hub')]);

    await expect(pending).resolves.toEqual({ kind: 'current', value: [agent('hub')] });
    expect(api.refreshAgentPresence).toHaveBeenCalledOnce();
    expect(api.listAgents).toHaveBeenCalledTimes(2);
  });

  it('keeps collection endpoints private to the generation authority', () => {
    const allowed = new Set(['collectionReadAuthority.ts', 'modelsApi.ts']);
    const violations = productFiles(__dirname).flatMap((path) => {
      if (allowed.has(path.split('/').at(-1) ?? '')) return [];
      return /\.(?:list(?:Agents|Sources)|refreshAgentPresence)\s*\(/.test(readFileSync(path, 'utf8')) ? [path] : [];
    });

    expect(violations).toEqual([]);
  });
});