aether-shards/test/grid/VoxelGrid.test.js
2025-12-16 15:52:58 -08:00

96 lines
3 KiB
JavaScript

import { expect } from "@esm-bundle/chai";
import { VoxelGrid } from "../../src/grid/VoxelGrid.js";
describe("Phase 1: VoxelGrid Data Structure", () => {
let grid;
const width = 10;
const height = 10;
const depth = 10;
beforeEach(() => {
grid = new VoxelGrid(width, height, depth);
});
it("CoA 1: Should initialize with correct dimensions and empty cells", () => {
expect(grid.width).to.equal(width);
expect(grid.height).to.equal(height);
expect(grid.depth).to.equal(depth);
expect(grid.cells).to.be.instanceOf(Uint8Array);
expect(grid.cells.length).to.equal(width * height * depth);
// Verify all are 0 (Air)
const isAllZero = grid.cells.every((val) => val === 0);
expect(isAllZero).to.be.true;
});
it("CoA 2: Should correctly validate bounds", () => {
// Inside bounds
expect(grid.isValidBounds(0, 0, 0)).to.be.true;
expect(grid.isValidBounds(5, 5, 5)).to.be.true;
expect(grid.isValidBounds(width - 1, height - 1, depth - 1)).to.be.true;
// Outside bounds (Negative)
expect(grid.isValidBounds(-1, 0, 0)).to.be.false;
expect(grid.isValidBounds(0, -1, 0)).to.be.false;
expect(grid.isValidBounds(0, 0, -1)).to.be.false;
// Outside bounds (Overflow)
expect(grid.isValidBounds(width, 0, 0)).to.be.false;
expect(grid.isValidBounds(0, height, 0)).to.be.false;
expect(grid.isValidBounds(0, 0, depth)).to.be.false;
});
it("CoA 3: Should store and retrieve voxel IDs", () => {
const x = 2,
y = 3,
z = 4;
const typeId = 5; // Arbitrary material ID
grid.setVoxel(x, y, z, typeId);
const result = grid.getVoxel(x, y, z);
expect(result).to.equal(typeId);
});
it("CoA 4: Should return 0 (Air) for out-of-bounds getVoxel", () => {
expect(grid.getVoxel(-5, 0, 0)).to.equal(0);
expect(grid.getVoxel(100, 100, 100)).to.equal(0);
});
it("CoA 5: Should handle isSolid checks", () => {
grid.setVoxel(1, 1, 1, 1); // Solid
grid.setVoxel(2, 2, 2, 0); // Air
expect(grid.isSolid(1, 1, 1)).to.be.true;
expect(grid.isSolid(2, 2, 2)).to.be.false;
expect(grid.isSolid(-1, -1, -1)).to.be.false; // Out of bounds is not solid
});
it("CoA 6: Should manage the dirty flag correctly", () => {
// Initial state
expect(grid.dirty).to.be.true;
// Reset flag manually (simulating a render cycle completion)
grid.dirty = false;
// Set voxel to SAME value
grid.setVoxel(0, 0, 0, 0);
expect(grid.dirty).to.be.false; // Should not dirty if value didn't change
// Set voxel to NEW value
grid.setVoxel(0, 0, 0, 1);
expect(grid.dirty).to.be.true; // Should be dirty now
});
it("CoA 7: Should calculate flat array index correctly", () => {
// Based on logic: (y * width * depth) + (z * width) + x
// x=1, y=0, z=0 -> 1
expect(grid.getIndex(1, 0, 0)).to.equal(1);
// x=0, y=0, z=1 -> 1 * 10 = 10
expect(grid.getIndex(0, 0, 1)).to.equal(10);
// x=0, y=1, z=0 -> 1 * 10 * 10 = 100
expect(grid.getIndex(0, 1, 0)).to.equal(100);
});
});