Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

8.5 KB
Raw Blame
import { EventEmitter } from 'node:events';

import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';

import { createDevOrchestrator } from '../devOrchestrator.mjs';

class FakeChild extends EventEmitter {
  kill = vi.fn();
}

const createHarness = (overrides = {}) => {
  const spawned = [];
  const watchers = [];
  const state = { bundlesExist: false, mtime: Date.now() + 1, portReady: false };

  const options = {
    checkPort: vi.fn(async () => state.portReady),
    desktopRoot: '/repo/apps/desktop',
    electronBin: '/bin/electron',
    exit: vi.fn(),
    existsSync: vi.fn(() => state.bundlesExist),
    log: vi.fn(),
    logError: vi.fn(),
    nodeBin: '/bin/node',
    rmSync: vi.fn(),
    spawn: vi.fn((bin, args, opts) => {
      const child = new FakeChild();
      spawned.push({ args, bin, child, opts });
      return child;
    }),
    statSync: vi.fn(() => ({ mtimeMs: state.mtime })),
    viteBin: '/repo/node_modules/vite/bin/vite.js',
    vitePort: 5199,
    watch: vi.fn((dir, cb) => {
      watchers.push(cb);
    }),
    ...overrides,
  };

  const orchestrator = createDevOrchestrator(options);
  const viteChildren = () => spawned.filter((s) => s.bin === '/bin/node');
  const electronSpawns = () => spawned.filter((s) => s.bin === '/bin/electron');

  const becomeReady = async () => {
    state.bundlesExist = true;
    state.portReady = true;
    await vi.advanceTimersByTimeAsync(2000);
  };

  return {
    becomeReady,
    electronSpawns,
    options,
    orchestrator,
    spawned,
    state,
    viteChildren,
    watchers,
  };
};

