2026-04-03 17:56:35 +08:00
|
|
|
import { describe, it, expect } from 'vitest';
|
2026-04-05 10:42:38 +08:00
|
|
|
import { flip, flipTo, roll, Part } from '@/core/part';
|
2026-04-03 17:56:35 +08:00
|
|
|
import { createRNG } from '@/utils/rng';
|
|
|
|
|
|
|
|
|
|
function createTestPart<TMeta = {}>(overrides: Partial<Part<TMeta>> & TMeta): Part<TMeta> {
|
|
|
|
|
return {
|
|
|
|
|
id: 'test-part',
|
|
|
|
|
regionId: 'test-region',
|
|
|
|
|
position: [0, 0],
|
|
|
|
|
...overrides,
|
|
|
|
|
} as Part<TMeta>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
describe('flip', () => {
|
|
|
|
|
it('should cycle to next side when sides is defined', () => {
|
|
|
|
|
const part = createTestPart({ sides: 2, side: 0 });
|
|
|
|
|
flip(part);
|
|
|
|
|
expect(part.side).toBe(1);
|
|
|
|
|
flip(part);
|
|
|
|
|
expect(part.side).toBe(0);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should wrap around when side reaches sides count', () => {
|
|
|
|
|
const part = createTestPart({ sides: 6, side: 5 });
|
|
|
|
|
flip(part);
|
|
|
|
|
expect(part.side).toBe(0);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should do nothing when sides is undefined', () => {
|
|
|
|
|
const part = createTestPart({ side: 3 });
|
|
|
|
|
flip(part);
|
|
|
|
|
expect(part.side).toBe(3);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should initialize side to 0 when side is undefined', () => {
|
|
|
|
|
const part = createTestPart({ sides: 4 });
|
|
|
|
|
flip(part);
|
|
|
|
|
expect(part.side).toBe(1);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('flipTo', () => {
|
|
|
|
|
it('should set side to specified value', () => {
|
|
|
|
|
const part = createTestPart({ sides: 6, side: 0 });
|
|
|
|
|
flipTo(part, 3);
|
|
|
|
|
expect(part.side).toBe(3);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should do nothing when sides is undefined', () => {
|
|
|
|
|
const part = createTestPart({ side: 2 });
|
|
|
|
|
flipTo(part, 5);
|
|
|
|
|
expect(part.side).toBe(2);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should do nothing when side exceeds sides count', () => {
|
|
|
|
|
const part = createTestPart({ sides: 6, side: 0 });
|
|
|
|
|
flipTo(part, 6);
|
|
|
|
|
expect(part.side).toBe(0);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should allow side equal to sides - 1', () => {
|
|
|
|
|
const part = createTestPart({ sides: 6, side: 0 });
|
|
|
|
|
flipTo(part, 5);
|
|
|
|
|
expect(part.side).toBe(5);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('roll', () => {
|
|
|
|
|
it('should randomize side when sides is defined', () => {
|
|
|
|
|
const part = createTestPart({ sides: 6 });
|
|
|
|
|
const rng = createRNG(12345);
|
|
|
|
|
roll(part, rng);
|
|
|
|
|
expect(part.side).toBeGreaterThanOrEqual(0);
|
|
|
|
|
expect(part.side).toBeLessThan(6);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should do nothing when sides is undefined', () => {
|
|
|
|
|
const part = createTestPart({ side: 3 });
|
|
|
|
|
const rng = createRNG(12345);
|
|
|
|
|
roll(part, rng);
|
|
|
|
|
expect(part.side).toBe(3);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should produce deterministic results with same seed', () => {
|
|
|
|
|
const part1 = createTestPart({ sides: 6 });
|
|
|
|
|
const part2 = createTestPart({ sides: 6 });
|
|
|
|
|
const rng1 = createRNG(42);
|
|
|
|
|
const rng2 = createRNG(42);
|
|
|
|
|
roll(part1, rng1);
|
|
|
|
|
roll(part2, rng2);
|
|
|
|
|
expect(part1.side).toBe(part2.side);
|
|
|
|
|
});
|
|
|
|
|
});
|