import { describe, expect, test } from 'bun:test'; import type { ActiveShip, MarketGood } from '../types'; import { affordableUnits, buildBody, describeOutcome, maxFor } from './forms'; const ship: ActiveShip = { symbol: 'ORION-1', status: 'docked', waypoint: 'AA-1-A', route: null, speed: 30, fuel: { current: 60, capacity: 100 }, cargo: { units: 10, capacity: 40, items: [{ good: 'iron', units: 7 }, { good: 'copper', units: 3 }] }, modules: [], hull: 80, insured_until: null, ready: null, trade: null }; const market: MarketGood[] = [ { good: 'iron', buy: 10, sell: 9, supply: 1_000_000, baseline: 1_000_000 }, { good: 'fuel', buy: 2, sell: 1, supply: 1_000_000, baseline: 1_000_000 } ]; test('buildBody parses integers and drops empty optional fields', () => { const fields = [ { name: 'good', type: 'string', required: true }, { name: 'units', type: 'integer', required: true }, { name: 'points', type: 'integer', required: false } ]; expect(buildBody(fields, { good: 'iron', units: ' 12 ', points: '' })).toEqual({ good: 'iron', units: 12 }); }); test('affordableUnits stops at the wallet', () => { expect(affordableUnits(market[0], 95)).toBe(9); expect(affordableUnits(market[0], 1e9, 5)).toBe(5); }); describe('maxFor', () => { const ctx = { ship, market, credits: 1_000 }; test('buy: limited by free hold, supply and credits', () => { expect(maxFor('buy', 'units', ctx, { good: 'iron' })).toBe(30); // free hold expect(maxFor('buy', 'units', { ...ctx, credits: 55 }, { good: 'iron' })).toBe(5); }); test('sell: what the hold carries', () => { expect(maxFor('sell', 'units', ctx, { good: 'iron' })).toBe(7); }); test('refuel: tank room, bounded by credits', () => { expect(maxFor('refuel', 'units', ctx, {})).toBe(40); expect(maxFor('refuel', 'units', { ...ctx, credits: 11 }, {})).toBe(5); // each unit a hair over 2 cr }); test('refine: two iron and one copper per alloy', () => { expect(maxFor('refine', 'units', ctx, {})).toBe(3); }); test('unknown fields have no bound', () => { expect(maxFor('launch', 'power', ctx, {})).toBeNull(); }); }); describe('describeOutcome', () => { test('lists route, fuel and cargo changes', () => { const after = { ...ship, status: 'in_transit', fuel: { current: 50, capacity: 100 }, route: { from: 'AA-1-A', to: 'AA-1-B', departed: '2026-11-06T12:00:00Z', arrives: '2026-11-06T12:01:05Z' } }; expect(describeOutcome(ship, after)).toEqual([ 'Trip to AA-1-B: 1m 05s', 'Fuel -10 → 50/100', 'Status: in transit' ]); }); test('ignores responses that are not this ship', () => { expect(describeOutcome(ship, { symbol: 'OTHER' })).toEqual([]); expect(describeOutcome(ship, null)).toEqual([]); }); });