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.

10.0 KB
Raw Blame
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import Fastify, { type FastifyInstance } from 'fastify'
const m = vi.hoisted(() => ({ auth: vi.fn(), read: vi.fn(), append: vi.fn(), seen: vi.fn(), touch: vi.fn(), readAcks: vi.fn(), appendAck: vi.fn() }))
vi.mock('../lib/deviceKeyLog.js', () => ({
  readDeviceKeyLog: m.read, appendDeviceKey: m.append, deviceKeysSeen: m.seen, touchDeviceKey: m.touch,
  readDeviceAcks: m.readAcks, appendDeviceAck: m.appendAck,
}))
vi.mock('../lib/ssoAuth.js', async original => ({ ...await original<typeof import('../lib/ssoAuth.js')>(), authenticateAccessToken: m.auth }))
import { deviceKeyRoutes } from './deviceKeys.js'
import { registerAuthMiddleware } from '../middlewares/authMiddleware.js'
import { errorHandler } from '../middlewares/errorHandler.js'

const user = { sub: 'user-1', email: '[email protected]', role: 'user', autonomousEnv: 'prod' as const }
const auth = { authorization: 'Bearer fixture' }
const head = { seq: 3, hash: Buffer.alloc(32, 7).toString('base64') }

describe('device key routes', () => {
  let app: FastifyInstance
  beforeEach(async () => {
    vi.resetAllMocks()
    m.auth.mockResolvedValue(user)
    app = Fastify(); app.setErrorHandler(errorHandler); registerAuthMiddleware(app, m.auth)
    await app.register(deviceKeyRoutes); await app.ready()
  })
  afterEach(async () => { await app.close() })

  it('needs a sign-in', async () => {
    expect((await app.inject({ method: 'GET', url: '/api/device-keys' })).statusCode).toBe(401)
  })

  it('reads the caller log from a head, uncached', async () => {
    m.read.mockResolvedValue({ acct: 'user-1', head, entries: [] })
    const r = await app.inject({ method: 'GET', url: '/api/device-keys?since=2', headers: auth })
    expect(r.statusCode).toBe(200)
    expect(r.headers['cache-control']).toBe('no-store')
    expect(r.json().data).toEqual({ acct: 'user-1', head, entries: [] })
    expect(m.read).toHaveBeenCalledWith('user-1', 2)
    expect(m.touch).not.toHaveBeenCalled()
  })

  it('counts a read as the reading key being in use, and reads the same either way', async () => {
    m.read.mockResolvedValue({ acct: 'user-1', head, entries: [] })
    const self = Buffer.alloc(32, 4).toString('base64')
    const r = await app.inject({ method: 'GET', url: `/api/device-keys?since=2&self=${encodeURIComponent(self)}`, headers: auth })
    expect(r.statusCode).toBe(200)
    expect(r.json().data).toEqual({ acct: 'user-1', head, entries: [] })
    expect(m.touch).toHaveBeenCalledWith('user-1', self)
    expect(m.read).toHaveBeenCalledWith('user-1', 2)
  })

  it('reads the log even when self is not a key', async () => {
    m.read.mockResolvedValue({ acct: 'user-1', head, entries: [] })
    for (const q of ['self=nope', 'self=a&self=b']) {
      const r = await app.inject({ method: 'GET', url: `/api/device-keys?since=0&${q}`, headers: auth })
      expect(r.statusCode).toBe(200)
      expect(r.json().data).toEqual({ acct: 'user-1', head, entries: [] })
    }
  })

  it('appends over the viewer channel, carrying a Harness session', async () => {
    m.auth.mockResolvedValue({ ...user, harnessSessionId: 'sess-9' })
    m.append.mockResolvedValue({ ok: true, head })
    const r = await app.inject({ method: 'POST', url: '/api/device-keys', headers: auth, payload: { entry: { v: 1 } } })
    expect(r.statusCode).toBe(200)
    expect(r.json().data).toEqual({ head })
    expect(m.append).toHaveBeenCalledWith('user-1', { v: 1 }, { kind: 'viewer', harnessSessionId: 'sess-9' })
  })

  it('answers a stale head with 409 and the head to rebuild on', async () => {
    m.append.mockResolvedValue({ ok: false, status: 409, code: 'STALE_HEAD', head })
    const r = await app.inject({ method: 'POST', url: '/api/device-keys', headers: auth, payload: { entry: {} } })
    expect(r.statusCode).toBe(409)
    expect(r.json()).toMatchObject({ success: false, error: { code: 'STALE_HEAD' }, data: { head } })
  })

  it('passes a refusal through with its code', async () => {
    m.append.mockResolvedValue({ ok: false, status: 403, code: 'WRONG_CHANNEL' })
    const r = await app.inject({ method: 'POST', url: '/api/device-keys', headers: auth, payload: { entry: {} } })
    expect(r.statusCode).toBe(403)
    expect(r.json().error.code).toBe('WRONG_CHANNEL')
  })

  it('says when each key was last seen, and fails — never "nothing seen" — when that cannot be read', async () => {
    m.seen.mockResolvedValue({ seen: { k: 5 }, since: 3 })
    const r = await app.inject({ method: 'GET', url: '/api/device-keys/seen', headers: auth })
    expect(r.json().data).toEqual({ seen: { k: 5 }, since: 3 })
    expect(m.seen).toHaveBeenCalledWith('user-1')
    m.seen.mockRejectedValue(new Error('redis down'))
    const down = await app.inject({ method: 'GET', url: '/api/device-keys/seen', headers: auth })
    expect(down.statusCode).toBe(503)
    expect(down.json().error.code).toBe('SEEN_UNAVAILABLE')
  })

  it('limits how often one account appends', async () => {
    m.auth.mockResolvedValue({ ...user, sub: 'user-busy' })
    m.append.mockResolvedValue({ ok: true, head })
    const codes: number[] = []
    for (let i = 0; i < 21; i++) codes.push((await app.inject({ method: 'POST', url: '/api/device-keys', headers: auth, payload: { entry: {} } })).statusCode)
    expect(codes.slice(0, 20).every((c) => c === 200)).toBe(true)
    expect(codes[20]).toBe(429)
  })

  it('refuses a body without an entry', async () => {
    const r = await app.inject({ method: 'POST', url: '/api/device-keys', headers: auth, payload: { other: 1 } })
    expect(r.statusCode).toBe(400)
    expect(m.append).not.toHaveBeenCalled()
  })

  describe('acknowledgements', () => {
    it('needs a sign-in', async () => {
      expect((await app.inject({ method: 'GET', url: '/api/device-keys/acks' })).statusCode).toBe(401)
      expect((await app.inject({ method: 'POST', url: '/api/device-keys/acks', payload: { ack: {} } })).statusCode).toBe(401)
      expect(m.appendAck).not.toHaveBeenCalled()
    })

    it('reads the caller\'s from an entry, uncached', async () => {
      m.readAcks.mockResolvedValue({ acks: [{ v: 1 }] })
      const r = await app.inject({ method: 'GET', url: '/api/device-keys/acks?since=4', headers: auth })
      expect(r.statusCode).toBe(200)
      expect(r.headers['cache-control']).toBe('no-store')
      expect(r.json().data).toEqual({ acks: [{ v: 1 }] })
      expect(m.readAcks).toHaveBeenCalledWith('user-1', 4)
      await app.inject({ method: 'GET', url: '/api/device-keys/acks', headers: auth })
      expect(m.readAcks).toHaveBeenLastCalledWith('user-1', 0)
      expect((await app.inject({ method: 'GET', url: '/api/device-keys/acks?since=-1', headers: auth })).statusCode).toBe(400)
    })

    it('keeps one for the caller\'s account, and says whether it was new', async () => {
      m.appendAck.mockResolvedValue({ ok: true, created: false })
      const r = await app.inject({ method: 'POST', url: '/api/device-keys/acks', headers: auth, payload: { ack: { v: 1 } } })
      expect(r.statusCode).toBe(200)
      expect(r.json().data).toEqual({ created: false })
      expect(m.appendAck).toHaveBeenCalledWith('user-1', { v: 1 })
    })

    it('passes a refusal through with its code', async () => {
      m.appendAck.mockResolvedValue({ ok: false, status: 400, code: 'SIGNER_NOT_ACTIVE' })
      const r = await app.inject({ method: 'POST', url: '/api/device-keys/acks', headers: auth, payload: { ack: {} } })
      expect(r.statusCode).toBe(400)
      expect(r.json().error.code).toBe('SIGNER_NOT_ACTIVE')
    })

    it('refuses a body without an acknowledgement, or with anything beside it', async () => {
      for (const payload of [{ other: 1 }, { ack: {}, extra: 1 }, { ack: 'x' }]) {
        expect((await app.inject({ method: 'POST', url: '/api/device-keys/acks', headers: auth, payload })).statusCode).toBe(400)
      }
      expect(m.appendAck).not.toHaveBeenCalled()
    })

    it('limits how often one account acknowledges, apart from its appends to the log', async () => {
      m.auth.mockResolvedValue({ ...user, sub: 'user-acking' })
      m.appendAck.mockResolvedValue({ ok: true, created: true })
      m.append.mockResolvedValue({ ok: true, head })
      const codes: number[] = []
      for (let i = 0; i < 21; i++) codes.push((await app.inject({ method: 'POST', url: '/api/device-keys/acks', headers: auth, payload: { ack: {} } })).statusCode)
      expect(codes.slice(0, 20).every((c) => c === 200)).toBe(true)
      expect(codes[20]).toBe(429)
      expect((await app.inject({ method: 'POST', url: '/api/device-keys', headers: auth, payload: { entry: {} } })).statusCode).toBe(200)
    })
  })
})

