garage44.eu/jeroen/midas

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

.. /web/src/lib/landing.test.ts

raw

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

import {groupOf} from '@/lib/groups'
import {deployAt, deployOf, journey, queue, shipOf} from '@/lib/landing'
import type {BoardCard, Hosted} from '@/lib/stream'
import type {Ship} from '@/lib/types'

const card = (session: string, stage: BoardCard['stage'], more: Partial<BoardCard> = {}): BoardCard =>
    ({kind: 'card', session, label: session, branch: null, link: null, stage, since: 100, node: null, position: null, badges: {overlaps: 0, notes: 0, decisions: []}, ...more}) as BoardCard
const hosted = (id: string, status: Hosted['status'], more: Partial<Hosted> = {}): Hosted =>
    ({kind: 'hosted', id, title: id, status, branch: '', owner: `session-${id}`, by: '', created: 0, updated: 0, base: 'main', ...more}) as Hosted
const target = (state: Ship['targets'][number]['state']) => ({target: 'web', state, words: '', asked: null, published: null})
const ship = (states: Ship['targets'][number]['state'][], more: Partial<Ship> = {}): Ship => ({repo: 'acme/cartopolis', landed_at: 1000, max_wait_s: 1800, targets: states.map(target), ...more})

test('the queue: the one being checked first, then the line in order', () => {
    const cs = [card('b', 'waiting_to_land', {position: 2}), card('x', 'working'), card('a', 'waiting_to_land', {position: 1}), card('g', 'landing')]
    expect(queue(cs).map((c) => c.session)).toEqual(['g', 'a', 'b'])
})

test('in line, then its gate, with the node the gate holds', () => {
    const q = card('session-s1', 'waiting_to_land', {position: 2, since: 50})
    expect(journey(hosted('s1', 'waiting'), q, [q], [ship(['live'])], [])).toMatchObject({phase: 'queued', position: 2, since: 50})
    const g = card('session-s1', 'landing', {step: 'gating 1a2b3c4d', node: 'vps-1', node_gate: true})
    expect(journey(hosted('s1', 'working'), g, [g], [ship(['live'])], [])).toMatchObject({phase: 'gate', step: 'gating 1a2b3c4d', node: 'vps-1'})
    // Its own build node is not the gate's.
    const own = card('session-s1', 'landing', {node: 'mac-1', node_gate: false})
    expect(journey(undefined, own, [own], null, [])?.node).toBeNull()
})

test('on main, the deploy waits for the landings in line, and says until when', () => {
    const me = card('session-s1', 'landed', {sha: 'abc', live: false, since: 900})
    const next = card('session-s2', 'waiting_to_land', {position: 1})
    const j = journey(hosted('s1', 'waiting', {finish: {sha: 'abc', at: 900, by: 'x'}}), me, [me, next], [ship(['waiting'])], ['acme/cartopolis'])
    expect(j).toMatchObject({phase: 'on_main', shipped: true, finishes: true})
    expect(j?.deploy).toMatchObject({kind: 'waits', by: 2800})
    expect(j?.deploy.kind === 'waits' && j.deploy.for.map((c) => c.session)).toEqual(['session-s2'])
})

test('announced, it says when it goes; landed a moment ago, that the hub has yet to plan it', () => {
    const announced = ship([], {tip: 'abc123', targets: [{...target('announced'), goes_at: 1300}]})
    const me = card('session-s1', 'landed', {sha: 'abc123', live: false})
    const j = journey(hosted('s1', 'waiting'), me, [me], [announced], ['acme/cartopolis'])
    expect(j?.deploy).toMatchObject({kind: 'notice', deferred: false, at: 1300})
    expect(j && deployAt(j.deploy)).toBe(1300)
    // The hub's plan is still for the main before this landing: all of it live, none of it this.
    const before = ship(['live'], {tip: '0ld000', landed_at: 50})
    expect(journey(hosted('s1', 'waiting'), me, [me], [before], ['acme/cartopolis'])?.deploy.kind).toBe('planning')
    // Another session landed on top of it: the plan for that tip carries this one too.
    const after = ship([], {tip: 'n3w000', landed_at: 150, targets: [{...target('announced'), goes_at: 1300}]})
    expect(journey(hosted('s1', 'waiting'), me, [me], [after], ['acme/cartopolis'])?.deploy).toMatchObject({kind: 'notice', at: 1300})
    // A session still marked at work, its landing under a newer tip that every target serves: live.
    const served = ship(['live'], {tip: 'n3w000', landed_at: 150})
    expect(journey(hosted('s1', 'working', {finish: {sha: 'abc123', at: 100, by: 'x'}}), undefined, [], [served], ['acme/cartopolis'])?.phase).toBe('live')
})

