garage44.eu/jeroen/midas

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

.. /web/src/lib/treetalk.test.ts

raw

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

import type {Goal, Round, Tech, TreeState} from '@/lib/api'
import {roundNews, treeText} from '@/lib/treetalk'

const tech = (id: string, extra: Partial<Tech> = {}): Tech => ({
    id, name: id, what: `what ${id} is`, who: '', differently: '', exists: '', size: 'M', evidence: [], needs: [], goals: ['g1'], repos: [],
    moves: ['map'], built: false, by: 'scout', at: 0, state: 'available', ...extra,
})
const goal = (id: string, extra: Partial<Goal> = {}): Goal => ({id, name: `goal ${id}`, statement: '', who: '', signs: '', never: [], parent: null, repos: ['acme/cartopolis'], by: 'p', at: 0, ...extra})
const round = (extra: Partial<Round> = {}): Round => ({id: 'r1', goal: 'g1', move: 'map', by: 'p', at: 100, state: 'done', ...extra})

test('the tree reads as its goal, its techs by state with ids, and where it heads', () => {
    const d: TreeState = {
        goals: [goal('g1', {statement: 'a weekly meeting in the world', never: ['ads']}), goal('g2', {parent: 'g1'})],
        techs: [tech('k1', {state: 'built', built: true}), tech('k2', {needs: ['k1']}), tech('k3', {state: 'fog'}), tech('k4', {state: 'dropped', dropped: {by: 'p', at: 0, why: 'not for us'}})],
        insights: [{id: 'i1', goal: 'g1', text: 'organisers plan on phones', about: null, evidence: [], move: 'person', by: 'p', at: 0, faded: false}],
        rounds: [round({focus: 'the phone'})],
        directions: [{id: 'd1', target: 'g1', name: 'goal g1', by: 'p', at: 0, at_once: 1, per_day: 4, paused: null, reached: null, runs: [], next: {tech: 'k2', why: 'soon', project: 'acme/app'}}],
        budget: {at_once: 3, per_day: 10, per_project: 1, going: 0, today: 0, week_cost: 0},
        moves: ['map'],
    }
    const t = treeText(d, 'g1')
    expect(t).toContain('g1 "goal g1" ← this one')
    expect(t).toContain('g2 "goal g2" (under g1)')
    expect(t).toContain('Never: ads')
    expect(t).toContain('Built (1):\n- k1 "k1"')
    expect(t).toContain('- k2 "k2" [M]: what k2 is Needs: k1.')
    expect(t).toContain('In the fog (a guess: Keep it or Not this) (1):\n- k3')
    expect(t).toContain('Not this: not for us')
    expect(t).toContain("- i1: organisers plan on phones (the person's own)")
    expect(t).toContain('1 going, in this order (the first starts first when the budget is short); together at most 3 at once, 10 a day and 1 at once in one project')
    expect(t).toContain('- d1: heading to "goal g1" (g1), on; at most 1 at once and 4 a day, the scout sent when the way runs out. Next: k2 (soon, in acme/app).')
    expect(t).toContain('(looking at: the phone)')
})

test('a tree with no goals says how to start one', () => {
    expect(treeText({goals: [], techs: [], insights: [], rounds: [], moves: []}, '')).toContain('no goals yet')
})

test("a round's news names its guesses, to keep or not", () => {
    const d: TreeState = {goals: [goal('g1')], techs: [tech('k5', {state: 'fog', at: 150}), tech('k6', {state: 'fog', at: 50})], insights: [], rounds: [], moves: []}
    const news = roundNews(d, round({summary: 'two things missing', counts: {added: 1, confirmed: 0, built: 0, insights: 0}}))
    expect(news).toContain('round r1 is done: two things missing It added 1 techs')
    expect(news).toContain('- k5 "k5": what k5 is')
    expect(news).not.toContain('k6')
    expect(roundNews(d, round({state: 'failed', error: 'no runner'}))).toContain('failed: no runner')
})