Initial Commit
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
import { describe, expect, it } from "@suaveplan/testing/runner";
|
||||
import { SpritePlayerEvents } from "./events.js";
|
||||
|
||||
describe("SpritePlayerEvents", () => {
|
||||
it("on subscribes and emit dispatches", { timeout: 5000 }, () => {
|
||||
const events = new SpritePlayerEvents();
|
||||
const seen: string[] = [];
|
||||
events.on("play", (p) => seen.push(p.handleId));
|
||||
events.emit("play", { handleId: "x", entryName: "hit", startedAt: 0 });
|
||||
expect(seen).toEqual(["x"]);
|
||||
});
|
||||
|
||||
it("unsubscribe stops dispatch", { timeout: 5000 }, () => {
|
||||
const events = new SpritePlayerEvents();
|
||||
const seen: string[] = [];
|
||||
const off = events.on("stop", (p) => seen.push(p.handleId));
|
||||
events.emit("stop", { handleId: "a", entryName: "x", fadeOutMs: 0 });
|
||||
off();
|
||||
events.emit("stop", { handleId: "b", entryName: "x", fadeOutMs: 0 });
|
||||
expect(seen).toEqual(["a"]);
|
||||
});
|
||||
|
||||
it("voice-stolen carries both handle ids", { timeout: 5000 }, () => {
|
||||
const events = new SpritePlayerEvents();
|
||||
let payload: { stolenId: string; replacedBy: string } | null = null;
|
||||
events.on("voice-stolen", (p) => {
|
||||
payload = p;
|
||||
});
|
||||
events.emit("voice-stolen", {
|
||||
stolenId: "a",
|
||||
replacedBy: "b",
|
||||
entryName: "hit",
|
||||
});
|
||||
expect(payload).not.toBeNull();
|
||||
expect(payload!.stolenId).toBe("a");
|
||||
expect(payload!.replacedBy).toBe("b");
|
||||
});
|
||||
|
||||
it("complete event includes duration", { timeout: 5000 }, () => {
|
||||
const events = new SpritePlayerEvents();
|
||||
const got: number[] = [];
|
||||
events.on("complete", (p) => got.push(p.durationMs));
|
||||
events.emit("complete", {
|
||||
handleId: "x",
|
||||
entryName: "hit",
|
||||
durationMs: 1000,
|
||||
});
|
||||
expect(got).toEqual([1000]);
|
||||
});
|
||||
|
||||
it("error event surfaces code and message", { timeout: 5000 }, () => {
|
||||
const events = new SpritePlayerEvents();
|
||||
let code = "";
|
||||
events.on("error", (p) => {
|
||||
code = p.code;
|
||||
});
|
||||
events.emit("error", {
|
||||
handleId: "x",
|
||||
entryName: "hit",
|
||||
code: "AUDIO_SPRITE_PLAYER.PLAYBACK_FAILED",
|
||||
message: "boom",
|
||||
});
|
||||
expect(code).toBe("AUDIO_SPRITE_PLAYER.PLAYBACK_FAILED");
|
||||
});
|
||||
|
||||
it("clear removes every listener", { timeout: 5000 }, () => {
|
||||
const events = new SpritePlayerEvents();
|
||||
const seen: string[] = [];
|
||||
events.on("play", (p) => seen.push(p.handleId));
|
||||
events.clear();
|
||||
events.emit("play", { handleId: "x", entryName: "x", startedAt: 0 });
|
||||
expect(seen).toEqual([]);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user