test('a hold, a deploy running and a failure each say so', () => {
    expect(deployOf(ship(['held'], {held: {by: 'web:sam', why: 'demo', at: 5}}), []).kind).toBe('held')
    expect(deployOf(ship(['deploying']), []).kind).toBe('deploying')
    expect(deployOf(ship(['live', 'failed']), []).kind).toBe('failed')
    expect(deployOf(ship(['waiting']), []).kind).toBe('running')
})

test('a project the hub does not deploy ends on main: no live step, nothing to wait for', () => {
    const s = [ship(['waiting'])]
    expect(shipOf(s, ['acme/midas'])).toBeNull()
    expect(shipOf(s, ['acme/cartopolis'])).toBe(s[0])
    const me = card('session-s1', 'landed', {sha: 'abc', live: false})
    expect(journey(hosted('s1', 'waiting'), me, [me], s, ['acme/midas'])).toMatchObject({phase: 'live', shipped: false})
})

test('each project waits on its own deploys', () => {
    const carto = ship(['live'])
    const dardasha = ship(['deploying'], {repo: 'acme/dardasha'})
    expect(shipOf([carto, dardasha], ['acme/dardasha'])).toBe(dardasha)
    const me = card('session-s1', 'landed', {sha: 'abc', live: false})
    const j = journey(hosted('s1', 'waiting'), me, [me], [carto, dardasha], ['acme/dardasha'])
    expect(j).toMatchObject({phase: 'on_main', shipped: true, ship: dardasha, deploy: {kind: 'deploying'}})
})

test('finished on landing; off the way when at work again', () => {
    expect(journey(hosted('s1', 'stopped', {finished: {outcome: 'landed', at: 50, sha: 'abc', live_at: 50}}), undefined, [], null, [])).toMatchObject({phase: 'finished', liveAt: 50})
    expect(journey(hosted('s1', 'working'), card('session-s1', 'working'), [], null, [])).toBeNull()
})

test('the lists group by where a session is', () => {
    const t = 100_000
    expect(groupOf(hosted('s1', 'needs_permission'), null, t)).toBe('asks')
    expect(groupOf(hosted('s1', 'waiting', {active: t - 60}), 'queued', t)).toBe('landing')
    expect(groupOf(hosted('s1', 'working'), null, t)).toBe('work')
    expect(groupOf(hosted('s1', 'waiting', {active: t - 60}), null, t)).toBe('work')
    expect(groupOf(hosted('s1', 'waiting', {active: t - 2 * 86400}), null, t)).toBe('idle')
    expect(groupOf(hosted('s1', 'stopped', {updated: t}), null, t)).toBe('idle')
    // Finished on landing today: Landed, archived or not; another day's, or dropped: the search's.
    expect(groupOf(hosted('s1', 'stopped', {archived: true, finished: {outcome: 'landed', at: t - 60}}), 'finished', t)).toBe('landed')
    expect(groupOf(hosted('s1', 'stopped', {archived: true, finished: {outcome: 'landed', at: t - 2 * 86400}}), 'finished', t)).toBeNull()
    expect(groupOf(hosted('s1', 'stopped', {archived: true, finished: {outcome: 'dropped', at: t}}), null, t)).toBeNull()
    // Landed with "keep working": Landed while its turn is over, At work once it works again.
    expect(groupOf(hosted('s1', 'waiting', {active: t}), 'live', t)).toBe('landed')
    expect(groupOf(hosted('s1', 'working'), 'live', t)).toBe('work')
    // Set to carry on later: Scheduled between turns, however long idle or ended; asking or at work comes first.
    const wake = {at: t + 3600, text: 'the rest', by: 'session-s1', set: t}
    expect(groupOf(hosted('s1', 'waiting', {active: t - 2 * 86400, wake}), null, t)).toBe('later')
    expect(groupOf(hosted('s1', 'stopped', {updated: t, wake}), null, t)).toBe('later')
    expect(groupOf(hosted('s1', 'working', {wake}), null, t)).toBe('work')
    expect(groupOf(hosted('s1', 'needs_permission', {wake}), null, t)).toBe('asks')
    // Waiting on the person for a step only they can take: Needs you between turns, landed or not;
    // put off, Scheduled until then; at work, At work.
    const awaits = {text: 'Paste a token', by: 'session-s1', at: t - 60}
    expect(groupOf(hosted('s1', 'waiting', {active: t, awaits}), 'live', t)).toBe('asks')
    expect(groupOf(hosted('s1', 'waiting', {active: t - 3 * 86400, awaits}), null, t)).toBe('asks')
    expect(groupOf(hosted('s1', 'stopped', {updated: t, awaits}), null, t)).toBe('asks')
    expect(groupOf(hosted('s1', 'working', {awaits}), 'live', t)).toBe('work')
    expect(groupOf(hosted('s1', 'waiting', {active: t, awaits: {...awaits, snooze: t + 3600}}), 'live', t)).toBe('later')
    expect(groupOf(hosted('s1', 'waiting', {active: t, awaits: {...awaits, snooze: t - 1}}), 'live', t)).toBe('asks')
})