test(03-13): cover direct event body execution

- Exercise eventInsertEvent with an inline Statement[] body
- Prove the body mutates the event layer without an event-store id
- Document the superseding direct-body event contract
This commit is contained in:
unanmed 2026-09-11 13:28:33 +08:00
parent 2f14d8a0ae
commit 7315681272
2 changed files with 24 additions and 15 deletions

View File

@ -118,14 +118,13 @@ interface IInsertEventsEventParam {
### `eventInsertEvent`
```ts
interface IInsertEventEventParam {
readonly id: string;
}
type IInsertEventEventParam = Statement[];
```
这是 `eventInsertEvents` 的单 id 形式:只临时执行 `id` 对应事件,复用当前 `env`
不写入事件存储。空 id、缺失事件、缺失事件存储或缺失执行器时安全返回 `void`
执行 Promise 必须等待。
接收一段 `Statement[]` 事件语句,直接使用现有 AnonTokyo 解释器执行该语句体,
参数为 `{ custom: {} }`,环境为当前 `env`。不读取事件存储、不解析事件 id
也不写入事件存储。空语句体、缺失执行器或达到嵌套插入深度上限时安全返回 `void`
解释器 Promise 必须等待。
## Registration

View File

@ -1,4 +1,5 @@
import { describe, expect, it } from 'vitest';
import { Statement, StatementType } from 'anon-tokyo';
import {
FaceDirection,
IGameEvent,
@ -28,11 +29,9 @@ type RegisteredBuiltin = ReturnType<
typeof createEventBuiltinRegistrations
>[number];
type BuiltinParameter = Parameters<RegisteredBuiltin['func']>[0];
function invokeBuiltin(
function invokeBuiltin<TParam>(
registration: RegisteredBuiltin,
param: BuiltinParameter,
param: TParam,
env: IBlockEventEnv
): Promise<void> {
return Promise.resolve(
@ -199,8 +198,8 @@ describe('event built-ins', () => {
expect(fixture.state.hero.location.x).toBe(0);
});
// 验证真实注册项按顺序等待临时事件序列和单事件
it('awaits temporary event sequences and single event insertion', async () => {
// 验证真实注册项按顺序等待临时事件序列并直接执行语句体
it('awaits id sequences and executes a direct statement body', async () => {
const fixture = createFixture();
const calls: string[] = [];
fixture.state.eventStore.addEvent(
@ -221,12 +220,23 @@ describe('event built-ins', () => {
{ ids: ['first', 'second', 'missing'] },
fixture.env
);
const body: Statement[] = [
{
type: StatementType.Call,
functionName: EventBuiltinName.SetBlock,
builtIn: true,
async: true,
parameters: { x: 3, y: 0, tile: 2 }
}
];
await invokeBuiltin(
getRegistration(EventBuiltinName.InsertEvent),
{ id: 'first' },
body,
fixture.env
);
expect(calls).toEqual(['first', 'second', 'first']);
expect(calls).toEqual(['first', 'second']);
expect(fixture.layer.getBlock(3, 0)).toBe(2);
expect(fixture.state.eventStore.getEvent('inline-body')).toBeNull();
});
// 验证默认注册项只包含批准的八个稳定名称
@ -288,7 +298,7 @@ describe('event built-ins', () => {
await expect(
invokeBuiltin(
getRegistration(EventBuiltinName.InsertEvent),
{ id: 'missing' },
[],
fixture.env
)
).resolves.toBeUndefined();