tobogganSe connecter

starfleet-ui

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

77 lignes · 2733 octetsBrut · Historique
1import { describe, expect, test } from 'bun:test';
2import type { ActiveShip, MarketGood } from '../types';
3import { affordableUnits, buildBody, describeOutcome, maxFor } from './forms';
4
5const ship: ActiveShip = {
6 symbol: 'ORION-1',
7 status: 'docked',
8 waypoint: 'AA-1-A',
9 route: null,
10 speed: 30,
11 fuel: { current: 60, capacity: 100 },
12 cargo: { units: 10, capacity: 40, items: [{ good: 'iron', units: 7 }, { good: 'copper', units: 3 }] },
13 modules: [],
14 hull: 80,
15 insured_until: null,
16 ready: null,
17 trade: null
18};
19const market: MarketGood[] = [
20 { good: 'iron', buy: 10, sell: 9, supply: 1_000_000, baseline: 1_000_000 },
21 { good: 'fuel', buy: 2, sell: 1, supply: 1_000_000, baseline: 1_000_000 }
22];
23
24test('buildBody parses integers and drops empty optional fields', () => {
25 const fields = [
26 { name: 'good', type: 'string', required: true },
27 { name: 'units', type: 'integer', required: true },
28 { name: 'points', type: 'integer', required: false }
29 ];
30 expect(buildBody(fields, { good: 'iron', units: ' 12 ', points: '' })).toEqual({ good: 'iron', units: 12 });
31});
32
33test('affordableUnits stops at the wallet', () => {
34 expect(affordableUnits(market[0], 95)).toBe(9);
35 expect(affordableUnits(market[0], 1e9, 5)).toBe(5);
36});
37
38describe('maxFor', () => {
39 const ctx = { ship, market, credits: 1_000 };
40 test('buy: limited by free hold, supply and credits', () => {
41 expect(maxFor('buy', 'units', ctx, { good: 'iron' })).toBe(30); // free hold
42 expect(maxFor('buy', 'units', { ...ctx, credits: 55 }, { good: 'iron' })).toBe(5);
43 });
44 test('sell: what the hold carries', () => {
45 expect(maxFor('sell', 'units', ctx, { good: 'iron' })).toBe(7);
46 });
47 test('refuel: tank room, bounded by credits', () => {
48 expect(maxFor('refuel', 'units', ctx, {})).toBe(40);
49 expect(maxFor('refuel', 'units', { ...ctx, credits: 11 }, {})).toBe(5); // each unit a hair over 2 cr
50 });
51 test('refine: two iron and one copper per alloy', () => {
52 expect(maxFor('refine', 'units', ctx, {})).toBe(3);
53 });
54 test('unknown fields have no bound', () => {
55 expect(maxFor('launch', 'power', ctx, {})).toBeNull();
56 });
57});
58
59describe('describeOutcome', () => {
60 test('lists route, fuel and cargo changes', () => {
61 const after = {
62 ...ship,
63 status: 'in_transit',
64 fuel: { current: 50, capacity: 100 },
65 route: { from: 'AA-1-A', to: 'AA-1-B', departed: '2026-11-06T12:00:00Z', arrives: '2026-11-06T12:01:05Z' }
66 };
67 expect(describeOutcome(ship, after)).toEqual([
68 'Trip to AA-1-B: 1m 05s',
69 'Fuel -10 → 50/100',
70 'Status: in transit'
71 ]);
72 });
73 test('ignores responses that are not this ship', () => {
74 expect(describeOutcome(ship, { symbol: 'OTHER' })).toEqual([]);
75 expect(describeOutcome(ship, null)).toEqual([]);
76 });
77});