chore: update api / test

This commit is contained in:
hypercross 2026-04-06 15:02:31 +08:00
parent 111d2e69eb
commit fe3bef0a01
4 changed files with 7 additions and 25 deletions

View File

@ -68,7 +68,7 @@ export class GameHost<TState extends Record<string, unknown>, TResult=unknown> {
this._activePromptPlayer.value = null; this._activePromptPlayer.value = null;
} }
onInput(input: string): string | null { tryInput(input: string): string | null {
if (this._isDisposed) { if (this._isDisposed) {
return 'GameHost is disposed'; return 'GameHost is disposed';
} }

View File

@ -11,7 +11,6 @@ import type {
import { parseCommand } from './command-parse'; import { parseCommand } from './command-parse';
import { applyCommandSchema } from './command-validate'; import { applyCommandSchema } from './command-validate';
import { parseCommandSchema } from './schema-parse'; import { parseCommandSchema } from './schema-parse';
import {AsyncQueue} from "@/utils/async-queue";
type CanRunParsed = { type CanRunParsed = {
runParsed<T=unknown>(command: Command): Promise<CommandResult<T>>, runParsed<T=unknown>(command: Command): Promise<CommandResult<T>>,
@ -89,7 +88,6 @@ type PromptEndListener = () => void;
export type CommandRunnerContextExport<TContext> = CommandRunnerContext<TContext> & { export type CommandRunnerContextExport<TContext> = CommandRunnerContext<TContext> & {
registry: CommandRegistry<TContext>; registry: CommandRegistry<TContext>;
promptQueue: AsyncQueue<PromptEvent>;
_activePrompt: PromptEvent | null; _activePrompt: PromptEvent | null;
_tryCommit: (commandOrInput: Command | string) => string | null; _tryCommit: (commandOrInput: Command | string) => string | null;
_cancel: (reason?: string) => void; _cancel: (reason?: string) => void;
@ -196,26 +194,12 @@ export function createCommandRunnerContext<TContext>(
_tryCommit: tryCommit, _tryCommit: tryCommit,
_cancel: cancel, _cancel: cancel,
_pendingInput: null, _pendingInput: null,
promptQueue: null!
}; };
Object.defineProperty(runnerCtx, '_activePrompt', { Object.defineProperty(runnerCtx, '_activePrompt', {
get: () => activePrompt, get: () => activePrompt,
}); });
let promptQueue: AsyncQueue<PromptEvent>;
Object.defineProperty(runnerCtx, 'promptQueue', {
get(){
if (!promptQueue) {
promptQueue = new AsyncQueue();
promptListeners.add(async (event) => {
promptQueue.push(event);
});
}
return promptQueue;
}
});
return runnerCtx; return runnerCtx;
} }

View File

@ -1,6 +1,4 @@
import type { Command, CommandSchema } from './types'; import type { Command, CommandSchema } from './types';
import { parseCommand } from './command-parse';
import { applyCommandSchema } from './command-validate';
export type PromptEvent = { export type PromptEvent = {
schema: CommandSchema; schema: CommandSchema;

View File

@ -55,11 +55,11 @@ describe('GameHost', () => {
}); });
}); });
describe('onInput', () => { describe('tryInput', () => {
it('should return "No active prompt" when no prompt is active', () => { it('should return "No active prompt" when no prompt is active', () => {
const { host } = createTestHost(); const { host } = createTestHost();
const result = host.onInput('play X 1 1'); const result = host.tryInput('play X 1 1');
expect(result).toBe('No active prompt'); expect(result).toBe('No active prompt');
}); });
@ -73,7 +73,7 @@ describe('GameHost', () => {
expect(promptEvent.schema.name).toBe('play'); expect(promptEvent.schema.name).toBe('play');
expect(host.activePromptSchema.value?.name).toBe('play'); expect(host.activePromptSchema.value?.name).toBe('play');
const error = host.onInput('play X 1 1'); const error = host.tryInput('play X 1 1');
expect(error).toBeNull(); expect(error).toBeNull();
// Cancel to end the game since start runs until game over // Cancel to end the game since start runs until game over
@ -97,7 +97,7 @@ describe('GameHost', () => {
const promptEvent = await promptPromise; const promptEvent = await promptPromise;
const error = host.onInput('invalid command'); const error = host.tryInput('invalid command');
expect(error).not.toBeNull(); expect(error).not.toBeNull();
promptEvent.cancel('test cleanup'); promptEvent.cancel('test cleanup');
@ -113,7 +113,7 @@ describe('GameHost', () => {
const { host } = createTestHost(); const { host } = createTestHost();
host.dispose(); host.dispose();
const result = host.onInput('play X 1 1'); const result = host.tryInput('play X 1 1');
expect(result).toBe('GameHost is disposed'); expect(result).toBe('GameHost is disposed');
}); });
}); });
@ -423,7 +423,7 @@ describe('GameHost', () => {
expect(promptEvent.schema.name).toBe('play'); expect(promptEvent.schema.name).toBe('play');
// Submit the move // Submit the move
const error = host.onInput(moves[i]); const error = host.tryInput(moves[i]);
expect(error).toBeNull(); expect(error).toBeNull();
// Wait for the command to complete before submitting next move // Wait for the command to complete before submitting next move