import {describe, expect, test} from 'bun:test' import type {Goal, Round, Tech, TreeState} from '@/lib/api' import {CARD_W, branches, GAP_X, depths, evidenceNames, goalsUnder, layout, frontier, lineOf, looksOf, moveTime, pathOf, progress, history, seenBy, techsOf, themes, wayOf} from '@/lib/tree' const tech = (id: string, extra: Partial = {}): Tech => ({ id, name: id, what: '', 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, parent: string | null = null): Goal => ({id, name: id, statement: '', who: '', signs: '', never: [], parent, repos: [], by: 'p', at: 0}) const tree = (techs: Tech[], goals = [goal('g1')]): TreeState => ({goals, techs, insights: [], rounds: [], moves: ['map', 'harvest']}) describe('the research tree', () => { test('with themes the tree is drawn in lanes, one per theme, each with room to look further', () => { const techs = [tech('a', {state: 'built'}), tech('b', {needs: ['a']}), tech('c'), tech('d', {state: 'built'})] const band = (t: Tech) => ({a: 0, b: 0, c: 1, d: 1})[t.id as 'a'] ?? -1 const l = layout(techs, band) expect(l.lanes.map((x) => x.band)).toEqual([0, 1]) const lane = (id: string) => l.lanes.findIndex((x) => { const c = l.cards.find((y) => y.tech.id === id)! return c.y >= x.y && c.y < x.y + x.h }) expect([lane('a'), lane('b'), lane('c'), lane('d')]).toEqual([0, 0, 1, 1]) // Lane 0 is open at b (column 1), so it looks further in column 2; lane 1 at c, in column 1. expect(l.ghosts.map((g) => [g.band, g.x])).toEqual([[0, 2 * (CARD_W + GAP_X)], [1, CARD_W + GAP_X]]) expect(l.goal.x).toBe(3 * (CARD_W + GAP_X)) }) test('a short tree is spread over the height the screen has, its lanes alike, its cards no further apart than they read', () => { const techs = [tech('a', {state: 'built'}), tech('b', {state: 'built'}), tech('c', {needs: ['a']}), tech('d')] const band = (t: Tech) => ({a: 0, b: 0, c: 0, d: 1})[t.id as 'a'] ?? -1 const tight = layout(techs, band) const filled = layout(techs, band, 900) expect(tight.height).toBeLessThan(400) expect(filled.height).toBe(900) expect(filled.lanes[1]!.y).toBeGreaterThan(tight.lanes[1]!.y) const y = (l: typeof tight, id: string) => l.cards.find((c) => c.tech.id === id)!.y const gap = y(filled, 'b') - y(filled, 'a') expect(gap).toBeGreaterThan(y(tight, 'b') - y(tight, 'a')) expect(gap).toBeLessThanOrEqual(58 + 10 + 36) // Every card stays inside its lane. for (const c of filled.cards) expect(filled.lanes.some((x) => c.y >= x.y && c.y + 58 <= x.y + x.h)).toBe(true) // Without lanes, the same: no shorter than asked. expect(layout(techs, undefined, 700).height).toBe(700) expect(layout(techs, undefined, 10).height).toBe(layout(techs).height) }) test('the file the scout reads lights the tech whose evidence names it, or a folder it is in', () => { const calls = tech('k1', {evidence: ['crates/calls/src/sfu.rs:40 forwards video', 'session s612: "screen share next"']}) const cal = tech('k2', {evidence: ['the calendar in crates/cartopolis/src/systems/gui/']}) const none = tech('k3', {evidence: ['README.md']}) expect(evidenceNames(calls, 'crates/calls/src/sfu.rs')).toBe(true) expect(evidenceNames(calls, 'crates/calls/src/lib.rs')).toBe(false) expect(evidenceNames(cal, 'crates/cartopolis/src/systems/gui/calendar.rs')).toBe(true) expect(evidenceNames(none, 'README.md')).toBe(false) expect(evidenceNames(none, 'docs/README.md')).toBe(false) }) test("a look gathers its moves and what they changed; a round before looks is a look of its own", () => { const r = (id: string, extra: Partial): Round => ({id, goal: 'g1', move: 'map', by: 'p', at: 1, state: 'done', ...extra}) const looks = looksOf([ r('r1', {}), r('r2', {look: 'r2', changed: {added: ['k1'], confirmed: [], built: [], drawn: [], insights: []}, counts: {added: 1, confirmed: 0, built: 0, insights: 0}}), r('r3', {look: 'r2', move: 'harvest', state: 'asked', changed: undefined}), ]) expect(looks.map((l) => l.id)).toEqual(['r1', 'r2']) expect(looks[1]!.going).toBe(true) expect(looks[1]!.changed.added).toEqual(['k1']) expect(looks[1]!.counts.added).toBe(1) expect(moveTime([r('a', {taken_at: 0, ended: 100}), r('b', {taken_at: 0, ended: 300}), r('c', {taken_at: 0, ended: 200})], 'map')).toBe(200) }) test('“Seen it” sticks: the hub records the page as web:', () => { const [l] = looksOf([{id: 'r1', goal: 'g1', move: 'map', by: 'p', at: 1, state: 'done', seen: ['web:ana']}]) expect(seenBy(l!, 'ana')).toBe(true) expect(seenBy(l!, 'jo')).toBe(false) expect(seenBy({...l!, seen: ['ana']}, 'ana')).toBe(true) }) test('a tech sits one row below the deepest of what it needs', () => { const d = depths([tech('a', {state: 'built'}), tech('b', {needs: ['a']}), tech('c', {needs: ['a', 'b']}), tech('d')]) expect([d.get('a'), d.get('b'), d.get('c'), d.get('d')]).toEqual([0, 1, 2, 0]) }) test('a guess with nothing before it stands by the goal, not among what is built', () => { const d = depths([tech('a', {state: 'built'}), tech('b', {needs: ['a']}), tech('f', {state: 'fog'})]) expect(d.get('f')).toBe(1) }) test('a card sits beside what it needs, so the lines do not cross', () => { const l = layout([tech('a', {state: 'built'}), tech('b', {state: 'built'}), tech('c', {needs: ['b'], name: 'a-first'}), tech('d', {needs: ['a'], name: 'z-last'})]) const y = (id: string) => l.cards.find((c) => c.tech.id === id)!.y expect(Math.sign(y('a') - y('b'))).toBe(Math.sign(y('d') - y('c'))) }) test('on a phone each tech stands under the deepest of what it needs, and names the other branch', () => { const b = branches([ tech('a', {state: 'built'}), tech('b', {needs: ['a']}), tech('x', {state: 'built'}), tech('c', {needs: ['x', 'b', 'a']}), tech('o', {needs: ['p']}), tech('p', {needs: ['o']}), ]) const a = b.find((r) => r.tech.id === 'a')! const c = a.children[0]!.children[0]! expect(c.tech.id).toBe('c') expect(c.also.map((t) => t.id)).toEqual(['x']) expect(b.map((r) => r.tech.id).sort()).toEqual(['a', 'o', 'p', 'x']) }) test('each branch wears its own colour, kept in the order its foundation was found', () => { const t = themes([tech('b', {state: 'built', at: 2}), tech('a', {state: 'built', at: 1}), tech('c', {needs: ['b', 'a']}), tech('f', {state: 'fog'})]) expect(t.list.map((x) => [x.root, x.slot, x.count])).toEqual([['a', 0, 1], ['b', 1, 2]]) expect([t.of.get('c'), t.of.get('f')]).toEqual([1, -1]) }) test('the goals under a goal are its themes once it has them', () => { const goals = [goal('g1'), goal('g3', 'g1'), goal('g2', 'g1'), goal('g4', 'g2')] goals[1]!.at = 2 goals[2]!.at = 1 const t = themes([tech('a', {goals: ['g1', 'g4']}), tech('b', {goals: ['g3']}), tech('c')], goals, 'g1') expect(t.list.map((x) => [x.goal, x.slot, x.count])).toEqual([['g2', 0, 1], ['g3', 1, 1]]) expect([t.of.get('a'), t.of.get('b'), t.of.get('c')]).toEqual([0, 1, -1]) }) test('the frontier is what is open and the built techs it stands on', () => { const f = frontier([tech('a', {state: 'built'}), tech('b', {state: 'built', needs: ['a']}), tech('c', {state: 'fog', needs: ['b']})]) expect(f.map((x) => x.id)).toEqual(['b', 'c']) }) test('a line of techs is what each needs back to the start and what needs it onward', () => { const ts = [tech('a', {state: 'built'}), tech('b', {needs: ['a']}), tech('c', {needs: ['b']}), tech('x', {state: 'built'}), tech('y', {needs: ['x']})] expect([...lineOf(ts, 'b')].sort()).toEqual(['a', 'b', 'c']) }) test('a circle in the needs does not hang the page', () => { const d = depths([tech('a', {needs: ['b']}), tech('b', {needs: ['a']})]) expect(d.size).toBe(2) }) test('a goal draws the techs of the goals under it, and what they need from elsewhere', () => { const t = tree([tech('a', {goals: ['g2']}), tech('b', {goals: ['g9'], state: 'built'}), tech('c', {goals: ['g1'], needs: ['b']}), tech('x', {goals: ['g9']})], [goal('g1'), goal('g2', 'g1'), goal('g9')]) expect(goalsUnder(t.goals, 'g1')).toEqual(['g1', 'g2']) expect(techsOf(t, 'g1').map((x) => x.id).sort()).toEqual(['a', 'b', 'c']) }) test('dropped techs stay off the tree unless asked for', () => { const t = tree([tech('a', {state: 'dropped'}), tech('b')]) expect(techsOf(t, 'g1').map((x) => x.id)).toEqual(['b']) expect(techsOf(t, 'g1', true).length).toBe(2) }) test('lines run from each need to what needs it, and from the open ends to the goal', () => { const l = layout([tech('a', {state: 'built'}), tech('b', {needs: ['a']}), tech('f', {state: 'fog'}), tech('z', {state: 'built'})]) expect(l.edges.filter((e) => e.b === 'goal').map((e) => e.a).sort()).toEqual(['b', 'f']) expect(l.edges.find((e) => e.a === 'a' && e.b === 'b')).toMatchObject({kind: 'path', from: {x: CARD_W}, to: {x: CARD_W + GAP_X}}) expect(l.edges.find((e) => e.kind === 'fog')).toBeTruthy() expect(l.goal.x).toBe(2 * (CARD_W + GAP_X)) }) test('progress counts built of known, and the fog apart', () => { expect(progress([tech('a', {state: 'built'}), tech('b', {size: 'S'}), tech('c', {state: 'fog'}), tech('d', {state: 'dropped'})])) .toEqual({built: 1, known: 2, fog: 1, sizes: {S: 1, M: 0, L: 0}}) }) }) describe('a direction on the tree', () => { test('its way is the target and all it needs, or a goal with the goals under it', () => { const t: TreeState = { goals: [goal('g1'), goal('g2', 'g1')], insights: [], rounds: [], moves: [], techs: [tech('a', {state: 'built'}), tech('b', {needs: ['a'], goals: ['g2']}), tech('c', {needs: ['b'], goals: ['g9']}), tech('x', {goals: ['g9']}), tech('d', {state: 'dropped'})], } expect([...pathOf(t, 'c')].sort()).toEqual(['a', 'b', 'c']) expect([...pathOf(t, 'g1')].sort()).toEqual(['a', 'b']) }) }) describe('the way to a goal', () => { test('numbered in the order the hub starts them: being built, then what is ready, small first, guesses last', () => { const way = wayOf([ tech('k1', {state: 'built'}), tech('k2', {needs: ['k1'], size: 'L'}), tech('k3', {needs: ['k1'], size: 'S'}), tech('k4', {needs: ['k3', 'k9'], state: 'locked'}), tech('k5', {state: 'fog', size: 'S'}), tech('k6', {state: 'researching', needs: ['k1']}), tech('k9', {state: 'researching'}), ]) expect(way.map((s) => s.tech.id)).toEqual(['k6', 'k9', 'k3', 'k4', 'k2', 'k5']) expect(way.find((s) => s.tech.id === 'k4')!.after).toEqual([2, 3]) expect(way.find((s) => s.tech.id === 'k3')!.after).toEqual([]) }) test('a circle in the needs still numbers every tech once', () => { const way = wayOf([tech('k1', {needs: ['k2'], state: 'locked'}), tech('k2', {needs: ['k1'], state: 'locked'})]) expect(way.map((s) => s.n)).toEqual([1, 2]) }) }) describe('how a goal went', () => { test('techs built a week, the sessions newest first, the median time and what they cost', () => { const now = 100 * 7 * 86400 const h = 3600 const techs = [ tech('a', {state: 'built', built_at: now - 3600, sessions: [{id: 's1', outcome: 'landed', at: now - 10 * h, ended: now - 8 * h, cost: 2.5}]}), tech('b', {state: 'built', built_by: 'research', built_at: now - 8 * 86400, sessions: [{id: 's2', outcome: 'dropped', at: now - 9 * 86400, ended: now - 9 * 86400 + 4 * h, cost: 1}]}), tech('c', {sessions: [{id: 's3', outcome: null, at: now - h, ended: null, cost: null}]}), tech('d', {state: 'built', built_at: now - 30 * 7 * 86400}), tech('e', {state: 'built', built_by: 'scout', built_at: now - 3600}), ] const x = history(techs, now, 4) expect(x.built).toEqual([0, 0, 1, 1]) expect(x.found).toEqual([0, 0, 0, 1]) expect(x.runs.map((r) => r.id)).toEqual(['s3', 's1', 's2']) expect([x.landed, x.ended]).toEqual([1, 2]) expect(x.hours).toBe(2) expect([x.cost, x.costed]).toEqual([3.5, 2]) }) })