refactor: fix reactivity

This commit is contained in:
2026-04-01 22:57:33 +08:00
parent 4761806a02
commit dbd2a25185
4 changed files with 102 additions and 39 deletions
+25 -25
View File
@@ -1,5 +1,5 @@
import { describe, it, expect } from 'vitest';
import { createRule, dispatchCommand, type RuleContext, type RuleRegistry } from '../../src/core/rule';
import { createRule, type RuleContext } from '../../src/core/rule';
import { createGameContext } from '../../src/core/context';
describe('Rule System', () => {
@@ -39,7 +39,7 @@ describe('Rule System', () => {
it('should invoke a registered rule and yield schema', () => {
const game = createTestGame();
game.rules.value.set('move', createRule('<from> <to>', function*(cmd) {
game.registerRule('move', createRule('<from> <to>', function*(cmd) {
yield { name: '', params: [], options: [], flags: [] };
return { moved: cmd.params[0] };
}));
@@ -55,7 +55,7 @@ describe('Rule System', () => {
it('should complete a rule when final command matches yielded schema', () => {
const game = createTestGame();
game.rules.value.set('move', createRule('<from> <to>', function*(cmd) {
game.registerRule('move', createRule('<from> <to>', function*(cmd) {
const confirm = yield { name: '', params: [], options: [], flags: [] };
return { moved: cmd.params[0], confirmed: confirm.name === 'confirm' };
}));
@@ -79,7 +79,7 @@ describe('Rule System', () => {
it('should pass the initial command to the generator', () => {
const game = createTestGame();
game.rules.value.set('attack', createRule('<target> [--power: number]', function*(cmd) {
game.registerRule('attack', createRule('<target> [--power: number]', function*(cmd) {
return { target: cmd.params[0], power: cmd.options.power || '1' };
}));
@@ -92,7 +92,7 @@ describe('Rule System', () => {
it('should complete immediately if generator does not yield', () => {
const game = createTestGame();
game.rules.value.set('look', createRule('[--at]', function*() {
game.registerRule('look', createRule('[--at]', function*() {
return 'looked';
}));
@@ -107,12 +107,12 @@ describe('Rule System', () => {
it('should prioritize new rule invocation over feeding yielded context', () => {
const game = createTestGame();
game.rules.value.set('move', createRule('<from> <to>', function*(cmd) {
game.registerRule('move', createRule('<from> <to>', function*(cmd) {
yield { name: '', params: [], options: [], flags: [] };
return { moved: cmd.params[0] };
}));
game.rules.value.set('confirm', createRule('', function*() {
game.registerRule('confirm', createRule('', function*() {
return 'new confirm rule';
}));
@@ -130,7 +130,7 @@ describe('Rule System', () => {
it('should feed a yielded context when command does not match any rule', () => {
const game = createTestGame();
game.rules.value.set('move', createRule('<from> <to>', function*(cmd) {
game.registerRule('move', createRule('<from> <to>', function*(cmd) {
const response = yield { name: '', params: [], options: [], flags: [] };
return { moved: cmd.params[0], response: response.name };
}));
@@ -145,7 +145,7 @@ describe('Rule System', () => {
it('should skip non-matching commands for yielded context', () => {
const game = createTestGame();
game.rules.value.set('move', createRule('<from> <to>', function*(cmd) {
game.registerRule('move', createRule('<from> <to>', function*(cmd) {
const response = yield '<item>';
return { response: response.params[0] };
}));
@@ -160,7 +160,7 @@ describe('Rule System', () => {
it('should validate command against yielded schema', () => {
const game = createTestGame();
game.rules.value.set('trade', createRule('<from> <to>', function*(cmd) {
game.registerRule('trade', createRule('<from> <to>', function*(cmd) {
const response = yield '<item> [amount: number]';
return { traded: response.params[0] };
}));
@@ -177,12 +177,12 @@ describe('Rule System', () => {
it('should feed the deepest yielded context', () => {
const game = createTestGame();
game.rules.value.set('parent', createRule('<action>', function*() {
game.registerRule('parent', createRule('<action>', function*() {
yield { name: '', params: [], options: [], flags: [] };
return 'parent done';
}));
game.rules.value.set('child', createRule('<target>', function*() {
game.registerRule('child', createRule('<target>', function*() {
yield { name: '', params: [], options: [], flags: [] };
return 'child done';
}));
@@ -201,12 +201,12 @@ describe('Rule System', () => {
it('should link child to parent', () => {
const game = createTestGame();
game.rules.value.set('parent', createRule('<action>', function*() {
game.registerRule('parent', createRule('<action>', function*() {
yield 'child_cmd';
return 'parent done';
}));
game.rules.value.set('child_cmd', createRule('<target>', function*() {
game.registerRule('child_cmd', createRule('<target>', function*() {
return 'child done';
}));
@@ -225,16 +225,16 @@ describe('Rule System', () => {
it('should discard previous children when a new child is invoked', () => {
const game = createTestGame();
game.rules.value.set('parent', createRule('<action>', function*() {
game.registerRule('parent', createRule('<action>', function*() {
yield 'child_a | child_b';
return 'parent done';
}));
game.rules.value.set('child_a', createRule('<target>', function*() {
game.registerRule('child_a', createRule('<target>', function*() {
return 'child_a done';
}));
game.rules.value.set('child_b', createRule('<target>', function*() {
game.registerRule('child_b', createRule('<target>', function*() {
return 'child_b done';
}));
@@ -259,7 +259,7 @@ describe('Rule System', () => {
it('should track rule contexts in ruleContexts signal', () => {
const game = createTestGame();
game.rules.value.set('test', createRule('<arg>', function*() {
game.registerRule('test', createRule('<arg>', function*() {
yield { name: '', params: [], options: [], flags: [] };
return 'done';
}));
@@ -275,7 +275,7 @@ describe('Rule System', () => {
it('should add context to the context stack', () => {
const game = createTestGame();
game.rules.value.set('test', createRule('<arg>', function*() {
game.registerRule('test', createRule('<arg>', function*() {
yield { name: '', params: [], options: [], flags: [] };
return 'done';
}));
@@ -292,7 +292,7 @@ describe('Rule System', () => {
it('should leave context in place when generator throws', () => {
const game = createTestGame();
game.rules.value.set('failing', createRule('<arg>', function*() {
game.registerRule('failing', createRule('<arg>', function*() {
throw new Error('rule error');
}));
@@ -304,12 +304,12 @@ describe('Rule System', () => {
it('should leave children in place when child generator throws', () => {
const game = createTestGame();
game.rules.value.set('parent', createRule('<action>', function*() {
game.registerRule('parent', createRule('<action>', function*() {
yield 'child';
return 'parent done';
}));
game.rules.value.set('child', createRule('<target>', function*() {
game.registerRule('child', createRule('<target>', function*() {
throw new Error('child error');
}));
@@ -331,7 +331,7 @@ describe('Rule System', () => {
flags: [],
};
game.rules.value.set('test', createRule('<arg>', function*() {
game.registerRule('test', createRule('<arg>', function*() {
const cmd = yield customSchema;
return { received: cmd.params[0] };
}));
@@ -346,7 +346,7 @@ describe('Rule System', () => {
it('should parse string schema on each yield', () => {
const game = createTestGame();
game.rules.value.set('multi', createRule('<start>', function*() {
game.registerRule('multi', createRule('<start>', function*() {
const a = yield '<value>';
const b = yield '<value>';
return { a: a.params[0], b: b.params[0] };
@@ -365,7 +365,7 @@ describe('Rule System', () => {
it('should handle a multi-step game flow', () => {
const game = createTestGame();
game.rules.value.set('start', createRule('<player>', function*(cmd) {
game.registerRule('start', createRule('<player>', function*(cmd) {
const player = cmd.params[0];
const action = yield { name: '', params: [], options: [], flags: [] };