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

3.4 KB
Raw Blame
import { expect, it, vi } from 'vitest';
import { createManagedVisitorHost } from './managedVisitorHost.js';
import { EIDOVERSE_PORT } from '../lib/eidoversePort.js';
it('disabled transport makes no requests and explicit configuration keeps host credentials on the fixed loopback namespace', async () => {
  const fetchImpl = vi.fn(async () => new Response(JSON.stringify({ capabilities: { managedVisitors: null } })));
  const disabled = createManagedVisitorHost({ token: '', fetchImpl });
  expect(await disabled.capabilities()).toBe(null); await expect(disabled.admit({})).rejects.toThrow(/not configured/);
  expect(fetchImpl).not.toHaveBeenCalled();
  const token = 'a'.repeat(64), host = createManagedVisitorHost({ token, fetchImpl }); await host.capabilities();
  const [url, options] = fetchImpl.mock.calls[0];
  expect(url).toBe(`http://127.0.0.1:${EIDOVERSE_PORT}/api/managed-visitors/v1/version`);
  expect(options.headers.Authorization).toBe(`Bearer ${token}`); expect(options.redirect).toBe('error'); expect(options.signal).toBeInstanceOf(AbortSignal);
});
it('host response buffering is bounded and malformed/non-success replies do not become capabilities', async () => {
  let canceled = false;
  const fetchImpl = vi.fn(async () => new Response(new ReadableStream({ pull(controller) { controller.enqueue(new Uint8Array(17000)); }, cancel() { canceled = true; } })));
  const host = createManagedVisitorHost({ token: 'a'.repeat(64), fetchImpl });
  await expect(host.observe('session', {})).rejects.toThrow(/bound/); expect(canceled).toBe(true);
  fetchImpl.mockResolvedValueOnce(new Response('not json')); expect(await host.capabilities()).toBe(null);
  fetchImpl.mockResolvedValueOnce(new Response('{}', { status: 401 })); await expect(host.admit({}, { deadlineMs: Date.now() + 1000 })).rejects.toThrow(/refused/);
});
it('an omitted token resolves live from settings (winning over the env var) or falls back to it, with no caching across calls', async () => {
  const originalEnv = process.env.PORTOS_EIDOVERSE_VISITOR_TOKEN;
  const envToken = 'e'.repeat(64), settingsToken = 'a'.repeat(64);
  process.env.PORTOS_EIDOVERSE_VISITOR_TOKEN = envToken;
  try {
    vi.resetModules();
    let stubbedSettings = { secrets: { eidoverse: { visitorToken: settingsToken } } };
    vi.doMock('./settings.js', () => ({ getSettings: async () => stubbedSettings }));
    const { createManagedVisitorHost: createHost } = await import('./managedVisitorHost.js');
    const fetchImpl = vi.fn(async () => new Response(JSON.stringify({ capabilities: { managedVisitors: null } })));
    const host = createHost({ fetchImpl }); // no explicit token: must resolve live
    await host.capabilities();
    expect(fetchImpl.mock.calls[0][1].headers.Authorization).toBe(`Bearer ${settingsToken}`); // settings wins over env

    stubbedSettings = { secrets: {} }; // settings cleared — must fall back to env, not a cached settings value
    await host.capabilities();
    expect(fetchImpl.mock.calls[1][1].headers.Authorization).toBe(`Bearer ${envToken}`);

    stubbedSettings = {}; process.env.PORTOS_EIDOVERSE_VISITOR_TOKEN = '';
    expect(await host.capabilities()).toBe(null); // neither source configured: disabled, no request made
    expect(fetchImpl).toHaveBeenCalledTimes(2);
  } finally {
    vi.doUnmock('./settings.js'); vi.resetModules();
    if (originalEnv === undefined) delete process.env.PORTOS_EIDOVERSE_VISITOR_TOKEN;
    else process.env.PORTOS_EIDOVERSE_VISITOR_TOKEN = originalEnv;
  }
});