Files

90 lines
3.3 KiB
TypeScript

import { describe, it, expect } from 'vitest';
import {
addToBackroom, backroomQty, createInventory, getSlot, itemKey, parseItemKey,
shelfItems, SLOT_CAPACITY, stockSlot, takeFromSlot, totalQty, unstockSlot,
} from '../../src/sim/inventory';
const tetrisNew = { gameId: 'tetris', condition: 'new' as const };
const smlLoose = { gameId: 'sml', condition: 'loose' as const };
describe('inventory', () => {
it('round-trips item keys', () => {
expect(parseItemKey(itemKey(smlLoose))).toEqual(smlLoose);
});
it('creates empty slots', () => {
const inv = createInventory(3);
expect(inv.slots.map((s) => s.id)).toEqual(['slot-0', 'slot-1', 'slot-2']);
expect(shelfItems(inv)).toEqual([]);
});
it('adds to the backroom', () => {
const inv = createInventory(1);
addToBackroom(inv, tetrisNew, 3);
addToBackroom(inv, tetrisNew, 2);
expect(backroomQty(inv, tetrisNew)).toBe(5);
});
it('stocks a slot up to capacity from the backroom', () => {
const inv = createInventory(1);
addToBackroom(inv, tetrisNew, SLOT_CAPACITY + 2);
const moved = stockSlot(inv, 'slot-0', tetrisNew, 100);
expect(moved).toBe(SLOT_CAPACITY);
expect(getSlot(inv, 'slot-0')).toEqual({ id: 'slot-0', item: tetrisNew, qty: SLOT_CAPACITY });
expect(backroomQty(inv, tetrisNew)).toBe(2);
expect(totalQty(inv, tetrisNew)).toBe(SLOT_CAPACITY + 2);
});
it('moves nothing when the backroom is empty', () => {
const inv = createInventory(1);
expect(stockSlot(inv, 'slot-0', tetrisNew, 3)).toBe(0);
expect(getSlot(inv, 'slot-0').item).toBeNull();
});
it('refuses mixing items in one slot', () => {
const inv = createInventory(1);
addToBackroom(inv, tetrisNew, 1);
addToBackroom(inv, smlLoose, 1);
stockSlot(inv, 'slot-0', tetrisNew, 1);
expect(() => stockSlot(inv, 'slot-0', smlLoose, 1)).toThrow('Slot slot-0 holds another item');
});
it('takes items from a slot and clears it when empty', () => {
const inv = createInventory(1);
addToBackroom(inv, tetrisNew, 2);
stockSlot(inv, 'slot-0', tetrisNew, 2);
expect(takeFromSlot(inv, 'slot-0')).toEqual(tetrisNew);
expect(getSlot(inv, 'slot-0').qty).toBe(1);
takeFromSlot(inv, 'slot-0');
expect(getSlot(inv, 'slot-0')).toEqual({ id: 'slot-0', item: null, qty: 0 });
expect(() => takeFromSlot(inv, 'slot-0')).toThrow('Slot slot-0 is empty');
});
it('moves items from a slot back to the backroom', () => {
const inv = createInventory(1);
addToBackroom(inv, tetrisNew, 4);
stockSlot(inv, 'slot-0', tetrisNew, 4);
expect(unstockSlot(inv, 'slot-0', 3)).toBe(3);
expect(getSlot(inv, 'slot-0').qty).toBe(1);
expect(backroomQty(inv, tetrisNew)).toBe(3);
});
it('caps unstocking to the slot quantity and clears the slot at 0', () => {
const inv = createInventory(1);
addToBackroom(inv, tetrisNew, 2);
stockSlot(inv, 'slot-0', tetrisNew, 2);
expect(unstockSlot(inv, 'slot-0', 10)).toBe(2);
expect(getSlot(inv, 'slot-0')).toEqual({ id: 'slot-0', item: null, qty: 0 });
expect(backroomQty(inv, tetrisNew)).toBe(2);
});
it('returns 0 when unstocking an empty slot', () => {
const inv = createInventory(1);
expect(unstockSlot(inv, 'slot-0', 3)).toBe(0);
});
it('throws on unknown slot', () => {
expect(() => getSlot(createInventory(1), 'slot-9')).toThrow('Unknown slot: slot-9');
});
});