|
| 1 | +import type EntityState from '../../src/World/Entity/State' |
| 2 | +import { bench, describe } from 'vitest' |
| 3 | +import NetworkedActor from '../../src/Networking/NetworkedActor' |
| 4 | +import NetworkedEntityState from '../../src/Networking/NetworkedEntityState' |
| 5 | +import { syncStateStack } from '../../src/Networking/syncState' |
| 6 | + |
| 7 | +class TestActor extends NetworkedActor { |
| 8 | + $typeName = 'testActor' |
| 9 | + updateFromNetwork = (_data: object) => {} |
| 10 | + update(_delta: number): void {} |
| 11 | +} |
| 12 | + |
| 13 | +class StateA extends NetworkedEntityState { |
| 14 | + readonly stateName = 'stateA' |
| 15 | + update(_delta: number): EntityState | void {} |
| 16 | +} |
| 17 | + |
| 18 | +class StateB extends NetworkedEntityState { |
| 19 | + readonly stateName = 'stateB' |
| 20 | + update(_delta: number): EntityState | void {} |
| 21 | +} |
| 22 | + |
| 23 | +const factories = { |
| 24 | + stateA: (entity: TestActor) => new StateA(entity), |
| 25 | + stateB: (entity: TestActor) => new StateB(entity), |
| 26 | +} |
| 27 | + |
| 28 | +describe('syncStateStack', () => { |
| 29 | + bench('no-op (states already match)', () => { |
| 30 | + const actor = new TestActor() |
| 31 | + actor.state.push(new StateA(actor)) |
| 32 | + syncStateStack(actor, [{ stateName: 'stateA' }], factories) |
| 33 | + }) |
| 34 | + |
| 35 | + bench('full replacement (stateB → stateA)', () => { |
| 36 | + const actor = new TestActor() |
| 37 | + actor.state.push(new StateB(actor)) |
| 38 | + syncStateStack(actor, [{ stateName: 'stateA' }], factories) |
| 39 | + }) |
| 40 | + |
| 41 | + bench('add to stack (1 → 2 states)', () => { |
| 42 | + const actor = new TestActor() |
| 43 | + actor.state.push(new StateA(actor)) |
| 44 | + syncStateStack(actor, [{ stateName: 'stateA' }, { stateName: 'stateB' }], factories) |
| 45 | + }) |
| 46 | + |
| 47 | + bench('remove from stack (2 → 1 states)', () => { |
| 48 | + const actor = new TestActor() |
| 49 | + actor.state.push(new StateA(actor)) |
| 50 | + actor.state.push(new StateB(actor)) |
| 51 | + syncStateStack(actor, [{ stateName: 'stateA' }], factories) |
| 52 | + }) |
| 53 | + |
| 54 | + bench('clear all states (2 → 0)', () => { |
| 55 | + const actor = new TestActor() |
| 56 | + actor.state.push(new StateA(actor)) |
| 57 | + actor.state.push(new StateB(actor)) |
| 58 | + syncStateStack(actor, [], factories) |
| 59 | + }) |
| 60 | +}) |
0 commit comments