chore: adds tests
This commit is contained in:
35
tests/__mocks__/screeps.ts
Normal file
35
tests/__mocks__/screeps.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
/**
|
||||
* Minimal Screeps global mocks used across tests.
|
||||
*/
|
||||
|
||||
// Constants
|
||||
(global as any).RESOURCE_ENERGY = 'energy';
|
||||
(global as any).ERR_NOT_IN_RANGE = -9;
|
||||
(global as any).OK = 0;
|
||||
(global as any).WORK = 'work';
|
||||
(global as any).CARRY = 'carry';
|
||||
(global as any).MOVE = 'move';
|
||||
|
||||
// RoomPosition constructor mock
|
||||
(global as any).RoomPosition = class RoomPosition {
|
||||
x: number;
|
||||
y: number;
|
||||
roomName: string;
|
||||
|
||||
constructor(x: number, y: number, roomName: string) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.roomName = roomName;
|
||||
}
|
||||
|
||||
getRangeTo(_target: any): number {
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
|
||||
// Game mock
|
||||
(global as any).Game = {
|
||||
getObjectById: jest.fn(),
|
||||
};
|
||||
|
||||
export {};
|
||||
371
tests/roleHandlers/harvester.handler.test.ts
Normal file
371
tests/roleHandlers/harvester.handler.test.ts
Normal file
@@ -0,0 +1,371 @@
|
||||
import HarvesterHandler from '@/roleHandlers/harvester.handler';
|
||||
import * as getById from '@/utils/funcs/getById';
|
||||
import {
|
||||
PositionSpotStatus,
|
||||
createSourcePositionMatrix,
|
||||
} from '@/utils/positions';
|
||||
import '~/tests/__mocks__/screeps';
|
||||
|
||||
// ─── Helpers ────────────────────────────────────────────────────────────────
|
||||
|
||||
function makeMatrix(fill: string = PositionSpotStatus.EMPTY) {
|
||||
const m = createSourcePositionMatrix(fill as any);
|
||||
return m;
|
||||
}
|
||||
|
||||
function makeState(overrides: Partial<GameState> = {}): GameState {
|
||||
const spots = makeMatrix();
|
||||
return {
|
||||
sourcesStates: {
|
||||
src1: { spots },
|
||||
},
|
||||
...overrides,
|
||||
} as unknown as GameState;
|
||||
}
|
||||
|
||||
function makeCreepMemory(overrides: Partial<CreepMemory> = {}): CreepMemory {
|
||||
return {
|
||||
role: 'harvester',
|
||||
spawnId: 'spawn1',
|
||||
...overrides,
|
||||
} as CreepMemory;
|
||||
}
|
||||
|
||||
function makeStore(used: number, capacity: number) {
|
||||
return {
|
||||
getFreeCapacity: (_res: string) => capacity - used,
|
||||
getUsedCapacity: (_res: string) => used,
|
||||
};
|
||||
}
|
||||
|
||||
function makePos(x = 0, y = 0, roomName = 'W1N1') {
|
||||
return new (global as any).RoomPosition(x, y, roomName);
|
||||
}
|
||||
|
||||
function makeSource(id = 'src1', x = 5, y = 5, roomName = 'W1N1') {
|
||||
return {
|
||||
id,
|
||||
pos: makePos(x, y, roomName),
|
||||
} as unknown as Source;
|
||||
}
|
||||
|
||||
function makeSpawn(id = 'spawn1') {
|
||||
return { id } as unknown as StructureSpawn;
|
||||
}
|
||||
|
||||
function makeCreep(
|
||||
memoryOverrides: Partial<CreepMemory> = {},
|
||||
storeUsed = 0,
|
||||
storeCapacity = 50
|
||||
): Creep {
|
||||
const memory = makeCreepMemory(memoryOverrides);
|
||||
return {
|
||||
name: 'Creep1',
|
||||
memory,
|
||||
store: makeStore(storeUsed, storeCapacity),
|
||||
pos: makePos(),
|
||||
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;
|
||||
}
|
||||
|
||||
// ─── Tests ───────────────────────────────────────────────────────────────────
|
||||
|
||||
describe('HarvesterHandler', () => {
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
jest.spyOn(console, 'log').mockImplementation(() => {});
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
jest.restoreAllMocks();
|
||||
});
|
||||
|
||||
// ── destroy ──────────────────────────────────────────────────────────────
|
||||
|
||||
describe('destroy', () => {
|
||||
it('releases the source spot from destination and clears it', () => {
|
||||
const state = makeState();
|
||||
state.sourcesStates['src1'].spots[0] = PositionSpotStatus.OCCUPIED;
|
||||
|
||||
const memory = makeCreepMemory({
|
||||
destination: { type: 'source', id: 'src1', sourceSpot: 0 },
|
||||
});
|
||||
|
||||
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 = makeState();
|
||||
state.sourcesStates['src1'].spots[2] = PositionSpotStatus.OCCUPIED;
|
||||
|
||||
const memory = makeCreepMemory({
|
||||
previousDestination: {
|
||||
type: 'source',
|
||||
id: 'src1',
|
||||
sourceSpot: 2,
|
||||
},
|
||||
});
|
||||
|
||||
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 = makeState();
|
||||
const memory = makeCreepMemory({
|
||||
destination: { type: 'spawn', id: 'spawn1' },
|
||||
});
|
||||
|
||||
HarvesterHandler.destroy(memory, state);
|
||||
|
||||
// None of the source spots should have changed
|
||||
expect(
|
||||
state.sourcesStates['src1'].spots.every(
|
||||
(s) =>
|
||||
s === PositionSpotStatus.EMPTY ||
|
||||
s === PositionSpotStatus.CENTER
|
||||
)
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it('does nothing when memory has no destination', () => {
|
||||
const state = makeState();
|
||||
const memory = makeCreepMemory();
|
||||
|
||||
expect(() => HarvesterHandler.destroy(memory, state)).not.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
// ── validateCreepMemory (via run) ────────────────────────────────────────
|
||||
|
||||
describe('validateCreepMemory (via run)', () => {
|
||||
it('releases previousDestination source spot and deletes it', () => {
|
||||
const state = makeState();
|
||||
state.sourcesStates['src1'].spots[1] = PositionSpotStatus.OCCUPIED;
|
||||
|
||||
const creep = makeCreep({
|
||||
previousDestination: {
|
||||
type: 'source',
|
||||
id: 'src1',
|
||||
sourceSpot: 1,
|
||||
},
|
||||
});
|
||||
|
||||
jest.spyOn(getById, 'getSourceById').mockReturnValue(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 = makeState();
|
||||
const source = makeSource();
|
||||
jest.spyOn(getById, 'getSourceById').mockReturnValue(source);
|
||||
jest.spyOn(getById, 'getSpawnById').mockReturnValue(makeSpawn());
|
||||
|
||||
const creep = makeCreep(
|
||||
{
|
||||
destination: { type: 'source', id: 'src1', sourceSpot: 0 },
|
||||
spawnId: 'spawn1',
|
||||
},
|
||||
50, // used = full
|
||||
50
|
||||
);
|
||||
|
||||
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 = makeState();
|
||||
jest.spyOn(getById, 'getSpawnById').mockReturnValue(makeSpawn());
|
||||
|
||||
const creep = makeCreep(
|
||||
{
|
||||
destination: { type: 'spawn', id: 'spawn1' },
|
||||
},
|
||||
0, // used = 0
|
||||
50
|
||||
);
|
||||
|
||||
HarvesterHandler.run(creep, state);
|
||||
|
||||
expect(creep.memory.destination).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
// ── onFindNewSource ───────────────────────────────────────────────────────
|
||||
|
||||
describe('onFindNewSource', () => {
|
||||
it('assigns the first available source spot to the creep', () => {
|
||||
const state = makeState();
|
||||
jest.spyOn(getById, 'getSourceById').mockReturnValue(makeSource());
|
||||
|
||||
const creep = makeCreep(); // no destination, empty store
|
||||
|
||||
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 = makeState();
|
||||
// Fill all non-center spots with OCCUPIED
|
||||
state.sourcesStates['src1'].spots = state.sourcesStates[
|
||||
'src1'
|
||||
].spots.map((s) =>
|
||||
s === PositionSpotStatus.CENTER
|
||||
? s
|
||||
: PositionSpotStatus.OCCUPIED
|
||||
) as any;
|
||||
|
||||
jest.spyOn(getById, 'getSourceById').mockReturnValue(makeSource());
|
||||
const creep = makeCreep();
|
||||
|
||||
HarvesterHandler.run(creep, state);
|
||||
|
||||
expect(creep.memory.destination).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
// ── onSourceDestination ───────────────────────────────────────────────────
|
||||
|
||||
describe('onSourceDestination', () => {
|
||||
it('harvests when in range', () => {
|
||||
const state = makeState();
|
||||
const source = makeSource();
|
||||
jest.spyOn(getById, 'getSourceById').mockReturnValue(source);
|
||||
|
||||
const creep = makeCreep(
|
||||
{ destination: { type: 'source', id: 'src1', sourceSpot: 0 } },
|
||||
0,
|
||||
50
|
||||
);
|
||||
(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 = makeState();
|
||||
const source = makeSource();
|
||||
jest.spyOn(getById, 'getSourceById').mockReturnValue(source);
|
||||
|
||||
const creep = makeCreep(
|
||||
{ destination: { type: 'source', id: 'src1', sourceSpot: 0 } },
|
||||
0,
|
||||
50
|
||||
);
|
||||
(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 = makeState();
|
||||
jest.spyOn(getById, 'getSourceById').mockReturnValue(null);
|
||||
|
||||
const creep = makeCreep(
|
||||
{
|
||||
destination: {
|
||||
type: 'source',
|
||||
id: 'unknown',
|
||||
sourceSpot: 0,
|
||||
},
|
||||
},
|
||||
0,
|
||||
50
|
||||
);
|
||||
|
||||
expect(() => HarvesterHandler.run(creep, state)).not.toThrow();
|
||||
expect(creep.harvest).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
// ── onSpawnDestination ────────────────────────────────────────────────────
|
||||
|
||||
describe('onSpawnDestination', () => {
|
||||
it('transfers energy when in range', () => {
|
||||
const state = makeState();
|
||||
const spawn = makeSpawn();
|
||||
jest.spyOn(getById, 'getSpawnById').mockReturnValue(spawn);
|
||||
|
||||
const creep = makeCreep(
|
||||
{ destination: { type: 'spawn', id: 'spawn1' } },
|
||||
50,
|
||||
50
|
||||
);
|
||||
(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 = makeState();
|
||||
const spawn = makeSpawn();
|
||||
jest.spyOn(getById, 'getSpawnById').mockReturnValue(spawn);
|
||||
|
||||
const creep = makeCreep(
|
||||
{ destination: { type: 'spawn', id: 'spawn1' } },
|
||||
50,
|
||||
50
|
||||
);
|
||||
(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 = makeState();
|
||||
jest.spyOn(getById, 'getSpawnById').mockReturnValue(null);
|
||||
|
||||
const creep = makeCreep(
|
||||
{ destination: { type: 'spawn', id: 'spawn1' } },
|
||||
50,
|
||||
50
|
||||
);
|
||||
|
||||
expect(() => HarvesterHandler.run(creep, state)).not.toThrow();
|
||||
expect(creep.transfer).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
});
|
||||
404
tests/roleHandlers/upgrader.handler.test.ts
Normal file
404
tests/roleHandlers/upgrader.handler.test.ts
Normal file
@@ -0,0 +1,404 @@
|
||||
import UpgraderHandler from '@/roleHandlers/upgrader.handler';
|
||||
import * as getById from '@/utils/funcs/getById';
|
||||
import {
|
||||
PositionSpotStatus,
|
||||
createSourcePositionMatrix,
|
||||
} from '@/utils/positions';
|
||||
import '~/tests/__mocks__/screeps';
|
||||
|
||||
// ─── Helpers ────────────────────────────────────────────────────────────────
|
||||
|
||||
function makeState(overrides: Partial<GameState> = {}): GameState {
|
||||
const spots = createSourcePositionMatrix(PositionSpotStatus.EMPTY);
|
||||
return {
|
||||
sourcesStates: {
|
||||
src1: { spots },
|
||||
},
|
||||
...overrides,
|
||||
} as unknown as GameState;
|
||||
}
|
||||
|
||||
function makeCreepMemory(overrides: Partial<CreepMemory> = {}): CreepMemory {
|
||||
return {
|
||||
role: 'upgrader',
|
||||
spawnId: 'spawn1',
|
||||
...overrides,
|
||||
} as CreepMemory;
|
||||
}
|
||||
|
||||
function makeStore(used: number, capacity: number) {
|
||||
return {
|
||||
getFreeCapacity: (_res: string) => capacity - used,
|
||||
getUsedCapacity: (_res: string) => used,
|
||||
};
|
||||
}
|
||||
|
||||
function makePos(x = 0, y = 0, roomName = 'W1N1') {
|
||||
return new (global as any).RoomPosition(x, y, roomName);
|
||||
}
|
||||
|
||||
function makeSource(id = 'src1', x = 5, y = 5, roomName = 'W1N1') {
|
||||
return {
|
||||
id,
|
||||
pos: makePos(x, y, roomName),
|
||||
} as unknown as Source;
|
||||
}
|
||||
|
||||
function makeController(id = 'ctrl1') {
|
||||
return { id } as unknown as StructureController;
|
||||
}
|
||||
|
||||
function makeCreep(
|
||||
memoryOverrides: Partial<CreepMemory> = {},
|
||||
storeUsed = 0,
|
||||
storeCapacity = 50,
|
||||
controller: StructureController | null = null
|
||||
): Creep {
|
||||
const memory = makeCreepMemory(memoryOverrides);
|
||||
return {
|
||||
name: 'Creep1',
|
||||
memory,
|
||||
store: makeStore(storeUsed, storeCapacity),
|
||||
pos: makePos(),
|
||||
room: {
|
||||
controller:
|
||||
controller !== null ? (controller ?? makeController()) : null,
|
||||
},
|
||||
harvest: jest.fn().mockReturnValue((global as any).OK),
|
||||
moveTo: jest.fn().mockReturnValue((global as any).OK),
|
||||
upgradeController: jest.fn().mockReturnValue((global as any).OK),
|
||||
} as unknown as Creep;
|
||||
}
|
||||
|
||||
// ─── Tests ───────────────────────────────────────────────────────────────────
|
||||
|
||||
describe('UpgraderHandler', () => {
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
jest.spyOn(console, 'log').mockImplementation(() => {});
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
jest.restoreAllMocks();
|
||||
});
|
||||
|
||||
// ── destroy ──────────────────────────────────────────────────────────────
|
||||
|
||||
describe('destroy', () => {
|
||||
it('releases the source spot from destination and clears it', () => {
|
||||
const state = makeState();
|
||||
state.sourcesStates['src1'].spots[0] = PositionSpotStatus.OCCUPIED;
|
||||
|
||||
const memory = makeCreepMemory({
|
||||
destination: { type: 'source', id: 'src1', sourceSpot: 0 },
|
||||
});
|
||||
|
||||
UpgraderHandler.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 = makeState();
|
||||
state.sourcesStates['src1'].spots[3] = PositionSpotStatus.OCCUPIED;
|
||||
|
||||
const memory = makeCreepMemory({
|
||||
previousDestination: {
|
||||
type: 'source',
|
||||
id: 'src1',
|
||||
sourceSpot: 3,
|
||||
},
|
||||
});
|
||||
|
||||
UpgraderHandler.destroy(memory, state);
|
||||
|
||||
expect(state.sourcesStates['src1'].spots[3]).toBe(
|
||||
PositionSpotStatus.EMPTY
|
||||
);
|
||||
expect(memory.previousDestination).toBeUndefined();
|
||||
});
|
||||
|
||||
it('does nothing when destination is not a source', () => {
|
||||
const state = makeState();
|
||||
const memory = makeCreepMemory({
|
||||
destination: { type: 'controller', id: 'ctrl1' },
|
||||
});
|
||||
|
||||
UpgraderHandler.destroy(memory, state);
|
||||
|
||||
expect(
|
||||
state.sourcesStates['src1'].spots.every(
|
||||
(s) =>
|
||||
s === PositionSpotStatus.EMPTY ||
|
||||
s === PositionSpotStatus.CENTER
|
||||
)
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it('does nothing when memory has no destination', () => {
|
||||
const state = makeState();
|
||||
const memory = makeCreepMemory();
|
||||
|
||||
expect(() => UpgraderHandler.destroy(memory, state)).not.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
// ── validateCreepMemory (via run) ────────────────────────────────────────
|
||||
|
||||
describe('validateCreepMemory (via run)', () => {
|
||||
it('releases previousDestination source spot and deletes it', () => {
|
||||
const state = makeState();
|
||||
state.sourcesStates['src1'].spots[1] = PositionSpotStatus.OCCUPIED;
|
||||
|
||||
const creep = makeCreep({
|
||||
previousDestination: {
|
||||
type: 'source',
|
||||
id: 'src1',
|
||||
sourceSpot: 1,
|
||||
},
|
||||
});
|
||||
|
||||
jest.spyOn(getById, 'getSourceById').mockReturnValue(null);
|
||||
|
||||
UpgraderHandler.run(creep, state);
|
||||
|
||||
expect(state.sourcesStates['src1'].spots[1]).toBe(
|
||||
PositionSpotStatus.EMPTY
|
||||
);
|
||||
expect(creep.memory.previousDestination).toBeUndefined();
|
||||
});
|
||||
|
||||
it('transitions from source to controller destination when energy is full', () => {
|
||||
const state = makeState();
|
||||
const source = makeSource();
|
||||
jest.spyOn(getById, 'getSourceById').mockReturnValue(source);
|
||||
|
||||
const controller = makeController();
|
||||
jest.spyOn(getById, 'getControllerById').mockReturnValue(
|
||||
controller
|
||||
);
|
||||
|
||||
const creep = makeCreep(
|
||||
{
|
||||
destination: { type: 'source', id: 'src1', sourceSpot: 0 },
|
||||
},
|
||||
50, // full
|
||||
50,
|
||||
controller
|
||||
);
|
||||
|
||||
UpgraderHandler.run(creep, state);
|
||||
|
||||
expect(creep.memory.destination?.type).toBe('controller');
|
||||
expect(creep.memory.previousDestination?.type).toBe('source');
|
||||
});
|
||||
|
||||
it('clears controller destination when energy is empty', () => {
|
||||
const state = makeState();
|
||||
const controller = makeController();
|
||||
jest.spyOn(getById, 'getControllerById').mockReturnValue(
|
||||
controller
|
||||
);
|
||||
|
||||
const creep = makeCreep(
|
||||
{ destination: { type: 'controller', id: 'ctrl1' } },
|
||||
0, // empty
|
||||
50,
|
||||
controller
|
||||
);
|
||||
|
||||
UpgraderHandler.run(creep, state);
|
||||
|
||||
expect(creep.memory.destination).toBeUndefined();
|
||||
});
|
||||
|
||||
it('clears destination and logs when room has no controller during source→controller transition', () => {
|
||||
const state = makeState();
|
||||
// Occupy all spots so onFindNewSource cannot reassign a source
|
||||
state.sourcesStates['src1'].spots = state.sourcesStates[
|
||||
'src1'
|
||||
].spots.map((s) =>
|
||||
s === PositionSpotStatus.CENTER
|
||||
? s
|
||||
: PositionSpotStatus.OCCUPIED
|
||||
) as any;
|
||||
|
||||
jest.spyOn(getById, 'getSourceById').mockReturnValue(makeSource());
|
||||
|
||||
const creep = makeCreep(
|
||||
{ destination: { type: 'source', id: 'src1', sourceSpot: 0 } },
|
||||
50,
|
||||
50,
|
||||
null // no controller in room
|
||||
);
|
||||
|
||||
UpgraderHandler.run(creep, state);
|
||||
|
||||
expect(creep.memory.destination).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
// ── onFindNewSource ───────────────────────────────────────────────────────
|
||||
|
||||
describe('onFindNewSource', () => {
|
||||
it('assigns the first available source spot to the creep', () => {
|
||||
const state = makeState();
|
||||
jest.spyOn(getById, 'getSourceById').mockReturnValue(makeSource());
|
||||
|
||||
const creep = makeCreep(); // no destination, empty store
|
||||
|
||||
UpgraderHandler.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 = makeState();
|
||||
state.sourcesStates['src1'].spots = state.sourcesStates[
|
||||
'src1'
|
||||
].spots.map((s) =>
|
||||
s === PositionSpotStatus.CENTER
|
||||
? s
|
||||
: PositionSpotStatus.OCCUPIED
|
||||
) as any;
|
||||
|
||||
jest.spyOn(getById, 'getSourceById').mockReturnValue(makeSource());
|
||||
const creep = makeCreep();
|
||||
|
||||
UpgraderHandler.run(creep, state);
|
||||
|
||||
expect(creep.memory.destination).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
// ── onSourceDestination ───────────────────────────────────────────────────
|
||||
|
||||
describe('onSourceDestination', () => {
|
||||
it('harvests when in range', () => {
|
||||
const state = makeState();
|
||||
const source = makeSource();
|
||||
jest.spyOn(getById, 'getSourceById').mockReturnValue(source);
|
||||
|
||||
const creep = makeCreep(
|
||||
{ destination: { type: 'source', id: 'src1', sourceSpot: 0 } },
|
||||
0,
|
||||
50
|
||||
);
|
||||
(creep.harvest as jest.Mock).mockReturnValue((global as any).OK);
|
||||
|
||||
UpgraderHandler.run(creep, state);
|
||||
|
||||
expect(creep.harvest).toHaveBeenCalledWith(source);
|
||||
expect(creep.moveTo).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('moves to source when not in range', () => {
|
||||
const state = makeState();
|
||||
const source = makeSource();
|
||||
jest.spyOn(getById, 'getSourceById').mockReturnValue(source);
|
||||
|
||||
const creep = makeCreep(
|
||||
{ destination: { type: 'source', id: 'src1', sourceSpot: 0 } },
|
||||
0,
|
||||
50
|
||||
);
|
||||
(creep.harvest as jest.Mock).mockReturnValue(
|
||||
(global as any).ERR_NOT_IN_RANGE
|
||||
);
|
||||
|
||||
UpgraderHandler.run(creep, state);
|
||||
|
||||
expect(creep.moveTo).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('logs and returns when source is not found', () => {
|
||||
const state = makeState();
|
||||
jest.spyOn(getById, 'getSourceById').mockReturnValue(null);
|
||||
|
||||
const creep = makeCreep(
|
||||
{
|
||||
destination: {
|
||||
type: 'source',
|
||||
id: 'unknown',
|
||||
sourceSpot: 0,
|
||||
},
|
||||
},
|
||||
0,
|
||||
50
|
||||
);
|
||||
|
||||
expect(() => UpgraderHandler.run(creep, state)).not.toThrow();
|
||||
expect(creep.harvest).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
// ── onControllerDestination ───────────────────────────────────────────────
|
||||
|
||||
describe('onControllerDestination', () => {
|
||||
it('upgrades controller when in range', () => {
|
||||
const state = makeState();
|
||||
const controller = makeController();
|
||||
jest.spyOn(getById, 'getControllerById').mockReturnValue(
|
||||
controller
|
||||
);
|
||||
|
||||
const creep = makeCreep(
|
||||
{ destination: { type: 'controller', id: 'ctrl1' } },
|
||||
50,
|
||||
50,
|
||||
controller
|
||||
);
|
||||
(creep.upgradeController as jest.Mock).mockReturnValue(
|
||||
(global as any).OK
|
||||
);
|
||||
|
||||
UpgraderHandler.run(creep, state);
|
||||
|
||||
expect(creep.upgradeController).toHaveBeenCalledWith(controller);
|
||||
expect(creep.moveTo).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('moves to controller when not in range', () => {
|
||||
const state = makeState();
|
||||
const controller = makeController();
|
||||
jest.spyOn(getById, 'getControllerById').mockReturnValue(
|
||||
controller
|
||||
);
|
||||
|
||||
const creep = makeCreep(
|
||||
{ destination: { type: 'controller', id: 'ctrl1' } },
|
||||
50,
|
||||
50,
|
||||
controller
|
||||
);
|
||||
(creep.upgradeController as jest.Mock).mockReturnValue(
|
||||
(global as any).ERR_NOT_IN_RANGE
|
||||
);
|
||||
|
||||
UpgraderHandler.run(creep, state);
|
||||
|
||||
expect(creep.moveTo).toHaveBeenCalledWith(
|
||||
controller,
|
||||
expect.any(Object)
|
||||
);
|
||||
});
|
||||
|
||||
it('logs and returns when controller is not found by ID', () => {
|
||||
const state = makeState();
|
||||
jest.spyOn(getById, 'getControllerById').mockReturnValue(null);
|
||||
|
||||
const creep = makeCreep(
|
||||
{ destination: { type: 'controller', id: 'ctrl1' } },
|
||||
50,
|
||||
50
|
||||
);
|
||||
|
||||
expect(() => UpgraderHandler.run(creep, state)).not.toThrow();
|
||||
expect(creep.upgradeController).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user