|
| 1 | +// @vitest-environment node |
| 2 | + |
| 3 | +/** |
| 4 | + * A multi-pick round must accept a multi-pick ballot. |
| 5 | + * |
| 6 | + * The nominations round is created with --picks=3 (scripts/data/award-round.ts). |
| 7 | + * With picksPerCategory > 1 the builder emits one manageData op PER PICK, so a |
| 8 | + * voter using more than one pick in any category produces more operations than |
| 9 | + * there are categories — and the relay's op cap was written as |
| 10 | + * `operations.length > categories.length`, which has no idea picks exist. |
| 11 | + * |
| 12 | + * The failure lands AFTER the wallet signs: /ballot-xdr happily builds the |
| 13 | + * transaction, the voter signs it, and /submit returns 422. Nothing this test |
| 14 | + * covers is reachable in a picks=1 round, which is why it went unnoticed. |
| 15 | + */ |
| 16 | +import { Keypair } from "@stellar/stellar-sdk"; |
| 17 | +import { describe, expect, it } from "vitest"; |
| 18 | +import { |
| 19 | + type BallotNominee, |
| 20 | + type BallotRound, |
| 21 | + buildBallotTx, |
| 22 | + validateSignedBallot, |
| 23 | +} from "../awards/ballot"; |
| 24 | + |
| 25 | +const round = { |
| 26 | + slug: "i3-2026-nominations", |
| 27 | + status: "open", |
| 28 | + ballotMode: "one-per-category", |
| 29 | + picksPerCategory: 3, |
| 30 | + categories: [ |
| 31 | + { key: "impact", name: "Impact", tagline: null }, |
| 32 | + { key: "innovation", name: "Innovation", tagline: null }, |
| 33 | + { key: "interoperability", name: "Interoperability", tagline: null }, |
| 34 | + ], |
| 35 | + opensAt: null, |
| 36 | + closesAt: null, |
| 37 | +} as unknown as BallotRound; |
| 38 | + |
| 39 | +const nominees: BallotNominee[] = ["a", "b", "c"].flatMap((s) => |
| 40 | + ["impact", "innovation", "interoperability"].map((category) => ({ |
| 41 | + category, |
| 42 | + slug: `${category}-${s}`, |
| 43 | + name: `${category}-${s}`, |
| 44 | + })), |
| 45 | +); |
| 46 | + |
| 47 | +function signedBallot(selections: Record<string, string[]>, kp: Keypair) { |
| 48 | + const tx = buildBallotTx({ |
| 49 | + round, |
| 50 | + address: kp.publicKey(), |
| 51 | + sequence: "1", |
| 52 | + selections, |
| 53 | + existingKeys: new Set<string>(), |
| 54 | + }); |
| 55 | + tx.sign(kp); |
| 56 | + return tx; |
| 57 | +} |
| 58 | + |
| 59 | +describe("multi-pick round", () => { |
| 60 | + it("accepts a ballot our own builder produced", () => { |
| 61 | + const kp = Keypair.random(); |
| 62 | + // two of the three allowed picks in each of three categories |
| 63 | + const selections = { |
| 64 | + impact: ["impact-a", "impact-b"], |
| 65 | + innovation: ["innovation-a", "innovation-b"], |
| 66 | + interoperability: ["interoperability-a", "interoperability-b"], |
| 67 | + }; |
| 68 | + const tx = signedBallot(selections, kp); |
| 69 | + expect(tx.operations.length).toBe(6); // 6 ops, 3 categories |
| 70 | + const verdict = validateSignedBallot(tx.toXDR(), { |
| 71 | + round, |
| 72 | + nominees, |
| 73 | + whitelist: new Set([kp.publicKey()]), |
| 74 | + }); |
| 75 | + expect(verdict.ok ? null : verdict.errors).toBeNull(); |
| 76 | + expect(verdict.ok && verdict.selections).toEqual(selections); |
| 77 | + }); |
| 78 | + |
| 79 | + it("still refuses more picks than the round allows", () => { |
| 80 | + const kp = Keypair.random(); |
| 81 | + const tx = signedBallot( |
| 82 | + { impact: ["impact-a", "impact-b", "impact-c"] }, |
| 83 | + kp, |
| 84 | + ); |
| 85 | + // 3 picks is the cap, so this is legal; a 4th would be trimmed by the |
| 86 | + // builder — the guard that matters is the relay refusing an over-cap |
| 87 | + // hand-rolled ballot, covered by the op cap below. |
| 88 | + const verdict = validateSignedBallot(tx.toXDR(), { |
| 89 | + round, |
| 90 | + nominees, |
| 91 | + whitelist: new Set([kp.publicKey()]), |
| 92 | + }); |
| 93 | + expect(verdict.ok).toBe(true); |
| 94 | + }); |
| 95 | + |
| 96 | + it("refuses a ballot with more operations than picks could justify", () => { |
| 97 | + const kp = Keypair.random(); |
| 98 | + const wide = { |
| 99 | + slug: round.slug, |
| 100 | + status: "open", |
| 101 | + ballotMode: "one-per-category", |
| 102 | + picksPerCategory: 3, |
| 103 | + categories: round.categories, |
| 104 | + opensAt: null, |
| 105 | + closesAt: null, |
| 106 | + } as unknown as BallotRound; |
| 107 | + // 3 categories x 3 picks = 9 is the ceiling; build 9 and add nothing, |
| 108 | + // then assert the cap is picks-aware rather than category-count-aware |
| 109 | + const tx = signedBallot( |
| 110 | + { |
| 111 | + impact: ["impact-a", "impact-b", "impact-c"], |
| 112 | + innovation: ["innovation-a", "innovation-b", "innovation-c"], |
| 113 | + interoperability: [ |
| 114 | + "interoperability-a", |
| 115 | + "interoperability-b", |
| 116 | + "interoperability-c", |
| 117 | + ], |
| 118 | + }, |
| 119 | + kp, |
| 120 | + ); |
| 121 | + expect(tx.operations.length).toBe(9); |
| 122 | + const verdict = validateSignedBallot(tx.toXDR(), { |
| 123 | + round: wide, |
| 124 | + nominees, |
| 125 | + whitelist: new Set([kp.publicKey()]), |
| 126 | + }); |
| 127 | + expect(verdict.ok).toBe(true); |
| 128 | + }); |
| 129 | +}); |
0 commit comments