import { describe, expect, it } from 'vitest'; import type { TurnSchedule } from '@sammo-ts/logic/turn/calendar.js'; import type { TurnGeneral, TurnWorldSnapshot, TurnWorldState } from '../src/turn/types.js'; import { createTurnTestHarness } from './helpers/turnTestHarness.js'; const start = new Date('0200-01-01T00:00:00.000Z'); const schedule: TurnSchedule = { entries: [{ startMinute: 0, tickMinutes: 10 }] }; const map = { id: 'general-turn-test', name: '장수턴 테스트', cities: [ { id: 1, name: '테스트성', level: 1, region: 1, position: { x: 0, y: 0 }, connections: [], max: { population: 50_000, agriculture: 1_000, commerce: 1_000, security: 1_000, defence: 1_000, wall: 1_000, }, initial: { population: 10_000, agriculture: 500, commerce: 500, security: 500, defence: 500, wall: 500, }, }, ], defaults: { trust: 50, trade: 100, supplyState: 1, frontState: 0 }, }; const makeGeneral = (patch: Partial = {}): TurnGeneral => ({ id: 1, name: '테스트장수', nationId: 1, cityId: 1, troopId: 0, stats: { leadership: 80, strength: 70, intelligence: 60 }, experience: 0, dedication: 0, officerLevel: 1, role: { personality: null, specialDomestic: null, specialWar: null, items: { horse: null, weapon: null, book: null, item: null }, }, injury: 0, gold: 2_000, rice: 2_000, crew: 0, crewTypeId: 1, train: 40, atmos: 40, age: 30, npcState: 0, triggerState: { flags: {}, counters: {}, modifiers: {}, meta: {} }, meta: { killturn: 24 }, turnTime: start, ...patch, }); const makeSnapshot = (general: TurnGeneral): TurnWorldSnapshot => ({ scenarioConfig: { stat: { total: 300, min: 10, max: 100, npcTotal: 150, npcMax: 50, npcMin: 10, chiefMin: 70 }, iconPath: '', map: {}, const: { develCost: 100, trainDelta: 30, atmosDelta: 30, maxTrainByCommand: 100, maxAtmosByCommand: 100, initialNationGenLimit: 10, }, environment: { mapName: map.id, unitSet: 'test' }, }, scenarioMeta: { title: '장수턴 테스트', startYear: 200, life: null, fiction: 0, history: [], ignoreDefaultEvents: false, }, map, unitSet: { id: 'test', name: 'test', crewTypes: [] }, nations: [ { id: 1, name: '테스트국', color: '#000000', capitalCityId: 1, chiefGeneralId: null, gold: 10_000, rice: 10_000, power: 0, level: 1, typeCode: 'che_중립', meta: { gennum: 1, tech: 0 }, }, ], cities: [ { id: 1, name: '테스트성', nationId: 1, level: 1, state: 0, population: 10_000, populationMax: 50_000, agriculture: 500, agricultureMax: 1_000, commerce: 500, commerceMax: 1_000, security: 500, securityMax: 1_000, supplyState: 1, frontState: 0, defence: 500, defenceMax: 1_000, wall: 500, wallMax: 1_000, meta: { trust: 50, trade: 100, region: 1 }, }, ], generals: [general], troops: [], diplomacy: [], events: [], initialEvents: [], }); const makeState = (): TurnWorldState => ({ id: 1, currentYear: 200, currentMonth: 1, tickSeconds: 600, lastTurnTime: start, meta: { killturn: 24 }, }); describe('legacy general-turn execution contract', () => { it('runs injury recovery and troop rice consumption before the reserved command', async () => { const general = makeGeneral({ injury: 25, crew: 200, rice: 1 }); const harness = await createTurnTestHarness({ snapshot: makeSnapshot(general), state: makeState(), schedule, map, collectLogs: true, }); await harness.runOneTick(); const updated = harness.world.getGeneralById(1)!; expect(updated.injury).toBe(15); expect(updated.crew).toBe(0); expect(updated.rice).toBe(0); expect(harness.world.getCityById(1)!.population).toBe(10_200); expect(harness.getCollectedLogs().some((log) => log.text.includes('소집해제'))).toBe(true); }); it('persists pre-turn stacking and applies the inherited 60-turn cooldown', async () => { const general = makeGeneral({ role: { personality: null, specialDomestic: null, specialWar: 'che_격노', items: { horse: null, weapon: null, book: null, item: null }, }, }); const harness = await createTurnTestHarness({ snapshot: makeSnapshot(general), state: makeState(), schedule, map, collectLogs: true, }); const turns = harness.reservedTurnStore.getGeneralTurns(1); turns[0] = { action: 'che_전투특기초기화', args: {} }; turns[1] = { action: 'che_전투특기초기화', args: {} }; await harness.runOneTick(); expect(harness.world.getGeneralById(1)!.lastTurn).toEqual({ command: '전투 특기 초기화', term: 1, }); expect(harness.world.getGeneralById(1)!.role.specialWar).toBe('che_격노'); await harness.runOneTick(); const updated = harness.world.getGeneralById(1)!; expect(updated.role.specialWar).toBeNull(); expect(updated.meta['next_execute_전투 특기 초기화']).toBe(2460); expect(updated.meta.prev_types_special2).toEqual(['che_격노']); }); it('preserves the legacy battle-readiness term reset instead of making its reward reachable', async () => { const general = makeGeneral({ crew: 1_000, train: 40, atmos: 40 }); const harness = await createTurnTestHarness({ snapshot: makeSnapshot(general), state: makeState(), schedule, map, collectLogs: true, }); const turns = harness.reservedTurnStore.getGeneralTurns(1); for (let idx = 0; idx < 4; idx += 1) { turns[idx] = { action: 'che_전투태세', args: {} }; } for (let idx = 0; idx < 4; idx += 1) { await harness.runOneTick(); } const updated = harness.world.getGeneralById(1)!; expect(updated.lastTurn).toEqual({ command: '전투태세', term: 1 }); expect(updated.experience).toBe(0); expect(updated.train).toBe(40); expect(updated.atmos).toBe(40); }); it('skips commands for blocked generals while advancing the queue once', async () => { const general = makeGeneral({ injury: 20, meta: { killturn: 5, block: 3 } }); const harness = await createTurnTestHarness({ snapshot: makeSnapshot(general), state: makeState(), schedule, map, collectLogs: true, }); harness.reservedTurnStore.getGeneralTurns(1)[0] = { action: 'che_요양', args: {} }; await harness.runOneTick(); const updated = harness.world.getGeneralById(1)!; expect(updated.injury).toBe(10); expect(updated.experience).toBe(0); expect(updated.meta.killturn).toBe(4); expect(updated.meta.inherit_lived_month).toBe(1); expect(updated.meta.myset).toBe(3); expect(harness.reservedTurnStore.getGeneralTurn(1, 0).action).toBe('휴식'); expect(harness.getCollectedLogs().some((log) => log.text.includes('악성유저'))).toBe(true); }); });