fix(api): key starting-sword seed idempotency on its real unique index
The CharacterItem idempotency check was looking up by the seed's own literal id instead of the (characterId, itemDefinitionId) unique index that CharacterItem actually enforces. If the demo character had already looted a worn-short-sword naturally, re-running the seed would miss that row and try to insert a colliding duplicate, breaking seeding instead of being a safe no-op. Look up by the real domain key and reuse whatever id is found when wiring up the CharacterEquipment row.
This commit is contained in:
@@ -419,4 +419,49 @@ describe('seedVisibleVerticalSlice', () => {
|
||||
);
|
||||
expect(characterItemRepository.rows).toHaveLength(1);
|
||||
});
|
||||
|
||||
it('reuses a naturally-looted starting sword instead of inserting a duplicate CharacterItem', async () => {
|
||||
const locationRepository = new InMemoryRepository();
|
||||
const connectionRepository = new InMemoryRepository();
|
||||
const characterRepository = new InMemoryRepository();
|
||||
const monsterRepository = new InMemoryRepository();
|
||||
const locationMonsterRepository = new InMemoryRepository();
|
||||
const characterItemRepository = new InMemoryRepository();
|
||||
const characterEquipmentRepository = new InMemoryRepository();
|
||||
const dataSource = createDataSource(
|
||||
locationRepository,
|
||||
connectionRepository,
|
||||
characterRepository,
|
||||
monsterRepository,
|
||||
locationMonsterRepository,
|
||||
new InMemoryRepository(),
|
||||
new InMemoryRepository(),
|
||||
new InMemoryRepository(),
|
||||
characterItemRepository,
|
||||
characterEquipmentRepository,
|
||||
);
|
||||
|
||||
// Simulate the demo character having already looted a worn-short-sword
|
||||
// naturally, under a DB-generated id that differs from the seed's
|
||||
// stable literal constant.
|
||||
const naturallyLootedItemId = 'naturally-looted-sword-item-id';
|
||||
characterItemRepository.rows.push({
|
||||
id: naturallyLootedItemId,
|
||||
characterId: DEMO_CHARACTER_ID,
|
||||
itemDefinitionId: ITEM_IDS['worn-short-sword'],
|
||||
quantity: 1,
|
||||
});
|
||||
|
||||
await seedVisibleVerticalSlice(dataSource);
|
||||
|
||||
expect(characterItemRepository.rows).toHaveLength(1);
|
||||
expect(characterEquipmentRepository.rows).toHaveLength(1);
|
||||
expect(characterEquipmentRepository.rows[0]).toEqual(
|
||||
expect.objectContaining({
|
||||
characterId: DEMO_CHARACTER_ID,
|
||||
slot: 'WEAPON',
|
||||
characterItemId: naturallyLootedItemId,
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user