Files
CONSOLGAMES/tests/sim/day.test.ts
T

75 lines
2.5 KiB
TypeScript

import { describe, it, expect } from 'vitest';
import { endDay } from '../../src/sim/day';
import { CLOSE_MINUTES, createNewGame, DAY_START_MINUTES } from '../../src/sim/state';
import { MONTHLY_RENT_CENTS } from '../../src/sim/economy';
import { placeOrder } from '../../src/sim/orders';
import { backroomQty } from '../../src/sim/inventory';
import { testCatalog } from './helpers';
const catalog = testCatalog();
function closed(seed = 1) {
const s = createNewGame(seed);
s.minutes = CLOSE_MINUTES;
return s;
}
describe('endDay', () => {
it('refuses to end the day before closing', () => {
expect(() => endDay(createNewGame(1))).toThrow('Shop is still open');
});
it('moves to the next morning and resets daily stats', () => {
const s = closed();
s.day.salesCount = 4;
const report = endDay(s);
expect(report.stats.salesCount).toBe(4);
expect(report.date).toEqual({ year: 1990, month: 9, day: 28 });
expect(s.date).toEqual({ year: 1990, month: 9, day: 29 });
expect(s.minutes).toBe(DAY_START_MINUTES);
expect(s.day.salesCount).toBe(0);
expect(report.rentPaidCents).toBe(0);
expect(report.skippedSunday).toBe(false);
});
it('skips Sunday and pays the rent when Sunday ends the month', () => {
const s = closed();
s.date = { year: 1990, month: 9, day: 29 };
const cash = s.cash;
const report = endDay(s);
expect(report.skippedSunday).toBe(true);
expect(report.rentPaidCents).toBe(MONTHLY_RENT_CENTS);
expect(report.newMonth).toBe(true);
expect(s.cash).toBe(cash - MONTHLY_RENT_CENTS);
expect(s.date).toEqual({ year: 1990, month: 10, day: 1 });
});
it('receives the deliveries for the next morning', () => {
const s = closed();
placeOrder(s, catalog, 'sml', 2);
const report = endDay(s);
expect(report.deliveries).toHaveLength(1);
expect(backroomQty(s.inventory, { gameId: 'sml', condition: 'new' })).toBe(2);
});
it('goes bankrupt after two negative month ends in a row', () => {
const s = closed();
s.cash = 0;
s.date = { year: 1990, month: 10, day: 31 };
expect(endDay(s).bankrupt).toBe(false);
expect(s.negativeMonthEnds).toBe(1);
s.date = { year: 1990, month: 11, day: 30 };
s.minutes = CLOSE_MINUTES;
expect(endDay(s).bankrupt).toBe(true);
expect(s.bankrupt).toBe(true);
});
it('resets the negative streak when a month ends positive', () => {
const s = closed();
s.negativeMonthEnds = 1;
s.date = { year: 1990, month: 10, day: 31 };
endDay(s);
expect(s.negativeMonthEnds).toBe(0);
});
});