import {expect, test} from 'bun:test' import {activity} from '@/lib/activity' import type {Hosted, LogItem} from '@/lib/stream' const s = (over: Partial = {}) => ({id: 's1', status: 'working', updated: 0, ...over}) as Hosted let n = 0 const it = (at: number, kind: LogItem['kind'], more: Partial = {}): LogItem => ({n: n++, at, kind, ...more}) test('a session waiting on what it left in the background says so, not that it thinks', () => { // s503 on 2026-10-29: it started the landing in the background and ended its turn. const log = [ it(100, 'tool', {id: 't1', title: 'Bash', input: {command: 'land'}}), it(100, 'background', {tasks: [{id: 'b0j6', kind: 'local_bash', description: '~/.claude/skills/finish-branch/land 2>&1 | tail -30'}], turn: false}), it(100, 'tool_update', {id: 't1', status: 'completed'}), it(102, 'agent', {text: 'Het landen loopt nu.'}), it(102, 'usage'), it(102, 'turn_end'), ] expect(activity(log, s({expect: {b0j6: 300}}))).toEqual({text: 'In the background: ~/.claude/skills/finish-branch/land 2>&1 | tail -30', since: 100, typical: 300}) // Woken by it: its own turn again. const woke = [...log, it(160, 'background', {tasks: [], turn: true})] expect(activity(woke, s())?.text).toBe('Thinking') }) test('a tool is timed from its start, thinking from the last thing that happened, writing from its first words', () => { const log = [it(10, 'user', {text: 'go'}), it(20, 'tool', {id: 't', title: 'Bash', input: {description: 'Run the tests'}})] expect(activity(log, s({expect: {t: 240}}))).toEqual({text: 'Run the tests', since: 20, typical: 240}) log.push(it(80, 'tool_update', {id: 't', status: 'completed'})) expect(activity(log, s())).toEqual({text: 'Thinking', since: 80, typical: null}) log.push(it(90, 'agent', {text: 'All'}), it(91, 'agent', {text: ' pass.'})) expect(activity(log, s())).toEqual({text: 'Writing', since: 90, typical: null}) }) test('waiting on a deploy, it names the step and times that step', () => { const steps = [ {name: 'get a node', state: 'done' as const, seconds: 12}, {name: 'bun run build', state: 'now' as const, since: 500, typical: 90}, {name: 'publish', state: 'todo' as const, typical: 6}, ] const d = {deploy: 'x', target: 'web', sha: 'abc', since: '2026-10-29T10:00:00Z', phase: 'building on vps-1', typical: 200, steps} expect(activity([], s({status: 'waiting', deploys: [d]}))).toEqual({text: 'web deploy, step 2 of 3: bun run build', since: 500, typical: 90}) expect(activity([], s({status: 'waiting'}))).toBeNull() })