100 lines
3.3 KiB
TypeScript
100 lines
3.3 KiB
TypeScript
import { Repository } from 'typeorm';
|
|
import { CharacterEquipment } from '../equipment/entities/character-equipment.entity';
|
|
import { CharacterItem } from '../items/entities/character-item.entity';
|
|
import { EquipmentSlot } from '../items/equipment-slot.enum';
|
|
import { ItemRarity } from '../items/item-rarity.enum';
|
|
import { ItemType } from '../items/item-type.enum';
|
|
import { InventoryService } from './inventory.service';
|
|
|
|
const CHARACTER_ID = 'character-1';
|
|
|
|
function characterItem(overrides: Partial<CharacterItem> = {}): CharacterItem {
|
|
return {
|
|
id: 'item-1',
|
|
characterId: CHARACTER_ID,
|
|
itemDefinitionId: 'def-1',
|
|
quantity: 1,
|
|
createdAt: new Date('2026-08-18T09:00:00.000Z'),
|
|
updatedAt: new Date('2026-08-18T09:00:00.000Z'),
|
|
itemDefinition: {
|
|
key: 'worn-short-sword',
|
|
name: 'Abgenutztes Kurzschwert',
|
|
description: 'Die Klinge eines Rekruten, öfter geschliffen als geführt.',
|
|
rarity: ItemRarity.COMMON,
|
|
type: ItemType.WEAPON,
|
|
equipmentSlot: EquipmentSlot.WEAPON,
|
|
requiredLevel: 1,
|
|
weaponDamage: 8,
|
|
bonusAttack: 0,
|
|
bonusHp: 0,
|
|
bonusArmor: 0,
|
|
iconPath: '/images/items/worn-short-sword.png',
|
|
},
|
|
...overrides,
|
|
} as CharacterItem;
|
|
}
|
|
|
|
describe('InventoryService', () => {
|
|
it('returns only the current character\'s items with definition data, quantity, and equipped state', async () => {
|
|
const items = [
|
|
characterItem({ id: 'item-1', quantity: 1 }),
|
|
characterItem({ id: 'item-2', quantity: 3, itemDefinitionId: 'def-2' }),
|
|
];
|
|
const characterItems = {
|
|
find: jest.fn().mockResolvedValue(items),
|
|
} as unknown as Repository<CharacterItem>;
|
|
const equipment = {
|
|
find: jest.fn().mockResolvedValue([
|
|
{ characterItemId: 'item-1', slot: EquipmentSlot.WEAPON } as CharacterEquipment,
|
|
]),
|
|
} as unknown as Repository<CharacterEquipment>;
|
|
const service = new InventoryService(characterItems, equipment);
|
|
|
|
const result = await service.getInventory(CHARACTER_ID);
|
|
|
|
expect(characterItems.find).toHaveBeenCalledWith({
|
|
where: { characterId: CHARACTER_ID },
|
|
relations: { itemDefinition: true },
|
|
order: { createdAt: 'ASC' },
|
|
});
|
|
expect(result.items).toEqual([
|
|
{
|
|
id: 'item-1',
|
|
quantity: 1,
|
|
equipped: true,
|
|
item: {
|
|
key: 'worn-short-sword',
|
|
name: 'Abgenutztes Kurzschwert',
|
|
description: 'Die Klinge eines Rekruten, öfter geschliffen als geführt.',
|
|
rarity: 'COMMON',
|
|
equipmentSlot: 'WEAPON',
|
|
requiredLevel: 1,
|
|
weaponDamage: 8,
|
|
bonusAttack: 0,
|
|
bonusHp: 0,
|
|
bonusArmor: 0,
|
|
iconPath: '/images/items/worn-short-sword.png',
|
|
},
|
|
},
|
|
{
|
|
id: 'item-2',
|
|
quantity: 3,
|
|
equipped: false,
|
|
item: expect.objectContaining({ key: 'worn-short-sword' }),
|
|
},
|
|
]);
|
|
});
|
|
|
|
it('returns an empty list when the character owns nothing', async () => {
|
|
const characterItems = {
|
|
find: jest.fn().mockResolvedValue([]),
|
|
} as unknown as Repository<CharacterItem>;
|
|
const equipment = {
|
|
find: jest.fn().mockResolvedValue([]),
|
|
} as unknown as Repository<CharacterEquipment>;
|
|
const service = new InventoryService(characterItems, equipment);
|
|
|
|
await expect(service.getInventory(CHARACTER_ID)).resolves.toEqual({ items: [] });
|
|
});
|
|
});
|