refactor: replace rule.ts with a command runner based solution

This commit is contained in:
hypercross 2026-04-02 09:33:03 +08:00
parent bcb31da773
commit 40788d445d
7 changed files with 505 additions and 721 deletions

View File

@ -2,20 +2,47 @@ import {createModel, Signal, signal} from '@preact/signals-core';
import {createEntityCollection} from "../utils/entity"; import {createEntityCollection} from "../utils/entity";
import {Part} from "./part"; import {Part} from "./part";
import {Region} from "./region"; import {Region} from "./region";
import {RuleDef, RuleRegistry, RuleContext, RuleEngineHost, dispatchCommand as dispatchRuleCommand} from "./rule"; import {createCommandRunnerContext, CommandRegistry, createCommandRegistry, type CommandRunnerContextExport} from "../utils/command";
import type {Command} from "../utils/command";
import {parseCommand} from "../utils/command/command-parse";
import {applyCommandSchema} from "../utils/command/command-validate";
import {parseCommandSchema} from "../utils/command/schema-parse";
import type {CommandRunner} from "../utils/command/command-runner";
export type Context = { export type Context = {
type: string; type: string;
} }
export type GameQueueState = 'idle' | 'processing' | 'waiting-for-prompt';
export interface IGameContext {
parts: ReturnType<typeof createEntityCollection<Part>>;
regions: ReturnType<typeof createEntityCollection<Region>>;
commandRegistry: Signal<CommandRegistry<IGameContext>>;
contexts: Signal<Signal<Context>[]>;
pushContext: (context: Context) => Context;
popContext: () => void;
latestContext: <T extends Context>(type: T['type']) => Signal<T> | undefined;
registerCommand: (name: string, runner: CommandRunner<IGameContext, unknown>) => void;
unregisterCommand: (name: string) => void;
enqueue: (input: string) => void;
enqueueAll: (inputs: string[]) => void;
dispatchCommand: (input: string) => void;
}
export const GameContext = createModel((root: Context) => { export const GameContext = createModel((root: Context) => {
const parts = createEntityCollection<Part>(); const parts = createEntityCollection<Part>();
const regions = createEntityCollection<Region>(); const regions = createEntityCollection<Region>();
const rules = signal<RuleRegistry>(new Map()); const commandRegistry = signal<CommandRegistry<IGameContext>>(createCommandRegistry());
const ruleContexts = signal<RuleContext<unknown>[]>([]);
const contexts = signal<Signal<Context>[]>([]); const contexts = signal<Signal<Context>[]>([]);
contexts.value = [signal(root)]; contexts.value = [signal(root)];
const inputQueue: string[] = [];
let processing = false;
let pendingPromptResolve: ((cmd: Command) => void) | null = null;
let pendingPromptReject: ((err: Error) => void) | null = null;
let activeRunnerCtx: CommandRunnerContextExport<IGameContext> | null = null;
function pushContext(context: Context) { function pushContext(context: Context) {
const ctxSignal = signal(context); const ctxSignal = signal(context);
contexts.value = [...contexts.value, ctxSignal]; contexts.value = [...contexts.value, ctxSignal];
@ -37,58 +64,165 @@ export const GameContext = createModel((root: Context) => {
return undefined; return undefined;
} }
function registerRule(name: string, rule: RuleDef<unknown, RuleEngineHost>) { function registerCommand(name: string, runner: CommandRunner<IGameContext, unknown>) {
const newRules = new Map(rules.value); const newRegistry = new Map(commandRegistry.value);
newRules.set(name, rule); newRegistry.set(name, runner);
rules.value = newRules; commandRegistry.value = newRegistry;
} }
function unregisterRule(name: string) { function unregisterCommand(name: string) {
const newRules = new Map(rules.value); const newRegistry = new Map(commandRegistry.value);
newRules.delete(name); newRegistry.delete(name);
rules.value = newRules; commandRegistry.value = newRegistry;
} }
function addRuleContext(ctx: RuleContext<unknown>) { function makeRunnerCtx(): CommandRunnerContextExport<IGameContext> {
ruleContexts.value = [...ruleContexts.value, ctx]; const ctx = createCommandRunnerContext(commandRegistry.value, instance as IGameContext);
ctx.prompt = async (schema) => {
const parsedSchema = typeof schema === 'string'
? parseCommandSchema(schema)
: schema;
return new Promise<Command>((resolve, reject) => {
pendingPromptResolve = resolve;
pendingPromptReject = reject;
const event = { schema: parsedSchema, resolve, reject };
for (const listener of (ctx as any)._listeners || []) {
listener(event);
}
});
};
const origRun = ctx.run.bind(ctx);
ctx.run = async (input: string) => {
const prevCtx = activeRunnerCtx;
activeRunnerCtx = ctx;
const result = await runCommand(ctx, input);
activeRunnerCtx = prevCtx;
return result;
};
return ctx;
} }
function removeRuleContext(ctx: RuleContext<unknown>) { async function runCommand(runnerCtx: CommandRunnerContextExport<IGameContext>, input: string): Promise<{ success: true; result: unknown } | { success: false; error: string }> {
ruleContexts.value = ruleContexts.value.filter(c => c !== ctx); const command = parseCommand(input);
const runner = runnerCtx.registry.get(command.name);
if (!runner) {
return { success: false, error: `Unknown command: ${command.name}` };
} }
function dispatchCommand(this: GameContextInstance, input: string) { const validationResult = applyCommandSchema(command, runner.schema);
return dispatchRuleCommand({ if (!validationResult.valid) {
rules: rules.value, return { success: false, error: validationResult.errors.join('; ') };
ruleContexts: ruleContexts.value, }
addRuleContext,
removeRuleContext, try {
pushContext, const result = await runner.run.call(runnerCtx, validationResult.command);
popContext, return { success: true, result };
latestContext, } catch (e) {
const error = e as Error;
return { success: false, error: error.message };
}
}
async function processQueue(): Promise<void> {
if (processing) return;
processing = true;
while (inputQueue.length > 0) {
if (pendingPromptResolve) {
const input = inputQueue.shift()!;
try {
const command = parseCommand(input);
pendingPromptResolve(command);
} catch (e) {
pendingPromptReject!(new Error(`Invalid input for prompt: ${input}`));
}
pendingPromptResolve = null;
pendingPromptReject = null;
continue;
}
const input = inputQueue.shift()!;
const runnerCtx = makeRunnerCtx();
const prevCtx = activeRunnerCtx;
activeRunnerCtx = runnerCtx;
runCommand(runnerCtx, input).finally(() => {
if (activeRunnerCtx === runnerCtx) {
activeRunnerCtx = prevCtx;
}
});
await Promise.resolve();
}
processing = false;
}
function enqueue(input: string) {
if (pendingPromptResolve) {
try {
const command = parseCommand(input);
pendingPromptResolve(command);
} catch (e) {
pendingPromptReject!(new Error(`Invalid input for prompt: ${input}`));
}
pendingPromptResolve = null;
pendingPromptReject = null;
} else {
inputQueue.push(input);
if (!processing) {
void processQueue();
}
}
}
function enqueueAll(inputs: string[]) {
for (const input of inputs) {
if (pendingPromptResolve) {
try {
const command = parseCommand(input);
pendingPromptResolve(command);
} catch (e) {
pendingPromptReject!(new Error(`Invalid input for prompt: ${input}`));
}
pendingPromptResolve = null;
pendingPromptReject = null;
} else {
inputQueue.push(input);
}
}
if (!processing) {
void processQueue();
}
}
function dispatchCommand(input: string) {
enqueue(input);
}
const instance: IGameContext = {
parts, parts,
regions, regions,
} as any, input); commandRegistry,
}
return {
parts,
regions,
rules,
ruleContexts,
contexts, contexts,
pushContext, pushContext,
popContext, popContext,
latestContext, latestContext,
registerRule, registerCommand,
unregisterRule, unregisterCommand,
enqueue,
enqueueAll,
dispatchCommand, dispatchCommand,
} };
return instance;
}) })
/** 创建游戏上下文实<E69687>?*/
export function createGameContext(root: Context = { type: 'game' }) { export function createGameContext(root: Context = { type: 'game' }) {
return new GameContext(root); return new GameContext(root);
} }
export type GameContextInstance = ReturnType<typeof createGameContext>; export type GameContextInstance = IGameContext;

View File

@ -1,233 +0,0 @@
import {Command, CommandSchema, parseCommand, parseCommandSchema, applyCommandSchema} from "../utils/command";
export type RuleState = 'running' | 'yielded' | 'waiting' | 'invoking' | 'done';
export type SchemaYield = { type: 'schema'; value: string | CommandSchema };
export type InvokeYield = { type: 'invoke'; rule: string; command: Command };
export type RuleYield = SchemaYield | InvokeYield;
export type RuleContext<T = unknown> = {
type: string;
schema?: CommandSchema;
generator: Generator<RuleYield, T, Command | RuleContext<unknown>>;
parent?: RuleContext<unknown>;
children: RuleContext<unknown>[];
state: RuleState;
resolution?: T;
}
export type RuleDef<T = unknown, H extends RuleEngineHost = RuleEngineHost> = {
schema: CommandSchema;
create: (this: H, cmd: Command) => Generator<RuleYield, T, Command | RuleContext<unknown>>;
};
export type RuleRegistry = Map<string, RuleDef<unknown, RuleEngineHost>>;
export type RuleEngineHost = {
rules: RuleRegistry;
ruleContexts: RuleContext<unknown>[];
addRuleContext: (ctx: RuleContext<unknown>) => void;
removeRuleContext: (ctx: RuleContext<unknown>) => void;
};
export function createRule<T, H extends RuleEngineHost = RuleEngineHost>(
schemaStr: string,
fn: (this: H, cmd: Command) => Generator<RuleYield, T, Command | RuleContext<unknown>>
): RuleDef<T, H> {
return {
schema: parseCommandSchema(schemaStr, ''),
create: fn as RuleDef<T, H>['create'],
};
}
function parseYieldedSchema(value: string | CommandSchema): CommandSchema {
if (typeof value === 'string') {
return parseCommandSchema(value, '');
}
return value;
}
function addContextToHost(host: RuleEngineHost, ctx: RuleContext<unknown>) {
host.addRuleContext(ctx);
}
function discardChildren(host: RuleEngineHost, parent: RuleContext<unknown>) {
for (const child of parent.children) {
host.removeRuleContext(child);
}
parent.children = [];
parent.state = 'yielded';
}
function commandMatchesSchema(command: Command, schema: CommandSchema): boolean {
const requiredParams = schema.params.filter(p => p.required);
const variadicParam = schema.params.find(p => p.variadic);
if (command.params.length < requiredParams.length) {
return false;
}
if (!variadicParam && command.params.length > schema.params.length) {
return false;
}
const requiredOptions = Object.values(schema.options).filter(o => o.required);
for (const opt of requiredOptions) {
const hasOption = opt.name in command.options || (opt.short && opt.short in command.options);
if (!hasOption) {
return false;
}
}
return true;
}
function applySchemaToCommand(command: Command, schema: CommandSchema): Command {
return applyCommandSchema(command, schema).command;
}
function findYieldedContext(contexts: RuleContext<unknown>[]): RuleContext<unknown> | undefined {
for (let i = contexts.length - 1; i >= 0; i--) {
const ctx = contexts[i];
if (ctx.state === 'yielded') {
return ctx;
}
}
return undefined;
}
function createContext<T>(
command: Command,
ruleDef: RuleDef<T>,
host: RuleEngineHost,
parent?: RuleContext<unknown>
): RuleContext<T> {
return {
type: ruleDef.schema.name,
schema: undefined,
generator: ruleDef.create.call(host, command),
parent,
children: [],
state: 'running',
resolution: undefined,
};
}
function handleGeneratorResult<T>(
host: RuleEngineHost,
ctx: RuleContext<T>,
result: IteratorResult<RuleYield, T>
): RuleContext<unknown> | undefined {
if (result.done) {
ctx.resolution = result.value;
ctx.state = 'done';
return resumeParentAfterChildComplete(host, ctx as RuleContext<unknown>);
}
const yielded = result.value;
if (yielded.type === 'invoke') {
const childRuleDef = host.rules.get(yielded.rule);
if (childRuleDef) {
ctx.state = 'invoking';
return invokeChildRule(host, yielded.rule, yielded.command, ctx as RuleContext<unknown>);
} else {
ctx.schema = parseYieldedSchema({ name: '', params: [], options: {}, flags: {} });
ctx.state = 'yielded';
}
} else {
ctx.schema = parseYieldedSchema(yielded.value);
ctx.state = 'yielded';
}
return undefined;
}
function stepGenerator<T>(
host: RuleEngineHost,
ctx: RuleContext<T>
): RuleContext<T> {
const result = ctx.generator.next();
const resumed = handleGeneratorResult(host, ctx, result);
if (resumed) return resumed as RuleContext<T>;
return ctx;
}
function invokeChildRule<T>(
host: RuleEngineHost,
ruleName: string,
command: Command,
parent: RuleContext<unknown>
): RuleContext<T> {
const ruleDef = host.rules.get(ruleName)!;
const ctx = createContext(command, ruleDef, host, parent);
parent.children.push(ctx as RuleContext<unknown>);
addContextToHost(host, ctx as RuleContext<unknown>);
return stepGenerator(host, ctx) as RuleContext<T>;
}
function resumeParentAfterChildComplete(
host: RuleEngineHost,
childCtx: RuleContext<unknown>
): RuleContext<unknown> | undefined {
const parent = childCtx.parent;
if (!parent || parent.state !== 'invoking') return undefined;
parent.children = parent.children.filter(c => c !== childCtx);
const result = parent.generator.next(childCtx);
const resumed = handleGeneratorResult(host, parent, result);
if (resumed) return resumed;
return parent;
}
function invokeRule<T>(
host: RuleEngineHost,
command: Command,
ruleDef: RuleDef<T>,
parent?: RuleContext<unknown>
): RuleContext<T> {
const ctx = createContext(command, ruleDef, host, parent);
if (parent) {
discardChildren(host, parent);
parent.children.push(ctx as RuleContext<unknown>);
parent.state = 'waiting';
}
addContextToHost(host, ctx as RuleContext<unknown>);
return stepGenerator(host, ctx);
}
function feedYieldedContext(
host: RuleEngineHost,
ctx: RuleContext<unknown>,
command: Command
): RuleContext<unknown> {
const typedCommand = applySchemaToCommand(command, ctx.schema!);
const result = ctx.generator.next(typedCommand);
const resumed = handleGeneratorResult(host, ctx, result);
return resumed ?? ctx;
}
export function dispatchCommand(host: RuleEngineHost, input: string): RuleContext<unknown> | undefined {
const command = parseCommand(input);
const matchedRule = host.rules.get(command.name);
if (matchedRule) {
const typedCommand = applySchemaToCommand(command, matchedRule.schema);
const parent = findYieldedContext(host.ruleContexts);
return invokeRule(host, typedCommand, matchedRule, parent);
}
for (let i = host.ruleContexts.length - 1; i >= 0; i--) {
const ctx = host.ruleContexts[i];
if (ctx.state === 'yielded' && ctx.schema && commandMatchesSchema(command, ctx.schema)) {
return feedYieldedContext(host, ctx, command);
}
}
return undefined;
}

View File

@ -4,7 +4,7 @@
*/ */
// Core types // Core types
export type { Context } from './core/context'; export type { Context, GameContextInstance, GameQueueState } from './core/context';
export { GameContext, createGameContext } from './core/context'; export { GameContext, createGameContext } from './core/context';
export type { Part } from './core/part'; export type { Part } from './core/part';
@ -13,15 +13,12 @@ export { flip, flipTo, roll } from './core/part';
export type { Region, RegionAxis } from './core/region'; export type { Region, RegionAxis } from './core/region';
export { applyAlign, shuffle } from './core/region'; export { applyAlign, shuffle } from './core/region';
export type { RuleContext, RuleState, RuleDef, RuleRegistry } from './core/rule';
export { createRule, dispatchCommand } from './core/rule';
// Utils // Utils
export type { Command, CommandSchema, CommandParamSchema, CommandOptionSchema, CommandFlagSchema } from './utils/command'; export type { Command, CommandSchema, CommandParamSchema, CommandOptionSchema, CommandFlagSchema } from './utils/command';
export { parseCommand, parseCommandSchema, validateCommand, parseCommandWithSchema, applyCommandSchema } from './utils/command'; export { parseCommand, parseCommandSchema, validateCommand, parseCommandWithSchema, applyCommandSchema } from './utils/command';
export type { CommandRunner, CommandRunnerHandler, CommandRegistry, CommandRunnerContext } from './utils/command'; export type { CommandRunner, CommandRunnerHandler, CommandRunnerContext, PromptEvent, CommandRunnerEvents } from './utils/command';
export { createCommandRegistry, registerCommand, unregisterCommand, hasCommand, getCommand, runCommand, createCommandRunnerContext } from './utils/command'; export { createCommandRegistry, registerCommand, unregisterCommand, hasCommand, getCommand, runCommand, runCommandParsed, createCommandRunnerContext, type CommandRegistry, type CommandRunnerContextExport } from './utils/command';
export type { Entity, EntityAccessor } from './utils/entity'; export type { Entity, EntityAccessor } from './utils/entity';
export { createEntityCollection } from './utils/entity'; export { createEntityCollection } from './utils/entity';

View File

@ -1,10 +1,9 @@
import { GameContextInstance } from '../core/context'; import { GameContextInstance } from '../core/context';
import type { RuleEngineHost, RuleContext } from '../core/rule'; import type { Command, CommandRunner, CommandRunnerContext } from '../utils/command';
import { createRule, type InvokeYield, type SchemaYield } from '../core/rule';
import type { Command } from '../utils/command';
import type { Part } from '../core/part'; import type { Part } from '../core/part';
import type { Region } from '../core/region'; import type { Region } from '../core/region';
import type { Context } from '../core/context'; import type { Context } from '../core/context';
import { parseCommandSchema } from '../utils/command/schema-parse';
export type TicTacToeState = Context & { export type TicTacToeState = Context & {
type: 'tic-tac-toe'; type: 'tic-tac-toe';
@ -17,25 +16,18 @@ type TurnResult = {
winner: 'X' | 'O' | 'draw' | null; winner: 'X' | 'O' | 'draw' | null;
}; };
type TicTacToeHost = RuleEngineHost & { function getBoardRegion(host: GameContextInstance) {
pushContext: (context: Context) => any;
latestContext: <T>(type: string) => { value: T } | undefined;
regions: { add: (...entities: any[]) => void; get: (id: string) => { value: { children: any[] } } };
parts: { add: (...entities: any[]) => void; get: (id: string) => any; collection: { value: Record<string, { value: Part }> } };
};
function getBoardRegion(host: TicTacToeHost) {
return host.regions.get('board'); return host.regions.get('board');
} }
function isCellOccupied(host: TicTacToeHost, row: number, col: number): boolean { function isCellOccupied(host: GameContextInstance, row: number, col: number): boolean {
const board = getBoardRegion(host); const board = getBoardRegion(host);
return board.value.children.some( return board.value.children.some(
(child: { value: { position: number[] } }) => child.value.position[0] === row && child.value.position[1] === col (child: { value: { position: number[] } }) => child.value.position[0] === row && child.value.position[1] === col
); );
} }
function checkWinner(host: TicTacToeHost): 'X' | 'O' | 'draw' | null { function checkWinner(host: GameContextInstance): 'X' | 'O' | 'draw' | null {
const parts = Object.values(host.parts.collection.value).map((s: { value: Part }) => s.value); const parts = Object.values(host.parts.collection.value).map((s: { value: Part }) => s.value);
const xPositions = parts.filter((_: Part, i: number) => i % 2 === 0).map((p: Part) => p.position); const xPositions = parts.filter((_: Part, i: number) => i % 2 === 0).map((p: Part) => p.position);
@ -66,7 +58,7 @@ function hasWinningLine(positions: number[][]): boolean {
); );
} }
function placePiece(host: TicTacToeHost, row: number, col: number, moveCount: number) { function placePiece(host: GameContextInstance, row: number, col: number, moveCount: number) {
const board = getBoardRegion(host); const board = getBoardRegion(host);
const piece: Part = { const piece: Part = {
id: `piece-${moveCount}`, id: `piece-${moveCount}`,
@ -79,18 +71,18 @@ function placePiece(host: TicTacToeHost, row: number, col: number, moveCount: nu
board.value.children.push(host.parts.get(piece.id)); board.value.children.push(host.parts.get(piece.id));
} }
const playSchema: SchemaYield = { type: 'schema', value: 'play <player> <row:number> <col:number>' }; export function createSetupCommand(): CommandRunner<GameContextInstance, { winner: 'X' | 'O' | 'draw' | null }> {
return {
export function createSetupRule() { schema: parseCommandSchema('start'),
return createRule('start', function*(this: TicTacToeHost) { run: async function(this: CommandRunnerContext<GameContextInstance>) {
this.pushContext({ this.context.pushContext({
type: 'tic-tac-toe', type: 'tic-tac-toe',
currentPlayer: 'X', currentPlayer: 'X',
winner: null, winner: null,
moveCount: 0, moveCount: 0,
} as TicTacToeState); } as TicTacToeState);
this.regions.add({ this.context.regions.add({
id: 'board', id: 'board',
axes: [ axes: [
{ name: 'x', min: 0, max: 2 }, { name: 'x', min: 0, max: 2 },
@ -103,58 +95,54 @@ export function createSetupRule() {
let turnResult: TurnResult | undefined; let turnResult: TurnResult | undefined;
while (true) { while (true) {
const yieldValue: InvokeYield = { const turnOutput = await this.run(`turn ${currentPlayer}`);
type: 'invoke', if (!turnOutput.success) throw new Error(turnOutput.error);
rule: 'turn', turnResult = turnOutput.result as TurnResult;
command: { name: 'turn', params: [currentPlayer], flags: {}, options: {} } as Command,
};
const ctx = yield yieldValue;
turnResult = (ctx as RuleContext<TurnResult>).resolution;
if (turnResult?.winner) break; if (turnResult?.winner) break;
currentPlayer = currentPlayer === 'X' ? 'O' : 'X'; currentPlayer = currentPlayer === 'X' ? 'O' : 'X';
const state = this.latestContext<TicTacToeState>('tic-tac-toe')!; const state = this.context.latestContext<TicTacToeState>('tic-tac-toe')!;
state.value.currentPlayer = currentPlayer; state.value.currentPlayer = currentPlayer;
} }
const state = this.latestContext<TicTacToeState>('tic-tac-toe')!; const state = this.context.latestContext<TicTacToeState>('tic-tac-toe')!;
state.value.winner = turnResult?.winner ?? null; state.value.winner = turnResult?.winner ?? null;
return { winner: state.value.winner }; return { winner: state.value.winner };
}); },
};
} }
export function createTurnRule() { export function createTurnCommand(): CommandRunner<GameContextInstance, TurnResult> {
return createRule('turn <player>', function*(this: TicTacToeHost, cmd) { return {
schema: parseCommandSchema('turn <player>'),
run: async function(this: CommandRunnerContext<GameContextInstance>, cmd: Command) {
while (true) { while (true) {
const received = yield playSchema; const playCmd = await this.prompt('play <player> <row:number> <col:number>');
if ('resolution' in received) continue;
const playCmd = received as Command; const row = Number(playCmd.params[1]);
if (playCmd.name !== 'play') continue; const col = Number(playCmd.params[2]);
const row = playCmd.params[1] as number;
const col = playCmd.params[2] as number;
if (isNaN(row) || isNaN(col) || row < 0 || row > 2 || col < 0 || col > 2) continue; if (isNaN(row) || isNaN(col) || row < 0 || row > 2 || col < 0 || col > 2) continue;
if (isCellOccupied(this, row, col)) continue; if (isCellOccupied(this.context, row, col)) continue;
const state = this.latestContext<TicTacToeState>('tic-tac-toe')!; const state = this.context.latestContext<TicTacToeState>('tic-tac-toe')!;
if (state.value.winner) continue; if (state.value.winner) continue;
placePiece(this, row, col, state.value.moveCount); placePiece(this.context, row, col, state.value.moveCount);
state.value.moveCount++; state.value.moveCount++;
const winner = checkWinner(this); const winner = checkWinner(this.context);
if (winner) return { winner }; if (winner) return { winner };
if (state.value.moveCount >= 9) return { winner: 'draw' as const }; if (state.value.moveCount >= 9) return { winner: 'draw' as const };
} }
}); },
};
} }
export function registerTicTacToeRules(game: GameContextInstance) { export function registerTicTacToeCommands(game: GameContextInstance) {
game.registerRule('start', createSetupRule()); game.registerCommand('start', createSetupCommand());
game.registerRule('turn', createTurnRule()); game.registerCommand('turn', createTurnCommand());
} }
export function startTicTacToe(game: GameContextInstance) { export function startTicTacToe(game: GameContextInstance) {

View File

@ -1,7 +1,7 @@
import type { Command } from './types.js'; import type { Command, CommandSchema } from './types.js';
import type { CommandRunner, CommandRunnerContext, PromptEvent } from './command-runner.js'; import type { CommandRunner, CommandRunnerContext, PromptEvent } from './command-runner.js';
import { parseCommand } from './command-parse.js'; import { parseCommand } from './command-parse.js';
import { applyCommandSchema } from './command-apply.js'; import { applyCommandSchema } from './command-validate.js';
import { parseCommandSchema } from './schema-parse.js'; import { parseCommandSchema } from './schema-parse.js';
export type CommandRegistry<TContext> = Map<string, CommandRunner<TContext, unknown>>; export type CommandRegistry<TContext> = Map<string, CommandRunner<TContext, unknown>>;
@ -42,6 +42,10 @@ type Listener = (e: PromptEvent) => void;
export type CommandRunnerContextExport<TContext> = CommandRunnerContext<TContext> & { export type CommandRunnerContextExport<TContext> = CommandRunnerContext<TContext> & {
registry: CommandRegistry<TContext>; registry: CommandRegistry<TContext>;
_activePrompt: PromptEvent | null;
_resolvePrompt: (command: Command) => void;
_rejectPrompt: (error: Error) => void;
_pendingInput: string | null;
}; };
export function createCommandRunnerContext<TContext>( export function createCommandRunnerContext<TContext>(
@ -58,9 +62,26 @@ export function createCommandRunnerContext<TContext>(
listeners.delete(listener); listeners.delete(listener);
}; };
let activePrompt: PromptEvent | null = null;
const resolvePrompt = (command: Command) => {
if (activePrompt) {
activePrompt.resolve(command);
activePrompt = null;
}
};
const rejectPrompt = (error: Error) => {
if (activePrompt) {
activePrompt.reject(error);
activePrompt = null;
}
};
const prompt = (schema: Parameters<CommandRunnerContext<TContext>['prompt']>[0]): Promise<Command> => { const prompt = (schema: Parameters<CommandRunnerContext<TContext>['prompt']>[0]): Promise<Command> => {
const resolvedSchema = typeof schema === 'string' ? parseCommandSchema(schema) : schema; const resolvedSchema = typeof schema === 'string' ? parseCommandSchema(schema) : schema;
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
activePrompt = { schema: resolvedSchema, resolve, reject };
const event: PromptEvent = { schema: resolvedSchema, resolve, reject }; const event: PromptEvent = { schema: resolvedSchema, resolve, reject };
for (const listener of listeners) { for (const listener of listeners) {
listener(event); listener(event);
@ -71,13 +92,21 @@ export function createCommandRunnerContext<TContext>(
const runnerCtx: CommandRunnerContextExport<TContext> = { const runnerCtx: CommandRunnerContextExport<TContext> = {
registry, registry,
context, context,
run: (input: string) => runCommandWithContext(registry, runnerCtx, input), run: (input: string) => runCommandWithContext(runnerCtx, input),
runParsed: (command: Command) => runCommandParsedWithContext(registry, runnerCtx, command), runParsed: (command: Command) => runCommandParsedWithContext(runnerCtx, command),
prompt, prompt,
on, on,
off, off,
_activePrompt: null,
_resolvePrompt: resolvePrompt,
_rejectPrompt: rejectPrompt,
_pendingInput: null,
}; };
Object.defineProperty(runnerCtx, '_activePrompt', {
get: () => activePrompt,
});
return runnerCtx; return runnerCtx;
} }
@ -101,16 +130,15 @@ export async function runCommand<TContext>(
input: string input: string
): Promise<{ success: true; result: unknown } | { success: false; error: string }> { ): Promise<{ success: true; result: unknown } | { success: false; error: string }> {
const runnerCtx = createCommandRunnerContext(registry, context); const runnerCtx = createCommandRunnerContext(registry, context);
return await runCommandWithContext(registry, runnerCtx, input); return await runCommandWithContext(runnerCtx, input);
} }
async function runCommandWithContext<TContext>( async function runCommandWithContext<TContext>(
registry: CommandRegistry<TContext>,
runnerCtx: CommandRunnerContextExport<TContext>, runnerCtx: CommandRunnerContextExport<TContext>,
input: string input: string
): Promise<{ success: true; result: unknown } | { success: false; error: string }> { ): Promise<{ success: true; result: unknown } | { success: false; error: string }> {
const command = parseCommand(input); const command = parseCommand(input);
return await runCommandParsedWithContext(registry, runnerCtx, command); return await runCommandParsedWithContext(runnerCtx, command);
} }
export async function runCommandParsed<TContext>( export async function runCommandParsed<TContext>(
@ -119,15 +147,14 @@ export async function runCommandParsed<TContext>(
command: Command command: Command
): Promise<{ success: true; result: unknown } | { success: false; error: string }> { ): Promise<{ success: true; result: unknown } | { success: false; error: string }> {
const runnerCtx = createCommandRunnerContext(registry, context); const runnerCtx = createCommandRunnerContext(registry, context);
return await runCommandParsedWithContext(registry, runnerCtx, command); return await runCommandParsedWithContext(runnerCtx, command);
} }
async function runCommandParsedWithContext<TContext>( async function runCommandParsedWithContext<TContext>(
registry: CommandRegistry<TContext>,
runnerCtx: CommandRunnerContextExport<TContext>, runnerCtx: CommandRunnerContextExport<TContext>,
command: Command command: Command
): Promise<{ success: true; result: unknown } | { success: false; error: string }> { ): Promise<{ success: true; result: unknown } | { success: false; error: string }> {
const runner = registry.get(command.name); const runner = runnerCtx.registry.get(command.name);
if (!runner) { if (!runner) {
return { success: false, error: `Unknown command: ${command.name}` }; return { success: false, error: `Unknown command: ${command.name}` };
} }

View File

@ -1,396 +1,244 @@
import { describe, it, expect } from 'vitest'; import { describe, it, expect } from 'vitest';
import { createRule, type RuleContext, type RuleEngineHost } from '../../src/core/rule';
import { createGameContext } from '../../src/core/context'; import { createGameContext } from '../../src/core/context';
import type { Command } from '../../src/utils/command'; import type { Command, CommandRunner, CommandRunnerContext } from '../../src/utils/command';
import { parseCommandSchema } from '../../src/utils/command/schema-parse';
function isCommand(value: Command | RuleContext<unknown>): value is Command { describe('Command System', () => {
return 'name' in value;
}
function schema(value: string | { name: string; params: any[]; options: any[]; flags: any[] }) {
return { type: 'schema' as const, value };
}
describe('Rule System', () => {
function createTestGame() { function createTestGame() {
const game = createGameContext(); const game = createGameContext();
return game; return game;
} }
describe('createRule', () => { function createRunner<T = unknown>(
it('should create a rule definition with parsed schema', () => { schemaStr: string,
const rule = createRule('<from> <to> [--force]', function*(cmd) { fn: (this: CommandRunnerContext<any>, cmd: Command) => Promise<T>
return { from: cmd.params[0], to: cmd.params[1] }; ): CommandRunner<any, T> {
}); return {
schema: parseCommandSchema(schemaStr),
run: fn,
};
}
expect(rule.schema.params).toHaveLength(2); describe('registerCommand', () => {
expect(rule.schema.params[0].name).toBe('from'); it('should register and execute a command', async () => {
expect(rule.schema.params[0].required).toBe(true);
expect(rule.schema.params[1].name).toBe('to');
expect(rule.schema.params[1].required).toBe(true);
expect(Object.keys(rule.schema.flags)).toHaveLength(1);
expect(rule.schema.flags.force.name).toBe('force');
});
it('should create a generator when called', () => {
const game = createTestGame();
const rule = createRule('<target>', function*(cmd) {
return cmd.params[0];
});
const gen = rule.create.call(game as unknown as RuleEngineHost, { name: 'test', params: ['card1'], flags: {}, options: {} });
const result = gen.next();
expect(result.done).toBe(true);
expect(result.value).toBe('card1');
});
});
describe('dispatchCommand - rule invocation', () => {
it('should invoke a registered rule and yield schema', () => {
const game = createTestGame(); const game = createTestGame();
game.registerRule('move', createRule('<from> <to>', function*(cmd) { game.registerCommand('look', createRunner('[--at]', async () => {
yield schema({ name: '', params: [], options: [], flags: [] });
return { moved: cmd.params[0] };
}));
const ctx = game.dispatchCommand('move card1 hand');
expect(ctx).toBeDefined();
expect(ctx!.state).toBe('yielded');
expect(ctx!.schema).toBeDefined();
expect(ctx!.resolution).toBeUndefined();
});
it('should complete a rule when final command matches yielded schema', () => {
const game = createTestGame();
game.registerRule('move', createRule('<from> <to>', function*(cmd) {
const confirm = yield schema({ name: '', params: [], options: [], flags: [] });
const confirmCmd = isCommand(confirm) ? confirm : undefined;
return { moved: cmd.params[0], confirmed: confirmCmd?.name === 'confirm' };
}));
game.dispatchCommand('move card1 hand');
const ctx = game.dispatchCommand('confirm');
expect(ctx).toBeDefined();
expect(ctx!.state).toBe('done');
expect(ctx!.resolution).toEqual({ moved: 'card1', confirmed: true });
});
it('should return undefined when command matches no rule and no yielded context', () => {
const game = createTestGame();
const result = game.dispatchCommand('unknown command');
expect(result).toBeUndefined();
});
it('should pass the initial command to the generator', () => {
const game = createTestGame();
game.registerRule('attack', createRule('<target> [--power: number]', function*(cmd) {
return { target: cmd.params[0], power: cmd.options.power || '1' };
}));
const ctx = game.dispatchCommand('attack goblin --power 5');
expect(ctx!.state).toBe('done');
expect(ctx!.resolution).toEqual({ target: 'goblin', power: 5 });
});
it('should complete immediately if generator does not yield', () => {
const game = createTestGame();
game.registerRule('look', createRule('[--at]', function*() {
return 'looked'; return 'looked';
})); }));
const ctx = game.dispatchCommand('look'); game.enqueue('look');
await new Promise(resolve => setTimeout(resolve, 50));
expect(ctx!.state).toBe('done'); expect(game.commandRegistry.value.has('look')).toBe(true);
expect(ctx!.resolution).toBe('looked');
});
}); });
describe('dispatchCommand - rule priority', () => { it('should return error for unknown command', async () => {
it('should prioritize new rule invocation over feeding yielded context', () => {
const game = createTestGame(); const game = createTestGame();
game.registerRule('move', createRule('<from> <to>', function*(cmd) { game.enqueue('unknown command');
yield schema({ name: '', params: [], options: [], flags: [] }); await new Promise(resolve => setTimeout(resolve, 50));
return { moved: cmd.params[0] };
}));
game.registerRule('confirm', createRule('', function*() {
return 'new confirm rule';
}));
game.dispatchCommand('move card1 hand');
const ctx = game.dispatchCommand('confirm');
expect(ctx!.state).toBe('done');
expect(ctx!.resolution).toBe('new confirm rule');
expect(ctx!.type).toBe('');
}); });
}); });
describe('dispatchCommand - fallback to yielded context', () => { describe('prompt and queue resolution', () => {
it('should feed a yielded context when command does not match any rule', () => { it('should resolve prompt from queue input', async () => {
const game = createTestGame(); const game = createTestGame();
let promptReceived: Command | null = null;
game.registerRule('move', createRule('<from> <to>', function*(cmd) { game.registerCommand('move', createRunner('<from> <to>', async function(this: CommandRunnerContext<any>, cmd) {
const response = yield schema({ name: '', params: [], options: [], flags: [] }); const confirm = await this.prompt('confirm');
const rcmd = isCommand(response) ? response : undefined; promptReceived = confirm;
return { moved: cmd.params[0], response: rcmd?.name }; return { moved: cmd.params[0], confirmed: confirm.name };
})); }));
game.dispatchCommand('move card1 hand'); game.enqueueAll([
const ctx = game.dispatchCommand('yes'); 'move card1 hand',
'confirm',
]);
expect(ctx!.state).toBe('done'); await new Promise(resolve => setTimeout(resolve, 100));
expect(ctx!.resolution).toEqual({ moved: 'card1', response: 'yes' });
expect(promptReceived).not.toBeNull();
expect(promptReceived!.name).toBe('confirm');
}); });
it('should skip non-matching commands for yielded context', () => { it('should handle multiple prompts in sequence', async () => {
const game = createTestGame(); const game = createTestGame();
const prompts: Command[] = [];
game.registerRule('move', createRule('<from> <to>', function*(cmd) { game.registerCommand('multi', createRunner('<start>', async function() {
const response = yield schema('<item>'); const a = await this.prompt('<value>');
const rcmd = isCommand(response) ? response : undefined; prompts.push(a);
return { response: rcmd?.params[0] }; const b = await this.prompt('<value>');
prompts.push(b);
return { a: a.params[0], b: b.params[0] };
})); }));
game.dispatchCommand('move card1 hand'); game.enqueueAll([
'multi init',
'first',
'second',
]);
const ctx = game.dispatchCommand('goblin'); await new Promise(resolve => setTimeout(resolve, 100));
expect(ctx).toBeUndefined(); expect(prompts).toHaveLength(2);
expect(prompts[0].params[0]).toBe('first');
expect(prompts[1].params[0]).toBe('second');
}); });
it('should validate command against yielded schema', () => { it('should handle command that completes without prompting', async () => {
const game = createTestGame(); const game = createTestGame();
let executed = false;
game.registerRule('trade', createRule('<from> <to>', function*(cmd) { game.registerCommand('attack', createRunner('<target> [--power: number]', async function(cmd) {
const response = yield schema('<item> [amount: number]'); executed = true;
const rcmd = isCommand(response) ? response : undefined; return { target: cmd.params[0], power: cmd.options.power || '1' };
return { traded: rcmd?.params[0] };
})); }));
game.dispatchCommand('trade player1 player2'); game.enqueue('attack goblin --power 5');
const ctx = game.dispatchCommand('offer gold 5'); await new Promise(resolve => setTimeout(resolve, 50));
expect(ctx!.state).toBe('done'); expect(executed).toBe(true);
expect(ctx!.resolution).toEqual({ traded: 'gold' });
}); });
}); });
describe('dispatchCommand - deepest context first', () => { describe('nested command execution', () => {
it('should feed the deepest yielded context', () => { it('should allow a command to run another command', async () => {
const game = createTestGame(); const game = createTestGame();
let childResult: unknown;
game.registerRule('parent', createRule('<action>', function*() { game.registerCommand('child', createRunner('<arg>', async (cmd) => {
yield schema({ name: '', params: [], options: [], flags: [] }); return `child:${cmd.params[0]}`;
return 'parent done';
})); }));
game.registerRule('child', createRule('<target>', function*() { game.registerCommand('parent', createRunner('<action>', async function() {
yield schema({ name: '', params: [], options: [], flags: [] }); const output = await this.run('child test_arg');
return 'child done'; if (!output.success) throw new Error(output.error);
childResult = output.result;
return `parent:${output.result}`;
})); }));
game.dispatchCommand('parent start'); game.enqueue('parent start');
game.dispatchCommand('child target1'); await new Promise(resolve => setTimeout(resolve, 100));
const ctx = game.dispatchCommand('grandchild_cmd'); expect(childResult).toBe('child:test_arg');
expect(ctx!.state).toBe('done');
expect(ctx!.resolution).toBe('child done');
});
}); });
describe('nested rule invocations', () => { it('should handle nested commands with prompts', async () => {
it('should link child to parent', () => {
const game = createTestGame(); const game = createTestGame();
let childPromptResult: Command | null = null;
game.registerRule('parent', createRule('<action>', function*() { game.registerCommand('child', createRunner('<target>', async function() {
yield schema('child_cmd'); const confirm = await this.prompt('yes | no');
return 'parent done'; childPromptResult = confirm;
return `child:${confirm.name}`;
})); }));
game.registerRule('child_cmd', createRule('<target>', function*() { game.registerCommand('parent', createRunner('<action>', async function() {
return 'child done'; const output = await this.run('child target1');
if (!output.success) throw new Error(output.error);
return `parent:${output.result}`;
})); }));
game.dispatchCommand('parent start'); game.enqueueAll([
const parentCtx = game.ruleContexts.value[0]; 'parent start',
'yes',
]);
game.dispatchCommand('child_cmd target1'); await new Promise(resolve => setTimeout(resolve, 100));
expect(parentCtx.state).toBe('waiting'); expect(childPromptResult).not.toBeNull();
expect(childPromptResult!.name).toBe('yes');
const childCtx = game.ruleContexts.value[1]; });
expect(childCtx.parent).toBe(parentCtx);
expect(parentCtx.children).toContain(childCtx);
}); });
it('should discard previous children when a new child is invoked', () => { describe('enqueueAll for action log replay', () => {
it('should process all inputs in order', async () => {
const game = createTestGame(); const game = createTestGame();
const results: string[] = [];
game.registerRule('parent', createRule('<action>', function*() { game.registerCommand('step', createRunner('<value>', async (cmd) => {
yield schema('child_a | child_b'); results.push(cmd.params[0] as string);
return 'parent done'; return cmd.params[0];
})); }));
game.registerRule('child_a', createRule('<target>', function*() { game.enqueueAll([
return 'child_a done'; 'step one',
})); 'step two',
'step three',
]);
game.registerRule('child_b', createRule('<target>', function*() { await new Promise(resolve => setTimeout(resolve, 100));
return 'child_b done';
}));
game.dispatchCommand('parent start'); expect(results).toEqual(['one', 'two', 'three']);
game.dispatchCommand('child_a target1');
expect(game.ruleContexts.value.length).toBe(2);
const oldParent = game.ruleContexts.value[0];
expect(oldParent.children).toHaveLength(1);
game.dispatchCommand('parent start');
game.dispatchCommand('child_b target2');
const newParent = game.ruleContexts.value[2];
expect(newParent.children).toHaveLength(1);
expect(newParent.children[0].resolution).toBe('child_b done');
});
}); });
describe('context tracking', () => { it('should buffer inputs and resolve prompts automatically', async () => {
it('should track rule contexts in ruleContexts signal', () => {
const game = createTestGame(); const game = createTestGame();
let prompted: Command | null = null;
game.registerRule('test', createRule('<arg>', function*() { game.registerCommand('interactive', createRunner('<start>', async function() {
yield schema({ name: '', params: [], options: [], flags: [] }); const response = await this.prompt('<reply>');
return 'done'; prompted = response;
return { start: 'start', reply: response.params[0] };
})); }));
expect(game.ruleContexts.value.length).toBe(0); game.enqueueAll([
'interactive begin',
'hello',
]);
game.dispatchCommand('test arg1'); await new Promise(resolve => setTimeout(resolve, 100));
expect(game.ruleContexts.value.length).toBe(1); expect(prompted).not.toBeNull();
expect(game.ruleContexts.value[0].state).toBe('yielded'); expect(prompted!.params[0]).toBe('hello');
}); });
}); });
describe('error handling', () => { describe('command schema validation', () => {
it('should leave context in place when generator throws', () => { it('should reject commands that do not match schema', async () => {
const game = createTestGame(); const game = createTestGame();
let errors: string[] = [];
game.registerRule('failing', createRule('<arg>', function*() { game.registerCommand('strict', createRunner('<required>', async () => {
throw new Error('rule error'); return 'ok';
})); }));
expect(() => game.dispatchCommand('failing arg1')).toThrow('rule error'); const originalError = console.error;
console.error = (...args: unknown[]) => {
expect(game.ruleContexts.value.length).toBe(1); errors.push(String(args[0]));
});
it('should leave children in place when child generator throws', () => {
const game = createTestGame();
game.registerRule('parent', createRule('<action>', function*() {
yield schema('child');
return 'parent done';
}));
game.registerRule('child', createRule('<target>', function*() {
throw new Error('child error');
}));
game.dispatchCommand('parent start');
expect(() => game.dispatchCommand('child target1')).toThrow('child error');
expect(game.ruleContexts.value.length).toBe(2);
});
});
describe('schema yielding', () => {
it('should accept a CommandSchema object as yield value', () => {
const game = createTestGame();
const customSchema = {
name: 'custom',
params: [{ name: 'x', required: true, variadic: false }],
options: [],
flags: [],
}; };
game.registerRule('test', createRule('<arg>', function*() { game.enqueue('strict');
const cmd = yield schema(customSchema); await new Promise(resolve => setTimeout(resolve, 50));
const rcmd = isCommand(cmd) ? cmd : undefined;
return { received: rcmd?.params[0] };
}));
game.dispatchCommand('test val1'); console.error = originalError;
const ctx = game.dispatchCommand('custom hello');
expect(ctx!.state).toBe('done'); expect(errors.some(e => e.includes('Unknown') || e.includes('error'))).toBe(true);
expect(ctx!.resolution).toEqual({ received: 'hello' }); });
}); });
it('should parse string schema on each yield', () => { describe('context management', () => {
it('should push and pop contexts', () => {
const game = createTestGame(); const game = createTestGame();
game.registerRule('multi', createRule('<start>', function*() { game.pushContext({ type: 'sub-game' });
const a = yield schema('<value>'); expect(game.contexts.value.length).toBe(2);
const b = yield schema('<value>');
const acmd = isCommand(a) ? a : undefined;
const bcmd = isCommand(b) ? b : undefined;
return { a: acmd?.params[0], b: bcmd?.params[0] };
}));
game.dispatchCommand('multi init'); game.popContext();
game.dispatchCommand('cmd first'); expect(game.contexts.value.length).toBe(1);
const ctx = game.dispatchCommand('cmd second');
expect(ctx!.state).toBe('done');
expect(ctx!.resolution).toEqual({ a: 'first', b: 'second' });
});
}); });
describe('complex flow', () => { it('should find latest context by type', () => {
it('should handle a multi-step game flow', () => {
const game = createTestGame(); const game = createTestGame();
game.registerRule('start', createRule('<player>', function*(cmd) { game.pushContext({ type: 'sub-game' });
const player = cmd.params[0]; const found = game.latestContext('sub-game');
const action = yield schema({ name: '', params: [], options: [], flags: [] });
if (isCommand(action)) { expect(found).toBeDefined();
if (action.name === 'move') { expect(found!.value.type).toBe('sub-game');
yield schema('<target>');
} else if (action.name === 'attack') {
yield schema('<target> [--power: number]');
}
}
return { player, action: isCommand(action) ? action.name : '' };
}));
const ctx1 = game.dispatchCommand('start alice');
expect(ctx1!.state).toBe('yielded');
const ctx2 = game.dispatchCommand('attack');
expect(ctx2!.state).toBe('yielded');
const ctx3 = game.dispatchCommand('attack goblin --power 3');
expect(ctx3!.state).toBe('done');
expect(ctx3!.resolution).toEqual({ player: 'alice', action: 'attack' });
}); });
}); });
}); });

View File

@ -1,11 +1,11 @@
import { describe, it, expect } from 'vitest'; import { describe, it, expect } from 'vitest';
import { createGameContext } from '../../src/core/context'; import { createGameContext } from '../../src/core/context';
import { registerTicTacToeRules, startTicTacToe, type TicTacToeState } from '../../src/samples/tic-tac-toe'; import { registerTicTacToeCommands, startTicTacToe, type TicTacToeState } from '../../src/samples/tic-tac-toe';
describe('Tic-Tac-Toe', () => { describe('Tic-Tac-Toe', () => {
function createGame() { function createGame() {
const game = createGameContext(); const game = createGameContext();
registerTicTacToeRules(game); registerTicTacToeCommands(game);
return game; return game;
} }
@ -13,9 +13,10 @@ describe('Tic-Tac-Toe', () => {
return game.latestContext<TicTacToeState>('tic-tac-toe')!.value; return game.latestContext<TicTacToeState>('tic-tac-toe')!.value;
} }
it('should initialize the board and start the game', () => { it('should initialize the board and start the game', async () => {
const game = createGame(); const game = createGame();
startTicTacToe(game); startTicTacToe(game);
await new Promise(resolve => setTimeout(resolve, 100));
const state = getBoardState(game); const state = getBoardState(game);
expect(state.currentPlayer).toBe('X'); expect(state.currentPlayer).toBe('X');
@ -28,95 +29,117 @@ describe('Tic-Tac-Toe', () => {
expect(board.value.axes[1].name).toBe('y'); expect(board.value.axes[1].name).toBe('y');
}); });
it('should play moves and determine a winner', () => { it('should play moves and determine a winner', async () => {
const game = createGame(); const game = createGame();
startTicTacToe(game); startTicTacToe(game);
await new Promise(resolve => setTimeout(resolve, 100));
// X wins with column 0 game.enqueueAll([
game.dispatchCommand('play X 0 0'); 'play X 0 0',
game.dispatchCommand('play O 0 1'); 'play O 0 1',
game.dispatchCommand('play X 1 0'); 'play X 1 0',
game.dispatchCommand('play O 1 1'); 'play O 1 1',
game.dispatchCommand('play X 2 0'); 'play X 2 0',
]);
await new Promise(resolve => setTimeout(resolve, 200));
const state = getBoardState(game); const state = getBoardState(game);
expect(state.winner).toBe('X'); expect(state.winner).toBe('X');
expect(state.moveCount).toBe(5); expect(state.moveCount).toBe(5);
}); });
it('should reject out-of-bounds moves', () => { it('should reject out-of-bounds moves', async () => {
const game = createGame(); const game = createGame();
startTicTacToe(game); startTicTacToe(game);
await new Promise(resolve => setTimeout(resolve, 100));
const beforeCount = getBoardState(game).moveCount; const beforeCount = getBoardState(game).moveCount;
game.dispatchCommand('play X 5 5'); game.enqueueAll([
game.dispatchCommand('play X -1 0'); 'play X 5 5',
game.dispatchCommand('play X 3 3'); 'play X -1 0',
'play X 3 3',
]);
await new Promise(resolve => setTimeout(resolve, 200));
expect(getBoardState(game).moveCount).toBe(beforeCount); expect(getBoardState(game).moveCount).toBe(beforeCount);
}); });
it('should reject moves on occupied cells', () => { it('should reject moves on occupied cells', async () => {
const game = createGame(); const game = createGame();
startTicTacToe(game); startTicTacToe(game);
await new Promise(resolve => setTimeout(resolve, 100));
game.dispatchCommand('play X 1 1'); game.enqueue('play X 1 1');
await new Promise(resolve => setTimeout(resolve, 100));
expect(getBoardState(game).moveCount).toBe(1); expect(getBoardState(game).moveCount).toBe(1);
// Try to play on the same cell game.enqueue('play O 1 1');
game.dispatchCommand('play O 1 1'); await new Promise(resolve => setTimeout(resolve, 100));
expect(getBoardState(game).moveCount).toBe(1); expect(getBoardState(game).moveCount).toBe(1);
}); });
it('should ignore moves after game is over', () => { it('should ignore moves after game is over', async () => {
const game = createGame(); const game = createGame();
startTicTacToe(game); startTicTacToe(game);
await new Promise(resolve => setTimeout(resolve, 100));
// X wins game.enqueueAll([
game.dispatchCommand('play X 0 0'); 'play X 0 0',
game.dispatchCommand('play O 0 1'); 'play O 0 1',
game.dispatchCommand('play X 1 0'); 'play X 1 0',
game.dispatchCommand('play O 1 1'); 'play O 1 1',
game.dispatchCommand('play X 2 0'); 'play X 2 0',
]);
await new Promise(resolve => setTimeout(resolve, 200));
expect(getBoardState(game).winner).toBe('X'); expect(getBoardState(game).winner).toBe('X');
const moveCountAfterWin = getBoardState(game).moveCount; const moveCountAfterWin = getBoardState(game).moveCount;
// Try to play more game.enqueueAll([
game.dispatchCommand('play X 2 1'); 'play X 2 1',
game.dispatchCommand('play O 2 2'); 'play O 2 2',
]);
await new Promise(resolve => setTimeout(resolve, 200));
expect(getBoardState(game).moveCount).toBe(moveCountAfterWin); expect(getBoardState(game).moveCount).toBe(moveCountAfterWin);
}); });
it('should detect a draw', () => { it('should detect a draw', async () => {
const game = createGame(); const game = createGame();
startTicTacToe(game); startTicTacToe(game);
await new Promise(resolve => setTimeout(resolve, 100));
// Fill board with no winner (cat's game) game.enqueueAll([
// X: (1,1), (0,2), (2,2), (1,0), (2,1) 'play X 1 1',
// O: (0,0), (2,0), (0,1), (1,2) 'play O 0 0',
game.dispatchCommand('play X 1 1'); // X 'play X 0 2',
game.dispatchCommand('play O 0 0'); // O 'play O 2 0',
game.dispatchCommand('play X 0 2'); // X 'play X 2 2',
game.dispatchCommand('play O 2 0'); // O 'play O 0 1',
game.dispatchCommand('play X 2 2'); // X 'play X 1 0',
game.dispatchCommand('play O 0 1'); // O 'play O 1 2',
game.dispatchCommand('play X 1 0'); // X 'play X 2 1',
game.dispatchCommand('play O 1 2'); // O ]);
game.dispatchCommand('play X 2 1'); // X (last move, draw)
await new Promise(resolve => setTimeout(resolve, 300));
const state = getBoardState(game); const state = getBoardState(game);
expect(state.winner).toBe('draw'); expect(state.winner).toBe('draw');
expect(state.moveCount).toBe(9); expect(state.moveCount).toBe(9);
}); });
it('should place parts on the board region at correct positions', () => { it('should place parts on the board region at correct positions', async () => {
const game = createGame(); const game = createGame();
startTicTacToe(game); startTicTacToe(game);
await new Promise(resolve => setTimeout(resolve, 100));
game.dispatchCommand('play X 1 2'); game.enqueue('play X 1 2');
await new Promise(resolve => setTimeout(resolve, 100));
const board = game.regions.get('board'); const board = game.regions.get('board');
expect(board.value.children).toHaveLength(1); expect(board.value.children).toHaveLength(1);