-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathbruteForceChoice.test.ts
More file actions
141 lines (103 loc) · 5.08 KB
/
bruteForceChoice.test.ts
File metadata and controls
141 lines (103 loc) · 5.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import { describe, it, expect } from "vitest";
import type { Answer } from "@kleros/kleros-sdk";
import { hashVote } from "utils/crypto/hashVote";
import { bruteForceChoice } from "./bruteForceChoice";
describe("bruteForceChoice", () => {
const createCommit = (choice: bigint, salt: `0x${string}`) => hashVote(choice, BigInt(salt));
const mockAnswers: Answer[] = [
{ id: "0x0", title: "Refuse To Arbitrate", description: "RTA" },
{ id: "0x1", title: "Option 1", description: "First option" },
{ id: "0x2", title: "Option 2", description: "Second option" },
{ id: "0x3", title: "Option 3", description: "Third option" },
];
describe("successful choice recovery", () => {
it("should recover choice 0 (Refuse To Arbitrate)", () => {
const salt = "0xabcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890" as `0x${string}`;
const commit = createCommit(0n, salt);
const result = bruteForceChoice(salt, mockAnswers, commit);
expect(result).toBe(0n);
});
it("should recover choice 1", () => {
const salt = "0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef" as `0x${string}`;
const commit = createCommit(1n, salt);
const result = bruteForceChoice(salt, mockAnswers, commit);
expect(result).toBe(1n);
});
it("should recover choice 2", () => {
const salt = "0xfedcba0987654321fedcba0987654321fedcba0987654321fedcba0987654321" as `0x${string}`;
const commit = createCommit(2n, salt);
const result = bruteForceChoice(salt, mockAnswers, commit);
expect(result).toBe(2n);
});
it("should recover the last choice in the list", () => {
const salt = "0x9999999999999999999999999999999999999999999999999999999999999999" as `0x${string}`;
const commit = createCommit(3n, salt);
const result = bruteForceChoice(salt, mockAnswers, commit);
expect(result).toBe(3n);
});
});
describe("empty answers handling", () => {
it("should default to RFA when answers array is empty", () => {
const salt = "0xabcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890" as `0x${string}`;
const commit = createCommit(0n, salt);
const result = bruteForceChoice(salt, [], commit);
expect(result).toBe(0n);
});
});
describe("error cases", () => {
it("should throw error when choice cannot be found", () => {
const salt = "0xabcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890" as `0x${string}`;
// Create a commit for choice 99 which doesn't exist in our answer set
const commit = createCommit(99n, salt);
expect(() => bruteForceChoice(salt, mockAnswers, commit)).toThrow("Unable to retrieve choice.");
});
it("should throw when commit doesn't match any answer", () => {
const salt = "0x1111111111111111111111111111111111111111111111111111111111111111" as `0x${string}`;
const fakeCommit = "0x0000000000000000000000000000000000000000000000000000000000000000";
expect(() => bruteForceChoice(salt, mockAnswers, fakeCommit)).toThrow("Unable to retrieve choice.");
});
});
describe("deterministic behavior", () => {
it("should return same choice for same inputs", () => {
const salt = "0xabcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890" as `0x${string}`;
const commit = createCommit(1n, salt);
const result1 = bruteForceChoice(salt, mockAnswers, commit);
const result2 = bruteForceChoice(salt, mockAnswers, commit);
expect(result1).toBe(result2);
});
it("iterates answers in the given order", () => {
const salt = "0xabcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890" as `0x${string}`;
const answers: Answer[] = [
{ id: "5", title: "Five", description: "" },
{ id: "1", title: "One", description: "" },
];
const commit = createCommit(1n, salt);
const result = bruteForceChoice(salt, answers, commit);
expect(result).toBe(1n);
});
});
describe("answer format handling", () => {
it("should handle answers with hex string IDs", () => {
const answersWithHexIds: Answer[] = [
{ id: "0x0", title: "A", description: "" },
{ id: "0xa", title: "B", description: "" },
{ id: "0xf", title: "C", description: "" },
];
const salt = "0xabcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890" as `0x${string}`;
const commit = createCommit(10n, salt); // 0xa = 10
const result = bruteForceChoice(salt, answersWithHexIds, commit);
expect(result).toBe(10n);
});
it("should handle answers with numeric string IDs", () => {
const answersWithNumericIds: Answer[] = [
{ id: "0", title: "A", description: "" },
{ id: "1", title: "B", description: "" },
{ id: "2", title: "C", description: "" },
];
const salt = "0xabcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890" as `0x${string}`;
const commit = createCommit(2n, salt);
const result = bruteForceChoice(salt, answersWithNumericIds, commit);
expect(result).toBe(2n);
});
});
});