|
| 1 | +import { it } from "node:test"; |
| 2 | +import * as assert from "node:assert"; |
| 3 | + |
| 4 | +import { getReasonForInvalidateBallot } from "./getReasonForInvalidateBallot.js"; |
| 5 | +import type { BallotFileFormat, VoteFileFormat } from "./parser.js"; |
| 6 | + |
| 7 | +const dummyVote: VoteFileFormat = { |
| 8 | + checksum: "/sjnc/rMa4Ux6zRgl3a/DvDpWp+hLwiAR6E2Nj34bUWzkojmy96uw7KkCV1FpKqU28rS3trF1AvKFOOwnDdxOg==", |
| 9 | + candidates: ["a", "b", "c", "d", "e", "f", "g"], |
| 10 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 11 | +} as any; |
| 12 | + |
| 13 | +it("should return null for valid ballots", () => { |
| 14 | + assert.strictEqual( |
| 15 | + getReasonForInvalidateBallot({ |
| 16 | + author: "John Smith", |
| 17 | + preferences: [ |
| 18 | + { title: "a", score: 0 }, |
| 19 | + { title: "b", score: 1 }, |
| 20 | + { title: "c", score: 1 }, |
| 21 | + { title: "d", score: -0 }, |
| 22 | + { title: "e", score: -2 }, |
| 23 | + { title: "f", score: -1 }, |
| 24 | + { title: "g", score: 0 }, |
| 25 | + ], |
| 26 | + poolChecksum: "/sjnc/rMa4Ux6zRgl3a/DvDpWp+hLwiAR6E2Nj34bUWzkojmy96uw7KkCV1FpKqU28rS3trF1AvKFOOwnDdxOg==", |
| 27 | + }, dummyVote), |
| 28 | + null, |
| 29 | + ); |
| 30 | +}); |
| 31 | +it("should report missing author", () => { |
| 32 | + assert.deepStrictEqual( |
| 33 | + getReasonForInvalidateBallot({ |
| 34 | + preferences: [ |
| 35 | + { title: "a", score: 0 }, |
| 36 | + { title: "b", score: 1 }, |
| 37 | + { title: "c", score: 1 }, |
| 38 | + { title: "d", score: -0 }, |
| 39 | + { title: "e", score: -2 }, |
| 40 | + { title: "f", score: -1 }, |
| 41 | + { title: "g", score: 0 }, |
| 42 | + ], |
| 43 | + poolChecksum: "/sjnc/rMa4Ux6zRgl3a/DvDpWp+hLwiAR6E2Nj34bUWzkojmy96uw7KkCV1FpKqU28rS3trF1AvKFOOwnDdxOg==", |
| 44 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 45 | + } satisfies Omit<BallotFileFormat, "author"> as any, dummyVote), |
| 46 | + { |
| 47 | + missingAuthor: true, |
| 48 | + }, |
| 49 | + ); |
| 50 | +}); |
| 51 | +it("should report empty author", () => { |
| 52 | + assert.deepStrictEqual( |
| 53 | + getReasonForInvalidateBallot({ |
| 54 | + author: "", |
| 55 | + preferences: [ |
| 56 | + { title: "a", score: 0 }, |
| 57 | + { title: "b", score: 1 }, |
| 58 | + { title: "c", score: 1 }, |
| 59 | + { title: "d", score: -0 }, |
| 60 | + { title: "e", score: -2 }, |
| 61 | + { title: "f", score: -1 }, |
| 62 | + { title: "g", score: 0 }, |
| 63 | + ], |
| 64 | + poolChecksum: "/sjnc/rMa4Ux6zRgl3a/DvDpWp+hLwiAR6E2Nj34bUWzkojmy96uw7KkCV1FpKqU28rS3trF1AvKFOOwnDdxOg==", |
| 65 | + }, dummyVote), |
| 66 | + { |
| 67 | + missingAuthor: true, |
| 68 | + }, |
| 69 | + ); |
| 70 | +}); |
| 71 | +it("should not report additional candidates", () => { |
| 72 | + assert.strictEqual( |
| 73 | + getReasonForInvalidateBallot({ |
| 74 | + author: "John Smith", |
| 75 | + preferences: [ |
| 76 | + { title: "a", score: 0 }, |
| 77 | + { title: "b", score: 1 }, |
| 78 | + { title: "c", score: 1 }, |
| 79 | + { title: "d", score: -0 }, |
| 80 | + { title: "e", score: -2 }, |
| 81 | + { title: "f", score: -1 }, |
| 82 | + { title: "g", score: 0 }, |
| 83 | + { title: "h", score: 0 }, |
| 84 | + ], |
| 85 | + poolChecksum: "/sjnc/rMa4Ux6zRgl3a/DvDpWp+hLwiAR6E2Nj34bUWzkojmy96uw7KkCV1FpKqU28rS3trF1AvKFOOwnDdxOg==", |
| 86 | + }, dummyVote), |
| 87 | + null, |
| 88 | + ); |
| 89 | +}); |
| 90 | +it("should report missing candidates", () => { |
| 91 | + assert.deepStrictEqual( |
| 92 | + getReasonForInvalidateBallot({ |
| 93 | + author: "John Smith", |
| 94 | + preferences: [ |
| 95 | + { title: "a", score: 0 }, |
| 96 | + { title: "bb", score: 1 }, |
| 97 | + { title: "c", score: -0 }, |
| 98 | + { title: "e", score: -2 }, |
| 99 | + { title: "f", score: -1 }, |
| 100 | + { title: "g", score: 0 }, |
| 101 | + ], |
| 102 | + poolChecksum: "/sjnc/rMa4Ux6zRgl3a/DvDpWp+hLwiAR6E2Nj34bUWzkojmy96uw7KkCV1FpKqU28rS3trF1AvKFOOwnDdxOg==", |
| 103 | + }, dummyVote), |
| 104 | + { |
| 105 | + missingCandidates: ["b", "d"], |
| 106 | + }, |
| 107 | + ); |
| 108 | +}); |
| 109 | +it("should report overflow integer scores", () => { |
| 110 | + assert.deepStrictEqual( |
| 111 | + getReasonForInvalidateBallot({ |
| 112 | + author: "John Smith", |
| 113 | + preferences: [ |
| 114 | + { |
| 115 | + title: "a", |
| 116 | + score: Number.MAX_SAFE_INTEGER + 1, |
| 117 | + }, |
| 118 | + { title: "b", score: Number.MAX_SAFE_INTEGER }, |
| 119 | + { title: "c", score: Number.MIN_SAFE_INTEGER }, |
| 120 | + { title: "d", score: Number.MIN_SAFE_INTEGER - 1 }, |
| 121 | + { title: "e", score: Number.MIN_VALUE }, |
| 122 | + { title: "f", score: Number.MAX_VALUE }, |
| 123 | + { title: "g", score: 0 }, |
| 124 | + ], |
| 125 | + poolChecksum: "/sjnc/rMa4Ux6zRgl3a/DvDpWp+hLwiAR6E2Nj34bUWzkojmy96uw7KkCV1FpKqU28rS3trF1AvKFOOwnDdxOg==", |
| 126 | + }, dummyVote), |
| 127 | + { |
| 128 | + candidatesWithInvalidScores: [{ |
| 129 | + score: 9007199254740992, |
| 130 | + title: "a", |
| 131 | + }, |
| 132 | + { |
| 133 | + score: -9007199254740992, |
| 134 | + title: "d", |
| 135 | + }, |
| 136 | + { |
| 137 | + score: 5e-324, |
| 138 | + title: "e", |
| 139 | + }, |
| 140 | + { |
| 141 | + score: 1.7976931348623157e+308, |
| 142 | + title: "f", |
| 143 | + }], |
| 144 | + }, |
| 145 | + ); |
| 146 | +}); |
| 147 | +it("should report non-integer scores", () => { |
| 148 | + assert.deepStrictEqual( |
| 149 | + getReasonForInvalidateBallot({ |
| 150 | + author: "John Smith", |
| 151 | + preferences: [ |
| 152 | + { |
| 153 | + title: "a", |
| 154 | + // @ts-expect-error score is purposefully mistyped |
| 155 | + score: "1", |
| 156 | + }, |
| 157 | + { title: "b", score: 1.1 }, |
| 158 | + { title: "c", score: 1.0 }, |
| 159 | + // @ts-expect-error score is purposefully mistyped |
| 160 | + { title: "d", score: [0] }, |
| 161 | + // @ts-expect-error score is purposefully mistyped |
| 162 | + { title: "e", score: { value: 1 } }, |
| 163 | + // @ts-expect-error score is purposefully mistyped |
| 164 | + { title: "f", score: 1n }, |
| 165 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 166 | + { title: "g" } as any as { title: string; score: number }, |
| 167 | + ], |
| 168 | + poolChecksum: "/sjnc/rMa4Ux6zRgl3a/DvDpWp+hLwiAR6E2Nj34bUWzkojmy96uw7KkCV1FpKqU28rS3trF1AvKFOOwnDdxOg==", |
| 169 | + }, dummyVote), |
| 170 | + { |
| 171 | + candidatesWithInvalidScores: [ |
| 172 | + { |
| 173 | + score: "1", |
| 174 | + title: "a", |
| 175 | + }, |
| 176 | + { |
| 177 | + score: 1.1, |
| 178 | + title: "b", |
| 179 | + }, |
| 180 | + { |
| 181 | + score: [0], |
| 182 | + title: "d", |
| 183 | + }, |
| 184 | + { |
| 185 | + score: { value: 1 }, |
| 186 | + title: "e", |
| 187 | + }, |
| 188 | + { |
| 189 | + score: 1n, |
| 190 | + title: "f", |
| 191 | + }, |
| 192 | + { title: "g" }, |
| 193 | + ], |
| 194 | + }, |
| 195 | + ); |
| 196 | +}); |
| 197 | + |
| 198 | +it("should report multiple errors at once", () => { |
| 199 | + assert.deepStrictEqual( |
| 200 | + getReasonForInvalidateBallot({ |
| 201 | + author: "", |
| 202 | + preferences: [ |
| 203 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 204 | + { title: "a" } as any as { title: string; score: number }, |
| 205 | + { title: "d", score: 1.1 }, |
| 206 | + { title: "c", score: -1.0 }, |
| 207 | + { title: "e", score: Infinity }, |
| 208 | + { title: "ff", score: 1 }, |
| 209 | + { title: "g", score: 0 }, |
| 210 | + { title: "h", score: 1 }, |
| 211 | + ], |
| 212 | + poolChecksum: "invalid", |
| 213 | + }, { |
| 214 | + checksum: "expected", |
| 215 | + candidates: ["a", "b", "c", "d", "e", "f", "g"], |
| 216 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 217 | + } as any), |
| 218 | + { |
| 219 | + missingAuthor: true, |
| 220 | + missingCandidates: ["b", "f"], |
| 221 | + invalidChecksum: { |
| 222 | + actual: "invalid", |
| 223 | + expected: "expected", |
| 224 | + }, |
| 225 | + candidatesWithInvalidScores: [ |
| 226 | + { title: "a" }, |
| 227 | + { |
| 228 | + score: 1.1, |
| 229 | + title: "d", |
| 230 | + }, |
| 231 | + { |
| 232 | + score: Infinity, |
| 233 | + title: "e", |
| 234 | + }, |
| 235 | + ], |
| 236 | + }, |
| 237 | + ); |
| 238 | +}); |
0 commit comments