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.

118 lignes · 4472 octetsBrut · Historique
1import { describe, expect, test } from 'bun:test';
2import type { MarketGood } from '../types';
3import { DETAILS } from './fixtures';
4import { buildGraph } from './navigation';
5import {
6 bestLot,
7 bestSellFor,
8 buyCost,
9 cheapestSource,
10 rankRoutes,
11 sellRevenue,
12 unitBuyPrice,
13 unitSellPrice,
14 type MarketSnapshot
15} from './trade';
16
17const good = (g: string, buy: number, sell: number, supply = 1000): MarketGood => ({
18 good: g,
19 buy,
20 sell,
21 supply,
22 baseline: supply
23});
24
25describe('price impact', () => {
26 test('first unit trades at the listed price', () => {
27 expect(unitBuyPrice(good('iron', 30, 28), 0)).toBe(30);
28 expect(unitSellPrice(good('iron', 30, 28), 0)).toBe(28);
29 });
30 test('buying raises and selling lowers the next price', () => {
31 const g = good('iron', 30, 28, 100);
32 expect(unitBuyPrice(g, 50)).toBe(60); // half the supply gone → double
33 expect(unitSellPrice(g, 100)).toBe(14); // supply doubled → half
34 });
35 test('cannot buy past the supply', () => {
36 expect(unitBuyPrice(good('iron', 30, 28, 5), 5)).toBe(Infinity);
37 });
38 test('totals sum the units', () => {
39 const g = good('iron', 10, 10, 1_000_000);
40 expect(buyCost(g, 3)).toBeCloseTo(30, 3);
41 expect(sellRevenue(g, 3)).toBeCloseTo(30, 3);
42 });
43});
44
45describe('bestLot', () => {
46 test('fills the hold when every unit pays', () => {
47 const lot = bestLot(good('iron', 10, 9), good('iron', 50, 45), 40, 1e9);
48 expect(lot.units).toBe(40);
49 expect(lot.revenue).toBeGreaterThan(lot.cost);
50 });
51 test('stops at credits', () => {
52 expect(bestLot(good('iron', 10, 9), good('iron', 50, 45), 40, 25).units).toBe(2);
53 });
54 test('stops when the margin closes', () => {
55 // thin markets: prices converge quickly
56 const lot = bestLot(good('iron', 10, 9, 20), good('iron', 12, 11, 20), 40, 1e9);
57 expect(lot.units).toBeGreaterThan(0);
58 expect(lot.units).toBeLessThan(10);
59 });
60 test('nothing when selling pays less than buying', () => {
61 expect(bestLot(good('iron', 30, 28), good('iron', 25, 20), 40, 1e9).units).toBe(0);
62 });
63});
64
65describe('rankRoutes', () => {
66 const graph = buildGraph(DETAILS);
67 const now = Date.parse('2026-11-06T12:00:00Z');
68 const at = new Date(now - 60_000).toISOString();
69 const snapshots: MarketSnapshot[] = [
70 { station: 'AA-1-A', system: 'AA-1', at, goods: [good('iron', 20, 18), good('fuel', 2, 1)] },
71 { station: 'BB-2-B', system: 'BB-2', at, goods: [good('iron', 45, 40), good('fuel', 3, 2)] },
72 { station: 'CC-3-B', system: 'CC-3', at, goods: [good('iron', 35, 31)] }
73 ];
74 const ship = { waypoint: 'AA-1-A', speed: 30, cargoCapacity: 40, fuel: 100, fuelCapacity: 100 };
75
76 test('finds the profitable run and orders by profit per minute', () => {
77 const routes = rankRoutes({ snapshots, graph, ship, credits: 100_000, now });
78 expect(routes[0]).toMatchObject({ good: 'iron', from: 'AA-1-A', to: 'BB-2-B', units: 40, fuelOk: true, stale: false });
79 expect(routes[0].profit).toBeGreaterThan(0);
80 expect(routes.every((r, i) => i === 0 || r.profitPerMinute <= routes[i - 1].profitPerMinute)).toBe(true);
81 });
82
83 test('charges fuel for the loaded leg at the buy station', () => {
84 const r = rankRoutes({ snapshots, graph, ship, credits: 100_000, now })[0];
85 expect(r.fuelCost).toBe(r.loaded.fuel * 2);
86 expect(r.profit).toBe(Math.round(r.revenue - r.cost - r.fuelCost));
87 });
88
89 test('flags routes the tank cannot cover', () => {
90 const routes = rankRoutes({ snapshots, graph, ship: { ...ship, fuel: 0 }, credits: 100_000, now });
91 expect(routes.find((r) => r.from === 'BB-2-B')?.fuelOk ?? false).toBe(false);
92 });
93
94 test('marks old snapshots stale', () => {
95 const later = now + 60 * 60_000;
96 expect(rankRoutes({ snapshots, graph, ship, credits: 100_000, now: later })[0].stale).toBe(true);
97 });
98
99 test('no credits, no routes', () => {
100 expect(rankRoutes({ snapshots, graph, ship, credits: 0, now })).toEqual([]);
101 });
102});
103
104describe('lookups', () => {
105 const at = '2026-11-06T12:00:00Z';
106 const snaps: MarketSnapshot[] = [
107 { station: 'A', system: 'S', at, goods: [good('ice', 10, 8)] },
108 { station: 'B', system: 'S', at, goods: [good('ice', 14, 12)] }
109 ];
110 test('bestSellFor skips the excluded station', () => {
111 expect(bestSellFor('ice', snaps)).toEqual({ station: 'B', price: 12 });
112 expect(bestSellFor('ice', snaps, 'B')).toEqual({ station: 'A', price: 8 });
113 });
114 test('cheapestSource prices the whole lot', () => {
115 expect(cheapestSource('ice', 5, snaps)?.station).toBe('A');
116 expect(cheapestSource('alloy', 5, snaps)).toBeNull();
117 });
118});