garage44.eu/jeroen/midas

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

.. /web/src/lib/discuss.test.ts

raw

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

import {aboutLabel, conversation, designs, instructions, openAsks, placeOf, report, setVoiceOf, toolsFor, voiceOf} from '@/lib/discuss'
import type {LogItem} from '@/lib/stream'

test('the coach hears its subject, the brief so far and the other sessions', () => {
    const text = instructions({kind: 'new'}, 'coach', 'They are designing a new session.', '# Dark mode', ['s3: "Brotli" (waiting)'])
    expect(text).toContain('the operator who gets work started')
    expect(text).toContain('ask_code what already does it')
    expect(text).toContain('A question (what is there, how something works, how work stands) is not yet a task')
    expect(text).not.toContain('design coach')
    expect(text).toContain('They are designing a new session.')
    expect(text).toContain('# Dark mode')
    expect(text).toContain('- s3: "Brotli" (waiting)')
})

test('an empty brief says so; no brief for Midas, and no list without sessions', () => {
    expect(instructions({kind: 'new'}, 'coach', '', '  ', [])).toContain('(empty')
    const plain = instructions({kind: 'board'}, 'midas', 'The board now.', 'ignored', [])
    expect(plain).not.toContain('write_brief')
    expect(plain).not.toContain('Sessions in this workspace')
})

test('it never hangs up on its own, and sending stays the person\'s call', () => {
    const text = instructions({kind: 'free'}, 'midas', '', '', [])
    expect(text).toContain('Never end it on your own')
    expect(text).toContain('only when they say to')
})

test('the coach designs and sees the work in progress; Midas manages; every one can be ended', () => {
    const coach = toolsFor({kind: 'new'}, 'coach')
    expect(coach).toContain('write_brief')
    expect(coach).toContain('read_board')
    // It builds along with the session it started: steers it, answers it, lands it on the person's word.
    for (const t of ['send_to_session', 'answer_question', 'land_session']) expect(coach).toContain(t)
    for (const t of ['deploy', 'stop_session', 'decide']) expect(coach).not.toContain(t)
    for (const about of [{kind: 'session', id: 's1'}, {kind: 'board'}, {kind: 'free'}] as const) {
        const midas = toolsFor(about, 'midas')
        expect(midas).not.toContain('write_brief')
        for (const t of ['answer_permission', 'answer_question', 'decide', 'stop_session', 'land_session', 'deploy']) expect(midas).toContain(t)
    }
    expect(toolsFor({kind: 'board'}, 'midas')).not.toContain('delete_session')
    for (const t of [coach, toolsFor({kind: 'board'}, 'midas'), toolsFor({kind: 'free'}, 'midas')]) expect(t).toContain('end_discussion')
})

test('Midas creates sessions and changes nodes; the coach does neither', () => {
    const midas = toolsFor({kind: 'board'}, 'midas')
    for (const t of ['create_session', 'send_to_session', 'drain_node', 'undrain_node', 'release_node']) expect(midas).toContain(t)
    const coach = toolsFor({kind: 'new'}, 'coach')
    for (const t of ['create_session', 'drain_node', 'release_node']) expect(coach).not.toContain(t)
})

test('every role but Midas designs, each in its own manner, and the operator stays the default', () => {
    for (const p of ['designer', 'executive', 'user'] as const) {
        expect(designs(p)).toBe(true)
        expect(toolsFor({kind: 'new'}, p)).toEqual(toolsFor({kind: 'new'}, 'coach'))
        const text = instructions({kind: 'new'}, p, '', '# Dark mode', [])
        expect(text).toContain('Keep the design current with write_brief')
        expect(text).not.toContain('Work as an operator')
    }
    expect(designs('midas')).toBe(false)
    expect(instructions({kind: 'new'}, 'executive', '', '', [])).toContain('CTO and CEO')
    expect(instructions({kind: 'new'}, 'user', '', '', [])).toContain('You play a customer')
    expect(instructions({kind: 'new'}, 'designer', '', '', [])).toContain('design coach')
    expect(aboutLabel({kind: 'new'}, 'coach')).toBe('Designing a new session')
    expect(aboutLabel({kind: 'new'}, 'executive')).toBe('Designing a new session with the CTO / CEO')
    expect(aboutLabel({kind: 'session', id: 's3'}, 'user')).toBe("Designing s3's next step with the customer")
})

test("a role's voice is the woman's until a man's is picked for it", () => {
    const store = new Map<string, string>()
    globalThis.localStorage = {getItem: (k: string) => store.get(k) ?? null, setItem: (k: string, v: string) => void store.set(k, v)} as Storage
    expect(voiceOf('executive')).toBe('female')
    setVoiceOf('executive', 'male')
    expect(voiceOf('executive')).toBe('male')
    expect(voiceOf('user')).toBe('female')
})

test('the page goes where Midas looks or acts', () => {
    expect(placeOf('read_board', {})).toBe('')
    expect(placeOf('read_session', {session: 's12'})).toBe('sessions/s12')
    expect(placeOf('send_to_session', {session: 's12', message: 'go'})).toBe('sessions/s12')
    expect(placeOf('land_session', {session: 's3', finish: true})).toBe('sessions/s3')
    expect(placeOf('create_session', {name: 'x', brief: 'y'}, 's40')).toBe('sessions/s40')
    expect(placeOf('create_session', {name: 'x', brief: 'y'})).toBeNull()
    for (const t of ['drain_node', 'undrain_node', 'release_node', 'deploy']) expect(placeOf(t, {node: 'vps-1', target: 'sim'})).toBe('workspace')
    expect(placeOf('find_sessions', {query: 'tiles'})).toBeNull()
    expect(placeOf('read_session', {session: '../x'})).toBeNull()
})