// Behind the REST gate (lib/teamSession.ts) these are refused already; the handlers refuse a team session themselves too,
// so a gate that ever let one through still could not add an app key or vouch for one.
describe('device key routes and a team session', () => {
  let app: FastifyInstance
  const team = { ...user, harnessSessionId: 'team-session', harnessSessionKind: 'team' as const, harnessComputerId: 'b'.repeat(32) }
  beforeEach(async () => {
    vi.resetAllMocks()
    app = Fastify(); app.setErrorHandler(errorHandler)
    app.addHook('onRequest', async request => { request.user = team })
    await app.register(deviceKeyRoutes); await app.ready()
  })
  afterEach(async () => { await app.close() })

  it('reads the log, but marks no key as seen', async () => {
    m.read.mockResolvedValue({ acct: 'user-1', head, entries: [] })
    const r = await app.inject({ method: 'GET', url: `/api/device-keys?since=0&self=${encodeURIComponent(Buffer.alloc(32, 1).toString('base64'))}` })
    expect(r.statusCode).toBe(200)
    expect(m.touch).not.toHaveBeenCalled()
  })

  it('never appends a key or an acknowledgement for it', async () => {
    for (const [url, payload] of [['/api/device-keys', { entry: { op: 'add' } }], ['/api/device-keys/acks', { ack: { pub: 'x' } }]] as const) {
      const r = await app.inject({ method: 'POST', url, payload })
      expect(r.statusCode).toBe(403)
      expect(r.json().error.code).toBe('TEAM_SESSION_LIMITED')
    }
    expect(m.append).not.toHaveBeenCalled()
    expect(m.appendAck).not.toHaveBeenCalled()
  })
})