Skip to content

A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.

TypeScript fluxerfluxerapp

test(voice-engine): remove event log replay and seam tests

Hampus47818d591d02
diff --git a/packages/voice_engine_v2/src/fuzz/ReducerFuzzer.ts b/packages/voice_engine_v2/src/fuzz/ReducerFuzzer.ts
index d773caf9a..71c746e09 100644
--- a/packages/voice_engine_v2/src/fuzz/ReducerFuzzer.ts
+++ b/packages/voice_engine_v2/src/fuzz/ReducerFuzzer.ts
@@ -11,7 +11,6 @@ import type {
import {createVoiceEngineV2MemoryEventLogSpillSink} from '@fluxer/voice_engine_v2/src/runtime/eventLogRing';
import {createVoiceEngineV2DeterministicClockPort} from '@fluxer/voice_engine_v2/src/runtime/platformPort';
import {assertEventLogInvariants, VoiceEngineV2Runtime} from '@fluxer/voice_engine_v2/src/runtime/VoiceEngineV2Runtime';
-import {hashSnapshot} from '@fluxer/voice_engine_v2/src/simulation/Simulator';
import {
type SimulatorDriverFaultPolicy,
VoiceEngineV2SimulatorDriver,
@@ -28,26 +27,21 @@ export const FUZZ_REDUCER_ARBITRARY_ITERATIONS = 200;
const FUZZ_REDUCER_FLUSH_MICROTASKS_MAX = 64;
-export const FUZZ_REDUCER_EXTERNAL_CONNECTION_ITERATIONS = 512;
-
-const FUZZ_EXTERNAL_CONNECTION_WEIGHT = 6;
-
interface ReducerFuzzerFailure {
seed: number;
iteration: number;
- mode: 'positive' | 'negative' | 'qualitative' | 'arbitraryOrder' | 'externalConnection';
+ mode: 'positive' | 'negative' | 'qualitative' | 'arbitraryOrder';
reason: string;
event: VoiceEngineV2Event | null;
}
interface ReducerFuzzerReport {
seed: number;
- mode: 'positive' | 'negative' | 'qualitative' | 'arbitraryOrder' | 'externalConnection';
+ mode: 'positive' | 'negative' | 'qualitative' | 'arbitraryOrder';
iterations: number;
dispatched: number;
rejected: number;
failures: ReadonlyArray<ReducerFuzzerFailure>;
- snapshotHash?: string;
}
const POSITIVE_EVENT_KINDS: ReadonlyArray<VoiceEngineV2Event['type']> = [
@@ -84,7 +78,7 @@ const POSITIVE_EVENT_KINDS: ReadonlyArray<VoiceEngineV2Event['type']> = [
'gateway.voiceStateUpdated',
];
-export const EXTERNAL_CONNECTION_EVENT_KINDS: ReadonlyArray<VoiceEngineV2Event['type']> = [
+const EXTERNAL_CONNECTION_EVENT_KINDS: ReadonlyArray<VoiceEngineV2Event['type']> = [
'connection.externallyEstablished',
'connection.remoteDisconnected',
];
@@ -94,25 +88,6 @@ const ARBITRARY_EVENT_KINDS: ReadonlyArray<VoiceEngineV2Event['type']> = [
...EXTERNAL_CONNECTION_EVENT_KINDS,
];
-const EXTERNAL_CONNECTION_CYCLE_KIND_POOL: ReadonlyArray<VoiceEngineV2Event['type']> = buildWeightedKindPool();
-
-function buildWeightedKindPool(): ReadonlyArray<VoiceEngineV2Event['type']> {
- assert.ok(FUZZ_EXTERNAL_CONNECTION_WEIGHT >= 1, 'external connection weight must be >= 1');
- assert.ok(FUZZ_EXTERNAL_CONNECTION_WEIGHT <= 16, 'external connection weight must stay bounded');
- const pool: Array<VoiceEngineV2Event['type']> = [...POSITIVE_EVENT_KINDS];
- for (let repeat = 0; repeat < FUZZ_EXTERNAL_CONNECTION_WEIGHT; repeat += 1) {
- for (const kind of EXTERNAL_CONNECTION_EVENT_KINDS) {
- pool.push(kind);
- }
- }
- assert.equal(
- pool.length,
- POSITIVE_EVENT_KINDS.length + EXTERNAL_CONNECTION_EVENT_KINDS.length * FUZZ_EXTERNAL_CONNECTION_WEIGHT,
- 'weighted kind pool must contain every positive kind plus the weighted external kinds',
- );
- return pool;
-}
-
const DISCONNECT_REASONS: ReadonlyArray<VoiceEngineV2DisconnectReason> = [
'user',
'server',
@@ -233,31 +208,6 @@ export class ReducerFuzzer {
runtime.dispose();
return {seed: this.seed, mode: 'arbitraryOrder', iterations, dispatched, rejected, failures};
}
-
- fuzzExternalConnectionCycles(iterations: number = FUZZ_REDUCER_EXTERNAL_CONNECTION_ITERATIONS): ReducerFuzzerReport {
- assert.ok(Number.isInteger(iterations), 'iterations must be an integer');
- assert.ok(iterations >= 1, 'iterations must be >= 1');
- assert.ok(iterations <= FUZZ_ITERATIONS_MAX, 'iterations must respect FUZZ_ITERATIONS_MAX');
- const prng = new FuzzPrng({seed: this.seed});
- const runtime = freshRuntime();
- const failures: Array<ReducerFuzzerFailure> = [];
- let dispatched = 0;
- let rejected = 0;
- for (let iteration = 0; iteration < iterations; iteration += 1) {
- const event = generateExternalConnectionCycleEvent(prng);
- const failure = tryDispatch(runtime, event, this.seed, iteration, 'externalConnection');
- if (failure !== null) {
- rejected += 1;
- continue;
- }
- dispatched += 1;
- const guardFailure = guardRuntimeInvariants(runtime, this.seed, iteration, 'externalConnection', event);
- if (guardFailure !== null) failures.push(guardFailure);
- }
- const snapshotHash = hashSnapshot(runtime.snapshot);
- runtime.dispose();
- return {seed: this.seed, mode: 'externalConnection', iterations, dispatched, rejected, failures, snapshotHash};
- }
}
function freshRuntime(): VoiceEngineV2Runtime {
@@ -314,12 +264,6 @@ function generateArbitraryEvent(prng: FuzzPrng): VoiceEngineV2Event {
return materialiseEvent(kind, prng, distort);
}
-function generateExternalConnectionCycleEvent(prng: FuzzPrng): VoiceEngineV2Event {
- assert.ok(EXTERNAL_CONNECTION_CYCLE_KIND_POOL.length >= 1, 'external connection kind pool must not be empty');
- const kind = prng.nextChoice(EXTERNAL_CONNECTION_CYCLE_KIND_POOL);
- return materialiseEvent(kind, prng, false);
-}
-
interface MaterialiseContext {
operationId: number;
identity: string;
diff --git a/packages/voice_engine_v2/src/fuzz/externalConnection.test.ts b/packages/voice_engine_v2/src/fuzz/externalConnection.test.ts
deleted file mode 100644
index 5bfb7e900..000000000
--- a/packages/voice_engine_v2/src/fuzz/externalConnection.test.ts
+++ /dev/null
@@ -1,35 +0,0 @@
-// SPDX-License-Identifier: AGPL-3.0-or-later
-
-import {
- FUZZ_REDUCER_ARBITRARY_ITERATIONS,
- FUZZ_REDUCER_EXTERNAL_CONNECTION_ITERATIONS,
- ReducerFuzzer,
-} from '@fluxer/voice_engine_v2/src/fuzz/ReducerFuzzer';
-import {describe, expect, it} from 'vitest';
-
-const SEEDS: ReadonlyArray<number> = [1, 2, 7, 13, 99];
-
-describe('ReducerFuzzer.fuzzExternalConnectionCycles', () => {
- it('survives weighted establish/disconnect cycles without invariant failures', () => {
- for (const seed of SEEDS) {
- const fuzzer = new ReducerFuzzer(seed);
- const report = fuzzer.fuzzExternalConnectionCycles(FUZZ_REDUCER_EXTERNAL_CONNECTION_ITERATIONS);
- expect(report.mode).toBe('externalConnection');
- expect(report.iterations).toBe(FUZZ_REDUCER_EXTERNAL_CONNECTION_ITERATIONS);
- expect(report.failures).toEqual([]);
- expect(report.dispatched + report.rejected).toBe(FUZZ_REDUCER_EXTERNAL_CONNECTION_ITERATIONS);
- expect(report.dispatched).toBeGreaterThan(0);
- }
- });
-});
-
-describe('ReducerFuzzer arbitrary-order generation with external connection events', () => {
- it('keeps the arbitrary-order mode crash-free now that external events are in the pool', () => {
- for (const seed of SEEDS) {
- const fuzzer = new ReducerFuzzer(seed);
- const report = fuzzer.fuzzArbitraryOrder(FUZZ_REDUCER_ARBITRARY_ITERATIONS);
- expect(report.failures).toEqual([]);
- expect(report.dispatched + report.rejected).toBe(FUZZ_REDUCER_ARBITRARY_ITERATIONS);
- }
- });
-});
diff --git a/packages/voice_engine_v2/src/runtime/frameCoalescing.test.ts b/packages/voice_engine_v2/src/runtime/frameCoalescing.test.ts
deleted file mode 100644
index 3abeb14ba..000000000
--- a/packages/voice_engine_v2/src/runtime/frameCoalescing.test.ts
+++ /dev/null
@@ -1,176 +0,0 @@
-// SPDX-License-Identifier: AGPL-3.0-or-later
-
-import type {VoiceEngineV2Event} from '@fluxer/voice_engine_v2/src/protocol/events';
-import {createVoiceEngineV2MemoryEventLogSpillSink} from '@fluxer/voice_engine_v2/src/runtime/eventLogRing';
-import {
- coalesceVoiceEngineV2EventSequence,
- VOICE_ENGINE_V2_COALESCED_TRACKS_CAP,
-} from '@fluxer/voice_engine_v2/src/runtime/frameCoalescing';
-import {VoiceEngineV2Runtime} from '@fluxer/voice_engine_v2/src/runtime/VoiceEngineV2Runtime';
-import {FakeVoiceEngineV2Driver} from '@fluxer/voice_engine_v2/src/testing/FakeVoiceEngineV2Driver';
-import {VoiceEngineV2TestImplementation} from '@fluxer/voice_engine_v2/src/testing/VoiceEngineV2TestImplementation';
-import {describe, expect, it} from 'vitest';
-
-function makeRuntime(options?: {eventLogCap?: number}): VoiceEngineV2Runtime {
- const driver = new FakeVoiceEngineV2Driver();
- return new VoiceEngineV2Runtime(new VoiceEngineV2TestImplementation(driver), {
- eventLogCap: options?.eventLogCap,
- eventLogSpillSink: createVoiceEngineV2MemoryEventLogSpillSink(),
- clock: {now: () => 0},
- verifyEventLogInvariantsOnDispatch: true,
- });
-}
-
-function subscribeEvent(trackSid: string): VoiceEngineV2Event {
- return {
- type: 'inboundVideo.trackSubscribed',
- track: {
- participantSid: 'PA_alice',
- participantIdentity: 'alice',
- trackSid,
- source: 'camera',
- },
- };
-}
-
-function frameEvent(trackSid: string, timestampUs: number): VoiceEngineV2Event {
- return {
- type: 'inboundVideo.frameReceived',
- frame: {
- participantSid: 'PA_alice',
- participantIdentity: 'alice',
- trackSid,
- width: 1280,
- height: 720,
- timestampUs,
- byteLength: 345_600,
- },
- };
-}
-
-describe('voice engine v2 frame-received coalescing', () => {
- it('collapses consecutive same-track frames into the retained tail entry, latest wins', () => {
- const runtime = makeRuntime();
- runtime.dispatch(subscribeEvent('TR_1'));
- runtime.dispatch(frameEvent('TR_1', 1000));
- runtime.dispatch(frameEvent('TR_1', 2000));
- runtime.dispatch(frameEvent('TR_1', 3000));
-
- expect(runtime.eventLog).toHaveLength(2);
- const tail = runtime.eventLog[1];
- if (tail === undefined) throw new Error('expected a retained tail entry');
- expect(tail.event.type).toBe('inboundVideo.frameReceived');
- if (tail.event.type !== 'inboundVideo.frameReceived') throw new Error('expected a frame event');
- expect(tail.event.frame.timestampUs).toBe(3000);
- expect(runtime.coalescedEventsCount).toBe(2);
-
- const track = runtime.snapshot.inboundVideo.tracks.TR_1;
- expect(track?.lastFrameTimestampUs).toBe(3000);
- expect(track?.frameCount).toBe(1);
- });
-
- it('keeps the event log bounded to one tail entry under a sustained same-track burst', () => {
- const runtime = makeRuntime({eventLogCap: 8});
- runtime.dispatch(subscribeEvent('TR_1'));
- const burst = 100;
- for (let index = 1; index <= burst; index += 1) {
- runtime.dispatch(frameEvent('TR_1', index * 1000));
- }
-
- expect(runtime.eventLog).toHaveLength(2);
- expect(runtime.droppedEventsCount).toBe(0);
- expect(runtime.coalescedEventsCount).toBe(burst - 1);
- expect(runtime.snapshot.inboundVideo.tracks.TR_1?.lastFrameTimestampUs).toBe(burst * 1000);
- });
-
- it('does not coalesce frames across interleaved tracks', () => {
- const runtime = makeRuntime();
- runtime.dispatch(subscribeEvent('TR_a'));
- runtime.dispatch(subscribeEvent('TR_b'));
- runtime.dispatch(frameEvent('TR_a', 1000));
- runtime.dispatch(frameEvent('TR_b', 1100));
- runtime.dispatch(frameEvent('TR_a', 2000));
- runtime.dispatch(frameEvent('TR_b', 2100));
-
- expect(runtime.eventLog).toHaveLength(6);
- expect(runtime.coalescedEventsCount).toBe(0);
- expect(runtime.snapshot.inboundVideo.tracks.TR_a?.frameCount).toBe(2);
- expect(runtime.snapshot.inboundVideo.tracks.TR_b?.frameCount).toBe(2);
- });
-
- it('does not coalesce across an intervening non-frame event', () => {
- const runtime = makeRuntime();
- runtime.dispatch(subscribeEvent('TR_1'));
- runtime.dispatch(frameEvent('TR_1', 1000));
- runtime.dispatch(subscribeEvent('TR_2'));
- runtime.dispatch(frameEvent('TR_1', 2000));
-
- expect(runtime.eventLog).toHaveLength(4);
- expect(runtime.coalescedEventsCount).toBe(0);
- expect(runtime.snapshot.inboundVideo.tracks.TR_1?.frameCount).toBe(2);
- });
-
- it('coalesces frames for unknown tracks so the drop counter reflects retained events only', () => {
- const runtime = makeRuntime();
- runtime.dispatch(frameEvent('TR_unknown', 1000));
- runtime.dispatch(frameEvent('TR_unknown', 2000));
- runtime.dispatch(frameEvent('TR_unknown', 3000));
-
- expect(runtime.eventLog).toHaveLength(1);
- expect(runtime.coalescedEventsCount).toBe(2);
- expect(runtime.snapshot.inboundVideo.droppedFrameCount).toBe(1);
- });
-
- it('matches the pure event-sequence coalescing function for a mixed sequence', () => {
- const rawEvents: Array<VoiceEngineV2Event> = [
- subscribeEvent('TR_1'),
- frameEvent('TR_1', 1000),
- frameEvent('TR_1', 2000),
- subscribeEvent('TR_2'),
- frameEvent('TR_2', 2100),
- frameEvent('TR_2', 2200),
- frameEvent('TR_1', 3000),
- frameEvent('TR_1', 4000),
- ];
- const runtime = makeRuntime();
- for (const event of rawEvents) {
- runtime.dispatch(event);
- }
-
- const retainedEvents = runtime.eventLog.map((entry) => entry.event);
- expect(retainedEvents).toEqual(coalesceVoiceEngineV2EventSequence(rawEvents));
- expect(runtime.coalescedEventsCount).toBe(rawEvents.length - retainedEvents.length);
- });
-
- it('bounds per-track bookkeeping at the named cap', () => {
- const runtime = makeRuntime();
- const total = VOICE_ENGINE_V2_COALESCED_TRACKS_CAP + 8;
- for (let index = 0; index < total; index += 1) {
- runtime.dispatch(frameEvent(`TR_${index}`, (index + 1) * 1000));
- }
-
- expect(runtime.coalesceTrackedTracksCount).toBe(VOICE_ENGINE_V2_COALESCED_TRACKS_CAP);
- expect(runtime.eventLog.length).toBeLessThanOrEqual(runtime.eventLogCap);
- });
-
- it('is deterministic across two runtimes fed the same event sequence', () => {
- const rawEvents: Array<VoiceEngineV2Event> = [
- subscribeEvent('TR_1'),
- frameEvent('TR_1', 1000),
- frameEvent('TR_1', 2000),
- frameEvent('TR_1', 3000),
- subscribeEvent('TR_2'),
- frameEvent('TR_2', 3100),
- ];
- const first = makeRuntime();
- const second = makeRuntime();
- for (const event of rawEvents) {
- first.dispatch(event);
- second.dispatch(event);
- }
-
- expect(second.eventLog.map((entry) => entry.event)).toEqual(first.eventLog.map((entry) => entry.event));
- expect(second.snapshot).toEqual(first.snapshot);
- expect(second.coalescedEventsCount).toBe(first.coalescedEventsCount);
- });
-});
diff --git a/packages/voice_engine_v2/src/runtime/platformPort.test.ts b/packages/voice_engine_v2/src/runtime/platformPort.test.ts
deleted file mode 100644
index 90bfcbab3..000000000
--- a/packages/voice_engine_v2/src/runtime/platformPort.test.ts
+++ /dev/null
@@ -1,76 +0,0 @@
-// SPDX-License-Identifier: AGPL-3.0-or-later
-
-import {createVoiceEngineV2SystemClockPort} from '@fluxer/voice_engine_v2/src/runtime/platformPort';
-import {afterEach, describe, expect, it, vi} from 'vitest';
-
-afterEach(() => {
- vi.restoreAllMocks();
-});
-
-describe('voice engine v2 platform port source failures', () => {
- it('keeps the clock non-throwing, counts failures, and logs exactly once', () => {
- const consoleError = vi.spyOn(console, 'error').mockImplementation(() => {});
- const clock = createVoiceEngineV2SystemClockPort({
- read(): number {
- throw new Error('clock source boom');
- },
- });
-
- expect(clock.now()).toBe(0);
- expect(clock.now()).toBe(0);
- expect(clock.now()).toBe(0);
-
- expect(clock.sourceFailureCount).toBe(3);
- expect(consoleError).toHaveBeenCalledTimes(1);
- });
-
- it('pins a failing clock to the last safe value instead of regressing', () => {
- vi.spyOn(console, 'error').mockImplementation(() => {});
- let healthy = true;
- const clock = createVoiceEngineV2SystemClockPort({
- read(): number {
- if (!healthy) throw new Error('clock source boom');
- return 1_000;
- },
- });
-
- expect(clock.now()).toBe(1_000);
- healthy = false;
- expect(clock.now()).toBe(1_000);
- expect(clock.sourceFailureCount).toBe(1);
- });
-
- it('reports zero failures and stays silent for healthy sources', () => {
- const consoleError = vi.spyOn(console, 'error').mockImplementation(() => {});
- const clock = createVoiceEngineV2SystemClockPort({
- read(): number {
- return 42;
- },
- });
-
- expect(clock.now()).toBe(42);
- expect(clock.sourceFailureCount).toBe(0);
- expect(consoleError).not.toHaveBeenCalled();
- });
-
- it('latches failure logging per port instance, not globally', () => {
- const consoleError = vi.spyOn(console, 'error').mockImplementation(() => {});
- const first = createVoiceEngineV2SystemClockPort({
- read(): number {
- throw new Error('first boom');
- },
- });
- const second = createVoiceEngineV2SystemClockPort({
- read(): number {
- throw new Error('second boom');
- },
- });
-
- first.now();
- second.now();
-
- expect(first.sourceFailureCount).toBe(1);
- expect(second.sourceFailureCount).toBe(1);
- expect(consoleError).toHaveBeenCalledTimes(2);
- });
-});
diff --git a/packages/voice_engine_v2/src/testing/eventLogReplay.test.ts b/packages/voice_engine_v2/src/testing/eventLogReplay.test.ts
deleted file mode 100644
index 33b7d8500..000000000
--- a/packages/voice_engine_v2/src/testing/eventLogReplay.test.ts
+++ /dev/null
@@ -1,141 +0,0 @@
-// SPDX-License-Identifier: AGPL-3.0-or-later
-
-import appVoiceSessionFixtureJson from '@fluxer/voice_engine_v2/fixtures/event_logs/app_voice_session.json';
-import type {VoiceEngineV2Event} from '@fluxer/voice_engine_v2/src/protocol/events';
-import {createVoiceEngineV2MemoryEventLogSpillSink} from '@fluxer/voice_engine_v2/src/runtime/eventLogRing';
-import {coalesceVoiceEngineV2EventSequence} from '@fluxer/voice_engine_v2/src/runtime/frameCoalescing';
-import {VoiceEngineV2Runtime} from '@fluxer/voice_engine_v2/src/runtime/VoiceEngineV2Runtime';
-import {
- replayVoiceEngineV2EventLogFixture,
- VOICE_ENGINE_V2_EVENT_LOG_FIXTURE_VERSION,
- type VoiceEngineV2EventLogFixture,
-} from '@fluxer/voice_engine_v2/src/testing/eventLogReplay';
-import {FakeVoiceEngineV2Driver} from '@fluxer/voice_engine_v2/src/testing/FakeVoiceEngineV2Driver';
-import {VoiceEngineV2TestImplementation} from '@fluxer/voice_engine_v2/src/testing/VoiceEngineV2TestImplementation';
-import {describe, expect, it} from 'vitest';
-
-const appVoiceSessionFixture = appVoiceSessionFixtureJson as unknown as VoiceEngineV2EventLogFixture;
-
-function makeCoalescingRuntime(): VoiceEngineV2Runtime {
- return new VoiceEngineV2Runtime(new VoiceEngineV2TestImplementation(new FakeVoiceEngineV2Driver()), {
- eventLogSpillSink: createVoiceEngineV2MemoryEventLogSpillSink(),
- clock: {now: () => 0},
- verifyEventLogInvariantsOnDispatch: true,
- });
-}
-
-function inboundVideoBurstEvents(): Array<VoiceEngineV2Event> {
- const events: Array<VoiceEngineV2Event> = [
- {
- type: 'inboundVideo.trackSubscribed',
- track: {participantSid: 'PA_alice', participantIdentity: 'alice', trackSid: 'TR_screen', source: 'screen'},
- },
- ];
- const burst = 12;
- for (let index = 1; index <= burst; index += 1) {
- events.push({
- type: 'inboundVideo.frameReceived',
- frame: {
- participantSid: 'PA_alice',
- participantIdentity: 'alice',
- trackSid: 'TR_screen',
- width: 1280,
- height: 720,
- timestampUs: index * 1000,
- byteLength: 1_382_400,
- },
- });
- }
- events.push({
- type: 'inboundVideo.trackSubscribed',
- track: {participantSid: 'PA_bob', participantIdentity: 'bob', trackSid: 'TR_camera', source: 'camera'},
- });
- events.push({
- type: 'inboundVideo.frameReceived',
- frame: {
- participantSid: 'PA_bob',
- participantIdentity: 'bob',
- trackSid: 'TR_camera',
- width: 640,
- height: 360,
- timestampUs: 99_000,
- byteLength: 345_600,
- },
- });
- return events;
-}
-
-describe('voice engine v2 event-log fixture replay', () => {
- it('replays command batches and final immutable snapshot goldens', () => {
- const replay = replayVoiceEngineV2EventLogFixture(appVoiceSessionFixture);
-
- expect(replay.fixtureName).toBe('app_voice_session');
- expect(replay.commandBatches).toEqual(appVoiceSessionFixture.steps.map((step) => step.commands));
- expect(replay.finalSnapshot).toEqual(appVoiceSessionFixture.expected.finalSnapshot);
- expect(replay.finalModel).toEqual(appVoiceSessionFixture.expected.finalModel);
- });
-
- it('is deterministic across repeated replays', () => {
- const first = replayVoiceEngineV2EventLogFixture(appVoiceSessionFixture);
- const second = replayVoiceEngineV2EventLogFixture(appVoiceSessionFixture);
-
- expect(second.commandBatches).toEqual(first.commandBatches);
- expect(second.finalSnapshot).toEqual(first.finalSnapshot);
- expect(second.finalModel).toEqual(first.finalModel);
- });
-
- it('preserves previous snapshots when later events transition state', () => {
- const replay = replayVoiceEngineV2EventLogFixture(appVoiceSessionFixture);
- const connectStep = replay.steps.find((step) => step.name === 'request voice connection');
- const connectedStep = replay.steps.find(
- (step) => step.name === 'voice connection succeeds and queued media publishes',
- );
-
- expect(connectStep?.previousSnapshot.connection.status).toBe('idle');
- expect(connectStep?.snapshot.connection.status).toBe('connecting');
- expect(connectStep?.previousSnapshot.microphone.status).toBe('idle');
- expect(connectStep?.snapshot.microphone.status).toBe('idle');
-
- expect(connectedStep?.previousSnapshot.connection.status).toBe('connecting');
- expect(connectedStep?.previousSnapshot.microphone.status).toBe('idle');
- expect(connectedStep?.snapshot.connection.status).toBe('connected');
- expect(connectedStep?.snapshot.microphone.status).toBe('publishing');
- });
-
- it('replays a runtime-coalesced frame log to the identical snapshot', () => {
- const rawEvents = inboundVideoBurstEvents();
- const runtime = makeCoalescingRuntime();
- for (const event of rawEvents) {
- runtime.dispatch(event);
- }
-
- expect(runtime.coalescedEventsCount).toBe(11);
- const retainedEvents = runtime.eventLog.map((entry) => entry.event);
- expect(retainedEvents).toEqual(coalesceVoiceEngineV2EventSequence(rawEvents));
-
- const fixture: VoiceEngineV2EventLogFixture = {
- version: VOICE_ENGINE_V2_EVENT_LOG_FIXTURE_VERSION,
- name: 'coalesced_inbound_video_burst',
- steps: runtime.eventLog.map((entry) => ({event: entry.event, commands: entry.commands})),
- expected: {finalSnapshot: runtime.snapshot},
- };
- const replay = replayVoiceEngineV2EventLogFixture(fixture);
-
- expect(replay.finalSnapshot).toEqual(runtime.snapshot);
- expect(replay.finalSnapshot.inboundVideo.tracks.TR_screen?.lastFrameTimestampUs).toBe(12_000);
- expect(replay.finalSnapshot.inboundVideo.tracks.TR_screen?.frameCount).toBe(1);
- });
-
- it('replays the same coalesced log deterministically across runtimes', () => {
- const rawEvents = inboundVideoBurstEvents();
- const first = makeCoalescingRuntime();
- const second = makeCoalescingRuntime();
- for (const event of rawEvents) {
- first.dispatch(event);
- second.dispatch(event);
- }
-
- expect(second.eventLog.map((entry) => entry.event)).toEqual(first.eventLog.map((entry) => entry.event));
- expect(second.snapshot).toEqual(first.snapshot);
- });
-});
diff --git a/packages/voice_engine_v2/src/testing/eventLogReplay.ts b/packages/voice_engine_v2/src/testing/eventLogReplay.ts
deleted file mode 100644
index 78daf0fca..000000000
--- a/packages/voice_engine_v2/src/testing/eventLogReplay.ts
+++ /dev/null
@@ -1,150 +0,0 @@
-// SPDX-License-Identifier: AGPL-3.0-or-later
-
-import assert from 'node:assert/strict';
-import {transitionVoiceEngineV2} from '@fluxer/voice_engine_v2/src/core/reducer';
-import {selectVoiceEngineV2Model} from '@fluxer/voice_engine_v2/src/core/selectors';
-import {
- availableVoiceEngineV2Capabilities,
- createVoiceEngineV2InitialSnapshot,
- type VoiceEngineV2Snapshot,
-} from '@fluxer/voice_engine_v2/src/core/state';
-import type {VoiceEngineV2Command} from '@fluxer/voice_engine_v2/src/protocol/commands';
-import type {VoiceEngineV2Event} from '@fluxer/voice_engine_v2/src/protocol/events';
-import type {VoiceEngineV2Capabilities, VoiceEngineV2Model} from '@fluxer/voice_engine_v2/src/protocol/types';
-
-export const VOICE_ENGINE_V2_EVENT_LOG_FIXTURE_VERSION = 1;
-
-export interface VoiceEngineV2EventLogFixtureStep {
- name?: string;
- event: VoiceEngineV2Event;
- commands: Array<VoiceEngineV2Command>;
-}
-
-export interface VoiceEngineV2EventLogFixtureExpected {
- finalSnapshot: VoiceEngineV2Snapshot;
- finalModel?: VoiceEngineV2Model;
-}
-
-export interface VoiceEngineV2EventLogFixture {
- version: typeof VOICE_ENGINE_V2_EVENT_LOG_FIXTURE_VERSION;
- name: string;
- description?: string;
- capabilities?: VoiceEngineV2Capabilities;
- initialSnapshot?: VoiceEngineV2Snapshot;
- steps: Array<VoiceEngineV2EventLogFixtureStep>;
- expected: VoiceEngineV2EventLogFixtureExpected;
-}
-
-export interface VoiceEngineV2EventLogReplayStepResult {
- index: number;
- name: string | null;
- event: VoiceEngineV2Event;
- previousSnapshot: VoiceEngineV2Snapshot;
- snapshot: VoiceEngineV2Snapshot;
- commands: Array<VoiceEngineV2Command>;
-}
-
-export interface VoiceEngineV2EventLogReplayResult {
- fixtureName: string;
- initialSnapshot: VoiceEngineV2Snapshot;
- steps: Array<VoiceEngineV2EventLogReplayStepResult>;
- finalSnapshot: VoiceEngineV2Snapshot;
- finalModel: VoiceEngineV2Model;
- commandBatches: Array<Array<VoiceEngineV2Command>>;
-}
-
-export function replayVoiceEngineV2EventLogFixture(
- fixture: VoiceEngineV2EventLogFixture,
-): VoiceEngineV2EventLogReplayResult {
- assertVoiceEngineV2EventLogFixture(fixture);
-
- const initialSnapshot = fixture.initialSnapshot
- ? cloneReplayValue(fixture.initialSnapshot)
- : createVoiceEngineV2InitialSnapshot(fixture.capabilities ?? availableVoiceEngineV2Capabilities());
- const initialSnapshotForResult = cloneReplayValue(initialSnapshot);
- let snapshot = initialSnapshot;
- const steps: Array<VoiceEngineV2EventLogReplayStepResult> = [];
-
- for (const [index, step] of fixture.steps.entries()) {
- const previousSnapshot = cloneReplayValue(snapshot);
- const previousSnapshotBeforeTransition = cloneReplayValue(snapshot);
- deepFreeze(snapshot);
-
- const transition = transitionVoiceEngineV2(snapshot, step.event);
-
- assert.deepStrictEqual(
- snapshot,
- previousSnapshotBeforeTransition,
- `${fixture.name} step ${index + 1} mutated its previous snapshot`,
- );
- assert.deepStrictEqual(
- transition.commands,
- step.commands,
- `${fixture.name} step ${index + 1} emitted an unexpected command batch`,
- );
-
- snapshot = transition.snapshot;
- steps.push({
- index,
- name: step.name ?? null,
- event: cloneReplayValue(step.event),
- previousSnapshot,
- snapshot: cloneReplayValue(snapshot),
- commands: cloneReplayValue(transition.commands),
- });
- }
-
- assert.deepStrictEqual(snapshot, fixture.expected.finalSnapshot, `${fixture.name} final snapshot drifted`);
-
- const finalModel = selectVoiceEngineV2Model(snapshot);
- if (fixture.expected.finalModel) {
- assert.deepStrictEqual(finalModel, fixture.expected.finalModel, `${fixture.name} final model drifted`);
- }
-
- return {
- fixtureName: fixture.name,
- initialSnapshot: initialSnapshotForResult,
- steps,
- finalSnapshot: cloneReplayValue(snapshot),
- finalModel: cloneReplayValue(finalModel),
- commandBatches: steps.map((step) => cloneReplayValue(step.commands)),
- };
-}
-
-export function assertVoiceEngineV2EventLogFixture(fixture: VoiceEngineV2EventLogFixture): void {
- assert.equal(
- fixture.version,
- VOICE_ENGINE_V2_EVENT_LOG_FIXTURE_VERSION,
- `${fixture.name} uses an unsupported voice engine v2 event-log fixture version`,
- );
- assert.ok(fixture.name.length > 0, 'voice engine v2 event-log fixtures must have a name');
- assert.ok(Array.isArray(fixture.steps), `${fixture.name} must provide replay steps`);
-
- for (const [index, step] of fixture.steps.entries()) {
- assert.ok(step.event && typeof step.event.type === 'string', `${fixture.name} step ${index + 1} has no event`);
- assert.ok(Array.isArray(step.commands), `${fixture.name} step ${index + 1} must declare commands`);
- }
-}
-
-function cloneReplayValue<Value>(value: Value): Value {
- if (Array.isArray(value)) return value.map((entry) => cloneReplayValue(entry)) as Value;
- if (!isFreezable(value)) return value;
- const clone: Record<PropertyKey, unknown> = {};
- for (const property of Reflect.ownKeys(value)) {
- clone[property] = cloneReplayValue((value as Record<PropertyKey, unknown>)[property]);
- }
- return clone as Value;
-}
-
-function deepFreeze<Value>(value: Value): Value {
- if (!isFreezable(value) || Object.isFrozen(value)) return value;
- for (const property of Reflect.ownKeys(value)) {
- deepFreeze((value as Record<PropertyKey, unknown>)[property]);
- }
- Object.freeze(value);
- return value;
-}
-
-function isFreezable(value: unknown): value is Record<PropertyKey, unknown> {
- return (typeof value === 'object' && value !== null) || typeof value === 'function';
-}