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.

130 lignes · 5566 octetsBrut · Historique
1import { describe, expect, test } from 'bun:test';
2import type { ActiveShip, CaptainContract, MarketGood } from '../types';
3import { adviseShip, attention, type AdviceContext } from './advisor';
4import type { TradeRoute } from './trade';
5
6const ship: ActiveShip = {
7 symbol: 'ORION-1',
8 status: 'docked',
9 waypoint: 'AA-1-A',
10 route: null,
11 speed: 30,
12 fuel: { current: 80, capacity: 100 },
13 cargo: { units: 0, capacity: 40, items: [] },
14 modules: [],
15 hull: 100,
16 insured_until: null,
17 ready: null,
18 trade: null
19};
20const iron: MarketGood = { good: 'iron', buy: 32, sell: 28, supply: 2000, baseline: 2000 };
21const now = Date.parse('2026-11-06T12:00:00Z');
22
23const ctx = (over: Partial<AdviceContext> = {}): AdviceContext => ({
24 ship,
25 actions: ['orbit', 'refuel', 'buy', 'sell', 'repair'],
26 here: 'station',
27 systemWaypoints: [],
28 market: [iron],
29 snapshots: [],
30 bestRoute: null,
31 contracts: [],
32 codex: null,
33 now,
34 ...over
35});
36
37const ids = (c: AdviceContext) => adviseShip(c).map((h) => h.id);
38
39describe('adviseShip', () => {
40 test('stranded ships get only the rescue hint', () => {
41 const hints = adviseShip(ctx({ ship: { ...ship, hull: 0 }, actions: ['distress'] }));
42 expect(hints).toEqual([expect.objectContaining({ id: 'stranded', level: 'danger', action: 'distress' })]);
43 });
44
45 test('ships in flight get no hints', () => {
46 expect(adviseShip(ctx({ ship: { ...ship, status: 'in_transit' } }))).toEqual([]);
47 });
48
49 test('low fuel at a station offers refuel first', () => {
50 const hints = adviseShip(ctx({ ship: { ...ship, fuel: { current: 10, capacity: 100 } } }));
51 expect(hints[0]).toMatchObject({ id: 'fuel', level: 'warn', action: 'refuel' });
52 });
53
54 test('cargo at a market: sell here when nothing better is known', () => {
55 const loaded = { ...ship, cargo: { units: 10, capacity: 40, items: [{ good: 'iron', units: 10 }] } };
56 expect(adviseShip(ctx({ ship: loaded }))[0]).toMatchObject({ id: 'sell', action: 'sell' });
57 });
58
59 test('cargo at a market: point to a much better price elsewhere', () => {
60 const loaded = { ...ship, cargo: { units: 10, capacity: 40, items: [{ good: 'iron', units: 10 }] } };
61 const snapshots = [{ station: 'BB-2-B', system: 'BB-2', at: '', goods: [{ ...iron, sell: 45 }] }];
62 expect(adviseShip(ctx({ ship: loaded, snapshots }))[0]).toMatchObject({ id: 'sell-elsewhere', target: 'BB-2-B' });
63 });
64
65 test('empty hold at the start of the best route: buy here', () => {
66 const bestRoute = { good: 'iron', from: 'AA-1-A', to: 'BB-2-B', units: 40, profit: 500 } as TradeRoute;
67 expect(adviseShip(ctx({ bestRoute }))[0]).toMatchObject({ id: 'trade', action: 'buy', target: 'BB-2-B' });
68 });
69
70 test('orbiting a station with cargo: dock', () => {
71 const loaded = { ...ship, status: 'in_orbit' as const, cargo: { units: 5, capacity: 40, items: [{ good: 'ice', units: 5 }] } };
72 expect(ids(ctx({ ship: loaded, actions: ['dock', 'navigate'], market: null }))).toContain('dock');
73 });
74
75 test('asteroid field with room: mine', () => {
76 const orbit = { ...ship, status: 'in_orbit' as const };
77 expect(ids(ctx({ ship: orbit, here: 'asteroid_field', actions: ['extract'], market: null }))).toEqual(['mine']);
78 });
79
80 test('contract cargo at its station: deliver', () => {
81 const loaded = { ...ship, cargo: { units: 5, capacity: 40, items: [{ good: 'ice', units: 5 }] } };
82 const contract: CaptainContract = {
83 id: 'C-1', station: 'AA-1-A', good: 'ice', units: 10, reward: 900, hours: 24,
84 status: 'open', delivered: 2, accepted_at: '', due: ''
85 };
86 const hints = adviseShip(ctx({ ship: loaded, contracts: [contract], actions: ['deliver'], market: null }));
87 expect(hints[0]).toMatchObject({ id: 'contract-C-1', action: 'deliver' });
88 expect(hints[0].text).toContain('Deliver 5 ice');
89 });
90
91 test('a derelict in the system is pointed out until the codex is done', () => {
92 const systemWaypoints = [{ symbol: 'AA-1-D', system: 'AA-1', kind: 'derelict' as const, x: 0, y: 0 }];
93 expect(ids(ctx({ systemWaypoints, market: null, codex: { have: 3, of: 12 } }))).toContain('derelict');
94 expect(ids(ctx({ systemWaypoints, market: null, codex: { have: 12, of: 12 } }))).not.toContain('derelict');
95 });
96
97 test('actions not on offer are never attached to hints', () => {
98 const hints = adviseShip(ctx({ ship: { ...ship, fuel: { current: 10, capacity: 100 } }, actions: [] }));
99 expect(hints.every((h) => h.action === undefined)).toBe(true);
100 });
101
102 test('idle fallback', () => {
103 expect(ids(ctx({ ship: { ...ship, status: 'in_orbit' }, here: 'planet', market: null }))).toEqual(['idle']);
104 });
105
106 test('at most three hints, most urgent first', () => {
107 const hints = adviseShip(
108 ctx({ ship: { ...ship, hull: 10, fuel: { current: 5, capacity: 100 }, cargo: { units: 5, capacity: 40, items: [{ good: 'iron', units: 5 }] } } })
109 );
110 expect(hints.length).toBeLessThanOrEqual(3);
111 expect(hints.slice(0, 2).every((h) => h.level === 'warn')).toBe(true);
112 });
113});
114
115describe('attention', () => {
116 test('flags stranded, low fuel, idle and arrived ships', () => {
117 expect(attention({ ...ship, hull: 0 }, now)).toBe('danger');
118 expect(attention({ ...ship, fuel: { current: 1, capacity: 100 } }, now)).toBe('warn');
119 expect(attention(ship, now)).toBe('info');
120 expect(attention({ symbol: 'X', status: 'beyond_the_lanes', returns: '' }, now)).toBeNull();
121 });
122 test('ships still flying need nothing', () => {
123 const flying = {
124 ...ship,
125 status: 'in_transit' as const,
126 route: { from: 'A', to: 'B', departed: '', arrives: new Date(now + 60_000).toISOString() }
127 };
128 expect(attention(flying, now)).toBeNull();
129 });
130});