import { describe, it, expect } from 'vitest'; import { autoplayDay, serveCustomer } from '../../src/sim/autoplay'; import { createNewGame } from '../../src/sim/state'; import { compareDates } from '../../src/sim/calendar'; import { loadDefaultCatalog } from '../../src/sim/catalog'; import { STARTING_CASH_CENTS } from '../../src/sim/economy'; import { getSlot } from '../../src/sim/inventory'; import { makeCustomer, stock, testCatalog } from './helpers'; describe('autoplay', () => { it('plays one day end to end', () => { const s = createNewGame(1); const report = autoplayDay(s, loadDefaultCatalog()); expect(report.date).toEqual({ year: 1990, month: 9, day: 28 }); expect(s.date).toEqual({ year: 1990, month: 9, day: 29 }); }); it('keeps the economy stable over one year', () => { const catalog = loadDefaultCatalog(); for (const seed of [1, 2, 3]) { const s = createNewGame(seed); let sales = 0; let lost = 0; while (compareDates(s.date, { year: 1991, month: 9, day: 28 }) < 0) { const report = autoplayDay(s, catalog); sales += report.stats.salesCount; lost += report.stats.lostRequests; expect(s.bankrupt).toBe(false); } expect(s.cash).toBeGreaterThan(0); expect(s.cash).toBeLessThan(STARTING_CASH_CENTS * 100); expect(sales).toBeGreaterThan(300); expect(lost).toBeGreaterThan(0); expect(s.reputation).toBeGreaterThan(0); } }); it('skips trades the shop cannot afford instead of throwing', () => { const catalog = testCatalog(); const s = createNewGame(1); const tetrisNew = { gameId: 'tetris', condition: 'new' as const }; stock(s, 'slot-0', tetrisNew, 1); s.prices['tetris|new'] = 100; s.cash = 0; const customer = makeCustomer({ intent: 'trade', offered: { gameId: 'sml', condition: 'complete' } }); expect(() => serveCustomer(s, catalog, customer)).not.toThrow(); expect(getSlot(s.inventory, 'slot-0').qty).toBe(1); expect(s.day.trades).toBe(0); }); });