import HarvesterHandler from '@/roleHandlers/harvester.handler'; import * as getById from '@/utils/funcs/getById'; import { PositionSpotStatus, createSourcePositionMatrix, } from '@/utils/positions'; import '~/tests/__mocks__/screeps'; describe('HarvesterHandler', () => { type FixtureOptions = { memory?: Partial; storeUsed?: number; storeCapacity?: number; stateOverrides?: Partial; sourceId?: string; spawnId?: string; }; const createFixture = ({ memory = {}, storeUsed = 0, storeCapacity = 50, stateOverrides = {}, sourceId = 'src1', spawnId = 'spawn1', }: FixtureOptions = {}) => { const state = { sourcesStates: { [sourceId]: { spots: createSourcePositionMatrix( PositionSpotStatus.EMPTY as any ), }, }, ...stateOverrides, } as unknown as GameState; const source = { id: sourceId, pos: new (global as any).RoomPosition(5, 5, 'W1N1'), } as unknown as Source; const spawn = { id: spawnId } as unknown as StructureSpawn; const creep = { name: 'Creep1', memory: { role: 'harvester', spawnId, ...memory, } as CreepMemory, store: { getFreeCapacity: (_res: string) => storeCapacity - storeUsed, getUsedCapacity: (_res: string) => storeUsed, }, pos: new (global as any).RoomPosition(0, 0, 'W1N1'), room: {}, harvest: jest.fn().mockReturnValue((global as any).OK), moveTo: jest.fn().mockReturnValue((global as any).OK), transfer: jest.fn().mockReturnValue((global as any).OK), } as unknown as Creep; return { state, source, spawn, creep, memory: creep.memory, mockSource: (value: Source | null = source) => jest.spyOn(getById, 'getSourceById').mockReturnValue(value), mockSpawn: (value: StructureSpawn | null = spawn) => jest.spyOn(getById, 'getSpawnById').mockReturnValue(value), }; }; beforeEach(() => { jest.clearAllMocks(); jest.spyOn(console, 'log').mockImplementation(() => {}); }); afterEach(() => { jest.restoreAllMocks(); }); describe('destroy', () => { it('releases the source spot from destination and clears it', () => { const { state, memory } = createFixture({ memory: { destination: { type: 'source', id: 'src1', sourceSpot: 0 }, }, }); state.sourcesStates['src1'].spots[0] = PositionSpotStatus.OCCUPIED; HarvesterHandler.destroy(memory, state); expect(state.sourcesStates['src1'].spots[0]).toBe( PositionSpotStatus.EMPTY ); expect(memory.destination).toBeUndefined(); }); it('releases the source spot from previousDestination and clears it', () => { const { state, memory } = createFixture({ memory: { previousDestination: { type: 'source', id: 'src1', sourceSpot: 2, }, }, }); state.sourcesStates['src1'].spots[2] = PositionSpotStatus.OCCUPIED; HarvesterHandler.destroy(memory, state); expect(state.sourcesStates['src1'].spots[2]).toBe( PositionSpotStatus.EMPTY ); expect(memory.previousDestination).toBeUndefined(); }); it('does nothing when destination is not a source', () => { const { state, memory } = createFixture({ memory: { destination: { type: 'spawn', id: 'spawn1' }, }, }); HarvesterHandler.destroy(memory, state); expect( state.sourcesStates['src1'].spots.every( (s: PositionSpotStatus) => s === PositionSpotStatus.EMPTY || s === PositionSpotStatus.CENTER ) ).toBe(true); }); it('does nothing when memory has no destination', () => { const { state, memory } = createFixture(); expect(() => HarvesterHandler.destroy(memory, state)).not.toThrow(); }); }); describe('validateCreepMemory (via run)', () => { it('releases previousDestination source spot and deletes it', () => { const { state, creep, mockSource } = createFixture({ memory: { previousDestination: { type: 'source', id: 'src1', sourceSpot: 1, }, }, }); state.sourcesStates['src1'].spots[1] = PositionSpotStatus.OCCUPIED; mockSource(null); HarvesterHandler.run(creep, state); expect(state.sourcesStates['src1'].spots[1]).toBe( PositionSpotStatus.EMPTY ); expect(creep.memory.previousDestination).toBeUndefined(); }); it('transitions from source to spawn destination when energy is full', () => { const { state, creep, mockSource, mockSpawn } = createFixture({ memory: { destination: { type: 'source', id: 'src1', sourceSpot: 0 }, spawnId: 'spawn1', }, storeUsed: 50, storeCapacity: 50, }); mockSource(); mockSpawn(); HarvesterHandler.run(creep, state); expect(creep.memory.destination?.type).toBe('spawn'); expect(creep.memory.previousDestination?.type).toBe('source'); }); it('clears spawn destination when energy is empty', () => { const { state, creep, mockSpawn } = createFixture({ memory: { destination: { type: 'spawn', id: 'spawn1' }, }, storeUsed: 0, storeCapacity: 50, }); mockSpawn(); HarvesterHandler.run(creep, state); expect(creep.memory.destination).toBeUndefined(); }); }); describe('onFindNewSource', () => { it('assigns the first available source spot to the creep', () => { const { state, creep, mockSource } = createFixture(); mockSource(); HarvesterHandler.run(creep, state); expect(creep.memory.destination?.type).toBe('source'); expect((creep.memory.destination as any).id).toBe('src1'); }); it('does not assign a source when all spots are occupied', () => { const { state, creep, mockSource } = createFixture(); state.sourcesStates['src1'].spots = state.sourcesStates[ 'src1' ].spots.map((s: PositionSpotStatus) => s === PositionSpotStatus.CENTER ? s : PositionSpotStatus.OCCUPIED ) as any; mockSource(); HarvesterHandler.run(creep, state); expect(creep.memory.destination).toBeUndefined(); }); }); describe('onSourceDestination', () => { it('harvests when in range', () => { const { state, source, creep, mockSource } = createFixture({ memory: { destination: { type: 'source', id: 'src1', sourceSpot: 0 }, }, }); mockSource(); (creep.harvest as jest.Mock).mockReturnValue((global as any).OK); HarvesterHandler.run(creep, state); expect(creep.harvest).toHaveBeenCalledWith(source); expect(creep.moveTo).not.toHaveBeenCalled(); }); it('moves to source when not in range', () => { const { state, creep, mockSource } = createFixture({ memory: { destination: { type: 'source', id: 'src1', sourceSpot: 0 }, }, }); mockSource(); (creep.harvest as jest.Mock).mockReturnValue( (global as any).ERR_NOT_IN_RANGE ); HarvesterHandler.run(creep, state); expect(creep.moveTo).toHaveBeenCalled(); }); it('logs and returns when source is not found', () => { const { state, creep, mockSource } = createFixture({ memory: { destination: { type: 'source', id: 'unknown', sourceSpot: 0, }, }, }); mockSource(null); expect(() => HarvesterHandler.run(creep, state)).not.toThrow(); expect(creep.harvest).not.toHaveBeenCalled(); }); }); describe('onSpawnDestination', () => { it('transfers energy when in range', () => { const { state, spawn, creep, mockSpawn } = createFixture({ memory: { destination: { type: 'spawn', id: 'spawn1' }, }, storeUsed: 50, storeCapacity: 50, }); mockSpawn(); (creep.transfer as jest.Mock).mockReturnValue((global as any).OK); HarvesterHandler.run(creep, state); expect(creep.transfer).toHaveBeenCalledWith( spawn, (global as any).RESOURCE_ENERGY ); expect(creep.moveTo).not.toHaveBeenCalled(); }); it('moves to spawn when not in range', () => { const { state, spawn, creep, mockSpawn } = createFixture({ memory: { destination: { type: 'spawn', id: 'spawn1' }, }, storeUsed: 50, storeCapacity: 50, }); mockSpawn(); (creep.transfer as jest.Mock).mockReturnValue( (global as any).ERR_NOT_IN_RANGE ); HarvesterHandler.run(creep, state); expect(creep.moveTo).toHaveBeenCalledWith( spawn, expect.any(Object) ); }); it('logs and returns when spawn is not found', () => { const { state, creep, mockSpawn } = createFixture({ memory: { destination: { type: 'spawn', id: 'spawn1' }, }, storeUsed: 50, storeCapacity: 50, }); mockSpawn(null); expect(() => HarvesterHandler.run(creep, state)).not.toThrow(); expect(creep.transfer).not.toHaveBeenCalled(); }); }); });