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.

8.7 KB
Raw Blame
import { describe, expect, it, vi } from 'vitest'
import { fakeCore } from '../testing/fakeCore.js'
import { createWifiLink, WIFI_WAIT_MS } from './wifiLink.js'
import type { ServiceFrame } from './serviceLinks.js'

function link(answer: Record<string, unknown> = {}) {
  const core = fakeCore()
  const sent: ServiceFrame[] = []
  const call = vi.fn(async () => answer)
  const log = vi.fn()
  const wifi = createWifiLink({ core, notify: (frame) => { sent.push(frame); return true }, call, log })
  const kinds = () => sent.map((frame) => (frame.payload as { kind: string }).kind)
  return { core, sent, call, log, wifi, kinds }
}

describe('the Wi-Fi device in the devices\' process, as the core sees it', () => {
  it('tells it everything as notices, in the order the core said it', async () => {
    const { wifi, sent, kinds } = link()
    const client = { role: 'device' as const, label: 'Desk', identity: 'id', direct: false }
    wifi.port.session('c', client)
    await wifi.port.request('c', { type: 'autonomous_device_request' }, { payload: { type: 'hello' } })
    wifi.port.dropped('c')
    wifi.port.revoked('id')
    await wifi.port.resume({ sessions: [{ connId: 'c', client }], helloed: [], focus: null })
    wifi.port.card({ type: 'commander_event' })
    wifi.port.card({ type: 'commander_event' }, 'whole')
    wifi.port.turnStarted('a')
    wifi.port.turnEnded('a', true)
    wifi.port.stream('a', [])
    wifi.port.transcript('a', 's', 'claude', 'line')
    wifi.port.delivery({ deliveryId: 'd', sessionId: 'a', state: 'delivered' })
    wifi.port.dispatched('a', 'd', 'hi')
    wifi.port.dispatched('a', 'd', 'hi', 's')
    wifi.port.inputStatus({ deliveryId: 'd' } as never)
    wifi.port.agentGone('a')
    await wifi.port.appFocus('m', null, 'w')
    wifi.port.revealed('op', 'a')
    await wifi.port.stop()
    expect(kinds()).toEqual(['session', 'request', 'dropped', 'revoked', 'resume', 'card', 'card', 'turnStarted', 'turnEnded', 'stream',
      'transcript', 'delivery', 'dispatched', 'dispatched', 'inputStatus', 'agentGone', 'appFocus', 'revealed'])
    expect(sent[0].payload).toEqual({ kind: 'session', connId: 'c', client })
    expect(sent[5].payload).toEqual({ kind: 'card', frame: { type: 'commander_event' } })
    expect(sent[6].payload).toEqual({ kind: 'card', frame: { type: 'commander_event' }, fullText: 'whole' })
    expect(sent[12].payload).toEqual({ kind: 'dispatched', agentId: 'a', deliveryId: 'd', text: 'hi' })
    expect(sent[13].payload).toMatchObject({ sessionId: 's' })
  })

  it('asks it for a receipt with a deadline, and says it is not running when it does not answer', async () => {
    const receipt = { idempotencyKey: 'k', state: 'completed' }
    const answered = link({ receipt })
    expect(await answered.wifi.port.receipt('dev', 'k')).toEqual({ receipt })
    expect(answered.call).toHaveBeenCalledWith('receipt', { deviceId: 'dev', idempotencyKey: 'k' }, WIFI_WAIT_MS)
    expect(await link({}).wifi.port.receipt('dev', 'k')).toEqual({ receipt: null })
    expect(await link({ error: 'SERVICE_UNAVAILABLE' }).wifi.port.receipt('dev', 'k')).toEqual({ unavailable: true })
  })

  it('asks it about device add-ons with a deadline, and passes the add-on layer\'s own refusal through', async () => {
    const answered = link({ result: { devices: [] } })
    expect(await answered.wifi.port.addon('state', { deviceId: 'lamp' })).toEqual({ result: { devices: [] } })
    expect(answered.call).toHaveBeenCalledWith('addon', { op: 'state', input: { deviceId: 'lamp' } }, WIFI_WAIT_MS)
    const refused = { error: { code: 'RATE_LIMITED', message: 'slow down' } }
    expect(await link(refused).wifi.port.addon('propose', {})).toEqual(refused)
    expect(await link({ error: 'SERVICE_UNAVAILABLE' }).wifi.port.addon('proposal', { proposalId: 'p' })).toEqual({ unavailable: true })
    // A proposal with no answer in time may have been sent: unknown, retried with its key, never "not running".
    expect(await link({ error: 'SERVICE_UNAVAILABLE' }).wifi.port.addon('propose', {})).toMatchObject({ error: { code: 'OUTCOME_UNKNOWN' } })
  })

  it('answers what it asks through the core\'s Wi-Fi doors', async () => {
    const { wifi, core } = link()
    expect(await wifi.answer('machine', {})).toEqual({ id: 'machine-1' })
    expect(await wifi.answer('view', {})).toEqual({ agents: [], store: [], hasWindow: true })
    vi.mocked(core.turns.recent).mockResolvedValueOnce([{ recap: 'done' }] as never)
    expect(await wifi.answer('recent', { agentId: 'a', n: 1 })).toEqual({ turns: [{ recap: 'done' }] })
    expect(core.turns.recent).toHaveBeenLastCalledWith('a', 1)
    await wifi.answer('recent', { agentId: 7 })
    expect(core.turns.recent).toHaveBeenLastCalledWith('', 3)
    expect(await wifi.answer('stop', { agentId: 'a' })).toEqual({ ok: true })
    expect(await wifi.answer('answer', { agentId: 'a', questionRequestId: 'r', answers: { q: 'yes' }, requestId: 'the-link-s-own' })).toEqual({ ok: true })
    expect(core.wifi.answer).toHaveBeenLastCalledWith('a', 'r', { q: 'yes' })
    await wifi.answer('answer', { agentId: 'a', questionRequestId: 'r', answers: ['no'] })
    expect(core.wifi.answer).toHaveBeenLastCalledWith('a', 'r', {})
    expect(await wifi.answer('create', { packageId: 'p', engine: 'claude', cwd: '/w' })).toEqual({ ok: false, error: 'UNSUPPORTED' })
    expect(await wifi.answer('stepFocus', { direction: 'previous', currentAgentId: 'a' })).toEqual({ step: 'no_agents' })
    expect(core.wifi.stepFocus).toHaveBeenLastCalledWith('previous', 'a')
    vi.mocked(core.wifi.stepFocus).mockResolvedValueOnce({ machineId: 'm', agentId: 'b' })
    expect(await wifi.answer('stepFocus', { direction: 'next' })).toEqual({ machineId: 'm', agentId: 'b' })
    expect(core.wifi.stepFocus).toHaveBeenLastCalledWith('next', undefined)
    expect(await wifi.answer('submit', { agentId: 'a', text: 'hi', deliveryId: 'd' })).toEqual({})
    expect(core.wifi.submit).toHaveBeenCalledWith('a', 'hi', 'd')
    expect(await wifi.answer('nothing', {})).toEqual({ error: 'UNKNOWN_QUERY' })
  })

  it('hears what it tells the core without asking, and says when it hears something else', () => {
    const { wifi, core, log } = link()
    wifi.notice({ kind: 'cancel', deliveryId: 'd' })
    wifi.notice({ kind: 'started', agentId: 'a', text: 'hi' })
    wifi.notice({ kind: 'scroll', phase: 'move', dy: 3, velocity: 9 })
    wifi.notice({ kind: 'scroll', phase: 'fling', dy: 3 })
    wifi.notice({ kind: 'scroll', phase: 'up', dy: 'far' })
    wifi.notice({ kind: 'focusApp', agentId: 'a', expiresAt: 5, focusRevision: 'r' })
    wifi.notice({ kind: 'reveal', operationId: 'op', agentId: 'a' })
    wifi.notice({ kind: 'send', connId: 'c', identity: 'id', type: 'autonomous_device_result', payload: { ok: true } })
    wifi.notice({ kind: 'hello', connId: 'c', identity: 'id' })
    wifi.notice({ kind: 'hello', connId: 'c', identity: null })
    wifi.notice({ kind: 'joined' })
    wifi.notice({ kind: 'ready' })
    wifi.notice({ kind: 'unpaired', identity: 'id' })
    wifi.notice({ kind: 'focus', revision: 'r' })
    wifi.notice({ kind: 'transcripts', agentId: 'a', seen: 2 })
    wifi.notice({ kind: 'watching', agentIds: ['a', 4] })
    wifi.notice({ kind: 'streams', agentIds: 'a' })
    expect(core.wifi.cancel).toHaveBeenCalledWith('d')
    expect(core.wifi.started).toHaveBeenCalledWith('a', 'hi')
    expect(core.wifi.scroll).toHaveBeenCalledTimes(2)
    expect(core.wifi.scroll).toHaveBeenCalledWith('move', 3, 9)
    expect(core.wifi.scroll).toHaveBeenLastCalledWith('up', 0, 0)
    expect(core.wifi.focusApp).toHaveBeenCalledWith('a', 5, 'r')
    expect(core.wifi.reveal).toHaveBeenCalledWith('op', 'a')
    expect(core.wifi.send).toHaveBeenCalledWith('c', 'id', 'autonomous_device_result', { ok: true })
    expect(core.wifi.hello).toHaveBeenCalledWith('c', 'id')
    expect(core.wifi.hello).toHaveBeenLastCalledWith('c', null)
    expect(core.wifi.joined).toHaveBeenCalled()
    expect(core.wifi.ready).toHaveBeenCalled()
    expect(core.wifi.unpaired).toHaveBeenCalledWith('id')
    expect(core.wifi.focus).toHaveBeenCalledWith('r')
    expect(core.wifi.transcripts).toHaveBeenCalledWith('a', 2)
    expect(core.wifi.watching).toHaveBeenCalledWith(['a'])
    expect(core.wifi.streams).toHaveBeenCalledWith([])
    wifi.notice({ kind: 'toString' })
    wifi.notice({})
    expect(log).toHaveBeenCalledWith('[services] wifi said toString, which the core does not hear')
    expect(log).toHaveBeenLastCalledWith('[services] wifi said nothing, which the core does not hear')
  })

  it('logs to the console by default', () => {
    const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})
    createWifiLink({ core: fakeCore(), notify: () => true, call: async () => ({}) }).notice({ kind: 'nope' })
    expect(warn).toHaveBeenCalledWith('[services] wifi said nope, which the core does not hear')
    warn.mockRestore()
  })
})