Compare commits
5 Commits
main
...
feature/im
| Author | SHA1 | Date | |
|---|---|---|---|
|
96cdb9ccf2
|
|||
|
b9d190c978
|
|||
|
754c6a1313
|
|||
|
4b6149da21
|
|||
|
50937d7e0c
|
@@ -44,7 +44,7 @@
|
|||||||
"jest": {
|
"jest": {
|
||||||
"testEnvironment": "node",
|
"testEnvironment": "node",
|
||||||
"transform": {
|
"transform": {
|
||||||
"^.+\\.(ts|tsx)$": "ts-jest"
|
"^.+\\.(ts|tsx)$": ["ts-jest", { "tsconfig": "tsconfig.test.json" }]
|
||||||
},
|
},
|
||||||
"moduleNameMapper": {
|
"moduleNameMapper": {
|
||||||
"\\.(png|jpg|jpeg|gif|webp|svg|ico)$": "<rootDir>/tests/setup/__mocks__/fileMock.js",
|
"\\.(png|jpg|jpeg|gif|webp|svg|ico)$": "<rootDir>/tests/setup/__mocks__/fileMock.js",
|
||||||
|
|||||||
@@ -9,25 +9,19 @@ import {
|
|||||||
|
|
||||||
class RoomInspector {
|
class RoomInspector {
|
||||||
public static inspectState(room: Room, state: GameState): GameState {
|
public static inspectState(room: Room, state: GameState): GameState {
|
||||||
if (!this.stateWasInitialized(state)) {
|
if (!state.sourcesStates) {
|
||||||
state = this.initializeState(room, state);
|
this.initializeState(room, state);
|
||||||
}
|
}
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static stateWasInitialized(state: GameState): boolean {
|
private static initializeState(room: Room, state: GameState): void {
|
||||||
return !!state.sourcesStates;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static initializeState(room: Room, state: GameState): GameState {
|
|
||||||
state.sourcesStates = {};
|
state.sourcesStates = {};
|
||||||
state.maxHarvesters = 0;
|
state.maxHarvesters = 0;
|
||||||
|
|
||||||
for (const source of room.find(FIND_SOURCES)) {
|
for (const source of room.find(FIND_SOURCES)) {
|
||||||
this.configureSourceState(source, state);
|
this.configureSourceState(source, state);
|
||||||
}
|
}
|
||||||
|
|
||||||
return state;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static configureSourceState(
|
private static configureSourceState(
|
||||||
|
|||||||
@@ -1,12 +0,0 @@
|
|||||||
export abstract class RoleHandler {
|
|
||||||
public static destroy(_creepMemory: CreepMemory, _state: GameState): void {
|
|
||||||
// Default implementation does nothing
|
|
||||||
// Subclasses should override this method
|
|
||||||
}
|
|
||||||
|
|
||||||
public static run(_creep: Creep, state: GameState): GameState {
|
|
||||||
// Default implementation returns state unchanged
|
|
||||||
// Subclasses should override this method
|
|
||||||
return state;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
9
src/roleHandlers/base.handler.ts
Normal file
9
src/roleHandlers/base.handler.ts
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
export abstract class RoleHandler {
|
||||||
|
public static destroy(_creepMemory: CreepMemory, _state: GameState): void {
|
||||||
|
throw Error('not implemented');
|
||||||
|
}
|
||||||
|
|
||||||
|
public static run(_creep: Creep, _state: GameState): GameState {
|
||||||
|
throw Error('not implemented');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
import { RoleHandler } from './BaseHandler.interface';
|
import { RoleHandler } from './base.handler';
|
||||||
import { SourceDestination } from '@/types/creeps';
|
import { SourceDestination } from '@/types/creeps';
|
||||||
import { getSourceById, getSpawnById } from '@/utils/funcs/getById';
|
import { getSourceById, getSpawnById } from '@/utils/funcs/getById';
|
||||||
import {
|
import {
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { RoleHandler } from './BaseHandler.interface';
|
import { RoleHandler } from './base.handler';
|
||||||
import HarvesterHandler from './harvester.handler';
|
import HarvesterHandler from './harvester.handler';
|
||||||
import UpgraderHandler from './upgrader.handler';
|
import UpgraderHandler from './upgrader.handler';
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { RoleHandler } from './BaseHandler.interface';
|
import { RoleHandler } from './base.handler';
|
||||||
import { SourceDestination } from '@/types/creeps';
|
import { SourceDestination } from '@/types/creeps';
|
||||||
import { getControllerById, getSourceById } from '@/utils/funcs/getById';
|
import { getControllerById, getSourceById } from '@/utils/funcs/getById';
|
||||||
import {
|
import {
|
||||||
|
|||||||
@@ -27,6 +27,9 @@ export const CreepRoles = {
|
|||||||
handler: HarvesterHandler,
|
handler: HarvesterHandler,
|
||||||
priority: 3,
|
priority: 3,
|
||||||
},
|
},
|
||||||
|
defender: {
|
||||||
|
name: 'defender',
|
||||||
|
}
|
||||||
} satisfies Record<string, RoleDefinition>;
|
} satisfies Record<string, RoleDefinition>;
|
||||||
|
|
||||||
export type CreepRole = keyof typeof CreepRoles;
|
export type CreepRole = keyof typeof CreepRoles;
|
||||||
|
|||||||
@@ -6,74 +6,73 @@ import {
|
|||||||
} from '@/utils/positions';
|
} from '@/utils/positions';
|
||||||
import '~/tests/__mocks__/screeps';
|
import '~/tests/__mocks__/screeps';
|
||||||
|
|
||||||
// ─── Helpers ────────────────────────────────────────────────────────────────
|
describe('HarvesterHandler', () => {
|
||||||
|
type FixtureOptions = {
|
||||||
function makeMatrix(fill: string = PositionSpotStatus.EMPTY) {
|
memory?: Partial<CreepMemory>;
|
||||||
const m = createSourcePositionMatrix(fill as any);
|
storeUsed?: number;
|
||||||
return m;
|
storeCapacity?: number;
|
||||||
}
|
stateOverrides?: Partial<GameState>;
|
||||||
|
sourceId?: string;
|
||||||
function makeState(overrides: Partial<GameState> = {}): GameState {
|
spawnId?: string;
|
||||||
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') {
|
const createFixture = ({
|
||||||
return new (global as any).RoomPosition(x, y, roomName);
|
memory = {},
|
||||||
}
|
|
||||||
|
|
||||||
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,
|
storeUsed = 0,
|
||||||
storeCapacity = 50
|
storeCapacity = 50,
|
||||||
): Creep {
|
stateOverrides = {},
|
||||||
const memory = makeCreepMemory(memoryOverrides);
|
sourceId = 'src1',
|
||||||
return {
|
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',
|
name: 'Creep1',
|
||||||
memory,
|
memory: {
|
||||||
store: makeStore(storeUsed, storeCapacity),
|
role: 'harvester',
|
||||||
pos: makePos(),
|
spawnId,
|
||||||
|
...memory,
|
||||||
|
} as CreepMemory,
|
||||||
|
store: {
|
||||||
|
getFreeCapacity: (_res: string) => storeCapacity - storeUsed,
|
||||||
|
getUsedCapacity: (_res: string) => storeUsed,
|
||||||
|
},
|
||||||
|
pos: new (global as any).RoomPosition(0, 0, 'W1N1'),
|
||||||
room: {},
|
room: {},
|
||||||
harvest: jest.fn().mockReturnValue((global as any).OK),
|
harvest: jest.fn().mockReturnValue((global as any).OK),
|
||||||
moveTo: jest.fn().mockReturnValue((global as any).OK),
|
moveTo: jest.fn().mockReturnValue((global as any).OK),
|
||||||
transfer: jest.fn().mockReturnValue((global as any).OK),
|
transfer: jest.fn().mockReturnValue((global as any).OK),
|
||||||
} as unknown as Creep;
|
} as unknown as Creep;
|
||||||
}
|
|
||||||
|
|
||||||
// ─── Tests ───────────────────────────────────────────────────────────────────
|
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),
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
describe('HarvesterHandler', () => {
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
jest.clearAllMocks();
|
jest.clearAllMocks();
|
||||||
jest.spyOn(console, 'log').mockImplementation(() => {});
|
jest.spyOn(console, 'log').mockImplementation(() => {});
|
||||||
@@ -83,16 +82,14 @@ describe('HarvesterHandler', () => {
|
|||||||
jest.restoreAllMocks();
|
jest.restoreAllMocks();
|
||||||
});
|
});
|
||||||
|
|
||||||
// ── destroy ──────────────────────────────────────────────────────────────
|
|
||||||
|
|
||||||
describe('destroy', () => {
|
describe('destroy', () => {
|
||||||
it('releases the source spot from destination and clears it', () => {
|
it('releases the source spot from destination and clears it', () => {
|
||||||
const state = makeState();
|
const { state, memory } = createFixture({
|
||||||
state.sourcesStates['src1'].spots[0] = PositionSpotStatus.OCCUPIED;
|
memory: {
|
||||||
|
|
||||||
const memory = makeCreepMemory({
|
|
||||||
destination: { type: 'source', id: 'src1', sourceSpot: 0 },
|
destination: { type: 'source', id: 'src1', sourceSpot: 0 },
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
state.sourcesStates['src1'].spots[0] = PositionSpotStatus.OCCUPIED;
|
||||||
|
|
||||||
HarvesterHandler.destroy(memory, state);
|
HarvesterHandler.destroy(memory, state);
|
||||||
|
|
||||||
@@ -103,16 +100,16 @@ describe('HarvesterHandler', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('releases the source spot from previousDestination and clears it', () => {
|
it('releases the source spot from previousDestination and clears it', () => {
|
||||||
const state = makeState();
|
const { state, memory } = createFixture({
|
||||||
state.sourcesStates['src1'].spots[2] = PositionSpotStatus.OCCUPIED;
|
memory: {
|
||||||
|
|
||||||
const memory = makeCreepMemory({
|
|
||||||
previousDestination: {
|
previousDestination: {
|
||||||
type: 'source',
|
type: 'source',
|
||||||
id: 'src1',
|
id: 'src1',
|
||||||
sourceSpot: 2,
|
sourceSpot: 2,
|
||||||
},
|
},
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
state.sourcesStates['src1'].spots[2] = PositionSpotStatus.OCCUPIED;
|
||||||
|
|
||||||
HarvesterHandler.destroy(memory, state);
|
HarvesterHandler.destroy(memory, state);
|
||||||
|
|
||||||
@@ -122,15 +119,41 @@ describe('HarvesterHandler', () => {
|
|||||||
expect(memory.previousDestination).toBeUndefined();
|
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', () => {
|
it('does nothing when destination is not a source', () => {
|
||||||
const state = makeState();
|
const { state, memory } = createFixture({
|
||||||
const memory = makeCreepMemory({
|
memory: {
|
||||||
destination: { type: 'spawn', id: 'spawn1' },
|
destination: { type: 'spawn', id: 'spawn1' },
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
HarvesterHandler.destroy(memory, state);
|
HarvesterHandler.destroy(memory, state);
|
||||||
|
|
||||||
// None of the source spots should have changed
|
|
||||||
expect(
|
expect(
|
||||||
state.sourcesStates['src1'].spots.every(
|
state.sourcesStates['src1'].spots.every(
|
||||||
(s: PositionSpotStatus) =>
|
(s: PositionSpotStatus) =>
|
||||||
@@ -141,29 +164,40 @@ describe('HarvesterHandler', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('does nothing when memory has no destination', () => {
|
it('does nothing when memory has no destination', () => {
|
||||||
const state = makeState();
|
const { state, memory } = createFixture();
|
||||||
const memory = makeCreepMemory();
|
|
||||||
|
|
||||||
expect(() => HarvesterHandler.destroy(memory, state)).not.toThrow();
|
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,
|
||||||
|
},
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
// ── validateCreepMemory (via run) ────────────────────────────────────────
|
expect(() => HarvesterHandler.destroy(memory, state)).not.toThrow();
|
||||||
|
expect(memory.destination).toBeUndefined();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('validateCreepMemory (via run)', () => {
|
describe('validateCreepMemory (via run)', () => {
|
||||||
it('releases previousDestination source spot and deletes it', () => {
|
it('releases previousDestination source spot and deletes it', () => {
|
||||||
const state = makeState();
|
const { state, creep, mockSource } = createFixture({
|
||||||
state.sourcesStates['src1'].spots[1] = PositionSpotStatus.OCCUPIED;
|
memory: {
|
||||||
|
|
||||||
const creep = makeCreep({
|
|
||||||
previousDestination: {
|
previousDestination: {
|
||||||
type: 'source',
|
type: 'source',
|
||||||
id: 'src1',
|
id: 'src1',
|
||||||
sourceSpot: 1,
|
sourceSpot: 1,
|
||||||
},
|
},
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
state.sourcesStates['src1'].spots[1] = PositionSpotStatus.OCCUPIED;
|
||||||
jest.spyOn(getById, 'getSourceById').mockReturnValue(null);
|
mockSource(null);
|
||||||
|
|
||||||
HarvesterHandler.run(creep, state);
|
HarvesterHandler.run(creep, state);
|
||||||
|
|
||||||
@@ -174,19 +208,16 @@ describe('HarvesterHandler', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('transitions from source to spawn destination when energy is full', () => {
|
it('transitions from source to spawn destination when energy is full', () => {
|
||||||
const state = makeState();
|
const { state, creep, mockSource, mockSpawn } = createFixture({
|
||||||
const source = makeSource();
|
memory: {
|
||||||
jest.spyOn(getById, 'getSourceById').mockReturnValue(source);
|
|
||||||
jest.spyOn(getById, 'getSpawnById').mockReturnValue(makeSpawn());
|
|
||||||
|
|
||||||
const creep = makeCreep(
|
|
||||||
{
|
|
||||||
destination: { type: 'source', id: 'src1', sourceSpot: 0 },
|
destination: { type: 'source', id: 'src1', sourceSpot: 0 },
|
||||||
spawnId: 'spawn1',
|
spawnId: 'spawn1',
|
||||||
},
|
},
|
||||||
50, // used = full
|
storeUsed: 50,
|
||||||
50
|
storeCapacity: 50,
|
||||||
);
|
});
|
||||||
|
mockSource();
|
||||||
|
mockSpawn();
|
||||||
|
|
||||||
HarvesterHandler.run(creep, state);
|
HarvesterHandler.run(creep, state);
|
||||||
|
|
||||||
@@ -195,16 +226,16 @@ describe('HarvesterHandler', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('clears spawn destination when energy is empty', () => {
|
it('clears spawn destination when energy is empty', () => {
|
||||||
const state = makeState();
|
const { state, creep, mockSpawn, mockSource } = createFixture({
|
||||||
jest.spyOn(getById, 'getSpawnById').mockReturnValue(makeSpawn());
|
memory: {
|
||||||
|
|
||||||
const creep = makeCreep(
|
|
||||||
{
|
|
||||||
destination: { type: 'spawn', id: 'spawn1' },
|
destination: { type: 'spawn', id: 'spawn1' },
|
||||||
},
|
},
|
||||||
0, // used = 0
|
storeUsed: 0,
|
||||||
50
|
storeCapacity: 50,
|
||||||
);
|
});
|
||||||
|
mockSpawn();
|
||||||
|
// Explicitly return null so onFindNewSource finds nothing
|
||||||
|
mockSource(null);
|
||||||
|
|
||||||
HarvesterHandler.run(creep, state);
|
HarvesterHandler.run(creep, state);
|
||||||
|
|
||||||
@@ -212,14 +243,10 @@ describe('HarvesterHandler', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// ── onFindNewSource ───────────────────────────────────────────────────────
|
|
||||||
|
|
||||||
describe('onFindNewSource', () => {
|
describe('onFindNewSource', () => {
|
||||||
it('assigns the first available source spot to the creep', () => {
|
it('assigns the first available source spot to the creep', () => {
|
||||||
const state = makeState();
|
const { state, creep, mockSource } = createFixture();
|
||||||
jest.spyOn(getById, 'getSourceById').mockReturnValue(makeSource());
|
mockSource();
|
||||||
|
|
||||||
const creep = makeCreep(); // no destination, empty store
|
|
||||||
|
|
||||||
HarvesterHandler.run(creep, state);
|
HarvesterHandler.run(creep, state);
|
||||||
|
|
||||||
@@ -228,38 +255,41 @@ describe('HarvesterHandler', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('does not assign a source when all spots are occupied', () => {
|
it('does not assign a source when all spots are occupied', () => {
|
||||||
const state = makeState();
|
const { state, creep, mockSource } = createFixture();
|
||||||
// Fill all non-center spots with OCCUPIED
|
|
||||||
state.sourcesStates['src1'].spots = state.sourcesStates[
|
state.sourcesStates['src1'].spots = state.sourcesStates[
|
||||||
'src1'
|
'src1'
|
||||||
].spots.map((s: PositionSpotStatus) =>
|
].spots.map((s: PositionSpotStatus) =>
|
||||||
s === PositionSpotStatus.CENTER
|
s === PositionSpotStatus.CENTER ? s : PositionSpotStatus.OCCUPIED
|
||||||
? s
|
|
||||||
: PositionSpotStatus.OCCUPIED
|
|
||||||
) as any;
|
) as any;
|
||||||
|
mockSource();
|
||||||
jest.spyOn(getById, 'getSourceById').mockReturnValue(makeSource());
|
|
||||||
const creep = makeCreep();
|
|
||||||
|
|
||||||
HarvesterHandler.run(creep, state);
|
HarvesterHandler.run(creep, state);
|
||||||
|
|
||||||
expect(creep.memory.destination).toBeUndefined();
|
expect(creep.memory.destination).toBeUndefined();
|
||||||
});
|
});
|
||||||
});
|
|
||||||
|
|
||||||
// ── onSourceDestination ───────────────────────────────────────────────────
|
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', () => {
|
describe('onSourceDestination', () => {
|
||||||
it('harvests when in range', () => {
|
it('harvests when in range', () => {
|
||||||
const state = makeState();
|
const { state, source, creep, mockSource } = createFixture({
|
||||||
const source = makeSource();
|
memory: {
|
||||||
jest.spyOn(getById, 'getSourceById').mockReturnValue(source);
|
destination: { type: 'source', id: 'src1', sourceSpot: 0 },
|
||||||
|
},
|
||||||
const creep = makeCreep(
|
});
|
||||||
{ destination: { type: 'source', id: 'src1', sourceSpot: 0 } },
|
mockSource();
|
||||||
0,
|
|
||||||
50
|
|
||||||
);
|
|
||||||
(creep.harvest as jest.Mock).mockReturnValue((global as any).OK);
|
(creep.harvest as jest.Mock).mockReturnValue((global as any).OK);
|
||||||
|
|
||||||
HarvesterHandler.run(creep, state);
|
HarvesterHandler.run(creep, state);
|
||||||
@@ -269,15 +299,12 @@ describe('HarvesterHandler', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('moves to source when not in range', () => {
|
it('moves to source when not in range', () => {
|
||||||
const state = makeState();
|
const { state, creep, mockSource } = createFixture({
|
||||||
const source = makeSource();
|
memory: {
|
||||||
jest.spyOn(getById, 'getSourceById').mockReturnValue(source);
|
destination: { type: 'source', id: 'src1', sourceSpot: 0 },
|
||||||
|
},
|
||||||
const creep = makeCreep(
|
});
|
||||||
{ destination: { type: 'source', id: 'src1', sourceSpot: 0 } },
|
mockSource();
|
||||||
0,
|
|
||||||
50
|
|
||||||
);
|
|
||||||
(creep.harvest as jest.Mock).mockReturnValue(
|
(creep.harvest as jest.Mock).mockReturnValue(
|
||||||
(global as any).ERR_NOT_IN_RANGE
|
(global as any).ERR_NOT_IN_RANGE
|
||||||
);
|
);
|
||||||
@@ -287,40 +314,53 @@ describe('HarvesterHandler', () => {
|
|||||||
expect(creep.moveTo).toHaveBeenCalled();
|
expect(creep.moveTo).toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('logs and returns when source is not found', () => {
|
it('moves to the computed spot position (not source pos) when not in range', () => {
|
||||||
const state = makeState();
|
const { state, source, creep, mockSource } = createFixture({
|
||||||
jest.spyOn(getById, 'getSourceById').mockReturnValue(null);
|
memory: {
|
||||||
|
destination: { type: 'source', id: 'src1', sourceSpot: 0 },
|
||||||
|
},
|
||||||
|
});
|
||||||
|
mockSource();
|
||||||
|
(creep.harvest as jest.Mock).mockReturnValue(
|
||||||
|
(global as any).ERR_NOT_IN_RANGE
|
||||||
|
);
|
||||||
|
|
||||||
const creep = makeCreep(
|
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: {
|
||||||
destination: {
|
destination: {
|
||||||
type: 'source',
|
type: 'source',
|
||||||
id: 'unknown',
|
id: 'unknown',
|
||||||
sourceSpot: 0,
|
sourceSpot: 0,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
0,
|
});
|
||||||
50
|
mockSource(null);
|
||||||
);
|
|
||||||
|
|
||||||
expect(() => HarvesterHandler.run(creep, state)).not.toThrow();
|
expect(() => HarvesterHandler.run(creep, state)).not.toThrow();
|
||||||
expect(creep.harvest).not.toHaveBeenCalled();
|
expect(creep.harvest).not.toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// ── onSpawnDestination ────────────────────────────────────────────────────
|
|
||||||
|
|
||||||
describe('onSpawnDestination', () => {
|
describe('onSpawnDestination', () => {
|
||||||
it('transfers energy when in range', () => {
|
it('transfers energy when in range', () => {
|
||||||
const state = makeState();
|
const { state, spawn, creep, mockSpawn } = createFixture({
|
||||||
const spawn = makeSpawn();
|
memory: {
|
||||||
jest.spyOn(getById, 'getSpawnById').mockReturnValue(spawn);
|
destination: { type: 'spawn', id: 'spawn1' },
|
||||||
|
},
|
||||||
const creep = makeCreep(
|
storeUsed: 50,
|
||||||
{ destination: { type: 'spawn', id: 'spawn1' } },
|
storeCapacity: 50,
|
||||||
50,
|
});
|
||||||
50
|
mockSpawn();
|
||||||
);
|
|
||||||
(creep.transfer as jest.Mock).mockReturnValue((global as any).OK);
|
(creep.transfer as jest.Mock).mockReturnValue((global as any).OK);
|
||||||
|
|
||||||
HarvesterHandler.run(creep, state);
|
HarvesterHandler.run(creep, state);
|
||||||
@@ -333,15 +373,14 @@ describe('HarvesterHandler', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('moves to spawn when not in range', () => {
|
it('moves to spawn when not in range', () => {
|
||||||
const state = makeState();
|
const { state, spawn, creep, mockSpawn } = createFixture({
|
||||||
const spawn = makeSpawn();
|
memory: {
|
||||||
jest.spyOn(getById, 'getSpawnById').mockReturnValue(spawn);
|
destination: { type: 'spawn', id: 'spawn1' },
|
||||||
|
},
|
||||||
const creep = makeCreep(
|
storeUsed: 50,
|
||||||
{ destination: { type: 'spawn', id: 'spawn1' } },
|
storeCapacity: 50,
|
||||||
50,
|
});
|
||||||
50
|
mockSpawn();
|
||||||
);
|
|
||||||
(creep.transfer as jest.Mock).mockReturnValue(
|
(creep.transfer as jest.Mock).mockReturnValue(
|
||||||
(global as any).ERR_NOT_IN_RANGE
|
(global as any).ERR_NOT_IN_RANGE
|
||||||
);
|
);
|
||||||
@@ -355,17 +394,28 @@ describe('HarvesterHandler', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('logs and returns when spawn is not found', () => {
|
it('logs and returns when spawn is not found', () => {
|
||||||
const state = makeState();
|
const { state, creep, mockSpawn } = createFixture({
|
||||||
jest.spyOn(getById, 'getSpawnById').mockReturnValue(null);
|
memory: {
|
||||||
|
destination: { type: 'spawn', id: 'spawn1' },
|
||||||
const creep = makeCreep(
|
},
|
||||||
{ destination: { type: 'spawn', id: 'spawn1' } },
|
storeUsed: 50,
|
||||||
50,
|
storeCapacity: 50,
|
||||||
50
|
});
|
||||||
);
|
mockSpawn(null);
|
||||||
|
|
||||||
expect(() => HarvesterHandler.run(creep, state)).not.toThrow();
|
expect(() => HarvesterHandler.run(creep, state)).not.toThrow();
|
||||||
expect(creep.transfer).not.toHaveBeenCalled();
|
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);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -6,73 +6,79 @@ import {
|
|||||||
} from '@/utils/positions';
|
} from '@/utils/positions';
|
||||||
import '~/tests/__mocks__/screeps';
|
import '~/tests/__mocks__/screeps';
|
||||||
|
|
||||||
// ─── Helpers ────────────────────────────────────────────────────────────────
|
describe('UpgraderHandler', () => {
|
||||||
|
type FixtureOptions = {
|
||||||
function makeState(overrides: Partial<GameState> = {}): GameState {
|
memory?: Partial<CreepMemory>;
|
||||||
const spots = createSourcePositionMatrix(PositionSpotStatus.EMPTY);
|
storeUsed?: number;
|
||||||
return {
|
storeCapacity?: number;
|
||||||
sourcesStates: {
|
stateOverrides?: Partial<GameState>;
|
||||||
src1: { spots },
|
sourceId?: string;
|
||||||
},
|
controllerId?: string;
|
||||||
...overrides,
|
controllerInRoom?: StructureController | null;
|
||||||
} 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') {
|
const createFixture = ({
|
||||||
return new (global as any).RoomPosition(x, y, roomName);
|
memory = {},
|
||||||
}
|
|
||||||
|
|
||||||
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,
|
storeUsed = 0,
|
||||||
storeCapacity = 50,
|
storeCapacity = 50,
|
||||||
controller: StructureController | null = null
|
stateOverrides = {},
|
||||||
): Creep {
|
sourceId = 'src1',
|
||||||
const memory = makeCreepMemory(memoryOverrides);
|
controllerId = 'ctrl1',
|
||||||
return {
|
controllerInRoom = null,
|
||||||
|
}: 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 controller = { id: controllerId } as StructureController;
|
||||||
|
|
||||||
|
const creep = {
|
||||||
name: 'Creep1',
|
name: 'Creep1',
|
||||||
memory,
|
memory: {
|
||||||
store: makeStore(storeUsed, storeCapacity),
|
role: 'upgrader',
|
||||||
pos: makePos(),
|
spawnId: 'spawn1',
|
||||||
|
...memory,
|
||||||
|
} as CreepMemory,
|
||||||
|
store: {
|
||||||
|
getFreeCapacity: (_res: string) => storeCapacity - storeUsed,
|
||||||
|
getUsedCapacity: (_res: string) => storeUsed,
|
||||||
|
},
|
||||||
|
pos: new (global as any).RoomPosition(0, 0, 'W1N1'),
|
||||||
room: {
|
room: {
|
||||||
controller:
|
controller: controllerInRoom,
|
||||||
controller !== null ? (controller ?? makeController()) : null,
|
|
||||||
},
|
},
|
||||||
harvest: jest.fn().mockReturnValue((global as any).OK),
|
harvest: jest.fn().mockReturnValue((global as any).OK),
|
||||||
moveTo: jest.fn().mockReturnValue((global as any).OK),
|
moveTo: jest.fn().mockReturnValue((global as any).OK),
|
||||||
upgradeController: jest.fn().mockReturnValue((global as any).OK),
|
upgradeController: jest.fn().mockReturnValue((global as any).OK),
|
||||||
} as unknown as Creep;
|
} as unknown as Creep;
|
||||||
}
|
|
||||||
|
|
||||||
// ─── Tests ───────────────────────────────────────────────────────────────────
|
return {
|
||||||
|
state,
|
||||||
|
source,
|
||||||
|
controller,
|
||||||
|
creep,
|
||||||
|
memory: creep.memory,
|
||||||
|
mockSource: (value: Source | null = source) =>
|
||||||
|
jest.spyOn(getById, 'getSourceById').mockReturnValue(value),
|
||||||
|
mockController: (value: StructureController | null = controller) =>
|
||||||
|
jest
|
||||||
|
.spyOn(getById, 'getControllerById')
|
||||||
|
.mockReturnValue(value),
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
describe('UpgraderHandler', () => {
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
jest.clearAllMocks();
|
jest.clearAllMocks();
|
||||||
jest.spyOn(console, 'log').mockImplementation(() => {});
|
jest.spyOn(console, 'log').mockImplementation(() => {});
|
||||||
@@ -82,16 +88,14 @@ describe('UpgraderHandler', () => {
|
|||||||
jest.restoreAllMocks();
|
jest.restoreAllMocks();
|
||||||
});
|
});
|
||||||
|
|
||||||
// ── destroy ──────────────────────────────────────────────────────────────
|
|
||||||
|
|
||||||
describe('destroy', () => {
|
describe('destroy', () => {
|
||||||
it('releases the source spot from destination and clears it', () => {
|
it('releases the source spot from destination and clears it', () => {
|
||||||
const state = makeState();
|
const { state, memory } = createFixture({
|
||||||
state.sourcesStates['src1'].spots[0] = PositionSpotStatus.OCCUPIED;
|
memory: {
|
||||||
|
|
||||||
const memory = makeCreepMemory({
|
|
||||||
destination: { type: 'source', id: 'src1', sourceSpot: 0 },
|
destination: { type: 'source', id: 'src1', sourceSpot: 0 },
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
state.sourcesStates['src1'].spots[0] = PositionSpotStatus.OCCUPIED;
|
||||||
|
|
||||||
UpgraderHandler.destroy(memory, state);
|
UpgraderHandler.destroy(memory, state);
|
||||||
|
|
||||||
@@ -102,16 +106,16 @@ describe('UpgraderHandler', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('releases the source spot from previousDestination and clears it', () => {
|
it('releases the source spot from previousDestination and clears it', () => {
|
||||||
const state = makeState();
|
const { state, memory } = createFixture({
|
||||||
state.sourcesStates['src1'].spots[3] = PositionSpotStatus.OCCUPIED;
|
memory: {
|
||||||
|
|
||||||
const memory = makeCreepMemory({
|
|
||||||
previousDestination: {
|
previousDestination: {
|
||||||
type: 'source',
|
type: 'source',
|
||||||
id: 'src1',
|
id: 'src1',
|
||||||
sourceSpot: 3,
|
sourceSpot: 3,
|
||||||
},
|
},
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
state.sourcesStates['src1'].spots[3] = PositionSpotStatus.OCCUPIED;
|
||||||
|
|
||||||
UpgraderHandler.destroy(memory, state);
|
UpgraderHandler.destroy(memory, state);
|
||||||
|
|
||||||
@@ -121,10 +125,37 @@ describe('UpgraderHandler', () => {
|
|||||||
expect(memory.previousDestination).toBeUndefined();
|
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: 3,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
state.sourcesStates['src1'].spots[0] = PositionSpotStatus.OCCUPIED;
|
||||||
|
state.sourcesStates['src1'].spots[3] = PositionSpotStatus.OCCUPIED;
|
||||||
|
|
||||||
|
UpgraderHandler.destroy(memory, state);
|
||||||
|
|
||||||
|
expect(state.sourcesStates['src1'].spots[0]).toBe(
|
||||||
|
PositionSpotStatus.EMPTY
|
||||||
|
);
|
||||||
|
expect(state.sourcesStates['src1'].spots[3]).toBe(
|
||||||
|
PositionSpotStatus.EMPTY
|
||||||
|
);
|
||||||
|
expect(memory.destination).toBeUndefined();
|
||||||
|
expect(memory.previousDestination).toBeUndefined();
|
||||||
|
});
|
||||||
|
|
||||||
it('does nothing when destination is not a source', () => {
|
it('does nothing when destination is not a source', () => {
|
||||||
const state = makeState();
|
const { state, memory } = createFixture({
|
||||||
const memory = makeCreepMemory({
|
memory: {
|
||||||
destination: { type: 'controller', id: 'ctrl1' },
|
destination: { type: 'controller', id: 'ctrl1' },
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
UpgraderHandler.destroy(memory, state);
|
UpgraderHandler.destroy(memory, state);
|
||||||
@@ -139,29 +170,40 @@ describe('UpgraderHandler', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('does nothing when memory has no destination', () => {
|
it('does nothing when memory has no destination', () => {
|
||||||
const state = makeState();
|
const { state, memory } = createFixture();
|
||||||
const memory = makeCreepMemory();
|
|
||||||
|
|
||||||
expect(() => UpgraderHandler.destroy(memory, state)).not.toThrow();
|
expect(() => UpgraderHandler.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,
|
||||||
|
},
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
// ── validateCreepMemory (via run) ────────────────────────────────────────
|
expect(() => UpgraderHandler.destroy(memory, state)).not.toThrow();
|
||||||
|
expect(memory.destination).toBeUndefined();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('validateCreepMemory (via run)', () => {
|
describe('validateCreepMemory (via run)', () => {
|
||||||
it('releases previousDestination source spot and deletes it', () => {
|
it('releases previousDestination source spot and deletes it', () => {
|
||||||
const state = makeState();
|
const { state, creep, mockSource } = createFixture({
|
||||||
state.sourcesStates['src1'].spots[1] = PositionSpotStatus.OCCUPIED;
|
memory: {
|
||||||
|
|
||||||
const creep = makeCreep({
|
|
||||||
previousDestination: {
|
previousDestination: {
|
||||||
type: 'source',
|
type: 'source',
|
||||||
id: 'src1',
|
id: 'src1',
|
||||||
sourceSpot: 1,
|
sourceSpot: 1,
|
||||||
},
|
},
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
state.sourcesStates['src1'].spots[1] = PositionSpotStatus.OCCUPIED;
|
||||||
jest.spyOn(getById, 'getSourceById').mockReturnValue(null);
|
mockSource(null);
|
||||||
|
|
||||||
UpgraderHandler.run(creep, state);
|
UpgraderHandler.run(creep, state);
|
||||||
|
|
||||||
@@ -172,23 +214,21 @@ describe('UpgraderHandler', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('transitions from source to controller destination when energy is full', () => {
|
it('transitions from source to controller destination when energy is full', () => {
|
||||||
const state = makeState();
|
const { state, controller, creep, mockController } =
|
||||||
const source = makeSource();
|
createFixture({
|
||||||
jest.spyOn(getById, 'getSourceById').mockReturnValue(source);
|
memory: {
|
||||||
|
destination: {
|
||||||
const controller = makeController();
|
type: 'source',
|
||||||
jest.spyOn(getById, 'getControllerById').mockReturnValue(
|
id: 'src1',
|
||||||
controller
|
sourceSpot: 0,
|
||||||
);
|
|
||||||
|
|
||||||
const creep = makeCreep(
|
|
||||||
{
|
|
||||||
destination: { type: 'source', id: 'src1', sourceSpot: 0 },
|
|
||||||
},
|
},
|
||||||
50, // full
|
},
|
||||||
50,
|
storeUsed: 50,
|
||||||
controller
|
storeCapacity: 50,
|
||||||
);
|
controllerInRoom: { id: 'ctrl1' } as StructureController,
|
||||||
|
});
|
||||||
|
// getSourceById is not called in this path — only getControllerById is needed
|
||||||
|
mockController(controller);
|
||||||
|
|
||||||
UpgraderHandler.run(creep, state);
|
UpgraderHandler.run(creep, state);
|
||||||
|
|
||||||
@@ -197,58 +237,54 @@ describe('UpgraderHandler', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('clears controller destination when energy is empty', () => {
|
it('clears controller destination when energy is empty', () => {
|
||||||
const state = makeState();
|
const { state, controller, creep, mockController } = createFixture({
|
||||||
const controller = makeController();
|
memory: {
|
||||||
jest.spyOn(getById, 'getControllerById').mockReturnValue(
|
destination: { type: 'controller', id: 'ctrl1' },
|
||||||
controller
|
},
|
||||||
);
|
storeUsed: 0,
|
||||||
|
storeCapacity: 50,
|
||||||
const creep = makeCreep(
|
controllerInRoom: { id: 'ctrl1' } as StructureController,
|
||||||
{ destination: { type: 'controller', id: 'ctrl1' } },
|
});
|
||||||
0, // empty
|
mockController(controller);
|
||||||
50,
|
|
||||||
controller
|
|
||||||
);
|
|
||||||
|
|
||||||
UpgraderHandler.run(creep, state);
|
UpgraderHandler.run(creep, state);
|
||||||
|
|
||||||
expect(creep.memory.destination).toBeUndefined();
|
expect(creep.memory.destination).toBeUndefined();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('clears destination and logs when room has no controller during source→controller transition', () => {
|
it('clears destination and logs when room has no controller during source->controller transition', () => {
|
||||||
const state = makeState();
|
const { state, creep, mockSource } = createFixture({
|
||||||
// Occupy all spots so onFindNewSource cannot reassign a source
|
memory: {
|
||||||
|
destination: { type: 'source', id: 'src1', sourceSpot: 0 },
|
||||||
|
},
|
||||||
|
storeUsed: 50,
|
||||||
|
storeCapacity: 50,
|
||||||
|
controllerInRoom: null,
|
||||||
|
});
|
||||||
state.sourcesStates['src1'].spots = state.sourcesStates[
|
state.sourcesStates['src1'].spots = state.sourcesStates[
|
||||||
'src1'
|
'src1'
|
||||||
].spots.map((s: PositionSpotStatus) =>
|
].spots.map((s: PositionSpotStatus) =>
|
||||||
s === PositionSpotStatus.CENTER
|
s === PositionSpotStatus.CENTER ? s : PositionSpotStatus.OCCUPIED
|
||||||
? s
|
|
||||||
: PositionSpotStatus.OCCUPIED
|
|
||||||
) as any;
|
) as any;
|
||||||
|
mockSource();
|
||||||
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);
|
UpgraderHandler.run(creep, state);
|
||||||
|
|
||||||
expect(creep.memory.destination).toBeUndefined();
|
expect(creep.memory.destination).toBeUndefined();
|
||||||
|
// previousDestination is set to the old source before checking room.controller,
|
||||||
|
// so the source spot can be released on the next tick
|
||||||
|
expect(creep.memory.previousDestination).toEqual({
|
||||||
|
type: 'source',
|
||||||
|
id: 'src1',
|
||||||
|
sourceSpot: 0,
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// ── onFindNewSource ───────────────────────────────────────────────────────
|
|
||||||
|
|
||||||
describe('onFindNewSource', () => {
|
describe('onFindNewSource', () => {
|
||||||
it('assigns the first available source spot to the creep', () => {
|
it('assigns the first available source spot to the creep', () => {
|
||||||
const state = makeState();
|
const { state, creep, mockSource } = createFixture();
|
||||||
jest.spyOn(getById, 'getSourceById').mockReturnValue(makeSource());
|
mockSource();
|
||||||
|
|
||||||
const creep = makeCreep(); // no destination, empty store
|
|
||||||
|
|
||||||
UpgraderHandler.run(creep, state);
|
UpgraderHandler.run(creep, state);
|
||||||
|
|
||||||
@@ -257,37 +293,41 @@ describe('UpgraderHandler', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('does not assign a source when all spots are occupied', () => {
|
it('does not assign a source when all spots are occupied', () => {
|
||||||
const state = makeState();
|
const { state, creep, mockSource } = createFixture();
|
||||||
state.sourcesStates['src1'].spots = state.sourcesStates[
|
state.sourcesStates['src1'].spots = state.sourcesStates[
|
||||||
'src1'
|
'src1'
|
||||||
].spots.map((s: PositionSpotStatus) =>
|
].spots.map((s: PositionSpotStatus) =>
|
||||||
s === PositionSpotStatus.CENTER
|
s === PositionSpotStatus.CENTER ? s : PositionSpotStatus.OCCUPIED
|
||||||
? s
|
|
||||||
: PositionSpotStatus.OCCUPIED
|
|
||||||
) as any;
|
) as any;
|
||||||
|
mockSource();
|
||||||
jest.spyOn(getById, 'getSourceById').mockReturnValue(makeSource());
|
|
||||||
const creep = makeCreep();
|
|
||||||
|
|
||||||
UpgraderHandler.run(creep, state);
|
UpgraderHandler.run(creep, state);
|
||||||
|
|
||||||
expect(creep.memory.destination).toBeUndefined();
|
expect(creep.memory.destination).toBeUndefined();
|
||||||
});
|
});
|
||||||
});
|
|
||||||
|
|
||||||
// ── onSourceDestination ───────────────────────────────────────────────────
|
it('marks the assigned source spot as OCCUPIED in state', () => {
|
||||||
|
const { state, creep, mockSource } = createFixture();
|
||||||
|
mockSource();
|
||||||
|
|
||||||
|
UpgraderHandler.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', () => {
|
describe('onSourceDestination', () => {
|
||||||
it('harvests when in range', () => {
|
it('harvests when in range', () => {
|
||||||
const state = makeState();
|
const { state, source, creep, mockSource } = createFixture({
|
||||||
const source = makeSource();
|
memory: {
|
||||||
jest.spyOn(getById, 'getSourceById').mockReturnValue(source);
|
destination: { type: 'source', id: 'src1', sourceSpot: 0 },
|
||||||
|
},
|
||||||
const creep = makeCreep(
|
});
|
||||||
{ destination: { type: 'source', id: 'src1', sourceSpot: 0 } },
|
mockSource();
|
||||||
0,
|
|
||||||
50
|
|
||||||
);
|
|
||||||
(creep.harvest as jest.Mock).mockReturnValue((global as any).OK);
|
(creep.harvest as jest.Mock).mockReturnValue((global as any).OK);
|
||||||
|
|
||||||
UpgraderHandler.run(creep, state);
|
UpgraderHandler.run(creep, state);
|
||||||
@@ -297,15 +337,12 @@ describe('UpgraderHandler', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('moves to source when not in range', () => {
|
it('moves to source when not in range', () => {
|
||||||
const state = makeState();
|
const { state, creep, mockSource } = createFixture({
|
||||||
const source = makeSource();
|
memory: {
|
||||||
jest.spyOn(getById, 'getSourceById').mockReturnValue(source);
|
destination: { type: 'source', id: 'src1', sourceSpot: 0 },
|
||||||
|
},
|
||||||
const creep = makeCreep(
|
});
|
||||||
{ destination: { type: 'source', id: 'src1', sourceSpot: 0 } },
|
mockSource();
|
||||||
0,
|
|
||||||
50
|
|
||||||
);
|
|
||||||
(creep.harvest as jest.Mock).mockReturnValue(
|
(creep.harvest as jest.Mock).mockReturnValue(
|
||||||
(global as any).ERR_NOT_IN_RANGE
|
(global as any).ERR_NOT_IN_RANGE
|
||||||
);
|
);
|
||||||
@@ -315,43 +352,54 @@ describe('UpgraderHandler', () => {
|
|||||||
expect(creep.moveTo).toHaveBeenCalled();
|
expect(creep.moveTo).toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('logs and returns when source is not found', () => {
|
it('moves to the computed spot position (not source pos) when not in range', () => {
|
||||||
const state = makeState();
|
const { state, creep, mockSource } = createFixture({
|
||||||
jest.spyOn(getById, 'getSourceById').mockReturnValue(null);
|
memory: {
|
||||||
|
destination: { type: 'source', id: 'src1', sourceSpot: 0 },
|
||||||
|
},
|
||||||
|
});
|
||||||
|
mockSource();
|
||||||
|
(creep.harvest as jest.Mock).mockReturnValue(
|
||||||
|
(global as any).ERR_NOT_IN_RANGE
|
||||||
|
);
|
||||||
|
|
||||||
const creep = makeCreep(
|
UpgraderHandler.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: {
|
||||||
destination: {
|
destination: {
|
||||||
type: 'source',
|
type: 'source',
|
||||||
id: 'unknown',
|
id: 'unknown',
|
||||||
sourceSpot: 0,
|
sourceSpot: 0,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
0,
|
});
|
||||||
50
|
mockSource(null);
|
||||||
);
|
|
||||||
|
|
||||||
expect(() => UpgraderHandler.run(creep, state)).not.toThrow();
|
expect(() => UpgraderHandler.run(creep, state)).not.toThrow();
|
||||||
expect(creep.harvest).not.toHaveBeenCalled();
|
expect(creep.harvest).not.toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// ── onControllerDestination ───────────────────────────────────────────────
|
|
||||||
|
|
||||||
describe('onControllerDestination', () => {
|
describe('onControllerDestination', () => {
|
||||||
it('upgrades controller when in range', () => {
|
it('upgrades controller when in range', () => {
|
||||||
const state = makeState();
|
const { state, controller, creep, mockController } = createFixture({
|
||||||
const controller = makeController();
|
memory: {
|
||||||
jest.spyOn(getById, 'getControllerById').mockReturnValue(
|
destination: { type: 'controller', id: 'ctrl1' },
|
||||||
controller
|
},
|
||||||
);
|
storeUsed: 50,
|
||||||
|
storeCapacity: 50,
|
||||||
const creep = makeCreep(
|
controllerInRoom: { id: 'ctrl1' } as StructureController,
|
||||||
{ destination: { type: 'controller', id: 'ctrl1' } },
|
});
|
||||||
50,
|
mockController(controller);
|
||||||
50,
|
|
||||||
controller
|
|
||||||
);
|
|
||||||
(creep.upgradeController as jest.Mock).mockReturnValue(
|
(creep.upgradeController as jest.Mock).mockReturnValue(
|
||||||
(global as any).OK
|
(global as any).OK
|
||||||
);
|
);
|
||||||
@@ -363,18 +411,15 @@ describe('UpgraderHandler', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('moves to controller when not in range', () => {
|
it('moves to controller when not in range', () => {
|
||||||
const state = makeState();
|
const { state, controller, creep, mockController } = createFixture({
|
||||||
const controller = makeController();
|
memory: {
|
||||||
jest.spyOn(getById, 'getControllerById').mockReturnValue(
|
destination: { type: 'controller', id: 'ctrl1' },
|
||||||
controller
|
},
|
||||||
);
|
storeUsed: 50,
|
||||||
|
storeCapacity: 50,
|
||||||
const creep = makeCreep(
|
controllerInRoom: { id: 'ctrl1' } as StructureController,
|
||||||
{ destination: { type: 'controller', id: 'ctrl1' } },
|
});
|
||||||
50,
|
mockController(controller);
|
||||||
50,
|
|
||||||
controller
|
|
||||||
);
|
|
||||||
(creep.upgradeController as jest.Mock).mockReturnValue(
|
(creep.upgradeController as jest.Mock).mockReturnValue(
|
||||||
(global as any).ERR_NOT_IN_RANGE
|
(global as any).ERR_NOT_IN_RANGE
|
||||||
);
|
);
|
||||||
@@ -388,17 +433,28 @@ describe('UpgraderHandler', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('logs and returns when controller is not found by ID', () => {
|
it('logs and returns when controller is not found by ID', () => {
|
||||||
const state = makeState();
|
const { state, creep, mockController } = createFixture({
|
||||||
jest.spyOn(getById, 'getControllerById').mockReturnValue(null);
|
memory: {
|
||||||
|
destination: { type: 'controller', id: 'ctrl1' },
|
||||||
const creep = makeCreep(
|
},
|
||||||
{ destination: { type: 'controller', id: 'ctrl1' } },
|
storeUsed: 50,
|
||||||
50,
|
storeCapacity: 50,
|
||||||
50
|
});
|
||||||
);
|
mockController(null);
|
||||||
|
|
||||||
expect(() => UpgraderHandler.run(creep, state)).not.toThrow();
|
expect(() => UpgraderHandler.run(creep, state)).not.toThrow();
|
||||||
expect(creep.upgradeController).not.toHaveBeenCalled();
|
expect(creep.upgradeController).not.toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('run', () => {
|
||||||
|
it('returns the state object', () => {
|
||||||
|
const { state, creep, mockSource } = createFixture();
|
||||||
|
mockSource(null);
|
||||||
|
|
||||||
|
const result = UpgraderHandler.run(creep, state);
|
||||||
|
|
||||||
|
expect(result).toBe(state);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
13
tsconfig.test.json
Normal file
13
tsconfig.test.json
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
{
|
||||||
|
"extends": "./tsconfig.json",
|
||||||
|
"compilerOptions": {
|
||||||
|
"noUnusedLocals": false,
|
||||||
|
"noUnusedParameters": false
|
||||||
|
},
|
||||||
|
"include": [
|
||||||
|
"src/global.d.ts",
|
||||||
|
"src/**/*.ts",
|
||||||
|
"tests/**/*.ts"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user