test('it answers short, and neither flatters nor fishes for more', () => {
    for (const persona of ['midas', 'coach'] as const) {
        const text = instructions({kind: 'free'}, persona, '', '', [])
        expect(text).toContain('one or two short sentences')
        expect(text).toContain('No compliments')
        expect(text).toContain('never end a turn by offering more')
    }
    expect(instructions({kind: 'new'}, 'coach', '', '', [])).not.toContain('Credit what is good')
})

test("a session's open requests and questions read with the ids to answer them by", () => {
    const at = 0
    const items: LogItem[] = [
        {n: 1, at, kind: 'permission', request: 'r1', input: {description: 'run cargo test'}, options: [{optionId: 'allow', name: 'Allow', kind: 'allow_once'}]},
        {n: 2, at, kind: 'permission', request: 'r0', title: 'edit a file', options: []},
        {n: 3, at, kind: 'permission_answered', request: 'r0'},
        {n: 4, at, kind: 'question', request: 'q1', message: 'Shall it land?', schema: {type: 'object', properties: {choice: {type: 'string', oneOf: [{const: 'land', title: 'Land it and finish'}, {const: 'keep'}]}}}},
    ]
    const asks = openAsks(items)
    expect(asks).toHaveLength(2)
    expect(asks[0]).toContain('Permission request r1: may it run cargo test? Options: "Allow" (option allow)')
    expect(asks[1]).toContain('Question q1: Shall it land?')
    expect(asks[1]).toContain('field choice: one of "land" (Land it and finish), "keep"')
})

test("a session's conversation reads as turns, its answer's pieces joined, clipped from the start", () => {
    const at = 0
    const items: LogItem[] = [
        {n: 1, at, kind: 'user', text: 'fix the build'},
        {n: 2, at, kind: 'agent', text: 'Looking '},
        {n: 3, at, kind: 'agent', text: 'at it.'},
        {n: 4, at, kind: 'tool', title: 'cargo check'},
    ]
    expect(conversation(items)).toBe('[the person] fix the build\n[agent] Looking at it.\n[ran] cargo check')
    expect(conversation(items, 10).startsWith('…')).toBe(true)
})

test("a followed session's news: its turn's last words, its question, its pictures; nothing while it works", () => {
    const at = 0
    const working: LogItem[] = [{n: 5, at, kind: 'tool', title: 'cargo test'}]
    expect(report('s7', working, working)).toBeNull()
    const turn: LogItem[] = [
        {n: 6, at, kind: 'agent', text: 'The header is done.'},
        {n: 7, at, kind: 'media', file: '1-shot.png', mime: 'image/png', caption: 'the new header'},
        {n: 8, at, kind: 'question', request: 'q2', message: 'Shall it land?', schema: {type: 'object', properties: {}}},
        {n: 9, at, kind: 'turn_end'},
    ]
    const news = report('s7', turn, [...working, ...turn])!
    expect(news.tell).toContain('News from session s7')
    expect(news.tell).toContain('Its last words: The header is done.')
    expect(news.tell).toContain('"the new header"')
    expect(news.tell).toContain('Question q2: Shall it land?')
    expect(news.tell).toContain('answer it only with what they choose')
    expect(news.images).toEqual(['1-shot.png'])
    // A question answered before it was read is not news.
    const answered: LogItem[] = [...turn.slice(2, 3), {n: 10, at, kind: 'answered', request: 'q2'}]
    expect(report('s7', answered, answered)).toBeNull()
})

test('every conversation follows its sessions; the operator asks only what it needs and adds no scope', () => {
    for (const persona of ['midas', 'coach'] as const) expect(instructions({kind: 'free'}, persona, '', '', [])).toContain('their news comes to you by itself')
    const coach = instructions({kind: 'new'}, 'coach', '', '', [])
    expect(coach).toContain('Work as an operator')
    expect(coach).toContain('no acceptance criteria')
    expect(coach).not.toContain('one point per turn')
    expect(coach).not.toContain('offer ideas they had not mentioned')
    expect(coach).toContain('"Decided" list')
    expect(coach).toContain('Pictures come into the conversation')
    expect(instructions({kind: 'board'}, 'midas', '', '', [])).not.toContain('Work as an operator')
})

test('on the research tree Midas reads it, changes it and sends the scout; the coach only reads it', () => {
    const tree = toolsFor({kind: 'tree'}, 'midas')
    for (const t of ['read_tree', 'change_tree', 'scout_tree', 'ask_code', 'end_discussion']) expect(tree).toContain(t)
    const coach = toolsFor({kind: 'new'}, 'coach')
    expect(coach).toContain('read_tree')
    for (const t of ['change_tree', 'scout_tree']) expect(coach).not.toContain(t)
    for (const t of ['read_tree', 'change_tree', 'scout_tree']) expect(placeOf(t, {goal: 'g1'})).toBe('research')
    expect(aboutLabel({kind: 'tree'}, 'midas')).toBe('About the research tree')
})