Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

3.4 KB
Raw Blame
import { mkdtempSync, mkdirSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { describe, expect, it } from 'vitest'
import { createGuestHostBackend, guestHostHome, guestHostTask, waitForGuestHost } from './guestHostBackend.js'

const discovery = (instance: string, version: string, protocol: number, pid: number) => JSON.stringify({
  protocol, instance, endpoint: `\\\\.\\pipe\\harness-pty-${instance}`, pid, version,
  token: '0123456789abcdef0123456789abcdef01234567',
})

describe('a guest host', () => {
  it('lives under the guest tree and starts the task the setup task registered', () => {
    expect(guestHostHome('hg_0123456789ab')).toBe('C:\\ProgramData\\HarnessGuests\\hosts\\hg_0123456789ab')
    expect(guestHostTask('hg_0123456789ab')).toBe('\\Harness\\Guests\\hg_0123456789ab')
  })

  it('picks the newest discovery file the guest published', async () => {
    const home = mkdtempSync(join(tmpdir(), 'guest-host-'))
    mkdirSync(join(home, 'run'))
    writeFileSync(join(home, 'run', 'pty-host-p2-0.0.1.json'), discovery('p2-0.0.1', '0.0.1', 2, 11))
    writeFileSync(join(home, 'run', 'pty-host-p2-9.9.9.json'), discovery('p2-9.9.9', '9.9.9', 2, 22))
    writeFileSync(join(home, 'run', 'other.json'), '{}')
    // Both hosts are alive in this test: the newest wins.
    expect((await waitForGuestHost(home, { user: 'hg_x', waitMs: 0, liveness: { alive: () => true } }))?.instance).toBe('p2-9.9.9')
  })

  it('never dials a discovery file whose host has gone, as a setup re-run leaves behind', async () => {
    const home = mkdtempSync(join(tmpdir(), 'guest-host-stale-'))
    mkdirSync(join(home, 'run'))
    writeFileSync(join(home, 'run', 'pty-host-p3-0.5.55.json'), discovery('p3-0.5.55', '0.5.55', 3, 11))
    writeFileSync(join(home, 'run', 'pty-host-p3-0.5.55-gu0123456789ab.json'), discovery('p3-0.5.55-gu0123456789ab', '0.5.55', 3, 22))
    // The old host's pid is gone; the tagged one runs. The dead file is newer only by name, never picked.
    const liveness = { alive: (pid: number) => pid === 22 }
    expect((await waitForGuestHost(home, { user: 'hg_x', waitMs: 0, liveness }))?.instance).toBe('p3-0.5.55-gu0123456789ab')
    expect(await waitForGuestHost(home, { user: 'hg_x', waitMs: 0, liveness: { alive: () => false } })).toBeNull()
  })

  it('answers an unavailable host, never a throw, when its task cannot start', async () => {
    const home = mkdtempSync(join(tmpdir(), 'guest-host-task-'))
    const backend = createGuestHostBackend({ user: 'hg_x', home, startTask: async () => { throw new Error('the task is gone') },
      list: () => [], waitMs: 0, sleep: async () => {} })
    expect(await backend.prepare({ ownedPanes: new Map() })).toMatchObject({ state: 'unavailable', code: 'PTY_HOST_UNAVAILABLE' })
    backend.dispose()
  })

  it('returns null when nothing is published in the window', async () => {
    const home = mkdtempSync(join(tmpdir(), 'guest-host-empty-'))
    expect(await waitForGuestHost(home, { user: 'hg_x', waitMs: 0 })).toBeNull()
  })

  it('uses only files whose instance and protocol a host really writes', async () => {
    const home = mkdtempSync(join(tmpdir(), 'guest-host-bad-'))
    mkdirSync(join(home, 'run'))
    writeFileSync(join(home, 'run', 'pty-host-p2-9.9.9.json'), JSON.stringify({ instance: 'p2-1.0.0', protocol: 2, endpoint: 'x', pid: 1, version: '9.9.9', token: 'short' }))
    expect(await waitForGuestHost(home, { user: 'hg_x', waitMs: 0 })).toBeNull()
  })
})