Repository navigation
Expand file tree
/
Copy pathvortexVote.test.js
More file actions
291 lines (254 loc) · 10.7 KB
/
Copy pathvortexVote.test.js
File metadata and controls
291 lines (254 loc) · 10.7 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import { expect, test, describe } from "bun:test";
import {
fastPath, middlePath, slowPath, runCyclicAudit, runVote,
Ok, Err, isOk, isErr, unwrap,
Outcome, VoteState, VotePath, ErrorCode, VortexVoteError,
default as vortexVote,
} from "./vortexVote.js";
const accept = () => ({ vote: "accept" });
const reject = () => ({ vote: "reject" });
describe("fastPath — business outcomes", () => {
test("commits when >=1/3 of stabilizers accept", () => {
const r = fastPath([accept(), accept(), accept()], [accept(), accept(), reject(), reject()]);
expect(isOk(r)).toBe(true);
expect(unwrap(r).outcome).toBe(Outcome.COMMIT);
});
test("rolls back when stabilizer acceptance is below threshold", () => {
const r = fastPath([accept(), accept(), accept()], [accept(), reject(), reject(), reject()]);
expect(unwrap(r).outcome).toBe(Outcome.ROLLBACK);
});
test("aborts (business outcome) when primary quorum fails", () => {
const r = fastPath([accept(), reject(), reject()], [accept(), accept(), accept(), accept()]);
expect(isOk(r)).toBe(true);
expect(unwrap(r).outcome).toBe(Outcome.ABORT);
});
test("threshold scales with actual stabilizer arity, not a hardcoded constant", () => {
// 6 stabilizers instead of 4 — threshold should be ceil(6/3) = 2, not fixed at 2-of-4
const r = fastPath(
[accept(), accept(), accept()],
[accept(), accept(), reject(), reject(), reject(), reject()]
);
expect(unwrap(r).details.threshold).toBe(2);
});
});
describe("fastPath — validation errors (caller bugs, not business outcomes)", () => {
test("Err when primaryVotes is not an array", () => {
const r = fastPath(undefined, [accept(), accept(), accept(), accept()]);
expect(isErr(r)).toBe(true);
expect(r.error.code).toBe(ErrorCode.INVALID_VOTE_ARRAY);
});
test("Err when a vote entry has an invalid shape", () => {
const r = fastPath([{ vote: "yolo" }, accept(), accept()], [accept(), accept(), accept(), accept()]);
expect(isErr(r)).toBe(true);
expect(r.error.code).toBe(ErrorCode.INVALID_VOTE_ENTRY);
});
test("malformed input is distinguishable from a legitimate ABORT", () => {
const malformed = fastPath(undefined, [accept(), accept(), accept(), accept()]);
const legitimate = fastPath([reject(), reject(), reject()], [accept(), accept(), accept(), accept()]);
expect(isErr(malformed)).toBe(true);
expect(isOk(legitimate)).toBe(true);
expect(unwrap(legitimate).outcome).toBe(Outcome.ABORT);
});
});
describe("middlePath", () => {
test("commits when cyclic audit passes reach threshold", () => {
const r = middlePath(
[accept(), accept(), accept()],
[accept(), accept(), accept(), accept(), accept(), reject()]
);
expect(unwrap(r).outcome).toBe(Outcome.COMMIT);
});
test("reconciles when validators are split evenly", () => {
const r = middlePath(
[accept(), accept(), accept()],
[accept(), accept(), accept(), reject(), reject(), reject()]
);
expect(unwrap(r).outcome).toBe(Outcome.RECONCILE);
});
test("Err when seedVotes is too short", () => {
const r = middlePath([accept(), accept()], [accept(), accept(), accept(), accept(), accept(), accept()]);
expect(isErr(r)).toBe(true);
expect(r.error.code).toBe(ErrorCode.INVALID_VOTE_ARRAY);
});
});
describe("runCyclicAudit — GAP-02 fix", () => {
test("without a pollFn, passes is deterministic (0 or auditCount)", () => {
const { passes, rounds } = runCyclicAudit([accept(), accept(), reject()], 9);
expect(rounds.length).toBe(9);
expect(passes === 0 || passes === 9).toBe(true);
});
test("with a pollFn, rounds can be independently pass/fail", () => {
const validators = [accept(), accept(), accept(), reject(), reject(), reject()];
const pollFn = (round, base) =>
round % 2 === 0 ? base : base.map((v, i) => (i === 0 ? reject() : v));
const { passes } = runCyclicAudit(validators, 9, pollFn);
// even rounds (5 of them: 0,2,4,6,8) pass 3-3 tie -> not > half -> fail actually;
// this just asserts independence is possible, not a specific count
expect(typeof passes).toBe("number");
expect(passes).toBeGreaterThanOrEqual(0);
expect(passes).toBeLessThanOrEqual(9);
});
});
describe("slowPath — business outcomes", () => {
test("holds when structural gates aren't all clear", () => {
const r = slowPath({ stateVotes: [VoteState.YES, VoteState.YES], gateChecks: [true, true, false, true] });
expect(unwrap(r).outcome).toBe(Outcome.HOLD);
});
test("escalates to human review when any agent votes escalate", () => {
const r = slowPath({
stateVotes: [VoteState.YES, VoteState.ESCALATE],
gateChecks: [true, true, true, true],
});
expect(unwrap(r).outcome).toBe(Outcome.HUMAN_REVIEW);
});
test("delays and audits when the entropy buffer is triggered", () => {
const r = slowPath({
stateVotes: [VoteState.YES, VoteState.YES],
gateChecks: [true, true, true, true],
entropyBufferTriggered: true,
});
expect(unwrap(r).outcome).toBe(Outcome.DELAY_AND_AUDIT);
});
test("safe-commits on majority yes", () => {
const r = slowPath({
stateVotes: [VoteState.YES, VoteState.YES, VoteState.MAYBE],
gateChecks: [true, true, true, true],
});
expect(unwrap(r).outcome).toBe(Outcome.SAFE_COMMIT);
});
test("aborts (business outcome) when no majority yes", () => {
const r = slowPath({
stateVotes: [VoteState.NO, VoteState.MAYBE, VoteState.WAIT],
gateChecks: [true, true, true, true],
});
expect(unwrap(r).outcome).toBe(Outcome.ABORT);
});
});
describe("slowPath — validation errors (caller bugs, not business outcomes)", () => {
test("Err on an unrecognized state value, instead of silently counting as not-yes", () => {
const r = slowPath({
stateVotes: [VoteState.YES, "yess"],
gateChecks: [true, true, true, true],
});
expect(isErr(r)).toBe(true);
expect(r.error.code).toBe(ErrorCode.INVALID_STATE_VALUE);
});
test("Err when gateChecks contains a non-boolean", () => {
const r = slowPath({
stateVotes: [VoteState.YES],
gateChecks: [true, true, "true", true],
});
expect(isErr(r)).toBe(true);
expect(r.error.code).toBe(ErrorCode.INVALID_GATE_ARRAY);
});
test("Err when gateChecks is too short", () => {
const r = slowPath({ stateVotes: [VoteState.YES], gateChecks: [true, true] });
expect(isErr(r)).toBe(true);
});
});
describe("runVote dispatcher", () => {
test("returns Err(VortexVoteError) for an unknown path, not a thrown exception", () => {
let threw = false;
let r;
try {
r = runVote("ludicrous-speed", {});
} catch {
threw = true;
}
expect(threw).toBe(false);
expect(isErr(r)).toBe(true);
expect(r.error.code).toBe(ErrorCode.INVALID_PATH);
});
test("dispatches correctly to each path", () => {
expect(unwrap(runVote(VotePath.FAST, {
primaryVotes: [accept(), accept(), accept()],
stabilizerVotes: [accept(), accept(), reject(), reject()],
})).outcome).toBe(Outcome.COMMIT);
expect(unwrap(runVote(VotePath.SLOW, {
stateVotes: [VoteState.YES, VoteState.YES],
gateChecks: [true, true, true, true],
})).outcome).toBe(Outcome.SAFE_COMMIT);
});
});
describe("Result helpers — Ok/Err/isOk/isErr/unwrap", () => {
test("Ok wraps a value with ok: true", () => {
expect(Ok(42)).toEqual({ ok: true, value: 42 });
});
test("Err wraps an error with ok: false", () => {
const e = new VortexVoteError(ErrorCode.INVALID_PATH, "boom");
expect(Err(e)).toEqual({ ok: false, error: e });
});
test("isOk/isErr are false for non-Result values, not throwing", () => {
expect(isOk(undefined)).toBe(false);
expect(isOk(null)).toBe(false);
expect(isOk({})).toBe(false);
expect(isErr(undefined)).toBe(false);
expect(isErr({})).toBe(false);
});
test("unwrap returns the value on Ok", () => {
expect(unwrap(Ok("value"))).toBe("value");
});
test("unwrap throws the underlying VortexVoteError instance on Err", () => {
const e = new VortexVoteError(ErrorCode.INVALID_PATH, "boom");
expect(() => unwrap(Err(e))).toThrow(e);
});
test("unwrap wraps a non-Error error value in a new Error", () => {
expect(() => unwrap(Err("plain string error"))).toThrow("plain string error");
});
});
describe("VortexVoteError — shape", () => {
test("carries name, code, context, and is a real Error", () => {
const r = fastPath(undefined, [accept(), accept(), accept(), accept()]);
expect(isErr(r)).toBe(true);
expect(r.error).toBeInstanceOf(Error);
expect(r.error).toBeInstanceOf(VortexVoteError);
expect(r.error.name).toBe("VortexVoteError");
expect(r.error.code).toBe(ErrorCode.INVALID_VOTE_ARRAY);
expect(r.error.context).toEqual({ fieldName: "primaryVotes", received: undefined });
});
});
describe("boolean vote shorthand (true/false alongside accept/reject)", () => {
test("fastPath treats {vote: true}/{vote: false} the same as accept/reject", () => {
const r = fastPath(
[{ vote: true }, { vote: true }, { vote: true }],
[{ vote: true }, { vote: true }, { vote: false }, { vote: false }]
);
expect(unwrap(r).outcome).toBe(Outcome.COMMIT);
});
});
describe("slowPath — gateChecks entries beyond index 4 are ignored", () => {
test("a non-boolean 5th entry does not invalidate the call", () => {
const r = slowPath({
stateVotes: [VoteState.YES, VoteState.YES],
gateChecks: [true, true, true, true, "garbage"],
});
expect(isOk(r)).toBe(true);
expect(unwrap(r).outcome).toBe(Outcome.SAFE_COMMIT);
});
test("entropyBufferTriggered defaults to false when omitted", () => {
const r = slowPath({
stateVotes: [VoteState.YES, VoteState.YES],
gateChecks: [true, true, true, true],
});
expect(unwrap(r).outcome).toBe(Outcome.SAFE_COMMIT);
});
});
describe("middlePath — custom passThreshold", () => {
test("a lower passThreshold can turn a RECONCILE into a COMMIT", () => {
const seeds = [accept(), accept(), accept()];
const validators = [accept(), accept(), accept(), reject(), reject(), reject()];
const reconciled = middlePath(seeds, validators);
expect(unwrap(reconciled).outcome).toBe(Outcome.RECONCILE);
const committed = middlePath(seeds, validators, { passThreshold: 0 });
expect(unwrap(committed).outcome).toBe(Outcome.COMMIT);
});
});
describe("default export", () => {
test("exposes the same functions and enums as the named exports", () => {
expect(vortexVote.fastPath).toBe(fastPath);
expect(vortexVote.runVote).toBe(runVote);
expect(vortexVote.Outcome).toBe(Outcome);
expect(vortexVote.VoteState).toBe(VoteState);
expect(vortexVote.VortexVoteError).toBe(VortexVoteError);
});
});