Close door after walking through again, and add more test coverage

This commit is contained in:
Peter Stockings
2026-01-05 22:14:10 +11:00
parent b35cf5a964
commit b3954a6408
10 changed files with 445 additions and 33 deletions

View File

@@ -1,5 +1,5 @@
import { describe, it, expect } from 'vitest';
import { idx, inBounds, isWall, isBlocked } from '../world/world-logic';
import { idx, inBounds, isWall, isBlocked, tryDestructTile, isPlayerOnExit } from '../world/world-logic';
import { type World, type Tile } from '../../core/types';
import { TileType } from '../../core/terrain';
@@ -114,4 +114,65 @@ describe('World Utilities', () => {
expect(isBlocked(world, 10, 10)).toBe(true);
});
});
describe('tryDestructTile', () => {
it('should destruct a destructible tile', () => {
const tiles = new Array(100).fill(TileType.EMPTY);
tiles[0] = TileType.GRASS;
const world = createTestWorld(10, 10, tiles);
const result = tryDestructTile(world, 0, 0);
expect(result).toBe(true);
expect(world.tiles[0]).toBe(TileType.GRASS_SAPLINGS);
});
it('should not destruct a non-destructible tile', () => {
const tiles = new Array(100).fill(TileType.EMPTY);
tiles[0] = TileType.WALL;
const world = createTestWorld(10, 10, tiles);
const result = tryDestructTile(world, 0, 0);
expect(result).toBe(false);
expect(world.tiles[0]).toBe(TileType.WALL);
});
it('should return false for out of bounds', () => {
const world = createTestWorld(10, 10, new Array(100).fill(TileType.EMPTY));
expect(tryDestructTile(world, -1, 0)).toBe(false);
});
});
describe('isPlayerOnExit', () => {
it('should return true when player is on exit', () => {
const world = createTestWorld(10, 10, new Array(100).fill(TileType.EMPTY));
world.exit = { x: 5, y: 5 };
world.actors.set(1, {
id: 1,
pos: { x: 5, y: 5 },
isPlayer: true
} as any);
expect(isPlayerOnExit(world, 1)).toBe(true);
});
it('should return false when player is not on exit', () => {
const world = createTestWorld(10, 10, new Array(100).fill(TileType.EMPTY));
world.exit = { x: 5, y: 5 };
world.actors.set(1, {
id: 1,
pos: { x: 4, y: 4 },
isPlayer: true
} as any);
expect(isPlayerOnExit(world, 1)).toBe(false);
});
it('should return false when player does not exist', () => {
const world = createTestWorld(10, 10, new Array(100).fill(TileType.EMPTY));
world.exit = { x: 5, y: 5 };
expect(isPlayerOnExit(world, 999)).toBe(false);
});
});
});