From fe3bef0a0127e07e4f237bff8d87c6505fe75c77 Mon Sep 17 00:00:00 2001 From: hypercross Date: Mon, 6 Apr 2026 15:02:31 +0800 Subject: [PATCH] chore: update api / test --- src/core/game-host.ts | 2 +- src/utils/command/command-registry.ts | 16 ---------------- src/utils/command/command-runner.ts | 2 -- tests/core/game-host.test.ts | 12 ++++++------ 4 files changed, 7 insertions(+), 25 deletions(-) diff --git a/src/core/game-host.ts b/src/core/game-host.ts index 6aa0968..26f04c4 100644 --- a/src/core/game-host.ts +++ b/src/core/game-host.ts @@ -68,7 +68,7 @@ export class GameHost, TResult=unknown> { this._activePromptPlayer.value = null; } - onInput(input: string): string | null { + tryInput(input: string): string | null { if (this._isDisposed) { return 'GameHost is disposed'; } diff --git a/src/utils/command/command-registry.ts b/src/utils/command/command-registry.ts index ec8f385..50ff49c 100644 --- a/src/utils/command/command-registry.ts +++ b/src/utils/command/command-registry.ts @@ -11,7 +11,6 @@ import type { import { parseCommand } from './command-parse'; import { applyCommandSchema } from './command-validate'; import { parseCommandSchema } from './schema-parse'; -import {AsyncQueue} from "@/utils/async-queue"; type CanRunParsed = { runParsed(command: Command): Promise>, @@ -89,7 +88,6 @@ type PromptEndListener = () => void; export type CommandRunnerContextExport = CommandRunnerContext & { registry: CommandRegistry; - promptQueue: AsyncQueue; _activePrompt: PromptEvent | null; _tryCommit: (commandOrInput: Command | string) => string | null; _cancel: (reason?: string) => void; @@ -196,26 +194,12 @@ export function createCommandRunnerContext( _tryCommit: tryCommit, _cancel: cancel, _pendingInput: null, - promptQueue: null! }; Object.defineProperty(runnerCtx, '_activePrompt', { get: () => activePrompt, }); - let promptQueue: AsyncQueue; - Object.defineProperty(runnerCtx, 'promptQueue', { - get(){ - if (!promptQueue) { - promptQueue = new AsyncQueue(); - promptListeners.add(async (event) => { - promptQueue.push(event); - }); - } - return promptQueue; - } - }); - return runnerCtx; } diff --git a/src/utils/command/command-runner.ts b/src/utils/command/command-runner.ts index 130e9e6..809e9bb 100644 --- a/src/utils/command/command-runner.ts +++ b/src/utils/command/command-runner.ts @@ -1,6 +1,4 @@ import type { Command, CommandSchema } from './types'; -import { parseCommand } from './command-parse'; -import { applyCommandSchema } from './command-validate'; export type PromptEvent = { schema: CommandSchema; diff --git a/tests/core/game-host.test.ts b/tests/core/game-host.test.ts index e1f6116..303d990 100644 --- a/tests/core/game-host.test.ts +++ b/tests/core/game-host.test.ts @@ -55,11 +55,11 @@ describe('GameHost', () => { }); }); - describe('onInput', () => { + describe('tryInput', () => { it('should return "No active prompt" when no prompt is active', () => { 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'); }); @@ -73,7 +73,7 @@ describe('GameHost', () => { expect(promptEvent.schema.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(); // Cancel to end the game since start runs until game over @@ -97,7 +97,7 @@ describe('GameHost', () => { const promptEvent = await promptPromise; - const error = host.onInput('invalid command'); + const error = host.tryInput('invalid command'); expect(error).not.toBeNull(); promptEvent.cancel('test cleanup'); @@ -113,7 +113,7 @@ describe('GameHost', () => { const { host } = createTestHost(); host.dispose(); - const result = host.onInput('play X 1 1'); + const result = host.tryInput('play X 1 1'); expect(result).toBe('GameHost is disposed'); }); }); @@ -423,7 +423,7 @@ describe('GameHost', () => { expect(promptEvent.schema.name).toBe('play'); // Submit the move - const error = host.onInput(moves[i]); + const error = host.tryInput(moves[i]); expect(error).toBeNull(); // Wait for the command to complete before submitting next move