feat: rng & commands impl & tests

This commit is contained in:
hyper
2026-04-01 17:34:21 +08:00
parent ea337acacb
commit df9698b67b
10 changed files with 835 additions and 221 deletions
+114
View File
@@ -0,0 +1,114 @@
import { describe, it, expect } from 'vitest';
import { parseCommand, type Command } from '../../src/utils/command';
describe('parseCommand', () => {
it('should parse empty string', () => {
const result = parseCommand('');
expect(result).toEqual({
name: '',
flags: {},
options: {},
params: []
});
});
it('should parse command name only', () => {
const result = parseCommand('move');
expect(result).toEqual({
name: 'move',
flags: {},
options: {},
params: []
});
});
it('should parse command with params', () => {
const result = parseCommand('move meeple1 region1');
expect(result).toEqual({
name: 'move',
flags: {},
options: {},
params: ['meeple1', 'region1']
});
});
it('should parse command with long flags', () => {
const result = parseCommand('move meeple1 --force --quiet');
expect(result).toEqual({
name: 'move',
flags: { force: true, quiet: true },
options: {},
params: ['meeple1']
});
});
it('should parse command with short flags', () => {
const result = parseCommand('move meeple1 -f -q');
expect(result).toEqual({
name: 'move',
flags: { f: true, q: true },
options: {},
params: ['meeple1']
});
});
it('should parse command with long options', () => {
const result = parseCommand('move meeple1 --x 10 --y 20');
expect(result).toEqual({
name: 'move',
flags: {},
options: { x: '10', y: '20' },
params: ['meeple1']
});
});
it('should parse command with short options', () => {
const result = parseCommand('move meeple1 -x 10 -y 20');
expect(result).toEqual({
name: 'move',
flags: {},
options: { x: '10', y: '20' },
params: ['meeple1']
});
});
it('should parse command with mixed flags和选项', () => {
const result = parseCommand('move meeple1 region1 --force -x 10 -q');
expect(result).toEqual({
name: 'move',
flags: { force: true, q: true },
options: { x: '10' },
params: ['meeple1', 'region1']
});
});
it('should handle extra whitespace', () => {
const result = parseCommand(' move meeple1 --force ');
expect(result).toEqual({
name: 'move',
flags: { force: true },
options: {},
params: ['meeple1']
});
});
it('should parse complex command', () => {
const result = parseCommand('place meeple1 board --x 5 --y 3 --rotate 90 --force');
expect(result).toEqual({
name: 'place',
flags: { force: true },
options: { x: '5', y: '3', rotate: '90' },
params: ['meeple1', 'board']
});
});
it('should treat negative number as option value', () => {
const result = parseCommand('set --value -10');
expect(result).toEqual({
name: 'set',
flags: {},
options: { value: '-10' },
params: []
});
});
});
+117
View File
@@ -0,0 +1,117 @@
import { describe, it, expect } from 'vitest';
import { createEntityCollection, type Entity } from '../../src/utils/entity';
interface TestEntity extends Entity {
name: string;
value: number;
}
describe('createEntityCollection', () => {
it('should create empty collection', () => {
const collection = createEntityCollection<TestEntity>();
expect(collection.collection.value).toEqual({});
});
it('should add single entity', () => {
const collection = createEntityCollection<TestEntity>();
const entity: TestEntity = { id: 'e1', name: 'Entity 1', value: 10 };
collection.add(entity);
expect(collection.collection.value).toHaveProperty('e1');
expect(collection.get('e1').value).toEqual(entity);
});
it('should add multiple entities', () => {
const collection = createEntityCollection<TestEntity>();
const entity1: TestEntity = { id: 'e1', name: 'Entity 1', value: 10 };
const entity2: TestEntity = { id: 'e2', name: 'Entity 2', value: 20 };
const entity3: TestEntity = { id: 'e3', name: 'Entity 3', value: 30 };
collection.add(entity1, entity2, entity3);
expect(Object.keys(collection.collection.value)).toHaveLength(3);
expect(collection.get('e1').value.name).toBe('Entity 1');
expect(collection.get('e2').value.name).toBe('Entity 2');
expect(collection.get('e3').value.name).toBe('Entity 3');
});
it('should remove single entity', () => {
const collection = createEntityCollection<TestEntity>();
const entity1: TestEntity = { id: 'e1', name: 'Entity 1', value: 10 };
const entity2: TestEntity = { id: 'e2', name: 'Entity 2', value: 20 };
collection.add(entity1, entity2);
collection.remove('e1');
expect(Object.keys(collection.collection.value)).toHaveLength(1);
expect(collection.collection.value).not.toHaveProperty('e1');
expect(collection.collection.value).toHaveProperty('e2');
});
it('should remove multiple entities', () => {
const collection = createEntityCollection<TestEntity>();
const entity1: TestEntity = { id: 'e1', name: 'Entity 1', value: 10 };
const entity2: TestEntity = { id: 'e2', name: 'Entity 2', value: 20 };
const entity3: TestEntity = { id: 'e3', name: 'Entity 3', value: 30 };
collection.add(entity1, entity2, entity3);
collection.remove('e1', 'e3');
expect(Object.keys(collection.collection.value)).toHaveLength(1);
expect(collection.collection.value).toHaveProperty('e2');
});
it('should update entity via accessor', () => {
const collection = createEntityCollection<TestEntity>();
const entity: TestEntity = { id: 'e1', name: 'Entity 1', value: 10 };
collection.add(entity);
const accessor = collection.get('e1');
accessor.value = { ...entity, value: 100, name: 'Updated' };
expect(collection.get('e1').value.value).toBe(100);
expect(collection.get('e1').value.name).toBe('Updated');
});
it('should return undefined for non-existent entity', () => {
const collection = createEntityCollection<TestEntity>();
expect(collection.get('nonexistent').value).toBeUndefined();
});
it('should have correct accessor id', () => {
const collection = createEntityCollection<TestEntity>();
const entity: TestEntity = { id: 'e1', name: 'Entity 1', value: 10 };
collection.add(entity);
const accessor = collection.get('e1');
expect(accessor.id).toBe('e1');
});
it('should handle removing non-existent entity', () => {
const collection = createEntityCollection<TestEntity>();
const entity: TestEntity = { id: 'e1', name: 'Entity 1', value: 10 };
collection.add(entity);
collection.remove('nonexistent');
expect(Object.keys(collection.collection.value)).toHaveLength(1);
});
it('should work with reactive updates', () => {
const collection = createEntityCollection<TestEntity>();
const entity: TestEntity = { id: 'e1', name: 'Entity 1', value: 10 };
collection.add(entity);
// 验证 accessor 可以正确获取和设置值
const accessor = collection.get('e1');
expect(accessor.value.value).toBe(10);
accessor.value = { ...entity, value: 50 };
expect(accessor.value.value).toBe(50);
});
});
+98
View File
@@ -0,0 +1,98 @@
import { describe, it, expect } from 'vitest';
import { createRNG } from '../../src/utils/rng';
describe('createRNG', () => {
it('should create RNG with default seed', () => {
const rng = createRNG();
expect(rng.getSeed()).toBe(1);
});
it('should create RNG with custom seed', () => {
const rng = createRNG(12345);
expect(rng.getSeed()).toBe(12345);
});
it('should generate numbers in range [0, 1)', () => {
const rng = createRNG(42);
for (let i = 0; i < 100; i++) {
const num = rng.next();
expect(num).toBeGreaterThanOrEqual(0);
expect(num).toBeLessThan(1);
}
});
it('should generate numbers with max parameter', () => {
const rng = createRNG(42);
for (let i = 0; i < 100; i++) {
const num = rng.next(100);
expect(num).toBeGreaterThanOrEqual(0);
expect(num).toBeLessThan(100);
}
});
it('should generate integers in range [0, max)', () => {
const rng = createRNG(42);
for (let i = 0; i < 100; i++) {
const num = rng.nextInt(10);
expect(Number.isInteger(num)).toBe(true);
expect(num).toBeGreaterThanOrEqual(0);
expect(num).toBeLessThan(10);
}
});
it('should be deterministic with same seed', () => {
const rng1 = createRNG(12345);
const rng2 = createRNG(12345);
const sequence1 = Array.from({ length: 10 }, () => rng1.next());
const sequence2 = Array.from({ length: 10 }, () => rng2.next());
expect(sequence1).toEqual(sequence2);
});
it('should produce different sequences with different seeds', () => {
const rng1 = createRNG(12345);
const rng2 = createRNG(54321);
const sequence1 = Array.from({ length: 10 }, () => rng1.next());
const sequence2 = Array.from({ length: 10 }, () => rng2.next());
expect(sequence1).not.toEqual(sequence2);
});
it('should reset seed with setSeed', () => {
const rng = createRNG(42);
const firstSequence = Array.from({ length: 5 }, () => rng.next());
rng.setSeed(42);
const secondSequence = Array.from({ length: 5 }, () => rng.next());
expect(firstSequence).toEqual(secondSequence);
});
it('should work as callable function', () => {
const rng = createRNG(42);
rng(123);
expect(rng.getSeed()).toBe(123);
});
it('should generate uniformly distributed integers', () => {
const rng = createRNG(42);
const buckets = new Array(10).fill(0);
const iterations = 10000;
for (let i = 0; i < iterations; i++) {
const num = rng.nextInt(10);
buckets[num]++;
}
// 每个桶应该大约有 10% 的值
const expected = iterations / 10;
const tolerance = expected * 0.3; // 30% 容差
buckets.forEach((count, index) => {
expect(count).toBeGreaterThan(expected - tolerance);
expect(count).toBeLessThan(expected + tolerance);
});
});
});