git clone https://garage44.eu/jeroen/midas.git.. /web/src/lib/status.test.ts
import {expect, test} from 'bun:test'
import {asksPerson, place, stateOf, turnOf} from '@/lib/status'
import type {Hosted} from '@/lib/stream'
const h = (more: Partial<Hosted>) => ({kind: 'hosted', id: 's1', title: '', status: 'working', branch: '', owner: '', by: '', created: 0, updated: 0, base: '', ...more}) as Hosted
test("a session says where it runs: an editor's machine, the hub's runner, or nowhere", () => {
expect(place(h({local: {client: 'Zed', machine: 'mini', cwd: '/x'}}))?.label).toBe('Zed · mini')
expect(place(h({}))?.label).toBe('midas')
const handed = place(h({handed: {from: {client: 'Zed', machine: 'mini', cwd: null}, at: 0, sha: ''}}))
expect([handed?.label, handed?.title]).toEqual(['midas', "runs on the hub's runner, handed over from Zed on mini"])
expect(place(h({imported: {key: '', from: '', cwd: '', complete: true, at: 0}}))).toBeNull()
})
test('a session works, asks or is idle; only asking is the person\'s move, however recent the turn', () => {
expect(['queued', 'starting', 'working', 'needs_permission', 'waiting', 'stopped', 'failed'].map((status) => stateOf(h({status: status as Hosted['status']}))))
.toEqual(['working', 'working', 'working', 'asks', 'idle', 'over', 'over'])
const t = 10 * 86400
expect(turnOf(h({status: 'waiting', active: t - 60, updated: t - 60}), t)).toEqual({yours: false, idle_s: 60})
expect(turnOf(h({status: 'needs_permission', active: t - 60}), t).yours).toBe(true)
// Its runner started again a minute ago, its last turn two days ago: idle two days.
expect(turnOf(h({status: 'waiting', active: t - 2 * 86400, updated: t - 60}), t).idle_s).toBe(2 * 86400)
// A hub from before `active`: its status clock stands in.
expect(turnOf(h({status: 'waiting', updated: t - 120}), t).idle_s).toBe(120)
})
test('on autopilot, a session asks the person only what the autopilot left to them', () => {
expect(asksPerson(h({status: 'needs_permission'}))).toBe(true)
expect(asksPerson(h({status: 'needs_permission', autopilot: {on: true, state: 'deciding'}}))).toBe(false)
expect(asksPerson(h({status: 'needs_permission', autopilot: {on: true, state: 'left'}}))).toBe(true)
// Left a "what next" to them: it asks though its turn is over.
expect(asksPerson(h({status: 'waiting', autopilot: {on: true, state: 'left'}}))).toBe(true)
expect(asksPerson(h({status: 'needs_permission', autopilot: {on: false}}))).toBe(true)
})