meat/tests/actors.test.ts

39 lines
1.2 KiB
TypeScript
Raw Normal View History

import { expect, test } from "bun:test"
2026-08-24 15:54:44 +02:00
import { Actor, type Actor as RuntimeActor, type ActorDefinition } from "engine/scene/Actor"
type World = { updates: string[] }
const numberDefinition: ActorDefinition<{ value: number }, World> = {
prototype: { groups: [], radius: 1, minY: 0, maxY: 1 },
update: (state, _dt, world) => {
state.value++
world.updates.push(String(state.value))
},
transform: (state) => ({ x: state.value, y: 0, z: 0, heading: 0, scale: 1 }),
}
const textDefinition: ActorDefinition<{ value: string }, World> = {
prototype: { groups: [], radius: 1, minY: 0, maxY: 1 },
update: (state, _dt, world) => {
state.value += "!"
world.updates.push(state.value)
},
transform: (state) => ({ x: state.value.length, y: 0, z: 0, heading: 0, scale: 1 }),
}
test("one actor collection safely erases unrelated state types", () => {
const actors: RuntimeActor<World>[] = [
Actor.create(numberDefinition, { value: 1 }),
Actor.create(textDefinition, { value: "a" }),
]
const world: World = { updates: [] }
for (const actor of actors) {
Actor.update(actor, 1, world)
}
expect(world.updates).toEqual(["2", "a!"])
expect(Actor.transform(actors[0]).x).toBe(2)
expect(Actor.transform(actors[1]).x).toBe(2)
})