forked from Astrea-Payouts/astrea
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathidempotency.test.ts
More file actions
44 lines (36 loc) · 1.39 KB
/
Copy pathidempotency.test.ts
File metadata and controls
44 lines (36 loc) · 1.39 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
import { describe, expect, it } from "vitest";
import { decidePrepare, decideSubmit } from "./idempotency";
describe("decidePrepare", () => {
it("builds when there is no prior record", () => {
expect(decidePrepare(null)).toEqual({ action: "build" });
});
it("builds when the prior attempt is still pending", () => {
expect(decidePrepare({ status: "PENDING" })).toEqual({ action: "build" });
});
it("builds again when the prior attempt failed", () => {
expect(decidePrepare({ status: "FAILED" })).toEqual({ action: "build" });
});
it("short-circuits once the operation has already succeeded", () => {
expect(decidePrepare({ status: "SUCCEEDED", txHash: "abc123" })).toEqual({
action: "already-succeeded",
txHash: "abc123",
});
});
});
describe("decideSubmit", () => {
it("throws if prepareOperation never ran for this key", () => {
expect(() => decideSubmit(null)).toThrow(/prepareOperation must run/);
});
it("submits when the prior attempt is still pending", () => {
expect(decideSubmit({ status: "PENDING" })).toEqual({ action: "submit" });
});
it("allows a retry after a prior failure", () => {
expect(decideSubmit({ status: "FAILED" })).toEqual({ action: "submit" });
});
it("never resubmits an already-succeeded operation", () => {
expect(decideSubmit({ status: "SUCCEEDED", txHash: "abc123" })).toEqual({
action: "already-succeeded",
txHash: "abc123",
});
});
});