describe('createDevOrchestrator', () => {
  beforeEach(() => {
    vi.useFakeTimers();
  });

  afterEach(() => {
    vi.useRealTimers();
  });

  it('spawns the three vite processes through the node binary (no pnpm shim)', () => {
    const h = createHarness();
    h.orchestrator.start();

    expect(h.viteChildren()).toHaveLength(3);
    for (const { args, opts } of h.viteChildren()) {
      expect(args[0]).toBe('/repo/node_modules/vite/bin/vite.js');
      expect(opts.cwd).toBe('/repo/apps/desktop');
    }
    const configs = h.viteChildren().map(({ args }) => args.at(-1));
    expect(configs).toEqual([
      'vite.renderer.config.ts',
      'vite.main.config.ts',
      'vite.preload.config.ts',
    ]);
  });

  it('wipes the stale dist before spawning the watch builds', () => {
    const h = createHarness();
    h.orchestrator.start();

    expect(h.options.rmSync).toHaveBeenCalledWith('/repo/apps/desktop/dist', {
      force: true,
      recursive: true,
    });
    expect(h.options.rmSync.mock.invocationCallOrder[0]).toBeLessThan(
      h.options.spawn.mock.invocationCallOrder[0],
    );
  });

  it('shuts everything down when a vite child exits early', () => {
    const h = createHarness();
    h.orchestrator.start();

    h.viteChildren()[1].child.emit('exit', 7);

    expect(h.options.exit).toHaveBeenCalledWith(7);
    for (const { child } of h.viteChildren()) {
      expect(child.kill).toHaveBeenCalled();
    }
    expect(h.electronSpawns()).toHaveLength(0);
  });

  it('launches electron only after bundles are stable and the renderer port accepts', async () => {
    const h = createHarness();
    h.orchestrator.start();

    h.state.bundlesExist = true;
    await vi.advanceTimersByTimeAsync(2000);
    expect(h.electronSpawns()).toHaveLength(0);

    h.state.portReady = true;
    await vi.advanceTimersByTimeAsync(400);

    expect(h.electronSpawns()).toHaveLength(1);
    const { args, opts } = h.electronSpawns()[0];
    expect(args).toEqual(['.']);
    expect(opts.env.ELECTRON_RENDERER_URL).toBe('http://127.0.0.1:5199');
    expect(opts.env.NODE_ENV).toBe('development');
    expect(h.options.watch).toHaveBeenCalledTimes(2);
  });

  it('waits for the bundle signature to stay quiet before launching', async () => {
    const h = createHarness();
    h.orchestrator.start();
    h.state.bundlesExist = true;
    h.state.portReady = true;

    await vi.advanceTimersByTimeAsync(600);
    h.state.mtime += 1;
    await vi.advanceTimersByTimeAsync(600);
    expect(h.electronSpawns()).toHaveLength(0);

    await vi.advanceTimersByTimeAsync(1400);
    expect(h.electronSpawns()).toHaveLength(1);
  });

  it('ignores bundles left over from a previous run until the watch build rewrites them', async () => {
    const h = createHarness();
    h.state.mtime = Date.now() - 1;
    h.orchestrator.start();
    h.state.bundlesExist = true;
    h.state.portReady = true;

    await vi.advanceTimersByTimeAsync(3000);
    expect(h.electronSpawns()).toHaveLength(0);

    h.state.mtime = Date.now();
    await vi.advanceTimersByTimeAsync(2000);
    expect(h.electronSpawns()).toHaveLength(1);
  });

  it('passes extra CLI args through to electron', async () => {
    const h = createHarness({ electronArgs: ['--remote-debugging-port=9223', '--foo'] });
    h.orchestrator.start();
    await h.becomeReady();

    expect(h.electronSpawns()[0].args).toEqual(['.', '--remote-debugging-port=9223', '--foo']);
  });

  it('restarts electron when a bundle change fires the watcher', async () => {
    const h = createHarness();
    h.orchestrator.start();
    await h.becomeReady();

    const electron = h.electronSpawns()[0].child;
    h.watchers[0]('change');
    await vi.advanceTimersByTimeAsync(500);
    expect(electron.kill).toHaveBeenCalled();

    electron.emit('exit', null);
    expect(h.electronSpawns()).toHaveLength(2);
    expect(h.options.exit).not.toHaveBeenCalled();
  });

  it('debounces rapid bundle changes into one restart', async () => {
    const h = createHarness();
    h.orchestrator.start();
    await h.becomeReady();

    const electron = h.electronSpawns()[0].child;
    h.watchers[0]('change');
    await vi.advanceTimersByTimeAsync(200);
    h.watchers[1]('change');
    await vi.advanceTimersByTimeAsync(500);

    expect(electron.kill).toHaveBeenCalledTimes(1);
  });

  it('names every changed bundle in the restart log, once', async () => {
    const h = createHarness();
    h.orchestrator.start();
    await h.becomeReady();

    h.watchers[0]('change', 'index.js');
    h.watchers[1]('change', 'index.js');
    await vi.advanceTimersByTimeAsync(500);

    const message = h.options.log.mock.calls.at(-1)[0];
    expect(message).toContain('[desktop-dev] main/preload bundle changed');
    expect(message).toContain('dist/main/index.js: change');
    expect(message).toContain('dist/preload/index.js: change');

    h.electronSpawns()[0].child.emit('exit', null);
    h.watchers[0]('change', 'index.js');
    await vi.advanceTimersByTimeAsync(500);

    expect(h.options.log.mock.calls.at(-1)[0]).not.toContain('dist/preload/index.js');
  });

  it('stops the watchers when electron is quit by hand', async () => {
    const h = createHarness();
    h.orchestrator.start();
    await h.becomeReady();

    h.electronSpawns()[0].child.emit('exit', 0);

    expect(h.options.exit).toHaveBeenCalledWith(0);
    for (const { child } of h.viteChildren()) {
      expect(child.kill).toHaveBeenCalled();
    }
  });

  it('shutdown kills electron and all vite children exactly once', async () => {
    const h = createHarness();
    h.orchestrator.start();
    await h.becomeReady();

    h.orchestrator.shutdown(0);
    h.orchestrator.shutdown(1);

    expect(h.electronSpawns()[0].child.kill).toHaveBeenCalledTimes(1);
    for (const { child } of h.viteChildren()) {
      expect(child.kill).toHaveBeenCalled();
    }
    // A group signal must not reach Electron too: its second SIGTERM is fatal.
    expect(h.electronSpawns()[0].opts.detached).toBe(process.platform !== 'win32');
    // Electron is still quitting: the launcher must outlive it.
    expect(h.options.exit).not.toHaveBeenCalled();

    h.electronSpawns()[0].child.emit('exit', 0);
    expect(h.options.exit).toHaveBeenCalledTimes(1);
    expect(h.options.exit).toHaveBeenCalledWith(0);
  });

  it('kills an electron whose quit hangs past the timeout', async () => {
    const h = createHarness({ electronQuitTimeoutMs: 5000 });
    h.orchestrator.start();
    await h.becomeReady();

    h.orchestrator.shutdown(0);
    await vi.advanceTimersByTimeAsync(5000);

    expect(h.electronSpawns()[0].child.kill).toHaveBeenLastCalledWith('SIGKILL');
    expect(h.options.exit).toHaveBeenCalledWith(0);
  });

  it('fails with exit(1) when the initial build never produces bundles', async () => {
    const h = createHarness();
    h.orchestrator.start();

    await vi.advanceTimersByTimeAsync(121_000);

    expect(h.options.logError).toHaveBeenCalled();
    expect(h.options.exit).toHaveBeenCalledWith(1);
    expect(h.electronSpawns()).toHaveLength(0);
  });
});