feat: add tokenReducer, vitest tests, fix merge label convention
This commit is contained in:
@@ -1,166 +0,0 @@
|
||||
import { Cube, FACES, COLORS } from "../src/utils/Cube.js";
|
||||
import assert from "assert";
|
||||
|
||||
console.log("Running Cube Integrity Tests...");
|
||||
|
||||
const cube = new Cube();
|
||||
|
||||
// Helper: Count colors on all faces
|
||||
const countColors = () => {
|
||||
const counts = {
|
||||
[COLORS.WHITE]: 0,
|
||||
[COLORS.YELLOW]: 0,
|
||||
[COLORS.ORANGE]: 0,
|
||||
[COLORS.RED]: 0,
|
||||
[COLORS.GREEN]: 0,
|
||||
[COLORS.BLUE]: 0,
|
||||
[COLORS.BLACK]: 0, // Should be ignored or internal
|
||||
};
|
||||
|
||||
cube.cubies.forEach((cubie) => {
|
||||
Object.values(cubie.faces).forEach((color) => {
|
||||
if (counts[color] !== undefined) {
|
||||
counts[color]++;
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
return counts;
|
||||
};
|
||||
|
||||
// Helper: Verify solved state counts
|
||||
const verifyCounts = (counts) => {
|
||||
// Each face has 9 stickers. 6 faces.
|
||||
// 9 * 6 = 54 colored stickers.
|
||||
// 27 cubies * 6 faces = 162 total faces.
|
||||
// 162 - 54 = 108 black faces (internal).
|
||||
|
||||
assert.strictEqual(counts[COLORS.WHITE], 9, "White count should be 9");
|
||||
assert.strictEqual(counts[COLORS.YELLOW], 9, "Yellow count should be 9");
|
||||
assert.strictEqual(counts[COLORS.ORANGE], 9, "Orange count should be 9");
|
||||
assert.strictEqual(counts[COLORS.RED], 9, "Red count should be 9");
|
||||
assert.strictEqual(counts[COLORS.GREEN], 9, "Green count should be 9");
|
||||
assert.strictEqual(counts[COLORS.BLUE], 9, "Blue count should be 9");
|
||||
};
|
||||
|
||||
// Helper: Verify piece integrity
|
||||
// Corners: 8 corners, each has 3 colors.
|
||||
// Edges: 12 edges, each has 2 colors.
|
||||
// Centers: 6 centers, each has 1 color.
|
||||
// Core: 1 core, 0 colors.
|
||||
const verifyPieceTypes = () => {
|
||||
let corners = 0;
|
||||
let edges = 0;
|
||||
let centers = 0;
|
||||
let cores = 0;
|
||||
|
||||
cube.cubies.forEach((cubie) => {
|
||||
const coloredFaces = Object.values(cubie.faces).filter(
|
||||
(c) => c !== COLORS.BLACK,
|
||||
).length;
|
||||
if (coloredFaces === 3) corners++;
|
||||
else if (coloredFaces === 2) edges++;
|
||||
else if (coloredFaces === 1) centers++;
|
||||
else if (coloredFaces === 0) cores++;
|
||||
else
|
||||
assert.fail(
|
||||
`Invalid cubie with ${coloredFaces} colors at (${cubie.x},${cubie.y},${cubie.z})`,
|
||||
);
|
||||
});
|
||||
|
||||
assert.strictEqual(corners, 8, "Should have 8 corners");
|
||||
assert.strictEqual(edges, 12, "Should have 12 edges");
|
||||
assert.strictEqual(centers, 6, "Should have 6 centers");
|
||||
assert.strictEqual(cores, 1, "Should have 1 core");
|
||||
};
|
||||
|
||||
// Helper: Verify specific relative positions of centers (they never change relative to each other)
|
||||
// Up (White) opposite Down (Yellow)
|
||||
// Front (Green) opposite Back (Blue)
|
||||
// Left (Orange) opposite Right (Red)
|
||||
const verifyCenters = () => {
|
||||
const centers = cube.cubies.filter(
|
||||
(c) =>
|
||||
Object.values(c.faces).filter((f) => f !== COLORS.BLACK).length === 1,
|
||||
);
|
||||
|
||||
// Find center by color
|
||||
const findCenter = (color) =>
|
||||
centers.find((c) => Object.values(c.faces).includes(color));
|
||||
|
||||
const white = findCenter(COLORS.WHITE);
|
||||
const yellow = findCenter(COLORS.YELLOW);
|
||||
const green = findCenter(COLORS.GREEN);
|
||||
const blue = findCenter(COLORS.BLUE);
|
||||
const orange = findCenter(COLORS.ORANGE);
|
||||
const red = findCenter(COLORS.RED);
|
||||
|
||||
// Check opposites
|
||||
// Distance between opposites should be 2 (e.g. y=1 and y=-1)
|
||||
// And they should be on same axis
|
||||
|
||||
// Note: After rotations, x/y/z coordinates change.
|
||||
// But relative vectors should hold?
|
||||
// Actually, centers DO rotate around the core.
|
||||
// But White is always opposite Yellow.
|
||||
// So vector(White) + vector(Yellow) == (0,0,0).
|
||||
|
||||
const checkOpposite = (c1, c2, name) => {
|
||||
assert.strictEqual(c1.x + c2.x, 0, `${name} X mismatch`);
|
||||
assert.strictEqual(c1.y + c2.y, 0, `${name} Y mismatch`);
|
||||
assert.strictEqual(c1.z + c2.z, 0, `${name} Z mismatch`);
|
||||
};
|
||||
|
||||
checkOpposite(white, yellow, "White-Yellow");
|
||||
checkOpposite(green, blue, "Green-Blue");
|
||||
checkOpposite(orange, red, "Orange-Red");
|
||||
};
|
||||
|
||||
// --- Test Execution ---
|
||||
|
||||
// 1. Initial State
|
||||
console.log("Test 1: Initial State Integrity");
|
||||
verifyCounts(countColors());
|
||||
verifyPieceTypes();
|
||||
verifyCenters();
|
||||
console.log("PASS Initial State");
|
||||
|
||||
// 2. Single Rotation (R)
|
||||
console.log("Test 2: Single Rotation (R)");
|
||||
cube.rotateLayer("x", 1, -1); // R
|
||||
verifyCounts(countColors());
|
||||
verifyPieceTypes();
|
||||
verifyCenters();
|
||||
console.log("PASS Single Rotation");
|
||||
|
||||
// 3. Multiple Rotations (R U R' U')
|
||||
console.log("Test 3: Sexy Move (R U R' U')");
|
||||
cube.reset();
|
||||
cube.move("R");
|
||||
cube.move("U");
|
||||
cube.move("R'");
|
||||
cube.move("U'");
|
||||
verifyCounts(countColors());
|
||||
verifyPieceTypes();
|
||||
verifyCenters();
|
||||
console.log("PASS Sexy Move");
|
||||
|
||||
// 4. Random Rotations (Fuzzing)
|
||||
console.log("Test 4: 100 Random Moves");
|
||||
cube.reset();
|
||||
const axes = ["x", "y", "z"];
|
||||
const indices = [-1, 0, 1];
|
||||
const dirs = [1, -1];
|
||||
|
||||
for (let i = 0; i < 100; i++) {
|
||||
const axis = axes[Math.floor(Math.random() * axes.length)];
|
||||
const index = indices[Math.floor(Math.random() * indices.length)];
|
||||
const dir = dirs[Math.floor(Math.random() * dirs.length)];
|
||||
cube.rotateLayer(axis, index, dir);
|
||||
}
|
||||
verifyCounts(countColors());
|
||||
verifyPieceTypes();
|
||||
verifyCenters();
|
||||
console.log("PASS 100 Random Moves");
|
||||
|
||||
console.log("ALL INTEGRITY TESTS PASSED");
|
||||
@@ -1,117 +0,0 @@
|
||||
import { Cube, FACES, COLORS } from "../src/utils/Cube.js";
|
||||
import assert from "assert";
|
||||
|
||||
console.log("Running Cube Logic Tests...");
|
||||
|
||||
const cube = new Cube();
|
||||
|
||||
// Helper to check a specific face color at a position
|
||||
const checkFace = (x, y, z, face, expectedColor, message) => {
|
||||
const cubie = cube.cubies.find((c) => c.x === x && c.y === y && c.z === z);
|
||||
if (!cubie) {
|
||||
console.error(`Cubie not found at ${x}, ${y}, ${z}`);
|
||||
return false;
|
||||
}
|
||||
const color = cubie.faces[face];
|
||||
if (color !== expectedColor) {
|
||||
console.error(
|
||||
`FAIL: ${message}. Expected ${expectedColor} at ${face} of (${x},${y},${z}), got ${color}`,
|
||||
);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
// Test 1: Initial State
|
||||
console.log("Test 1: Initial State");
|
||||
// Top-Front-Right corner (1, 1, 1) should have Up=White, Front=Green, Right=Red
|
||||
checkFace(1, 1, 1, FACES.UP, COLORS.WHITE, "Initial Top-Right-Front UP");
|
||||
checkFace(1, 1, 1, FACES.FRONT, COLORS.GREEN, "Initial Top-Right-Front FRONT");
|
||||
checkFace(1, 1, 1, FACES.RIGHT, COLORS.RED, "Initial Top-Right-Front RIGHT");
|
||||
|
||||
// Test 2: Rotate Right Face (R) -> Axis X, index 1, direction -1 (based on previous mapping)
|
||||
// Wait, let's test `rotateLayer` directly first with axis 'x'.
|
||||
// Axis X Positive Rotation (direction 1).
|
||||
// Up (y=1) -> Front (z=1).
|
||||
// The cubie at (1, 1, 1) (Top-Front-Right)
|
||||
// Should move to (1, 0, 1)? No.
|
||||
// (x, y, z) -> (x, -z, y).
|
||||
// (1, 1, 1) -> (1, -1, 1). (Bottom-Front-Right).
|
||||
// Let's trace the color.
|
||||
// The White color was on UP.
|
||||
// The cubie moves to Bottom-Front.
|
||||
// The UP face of the cubie now points FRONT.
|
||||
// So the cubie at (1, -1, 1) should have FRONT = WHITE.
|
||||
|
||||
console.log("Test 2: Rotate X Axis +90 (Right Layer)");
|
||||
cube.rotateLayer("x", 1, 1);
|
||||
|
||||
// Cubie originally at (1, 1, 1) [White Up] moves to (1, -1, 1).
|
||||
// Check (1, -1, 1).
|
||||
// Its Front face should be White.
|
||||
const result1 = checkFace(
|
||||
1,
|
||||
-1,
|
||||
1,
|
||||
FACES.FRONT,
|
||||
COLORS.WHITE,
|
||||
"After X+90: Old Up(White) should be on Front",
|
||||
);
|
||||
|
||||
// Cubie originally at (1, 1, -1) [Blue Back, White Up] (Top-Back-Right)
|
||||
// (1, 1, -1) -> (1, 1, 1). (Top-Front-Right).
|
||||
// Wait. ny = -z = -(-1) = 1. nz = y = 1.
|
||||
// So Top-Back moves to Top-Front.
|
||||
// Its UP face (White) moves to FRONT?
|
||||
// No. The rotation is around X.
|
||||
// Top-Back (y=1, z=-1).
|
||||
// Rot +90 X: y->z, z->-y ? No.
|
||||
// ny = -z = 1. nz = y = 1.
|
||||
// New pos: (1, 1, 1).
|
||||
// The cubie moves from Top-Back to Top-Front.
|
||||
// Its Up face (White) stays Up?
|
||||
// No, the cubie rotates.
|
||||
// Up face rotates to Front?
|
||||
// Rotation around X axis.
|
||||
// Top (Y+) rotates to Front (Z+)?
|
||||
// Yes.
|
||||
// So the cubie at (1, 1, 1) (new position) should have FRONT = WHITE.
|
||||
const result2 = checkFace(
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
FACES.FRONT,
|
||||
COLORS.WHITE,
|
||||
"After X+90: Old Top-Back Up(White) should be on Front",
|
||||
);
|
||||
|
||||
if (result1 && result2) {
|
||||
console.log("PASS: X Axis Rotation Logic seems correct (if fixed)");
|
||||
} else {
|
||||
console.log("FAIL: X Axis Rotation Logic is broken");
|
||||
}
|
||||
|
||||
// Reset for Y test
|
||||
cube.reset();
|
||||
console.log("Test 3: Rotate Y Axis +90 (Top Layer)");
|
||||
// Top Layer (y=1).
|
||||
// Rotate Y+ (direction 1).
|
||||
// Front (z=1) -> Right (x=1).
|
||||
// Cubie at (0, 1, 1) (Front-Top-Center) [Green Front, White Up].
|
||||
// Moves to (1, 1, 0) (Right-Top-Center).
|
||||
// Its Front Face (Green) should move to Right Face.
|
||||
cube.rotateLayer("y", 1, 1);
|
||||
const resultY = checkFace(
|
||||
1,
|
||||
1,
|
||||
0,
|
||||
FACES.RIGHT,
|
||||
COLORS.GREEN,
|
||||
"After Y+90: Old Front(Green) should be on Right",
|
||||
);
|
||||
|
||||
if (resultY) {
|
||||
console.log("PASS: Y Axis Rotation Logic seems correct");
|
||||
} else {
|
||||
console.log("FAIL: Y Axis Rotation Logic is broken");
|
||||
}
|
||||
@@ -1,107 +0,0 @@
|
||||
import { Cube, FACES, COLORS } from "../src/utils/Cube.js";
|
||||
import assert from "assert";
|
||||
|
||||
console.log("Running Cube Matrix Rotation Tests...");
|
||||
|
||||
const cube = new Cube();
|
||||
|
||||
// Helper to check position and face
|
||||
const checkCubie = (origX, origY, origZ, newX, newY, newZ, faceCheck) => {
|
||||
const cubie = cube.cubies.find(
|
||||
(c) => c.x === newX && c.y === newY && c.z === newZ,
|
||||
);
|
||||
if (!cubie) {
|
||||
console.error(`FAIL: Cubie not found at ${newX}, ${newY}, ${newZ}`);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Verify it's the correct original cubie (tracking ID would be better, but position logic is enough if unique)
|
||||
// Let's assume we track a specific cubie.
|
||||
return true;
|
||||
};
|
||||
|
||||
// Test 1: Z-Axis Rotation (Front Face)
|
||||
// Front Face is z=1.
|
||||
// Top-Left (x=-1, y=1) -> Top-Right (x=1, y=1)?
|
||||
// Physical CW (Z-Axis): Up -> Right.
|
||||
// Top-Middle (0, 1) -> Right-Middle (1, 0).
|
||||
console.log("Test 1: Z-Axis CW (Front)");
|
||||
cube.reset();
|
||||
// Find Top-Middle of Front Face: (0, 1, 1). White Up, Green Front.
|
||||
const topMid = cube.cubies.find((c) => c.x === 0 && c.y === 1 && c.z === 1);
|
||||
assert.strictEqual(topMid.faces[FACES.UP], COLORS.WHITE);
|
||||
assert.strictEqual(topMid.faces[FACES.FRONT], COLORS.GREEN);
|
||||
|
||||
cube.rotateLayer("z", 1, -1); // CW (direction -1 in move(), but rotateLayer takes direction. Standard move F is direction -1?)
|
||||
// move('F') calls rotateLayer('z', 1, -1).
|
||||
// So let's test rotateLayer('z', 1, -1).
|
||||
|
||||
// Expect: (0, 1, 1) -> (1, 0, 1). (Right-Middle of Front).
|
||||
// Faces: Old Up (White) becomes Right?
|
||||
// Z-Axis CW: Up -> Right.
|
||||
// So new pos should have Right=White.
|
||||
// Old Front (Green) stays Front.
|
||||
const newPos = cube.cubies.find((c) => c.id === topMid.id);
|
||||
console.log(`Moved to: (${newPos.x}, ${newPos.y}, ${newPos.z})`);
|
||||
assert.strictEqual(newPos.x, 1);
|
||||
assert.strictEqual(newPos.y, 0);
|
||||
assert.strictEqual(newPos.z, 1);
|
||||
assert.strictEqual(newPos.faces[FACES.RIGHT], COLORS.WHITE);
|
||||
assert.strictEqual(newPos.faces[FACES.FRONT], COLORS.GREEN);
|
||||
console.log("PASS Z-Axis CW");
|
||||
|
||||
// Test 2: X-Axis Rotation (Right Face)
|
||||
// Right Face is x=1.
|
||||
// Top-Front (1, 1, 1) -> Top-Back (1, 1, -1)?
|
||||
// Physical CW (X-Axis): Up -> Front.
|
||||
// Top-Middle (1, 1, 0) -> Front-Middle (1, 0, 1).
|
||||
console.log("Test 2: X-Axis CW (Right)");
|
||||
cube.reset();
|
||||
// Find Top-Middle of Right Face: (1, 1, 0). White Up, Red Right.
|
||||
const rightTop = cube.cubies.find((c) => c.x === 1 && c.y === 1 && c.z === 0);
|
||||
|
||||
cube.rotateLayer("x", 1, -1); // CW (direction -1 for R in move()?)
|
||||
// move('R') calls rotateLayer('x', 1, -1).
|
||||
// So let's test -1.
|
||||
|
||||
// Expect: (1, 1, 0) -> (1, 0, -1).
|
||||
// Faces: Old Up (White) becomes Back?
|
||||
// X-Axis CW (Right Face): Up -> Back.
|
||||
// So new pos should have Back=White.
|
||||
// Old Right (Red) stays Right.
|
||||
const newRightPos = cube.cubies.find((c) => c.id === rightTop.id);
|
||||
console.log(`Moved to: (${newRightPos.x}, ${newRightPos.y}, ${newRightPos.z})`);
|
||||
assert.strictEqual(newRightPos.x, 1);
|
||||
assert.strictEqual(newRightPos.y, 0);
|
||||
assert.strictEqual(newRightPos.z, -1);
|
||||
assert.strictEqual(newRightPos.faces[FACES.BACK], COLORS.WHITE);
|
||||
assert.strictEqual(newRightPos.faces[FACES.RIGHT], COLORS.RED);
|
||||
console.log("PASS X-Axis CW");
|
||||
|
||||
// Test 3: Y-Axis Rotation (Up Face)
|
||||
// Up Face is y=1.
|
||||
// Front-Middle (0, 1, 1) -> Left-Middle (-1, 1, 0).
|
||||
// Physical CW (Y-Axis): Front -> Left.
|
||||
// Wait. move('U') calls rotateLayer('y', 1, -1).
|
||||
// Standard U is CW. Y-Axis direction?
|
||||
// move('U'): dir = -1.
|
||||
console.log("Test 3: Y-Axis CW (Up)");
|
||||
cube.reset();
|
||||
// Find Front-Middle of Up Face: (0, 1, 1). Green Front, White Up.
|
||||
const upFront = cube.cubies.find((c) => c.x === 0 && c.y === 1 && c.z === 1);
|
||||
|
||||
cube.rotateLayer("y", 1, -1); // CW (direction -1).
|
||||
|
||||
// Expect: (0, 1, 1) -> (-1, 1, 0). (Left-Middle).
|
||||
// Faces: Old Front (Green) becomes Left?
|
||||
// Y-Axis CW (U): Front -> Left.
|
||||
// So new pos should have Left=Green.
|
||||
// Old Up (White) stays Up.
|
||||
const newUpPos = cube.cubies.find((c) => c.id === upFront.id);
|
||||
console.log(`Moved to: (${newUpPos.x}, ${newUpPos.y}, ${newUpPos.z})`);
|
||||
assert.strictEqual(newUpPos.x, -1);
|
||||
assert.strictEqual(newUpPos.y, 1);
|
||||
assert.strictEqual(newUpPos.z, 0);
|
||||
assert.strictEqual(newUpPos.faces[FACES.LEFT], COLORS.GREEN);
|
||||
assert.strictEqual(newUpPos.faces[FACES.UP], COLORS.WHITE);
|
||||
console.log("PASS Y-Axis CW");
|
||||
70
test/tokenReducer.test.js
Normal file
70
test/tokenReducer.test.js
Normal file
@@ -0,0 +1,70 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { tokenReducer, parseToken } from '../src/utils/tokenReducer.js';
|
||||
|
||||
describe('parseToken', () => {
|
||||
it('parses simple move', () => {
|
||||
expect(parseToken('D')).toEqual({ token: 'D', name: 'D', mod: '' });
|
||||
});
|
||||
|
||||
it('parses prime move', () => {
|
||||
expect(parseToken("U'")).toEqual({ token: "U'", name: 'U', mod: "'" });
|
||||
});
|
||||
|
||||
it('parses double move', () => {
|
||||
expect(parseToken('R2')).toEqual({ token: 'R2', name: 'R', mod: '2' });
|
||||
});
|
||||
});
|
||||
|
||||
describe('tokenReducer', () => {
|
||||
it('user example: mixed faces', () => {
|
||||
const result = tokenReducer(['D', 'U2', 'U2', 'B2', "B'", 'B2', "U'", 'U2']);
|
||||
expect(result.tokens).toEqual(['D', "B'", 'U']);
|
||||
});
|
||||
|
||||
it('cancellation: same move 4 times = identity', () => {
|
||||
expect(tokenReducer(['R', 'R', 'R', 'R']).tokens).toEqual([]);
|
||||
});
|
||||
|
||||
it('cancellation: move + inverse = identity', () => {
|
||||
expect(tokenReducer(["F'", 'F']).tokens).toEqual([]);
|
||||
});
|
||||
|
||||
it('cancellation: double move twice = identity', () => {
|
||||
expect(tokenReducer(['D2', 'D2']).tokens).toEqual([]);
|
||||
});
|
||||
|
||||
it('merge: move + move = double', () => {
|
||||
expect(tokenReducer(['U', 'U']).tokens).toEqual(['U2']);
|
||||
});
|
||||
|
||||
it('merge: double + move = prime', () => {
|
||||
expect(tokenReducer(['R2', 'R']).tokens).toEqual(["R'"]);
|
||||
});
|
||||
|
||||
it('D2 D2 D\' D cancels to empty', () => {
|
||||
expect(tokenReducer(['D2', 'D2', "D'", 'D']).tokens).toEqual([]);
|
||||
});
|
||||
|
||||
it('preserves non-adjacent different faces', () => {
|
||||
expect(tokenReducer(['R', 'U', 'R']).tokens).toEqual(['R', 'U', 'R']);
|
||||
});
|
||||
|
||||
it('reduces only consecutive same-face groups', () => {
|
||||
expect(tokenReducer(['F', 'F', 'U', "U'"]).tokens).toEqual(['F2']);
|
||||
});
|
||||
|
||||
it('handles single move unchanged', () => {
|
||||
expect(tokenReducer(['B']).tokens).toEqual(['B']);
|
||||
});
|
||||
|
||||
it('handles empty input', () => {
|
||||
expect(tokenReducer([]).tokens).toEqual([]);
|
||||
});
|
||||
|
||||
it('desc contains group info', () => {
|
||||
const result = tokenReducer(['R', 'R']);
|
||||
expect(result.desc).toHaveLength(1);
|
||||
expect(result.desc[0].reduced).toBe('R2');
|
||||
expect(result.desc[0].group).toHaveLength(2);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user