From 912ef6e283e555aaf7a265ca990aafe4117b4c28 Mon Sep 17 00:00:00 2001 From: hypercross Date: Sat, 4 Apr 2026 23:50:46 +0800 Subject: [PATCH] refactor: make game host return the setup promise --- src/core/game-host.ts | 10 ++++------ tests/core/game-host.test.ts | 10 +++++----- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/src/core/game-host.ts b/src/core/game-host.ts index a41a9f5..0131859 100644 --- a/src/core/game-host.ts +++ b/src/core/game-host.ts @@ -93,7 +93,7 @@ export class GameHost> { this._state.clearInterruptions(); } - async setup(setupCommand: string): Promise { + setup(setupCommand: string): Promise { if (this._isDisposed) { throw new Error('GameHost is disposed'); } @@ -103,14 +103,12 @@ export class GameHost> { const initialState = this._createInitialState(); this._state.value = initialState as any; - // Start the setup command but don't wait for it to complete - // The command will run in the background and prompt for input - this._commands.run(setupCommand).catch(() => { - // Command may be cancelled or fail, which is expected - }); + const promise = this._commands.run(setupCommand); this._status.value = 'running'; this._emitEvent('setup'); + + return promise; } dispose(): void { diff --git a/tests/core/game-host.test.ts b/tests/core/game-host.test.ts index 240ea7f..7c85bfd 100644 --- a/tests/core/game-host.test.ts +++ b/tests/core/game-host.test.ts @@ -150,7 +150,7 @@ describe('GameHost', () => { const newPromptPromise = waitForPromptEvent(host); // Reset - should reset state and start new game - await host.setup('setup'); + host.setup('setup'); // State should be back to initial expect(host.context._state.value.currentPlayer).toBe('X'); @@ -173,7 +173,7 @@ describe('GameHost', () => { await promptPromise; // Setup should cancel the active prompt and reset state - await host.setup('setup'); + host.setup('setup'); // The original runPromise should be rejected due to cancellation try { @@ -188,11 +188,11 @@ describe('GameHost', () => { expect(host.context._state.value.turn).toBe(0); }); - it('should throw error when disposed', async () => { + it('should throw error when disposed', () => { const { host } = createTestHost(); host.dispose(); - await expect(host.setup('setup')).rejects.toThrow('GameHost is disposed'); + expect(() => host.setup('setup')).toThrow('GameHost is disposed'); }); }); @@ -245,7 +245,7 @@ describe('GameHost', () => { const promptPromise = waitForPromptEvent(host); // Initial setup via reset - await host.setup('setup'); + host.setup('setup'); expect(setupCount).toBe(1); // State should be running