forked from Lilly-Protocol/lily-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagents.test.ts
More file actions
188 lines (157 loc) · 6.19 KB
/
Copy pathagents.test.ts
File metadata and controls
188 lines (157 loc) · 6.19 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
import type { Express } from "express";
import request from "supertest";
import { beforeEach, describe, expect, it } from "vitest";
import { createIsolatedTestApp } from "./helpers/create-test-app";
describe("agent endpoints", () => {
let app: Express;
beforeEach(async () => {
app = await createIsolatedTestApp();
});
it("exposes reset that restores the seeded agents for test isolation", async () => {
const { agentsService } =
await import("../src/modules/agents/agents.service");
agentsService.reset();
const response = await request(app).get("/api/v1/agents");
expect(response.status).toBe(200);
expect(response.body.data.total).toBe(1);
});
it("returns seeded agents so contributors can inspect a real module", async () => {
const response = await request(app).get("/api/v1/agents");
expect(response.status).toBe(200);
expect(response.body.success).toBe(true);
expect(response.body.data.total).toBe(1);
expect(response.body.data.agents[0]).toMatchObject({
id: "agentlily_demo_001",
name: "Treasury Settlement Agent",
walletAddress: expect.stringMatching(/^G[A-Z0-9]+$/),
status: "active",
});
});
it("returns a list envelope whose total matches the number of agents", async () => {
const response = await request(app).get("/api/v1/agents");
expect(response.status).toBe(200);
expect(response.body.success).toBe(true);
expect(Array.isArray(response.body.data.agents)).toBe(true);
expect(response.body.data.total).toBe(response.body.data.agents.length);
});
it("creates an agent with validated input", async () => {
const response = await request(app)
.post("/api/v1/agents")
.send({
name: "Liquidity Bot",
description:
"AgentLily responsible for orchestrating liquidity and payment workflows.",
capabilities: ["usdc-payments", "payments"],
});
expect(response.status).toBe(201);
expect(response.body.success).toBe(true);
expect(response.body.data.agent).toMatchObject({
id: "agentlily_2",
name: "Liquidity Bot",
description:
"AgentLily responsible for orchestrating liquidity and payment workflows.",
status: "active",
capabilities: ["usdc-payments", "payments"],
});
expect(response.body.data.agent.walletAddress).toMatch(/^GLIQUIDITYBOT0+/);
});
it("persists a created agent in the list endpoint", async () => {
await request(app)
.post("/api/v1/agents")
.send({
name: "Marketplace Runner",
description:
"AgentLily responsible for purchasing tools and settling marketplace invoices.",
capabilities: ["settlement", "settlement"],
});
const response = await request(app).get("/api/v1/agents");
expect(response.status).toBe(200);
expect(response.body.data.total).toBe(3);
expect(
response.body.data.agents.map((agent: { id: string }) => agent.id),
).toEqual(["agentlily_demo_001", "agentlily_2", "agentlily_3"]);
expect(response.body.data.agents[2]).toMatchObject({
name: "Marketplace Runner",
capabilities: ["settlement"],
});
});
it("rejects unknown keys in agent creation payloads", async () => {
const response = await request(app)
.post("/api/v1/agents")
.send({
name: "Treasury Bot",
description:
"AgentLily responsible for treasury management and payment routing.",
capabilities: ["treasury-management"],
admin: true,
});
expect(response.status).toBe(400);
expect(response.body.success).toBe(false);
expect(response.body.message).toBe("Request validation failed");
expect(response.body.details.fieldErrors).toMatchObject({
admin: [expect.stringContaining("Unrecognized key")],
});
});
it("rejects invalid agent payloads with typed validation errors", async () => {
const response = await request(app).post("/api/v1/agents").send({
name: "A",
description: "too short",
capabilities: [],
});
expect(response.status).toBe(400);
expect(response.body.success).toBe(false);
expect(response.body.code).toBe("VALIDATION_ERROR");
expect(response.body.message).toBe("Request validation failed");
expect(response.body.details.fieldErrors).toMatchObject({
name: [expect.any(String)],
description: [expect.any(String)],
capabilities: [expect.any(String)],
});
});
it("pauses an existing agent", async () => {
const response = await request(app)
.patch("/api/v1/agents/agentlily_demo_001")
.send({ status: "paused" });
expect(response.status).toBe(200);
expect(response.body.success).toBe(true);
expect(response.body.data.agent.id).toBe("agentlily_demo_001");
expect(response.body.data.agent.status).toBe("paused");
});
it("resumes the same agent", async () => {
await request(app)
.patch("/api/v1/agents/agentlily_demo_001")
.send({ status: "active" });
const response = await request(app)
.patch("/api/v1/agents/agentlily_demo_001")
.send({ status: "active" });
expect(response.status).toBe(200);
expect(response.body.success).toBe(true);
expect(response.body.data.agent.status).toBe("active");
});
it("rejects invalid status with 400", async () => {
const response = await request(app)
.patch("/api/v1/agents/agentlily_demo_001")
.send({ status: "disabled" });
expect(response.status).toBe(400);
expect(response.body.success).toBe(false);
expect(response.body.message).toBe("Request validation failed");
});
it("returns 404 for unknown agent ID", async () => {
const response = await request(app)
.patch("/api/v1/agents/does-not-exist")
.send({ status: "paused" });
expect(response.status).toBe(404);
expect(response.body.success).toBe(false);
});
it("persists status change in the list endpoint", async () => {
await request(app)
.patch("/api/v1/agents/agentlily_demo_001")
.send({ status: "paused" });
const response = await request(app).get("/api/v1/agents");
expect(response.status).toBe(200);
expect(response.body.data.agents[0]).toMatchObject({
id: "agentlily_demo_001",
status: "paused",
});
});
});