import { describe, expect, it } from "vitest"; import { breakTieWithInitiator, buildPollWeights, pickRandomTiedOption, resolvePollTally, resolvePollWeight, tallyPoll, type PollOption, type PollWeightTier, } from "@/lib/polls/tally"; const OPTIONS: PollOption[] = [ { key: "accept", label: "Work with the faction" }, { key: "decline", label: "Stay independent" }, { key: "defer", label: "Defer the decision" }, ]; const tiers: PollWeightTier[] = [ { label: "Logistics", roleIds: [4], multiplier: 1.5 }, { label: "Command", roleIds: [1, 2], multiplier: 2 }, ]; const tallyOf = ( votes: { userId: number; answers: string[] }[], weights: Map = new Map(), ) => tallyPoll({ options: OPTIONS, votes, weights }); describe("resolvePollWeight", () => { it("falls back to the default when no tier matches", () => { expect(resolvePollWeight({ roleIds: [9], tiers })).toEqual({ multiplier: 1, label: null }); expect( resolvePollWeight({ roleIds: [], tiers, defaultWeight: 1.25 }), ).toEqual({ multiplier: 1.25, label: null }); }); it("uses the highest matching tier instead of stacking them", () => { // Holds Logistics and Command: 2 wins, 1.5 + 2 = 3.5 would not. expect(resolvePollWeight({ roleIds: [4, 2], tiers })).toEqual({ multiplier: 2, label: "Command", }); }); it("keeps the earlier tier when two matching multipliers are equal", () => { const flat: PollWeightTier[] = [ { label: "First", roleIds: [7], multiplier: 1.5 }, { label: "Second", roleIds: [8], multiplier: 1.5 }, ]; expect(resolvePollWeight({ roleIds: [8, 7], tiers: flat })).toEqual({ multiplier: 1.5, label: "First", }); }); it("clamps a non-positive multiplier to 1 rather than muting the vote", () => { const broken: PollWeightTier[] = [{ label: "Broken", roleIds: [4], multiplier: 0 }]; expect(resolvePollWeight({ roleIds: [4], tiers: broken })).toEqual({ multiplier: 1, label: "Broken", }); }); }); describe("buildPollWeights", () => { it("weights each voter once, from the roles they hold now", () => { const weights = buildPollWeights({ votes: [ { userId: 1, answers: ["accept"] }, { userId: 2, answers: ["decline"] }, { userId: 3, answers: ["defer"] }, ], roleIdsByUser: new Map([ [1, [4]], [2, [1]], [3, [99]], ]), tiers, }); expect([...weights.entries()].sort()).toEqual([ [1, 1.5], [2, 2], [3, 1], ]); }); it("gives an unlisted voter the default weight", () => { const weights = buildPollWeights({ votes: [{ userId: 5, answers: ["accept"] }], roleIdsByUser: new Map(), tiers, defaultWeight: 1, }); expect(weights.get(5)).toBe(1); }); }); describe("tallyPoll", () => { it("counts raw and weighted scores separately", () => { const tally = tallyOf( [ { userId: 1, answers: ["accept"] }, { userId: 2, answers: ["accept"] }, { userId: 3, answers: ["decline"] }, ], new Map([ [1, 2], [2, 1], [3, 1.5], ]), ); expect(tally.counts.accept).toEqual({ raw: 2, weighted: 3 }); expect(tally.counts.decline).toEqual({ raw: 1, weighted: 1.5 }); expect(tally.totalVotes).toBe(3); expect(tally.totalWeight).toBe(4.5); expect(tally.topKeys).toEqual(["accept"]); }); it("counts every answer on a multi-select poll", () => { const tally = tallyOf( [ { userId: 1, answers: ["accept", "defer"] }, { userId: 2, answers: ["decline"] }, ], new Map([ [1, 1], [2, 1], ]), ); expect(tally.counts.accept.raw).toBe(1); expect(tally.counts.defer.raw).toBe(1); // Turnout is per voter, not per answer. expect(tally.totalVotes).toBe(2); expect(tally.totalWeight).toBe(2); }); it("dedupes a repeated key inside one vote", () => { const tally = tallyOf( [{ userId: 1, answers: ["accept", "accept", "accept"] }], new Map([[1, 2]]), ); expect(tally.counts.accept).toEqual({ raw: 1, weighted: 2 }); }); it("ignores answers naming an option that is not on the poll", () => { const tally = tallyOf( [ { userId: 1, answers: ["accept", "ghost"] }, { userId: 2, answers: ["ghost"] }, ], new Map([ [1, 1], [2, 1], ]), ); expect(tally.counts.accept.raw).toBe(1); expect(tally.counts.ghost).toBeUndefined(); // The vote naming only a dead option is not turnout. expect(tally.totalVotes).toBe(1); }); it("compares weights exactly, so decimal multipliers cannot fake a tie", () => { // 1.1 + 1.1 against a single 2.2: float addition would miss the tie. const tally = tallyOf( [ { userId: 1, answers: ["accept"] }, { userId: 2, answers: ["accept"] }, { userId: 3, answers: ["decline"] }, ], new Map([ [1, 1.1], [2, 1.1], [3, 2.2], ]), ); expect(tally.counts.accept.weighted).toBe(2.2); expect(tally.topKeys).toEqual(["accept", "decline"]); }); it("reports no top option when nobody voted", () => { const tally = tallyOf([]); expect(tally.topScore).toBe(0); expect(tally.topKeys).toEqual([]); expect(tally.totalVotes).toBe(0); }); }); describe("breakTieWithInitiator", () => { it("picks the one tied option the initiator voted for", () => { expect( breakTieWithInitiator({ tiedKeys: ["accept", "decline"], initiatorAnswers: ["decline"], }), ).toBe("decline"); }); it("refuses when the initiator picked several tied options", () => { expect( breakTieWithInitiator({ tiedKeys: ["accept", "decline"], initiatorAnswers: ["accept", "decline"], }), ).toBeNull(); }); it("refuses when the initiator voted outside the tie, or not at all", () => { expect( breakTieWithInitiator({ tiedKeys: ["accept", "decline"], initiatorAnswers: ["defer"] }), ).toBeNull(); expect(breakTieWithInitiator({ tiedKeys: ["accept"], initiatorAnswers: [] })).toBeNull(); expect(breakTieWithInitiator({ tiedKeys: ["accept"], initiatorAnswers: null })).toBeNull(); }); }); describe("resolvePollTally", () => { const tiedTally = () => tallyOf( [ { userId: 1, answers: ["accept"] }, { userId: 2, answers: ["decline"] }, ], new Map([ [1, 1], [2, 1], ]), ); it("decides a unique winner on votes", () => { const tally = tallyOf( [ { userId: 1, answers: ["accept"] }, { userId: 2, answers: ["accept"] }, { userId: 3, answers: ["decline"] }, ], new Map([ [1, 1], [2, 1], [3, 1], ]), ); expect(resolvePollTally({ tally, initiatorAnswers: ["decline"] })).toEqual({ status: "decided", winnerKey: "accept", method: "votes", }); }); it("breaks a tie with the initiator's vote", () => { expect(resolvePollTally({ tally: tiedTally(), initiatorAnswers: ["decline"] })).toEqual({ status: "decided", winnerKey: "decline", method: "initiator", }); }); it("escalates a tie the initiator cannot break", () => { expect( resolvePollTally({ tally: tiedTally(), initiatorAnswers: ["accept", "decline"] }), ).toEqual({ status: "pending", tiedKeys: ["accept", "decline"] }); expect(resolvePollTally({ tally: tiedTally(), initiatorAnswers: null })).toEqual({ status: "pending", tiedKeys: ["accept", "decline"], }); }); it("returns none when nothing counted", () => { expect(resolvePollTally({ tally: tallyOf([]), initiatorAnswers: ["accept"] })).toEqual({ status: "none", }); }); }); describe("pickRandomTiedOption", () => { it("is deterministic under an injected rng", () => { expect(pickRandomTiedOption(["a", "b", "c"], () => 0)).toBe("a"); expect(pickRandomTiedOption(["a", "b", "c"], () => 0.5)).toBe("b"); expect(pickRandomTiedOption(["a", "b", "c"], () => 0.999)).toBe("c"); }); it("clamps an rng that returns exactly 1", () => { expect(pickRandomTiedOption(["a", "b"], () => 1)).toBe("b"); }); it("returns null with nothing to pick", () => { expect(pickRandomTiedOption([])).toBeNull(); }); });