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

4.3 KB
Raw Blame
import { afterAll, afterEach, describe, expect, it, vi } from 'vitest';
import { rmSync, watch } from 'node:fs';

const gate = vi.hoisted(() => ({ root: null, admission: null, notify: null }));
// The contract is the service's response to journal invalidations, not native
// filesystem delivery timing. Keep the real journal and inject only its watcher.
vi.mock('node:fs', async original => {
  const actual = await original();
  const { EventEmitter } = await import('node:events');
  return { ...actual, watch: vi.fn((_path, notify) => {
    gate.notify = notify;
    const watcher = new EventEmitter();
    watcher.close = vi.fn();
    watcher.unref = vi.fn();
    return watcher;
  }) };
});
vi.mock('../lib/maintenanceAdmission.js', async () => {
  const actual = await vi.importActual('../lib/maintenanceAdmission.js');
  const { mkdtempSync: mk } = await import('node:fs');
  const { tmpdir: tmp } = await import('node:os');
  const { join: j } = await import('node:path');
  gate.root = mk(j(tmp(), 'maintenance-control-'));
  gate.admission = actual.createMaintenanceAdmission(gate.root);
  return { ...actual, maintenance: gate.admission };
});
import { bindMaintenanceIo, _getMaintenanceWatcher } from './maintenanceControl.js';

afterAll(() => {
  _getMaintenanceWatcher()?.close();
  rmSync(gate.root, { recursive: true, force: true });
});

const sent = [];
const io = { emit: (name, payload) => sent.push({ name, payload }) };
const count = name => sent.filter(e => e.name === name).length;
afterEach(() => vi.useRealTimers());
const notifyJournalChange = async () => {
  const previousCount = sent.length;
  gate.notify('rename', 'state.json');
  await vi.advanceTimersByTimeAsync(99);
  expect(sent).toHaveLength(previousCount);
  await vi.advanceTimersByTimeAsync(1);
};

describe('maintenance notifications', () => {
  it('keeps detailed invalidations but only pushes the coarse readiness projection when it changes', async () => {
    vi.useFakeTimers();
    bindMaintenanceIo(io);
    const maintenance = gate.admission;
    expect(watch).toHaveBeenCalledWith(maintenance.directory, expect.any(Function));

    // Normal admit/settle churn rewrites the journal but not the banner projection.
    const op = maintenance.admit('agent', 'resource-that-must-not-leak');
    await notifyJournalChange();
    expect(count('maintenance:changed')).toBeGreaterThanOrEqual(1);
    expect(await op.finish()).toBe(true);
    expect(maintenance.status().blockers).toEqual([]);
    await notifyJournalChange();
    expect(count('maintenance:changed')).toBeGreaterThanOrEqual(2);
    expect(count('maintenance:readiness')).toBe(0);

    // Hold with a running operation: draining with a blocker count.
    const running = maintenance.admit('agent', 'resource-that-must-not-leak');
    maintenance.begin({ reason: 'Service work', owner: 'Operator' });
    await notifyJournalChange();
    expect(count('maintenance:readiness')).toBe(1);
    expect(sent.find(e => e.name === 'maintenance:readiness').payload).toEqual({ state: 'draining', blockerCount: 1 });

    // Settling the blocker moves to ready (count no longer displayed).
    expect(await running.finish()).toBe(true);
    await notifyJournalChange();
    expect(count('maintenance:readiness')).toBe(2);
    expect(sent.filter(e => e.name === 'maintenance:readiness')[1].payload).toEqual({ state: 'ready', blockerCount: 0 });

    // Leaving the hold returns to normal.
    const { hold } = maintenance.status();
    maintenance.resume({ id: hold.id, revision: hold.revision });
    await notifyJournalChange();
    expect(count('maintenance:readiness')).toBe(3);

    // Nothing private is broadcast.
    expect(JSON.stringify(sent)).not.toMatch(/resource-that-must-not-leak|pid|owner|hold/i);
  });

  it('handles watcher error without throwing and recovers watching on next emit', async () => {
    const watcher = _getMaintenanceWatcher();
    expect(watcher).toBeDefined();
    expect(watcher.listenerCount('error')).toBe(1);

    // Emitting error should close watcher and not throw
    expect(() => {
      watcher.emit('error', new Error('EPERM: operation not permitted'));
    }).not.toThrow();

    expect(_getMaintenanceWatcher()).toBeNull();
    expect(watcher.close).toHaveBeenCalledOnce();

    // Re-arming through bindMaintenanceIo or emitChanges restores the watcher
    bindMaintenanceIo(io);
    expect(_getMaintenanceWatcher()).not.toBeNull();
  });
});