feat: implement RoleHandler with abstract methods for destroy and run

This commit is contained in:
2026-04-24 00:44:07 -03:00
parent 4b6149da21
commit 754c6a1313
7 changed files with 194 additions and 18 deletions

View File

@@ -119,6 +119,32 @@ describe('HarvesterHandler', () => {
expect(memory.previousDestination).toBeUndefined();
});
it('releases both destination and previousDestination source spots when both are set', () => {
const { state, memory } = createFixture({
memory: {
destination: { type: 'source', id: 'src1', sourceSpot: 0 },
previousDestination: {
type: 'source',
id: 'src1',
sourceSpot: 2,
},
},
});
state.sourcesStates['src1'].spots[0] = PositionSpotStatus.OCCUPIED;
state.sourcesStates['src1'].spots[2] = PositionSpotStatus.OCCUPIED;
HarvesterHandler.destroy(memory, state);
expect(state.sourcesStates['src1'].spots[0]).toBe(
PositionSpotStatus.EMPTY
);
expect(state.sourcesStates['src1'].spots[2]).toBe(
PositionSpotStatus.EMPTY
);
expect(memory.destination).toBeUndefined();
expect(memory.previousDestination).toBeUndefined();
});
it('does nothing when destination is not a source', () => {
const { state, memory } = createFixture({
memory: {
@@ -142,6 +168,21 @@ describe('HarvesterHandler', () => {
expect(() => HarvesterHandler.destroy(memory, state)).not.toThrow();
});
it('does not throw when destination source ID is missing from state', () => {
const { state, memory } = createFixture({
memory: {
destination: {
type: 'source',
id: 'nonexistent',
sourceSpot: 0,
},
},
});
expect(() => HarvesterHandler.destroy(memory, state)).not.toThrow();
expect(memory.destination).toBeUndefined();
});
});
describe('validateCreepMemory (via run)', () => {
@@ -185,7 +226,7 @@ describe('HarvesterHandler', () => {
});
it('clears spawn destination when energy is empty', () => {
const { state, creep, mockSpawn } = createFixture({
const { state, creep, mockSpawn, mockSource } = createFixture({
memory: {
destination: { type: 'spawn', id: 'spawn1' },
},
@@ -193,6 +234,8 @@ describe('HarvesterHandler', () => {
storeCapacity: 50,
});
mockSpawn();
// Explicitly return null so onFindNewSource finds nothing
mockSource(null);
HarvesterHandler.run(creep, state);
@@ -224,6 +267,19 @@ describe('HarvesterHandler', () => {
expect(creep.memory.destination).toBeUndefined();
});
it('marks the assigned source spot as OCCUPIED in state', () => {
const { state, creep, mockSource } = createFixture();
mockSource();
HarvesterHandler.run(creep, state);
const spotIndex = (creep.memory.destination as any)?.sourceSpot;
expect(spotIndex).toBeDefined();
expect(state.sourcesStates['src1'].spots[spotIndex]).toBe(
PositionSpotStatus.OCCUPIED
);
});
});
describe('onSourceDestination', () => {
@@ -258,6 +314,26 @@ describe('HarvesterHandler', () => {
expect(creep.moveTo).toHaveBeenCalled();
});
it('moves to the computed spot position (not source pos) when not 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).ERR_NOT_IN_RANGE
);
HarvesterHandler.run(creep, state);
expect(creep.moveTo).toHaveBeenCalledWith(
// spot 0 → delta (-1,-1) from source pos (5,5) → (4,4)
expect.objectContaining({ x: 4, y: 4, roomName: 'W1N1' }),
expect.objectContaining({ reusePath: 10 })
);
});
it('logs and returns when source is not found', () => {
const { state, creep, mockSource } = createFixture({
memory: {
@@ -331,4 +407,15 @@ describe('HarvesterHandler', () => {
expect(creep.transfer).not.toHaveBeenCalled();
});
});
describe('run', () => {
it('returns the state object', () => {
const { state, creep, mockSource } = createFixture();
mockSource(null);
const result = HarvesterHandler.run(creep, state);
expect(result).toBe(state);
});
});
});