garage44.eu/jeroen/midas

logclone: git clone https://garage44.eu/jeroen/midas.git

.. /web/src/lib/nodeline.test.ts

raw

import {expect, test} from 'bun:test'

import {nodeLine, nodeStep, nodeStepWords, wantsWords} from '@/lib/nodeline'
import type {BoardCard, LogItem} from '@/lib/stream'
import type {Lease} from '@/lib/types'

const card = (more: Partial<BoardCard> = {}): BoardCard =>
    ({kind: 'card', session: 'session-s1', label: null, branch: null, link: null, stage: 'working', since: 100, node: null, position: null, badges: {overlaps: 0, notes: 0, decisions: []}, ...more}) as BoardCard
const lease = (renewed_at: number) => ({owner: 'session-s1', claimed_at: 0, renewed_at}) as Lease
const bash = (command: string, output: string | undefined, status = 'completed'): LogItem =>
    ({n: 1, at: 0, kind: 'tool', id: 't', title: `Bash: ${command}`, input: {command}, output, status}) as LogItem

const NODES = [
    {id: 'vps-1', machine: 'vps'},
    {id: 'mac-1', machine: 'mini'},
    {id: 'mac-2', machine: 'mini'},
]

test('on a node: building while a command runs there, else idle since its lease was last renewed', () => {
    expect(nodeLine(card({stage: 'on_node', node: 'mac-1', busy: true}), {'mac-1': lease(500)}, 1000)).toMatchObject({kind: 'on', node: 'mac-1', busy: true, idle_s: 0})
    expect(nodeLine(card({stage: 'on_node', node: 'vps-1'}), {'vps-1': lease(520)}, 1000)).toMatchObject({kind: 'on', busy: false, idle_s: 480, quiet: null})
})

test('a command that runs but does nothing is quiet, not building', () => {
    const quiet = {command: 'cargo clippy … | grep … | head', quiet_s: 1200}
    expect(nodeLine(card({stage: 'on_node', node: 'mac-2', busy: true, quiet}), {'mac-2': lease(500)}, 1000)).toMatchObject({kind: 'on', busy: true, quiet})
    // A reading from before the command ended says nothing about now.
    expect(nodeLine(card({stage: 'on_node', node: 'mac-2', busy: false, quiet}), {'mac-2': lease(500)}, 1000)).toMatchObject({quiet: null})
})

test('in line for a node, with who holds those it may take; its gate waiting is To live, not this', () => {
    const held = [{node: 'mac-1', by: 'session-s2', since: 280}]
    expect(nodeLine(card({stage: 'waiting_for_node', node_wait: {position: 2, wants: 'mini', gate: false, since: 300, held}}), {}, 1000)).toEqual({kind: 'waits', wants: 'mini', position: 2, since: 300, held})
    expect(nodeLine(card({stage: 'waiting_to_land', node_wait: {position: 1, wants: 'any', gate: true, since: 300}}), {}, 1000)).toBeNull()
    // The gate's node is not the session's own.
    expect(nodeLine(card({stage: 'landing', node: 'vps-1', node_gate: true}), {}, 1000)).toBeNull()
    expect(nodeLine(card(), {}, 1000)).toBeNull()
})

test('what it waits for, as one says it', () => {
    expect(wantsWords('any', NODES)).toBe('a node')
    expect(wantsWords('gpu', NODES)).toBe('a GPU node')
    expect(wantsWords('mac-2', NODES)).toBe('mac-2')
    expect(wantsWords('mini', NODES)).toBe('a mac')
    expect(wantsWords('deck', [{id: 'steamdeck', machine: 'deck'}])).toBe('a node on deck')
})

test('take, wait and release on their own are node steps; a command that does more stays a tool call', () => {
    expect(nodeStep(bash('cd /w/s545/cartopolis && midas take 2>&1 | tail -3', "mac-1 follows this worktree (35c59305): every ssh to it sends your latest edits first\nrun there:  ssh mac-1 'cargo test …'   ·   give it back: midas release\nmac-1"))).toMatchObject({verb: 'take', state: 'took', node: 'mac-1'})
    expect(nodeStep(bash('midas take gpu', 'took deck-1\n'))).toMatchObject({state: 'took', node: 'deck-1'})
    expect(nodeStep(bash('env -u MIDAS_OWNER midas take mini', 'took mac-2'))?.node).toBe('mac-2')
    expect(nodeStep(bash('midas release 2>&1 | tail -1', 'released mac-2'))).toMatchObject({verb: 'release', state: 'released', node: 'mac-2'})
    expect(nodeStep(bash('git commit -qm x && midas release', 'released mac-1'))).toBeNull()
    expect(nodeStep(bash('git fetch -q origin; midas take 2>&1 | tail -3', 'took mac-1'))).toBeNull()
    expect(nodeStep(bash('midas status', ''))).toBeNull()
})

test('a take with no node free keeps its place; one in the background or running says so', () => {
    const queued = nodeStep(bash('midas take', 'no node free yet (mac-1 held, mac-2 held); you are still in line — a mini node, 2nd in line: mac-1 held by session-s2 for 12m.\nKeep your place with `midas wait` within two minutes (run it in the background).'))
    expect(queued).toMatchObject({state: 'queued', place: '2nd in line'})
    expect(nodeStepWords(queued!)).toBe('No node free: 2nd in line, keeps its place')
    expect(nodeStep(bash('midas wait 2>&1 | tail -3', 'Command running in background with ID: bv4kzhxfr. Output is being written to: /tmp/x.'))).toMatchObject({verb: 'wait', state: 'background'})
    expect(nodeStep(bash('midas take', undefined, 'in_progress'))?.state).toBe('running')
    expect(nodeStepWords(nodeStep(bash('midas take --node mac-2 --next', 'mac-2 is yours next: session-s2 has been told, and keeps it until they release it'))!)).toBe('mac-2 is its next: its holder has been told')
})

test('yield: gone at once when nothing ran, else when its build ends', () => {
    expect(nodeStep(bash('midas yield', 'mac-1 released: nothing was running on it'))).toMatchObject({verb: 'release', state: 'released', node: 'mac-1'})
    expect(nodeStepWords(nodeStep(bash('midas yield', 'mac-1 goes back when your current session on it ends'))!)).toBe('Gives mac-1 back when its build there ends')
})

test('the MCP tools are node steps too', () => {
    const t = {n: 1, at: 0, kind: 'tool', id: 't', title: 'mcp__midas__node_release', input: {}, output: 'released vps-1', status: 'completed'} as LogItem
    expect(nodeStep(t)).toMatchObject({verb: 'release', state: 'released', node: 'vps-1'})
})