forked from Lilly-Protocol/lily-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathidempotency.test.ts
More file actions
118 lines (96 loc) · 3.12 KB
/
Copy pathidempotency.test.ts
File metadata and controls
118 lines (96 loc) · 3.12 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
import request from "supertest";
import { beforeEach, describe, expect, it } from "vitest";
import { createApp } from "../src/app";
import { clearIdempotencyStore } from "../src/common/http/idempotency.middleware";
import { agentsService } from "../src/modules/agents/agents.service";
const app = createApp();
describe("Idempotency-Key middleware", () => {
beforeEach(() => {
clearIdempotencyStore();
agentsService.reset();
});
it("returns the original agent on replay with same key", async () => {
const payload = {
name: "Test Agent",
description: "A test agent for idempotency checks",
capabilities: ["testing"],
};
const first = await request(app)
.post("/api/v1/agents")
.set("Idempotency-Key", "key-001")
.send(payload)
.expect(201);
const second = await request(app)
.post("/api/v1/agents")
.set("Idempotency-Key", "key-001")
.send(payload)
.expect(201);
expect(second.body).toEqual(first.body);
expect(second.body.data.agent.id).toBe(first.body.data.agent.id);
const list = await request(app).get("/api/v1/agents").expect(200);
expect(list.body.data.total).toBe(2);
});
it("creates separate agents when no key is provided", async () => {
const payload = {
name: "No Key Agent",
description: "An agent without idempotency key header",
capabilities: ["testing"],
};
const first = await request(app)
.post("/api/v1/agents")
.send(payload)
.expect(201);
const second = await request(app)
.post("/api/v1/agents")
.send(payload)
.expect(201);
expect(second.body.data.agent.id).not.toBe(first.body.data.agent.id);
const list = await request(app).get("/api/v1/agents").expect(200);
expect(list.body.data.total).toBe(3);
});
it("creates separate agents with different keys", async () => {
const payload = {
name: "Diff Key Agent",
description: "An agent with different idempotency keys",
capabilities: ["testing"],
};
const first = await request(app)
.post("/api/v1/agents")
.set("Idempotency-Key", "key-alpha")
.send(payload)
.expect(201);
const second = await request(app)
.post("/api/v1/agents")
.set("Idempotency-Key", "key-beta")
.send(payload)
.expect(201);
expect(second.body.data.agent.id).not.toBe(first.body.data.agent.id);
});
it("does not cache error responses", async () => {
const badPayload = {
name: "x",
description: "too short",
capabilities: [],
};
await request(app)
.post("/api/v1/agents")
.set("Idempotency-Key", "key-err")
.send(badPayload)
.expect(400);
await request(app)
.post("/api/v1/agents")
.set("Idempotency-Key", "key-err")
.send(badPayload)
.expect(400);
});
it("ignores idempotency on GET requests (no caching)", async () => {
await request(app)
.get("/api/v1/agents")
.set("Idempotency-Key", "key-get")
.expect(200);
await request(app)
.get("/api/v1/agents")
.set("Idempotency-Key", "key-get")
.expect(200);
});
});