69 lines
2.1 KiB
TypeScript
69 lines
2.1 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
import {
|
|
MAX_COMBAT_EVENT_HISTORY,
|
|
appendCombatEvent,
|
|
subscribeCombatEvents,
|
|
type CombatEventInput,
|
|
type StructuredCombatEvent,
|
|
} from "./combatEvents";
|
|
|
|
function eventInput(index: number): CombatEventInput {
|
|
return {
|
|
occurredAt: index * 100,
|
|
kind: "damage",
|
|
sourceActorId: "benchmark-source",
|
|
targetActorId: "benchmark-target",
|
|
abilityId: "benchmark-strike",
|
|
school: "physical",
|
|
rawAmount: index,
|
|
effectiveAmount: index,
|
|
overheal: 0,
|
|
absorbed: 0,
|
|
critical: false,
|
|
};
|
|
}
|
|
|
|
describe("combat event subscriptions", () => {
|
|
it("publishes every event while retaining only the bounded HUD history", () => {
|
|
const published: StructuredCombatEvent[] = [];
|
|
const unsubscribe = subscribeCombatEvents((event) => published.push(event));
|
|
let history: readonly StructuredCombatEvent[] = [];
|
|
const eventCount = MAX_COMBAT_EVENT_HISTORY + 75;
|
|
|
|
try {
|
|
for (let id = 1; id <= eventCount; id += 1) {
|
|
history = appendCombatEvent(history, eventInput(id), id);
|
|
}
|
|
} finally {
|
|
unsubscribe();
|
|
}
|
|
|
|
expect(published).toHaveLength(eventCount);
|
|
expect(published.map((event) => event.id)).toEqual(
|
|
Array.from({ length: eventCount }, (_, index) => index + 1),
|
|
);
|
|
expect(history).toHaveLength(MAX_COMBAT_EVENT_HISTORY);
|
|
expect(history[0]?.id).toBe(eventCount - MAX_COMBAT_EVENT_HISTORY + 1);
|
|
expect(history.at(-1)?.id).toBe(eventCount);
|
|
|
|
appendCombatEvent(history, eventInput(eventCount + 1), eventCount + 1);
|
|
expect(published).toHaveLength(eventCount);
|
|
});
|
|
|
|
it("isolates authoritative recording from a failing subscriber", () => {
|
|
const published: number[] = [];
|
|
const unsubscribeFailing = subscribeCombatEvents(() => {
|
|
throw new Error("metrics consumer failed");
|
|
});
|
|
const unsubscribeHealthy = subscribeCombatEvents((event) => published.push(event.id));
|
|
|
|
try {
|
|
expect(appendCombatEvent([], eventInput(1), 1)).toHaveLength(1);
|
|
expect(published).toEqual([1]);
|
|
} finally {
|
|
unsubscribeHealthy();
|
|
unsubscribeFailing();
|
|
}
|
|
});
|
|
});
|