test: add desire lifecycle integration test

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
root
2026-03-10 00:04:30 +00:00
parent 7ef9636307
commit c548d0ac05
@@ -0,0 +1,60 @@
import { describe, it, expect } from 'vitest';
import { World } from '../../ecs/World.js';
import { desireFulfillmentSystem } from '../desireFulfillmentSystem.js';
import { industrySystem } from '../industrySystem.js';
import { GameMap } from '../../map/GameMap.js';
import { ItemRegistry } from '../../industry/itemRegistry.js';
import { RecipeRegistry } from '../../industry/recipeRegistry.js';
import type { Desire, Stats, NPCBrain, Needs } from '@dflike/shared';
function setupIntegrationWorld() {
const world = new World();
const map = new GameMap(20, 20);
const itemRegistry = ItemRegistry.createDefault();
const recipeRegistry = RecipeRegistry.createDefault();
world.setSingleton('itemRegistry', itemRegistry);
world.setSingleton('recipeRegistry', recipeRegistry);
return { world, map };
}
describe('desire lifecycle integration', () => {
it('desire drives crafting priority then gets fulfilled', () => {
const { world, map } = setupIntegrationWorld();
// Create NPC with materials for both axe and hammer, but desires hammer
const entity = world.createEntity();
world.addComponent(entity, 'position', { x: 5, y: 5 });
world.addComponent(entity, 'name', 'TestNPC');
const inv = new Map([['log', 4], ['stone', 2]]);
world.addComponent(entity, 'inventory', inv);
world.addComponent<Stats>(entity, 'stats', {
strength: 10, dexterity: 10, constitution: 10, intelligence: 10, perception: 10,
sociability: 10, courage: 10, curiosity: 10, empathy: 10, temperament: 10,
});
world.addComponent(entity, 'statModifiers', { modifiers: [] });
world.addComponent<Needs>(entity, 'needs', { hunger: 80, energy: 80, productivity: 20 });
world.addComponent<NPCBrain>(entity, 'npcBrain', { currentGoal: 'wander', goalQueue: [] });
world.addComponent(entity, 'movement', { state: 'idle', target: null, path: [], direction: 0, moveProgress: 0 });
const hammerDesire: Desire = {
id: 'test_hammer', description: 'wants a hammer', category: 'material',
fulfillment: { type: 'own_item', itemId: 'hammer', quantity: 1 },
priority: 0.9, source: 'spawn', createdAtTick: 0,
};
world.addComponent<Desire[]>(entity, 'desires', [hammerDesire]);
// Industry should pick hammer recipe due to desire
industrySystem(world, map);
const craftingState = world.getComponent<any>(entity, 'craftingState');
expect(craftingState).toBeTruthy();
expect(craftingState.recipeId).toBe('craft_hammer');
// Simulate crafting completion: add hammer to inventory
inv.set('hammer', 1);
// Fulfillment check should remove the desire
desireFulfillmentSystem(world, 100);
const desires = world.getComponent<Desire[]>(entity, 'desires')!;
expect(desires).toHaveLength(0);
});
});