89 lines
2.3 KiB
TypeScript
89 lines
2.3 KiB
TypeScript
import assert from "node:assert/strict";
|
|
import test from "node:test";
|
|
import {
|
|
appendAuctionReaction,
|
|
isAuctionReaction,
|
|
MAX_VISIBLE_REACTIONS,
|
|
REACTION_LIFETIME_MS,
|
|
sendAuctionReaction,
|
|
} from "../src/api/auction-reactions.ts";
|
|
|
|
const reaction = {
|
|
id: "reaction-1",
|
|
kind: "clap" as const,
|
|
displayName: "팀장",
|
|
sentAt: 1000,
|
|
};
|
|
|
|
test("rejects unsupported or malformed realtime reactions", () => {
|
|
assert.equal(isAuctionReaction(reaction), true);
|
|
for (const value of [
|
|
null,
|
|
{},
|
|
{ ...reaction, kind: "__proto__" },
|
|
{ ...reaction, kind: "custom" },
|
|
{ ...reaction, id: "" },
|
|
{ ...reaction, sentAt: Infinity },
|
|
{ ...reaction, displayName: "a".repeat(161) },
|
|
]) {
|
|
assert.equal(isAuctionReaction(value), false);
|
|
}
|
|
});
|
|
|
|
test("deduplicates the broadcast and acknowledgement and caps simultaneous effects", () => {
|
|
const first = appendAuctionReaction([], reaction, 1000);
|
|
assert.deepEqual(appendAuctionReaction(first, reaction, 1100), first);
|
|
let current = first;
|
|
for (let index = 0; index < 40; index++) {
|
|
current = appendAuctionReaction(
|
|
current,
|
|
{ ...reaction, id: String(index) },
|
|
1200,
|
|
);
|
|
}
|
|
assert.equal(current.length, MAX_VISIBLE_REACTIONS);
|
|
assert.equal(current.at(-1)?.id, "39");
|
|
});
|
|
|
|
test("expires by local receipt time even when the server clock differs", () => {
|
|
const first = appendAuctionReaction(
|
|
[],
|
|
{ ...reaction, sentAt: 9_999_999 },
|
|
1000,
|
|
);
|
|
const next = appendAuctionReaction(
|
|
first,
|
|
{ ...reaction, id: "next" },
|
|
1000 + REACTION_LIFETIME_MS,
|
|
);
|
|
assert.deepEqual(
|
|
next.map((value) => value.id),
|
|
["next"],
|
|
);
|
|
});
|
|
|
|
test("only accepts valid server acknowledgements and explains unavailable sends", async () => {
|
|
assert.equal(
|
|
await sendAuctionReaction(async () => ({ ok: true, reaction })),
|
|
reaction,
|
|
);
|
|
await assert.rejects(sendAuctionReaction(undefined), /연결되면/);
|
|
await assert.rejects(
|
|
sendAuctionReaction(async () => ({
|
|
ok: false,
|
|
code: "REACTION_RATE_LIMITED",
|
|
})),
|
|
/기다렸다가/,
|
|
);
|
|
await assert.rejects(
|
|
sendAuctionReaction(async () => {
|
|
throw new Error("timeout");
|
|
}),
|
|
/보내지 못했습니다/,
|
|
);
|
|
await assert.rejects(
|
|
sendAuctionReaction(async () => ({ ok: true })),
|
|
/보내지 못했습니다/,
|
|
);
|
|
});
|