-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathdata-set-lifecycle.service.spec.ts
More file actions
348 lines (291 loc) · 13.3 KB
/
Copy pathdata-set-lifecycle.service.spec.ts
File metadata and controls
348 lines (291 loc) · 13.3 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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { DataSetLifecycleCheckMetrics } from "../metrics-prometheus/check-metrics.service.js";
import type { StorageProviderRepository } from "../providers/repositories/storage-provider.repository.js";
import { WalletSdkService } from "../wallet-sdk/wallet-sdk.service.js";
import { DataSetLifecycleService } from "./data-set-lifecycle.service.js";
vi.mock("@filoz/synapse-core/sp", () => ({
createDataSet: vi.fn(),
waitForCreateDataSet: vi.fn(),
uploadPieceStreaming: vi.fn(),
findPiece: vi.fn(),
createDataSetAndAddPieces: vi.fn(),
waitForCreateDataSetAddPieces: vi.fn(),
terminateService: vi.fn(),
waitForTerminateService: vi.fn(),
}));
const {
createDataSet,
waitForCreateDataSet,
uploadPieceStreaming,
findPiece,
createDataSetAndAddPieces,
waitForCreateDataSetAddPieces,
terminateService,
waitForTerminateService,
} = await import("@filoz/synapse-core/sp");
const mockClient = { account: { address: "0xwallet" } };
const mockPieceCid = { toString: () => "baga6ea4seaqao7s73y24kcutaosvacpdjgfe5pw76ooefnyqw4ynr3d2y6x2mpq" };
const mockProviderInfo = {
id: 1n,
name: "test-sp",
isApproved: true,
serviceProvider: "0xsp" as `0x${string}`,
payee: "0xpayee" as `0x${string}`,
pdp: { serviceURL: "https://sp.example.com" },
};
const mockWalletSdkService = {
getSynapseClient: vi.fn(() => mockClient),
} as unknown as WalletSdkService;
const mockStorageProviderRepository = {
findByAddress: vi.fn(() => mockProviderInfo),
} as unknown as StorageProviderRepository;
const mockMetrics = {
observeCheckDuration: vi.fn(),
recordStatus: vi.fn(),
} as unknown as DataSetLifecycleCheckMetrics;
function setupEmptyVariantMocks() {
vi.mocked(createDataSet).mockResolvedValue({ txHash: "0xhash1", statusUrl: "https://sp.example.com/status/1" });
vi.mocked(waitForCreateDataSet).mockResolvedValue({
dataSetId: 42n,
dataSetCreated: true,
txStatus: "confirmed",
ok: true,
createMessageHash: "0xmsg",
service: "https://sp.example.com",
});
}
function setupWithPiecesVariantMocks() {
vi.mocked(uploadPieceStreaming).mockResolvedValue({ pieceCid: mockPieceCid as any, size: 256 });
vi.mocked(findPiece).mockResolvedValue(mockPieceCid as any);
vi.mocked(createDataSetAndAddPieces).mockResolvedValue({
txHash: "0xhash2",
statusUrl: "https://sp.example.com/status/2",
});
vi.mocked(waitForCreateDataSetAddPieces).mockResolvedValue({
hash: "0xhash2",
dataSetId: 77n,
piecesIds: [1n],
});
}
function setupTerminateMocks() {
vi.mocked(terminateService).mockResolvedValue({ statusUrl: "https://sp.example.com/terminate/status" });
vi.mocked(waitForTerminateService).mockResolvedValue({
terminationTxHash: "0xterminate",
txStatus: "confirmed",
txSuccess: true,
fwssTerminated: true,
serviceTerminationEpoch: 100n,
} as any);
}
function setupAllVariantMocks() {
setupEmptyVariantMocks();
setupWithPiecesVariantMocks();
setupTerminateMocks();
}
describe("DataSetLifecycleService", () => {
let service: DataSetLifecycleService;
beforeEach(() => {
vi.clearAllMocks();
service = new DataSetLifecycleService(mockWalletSdkService, mockStorageProviderRepository, mockMetrics);
});
afterEach(() => {
vi.restoreAllMocks();
});
// ─── Happy path: both variants run in parallel ─────────────────────────────
it("runs both variants in parallel, records success for each, and resolves", async () => {
setupAllVariantMocks();
await service.runLifecycleCheck("0xsp", "calibration", { dealbotLifecycleCheck: "nonce-123" });
// empty variant
expect(createDataSet).toHaveBeenCalledWith(
mockClient,
expect.objectContaining({
cdn: false,
payee: "0xpayee",
serviceURL: "https://sp.example.com",
metadata: { dealbotLifecycleCheck: "nonce-123" },
}),
);
expect(waitForCreateDataSet).toHaveBeenCalledWith(
expect.objectContaining({ statusUrl: "https://sp.example.com/status/1" }),
);
// with-pieces variant
expect(uploadPieceStreaming).toHaveBeenCalledWith(
expect.objectContaining({ serviceURL: "https://sp.example.com" }),
);
expect(findPiece).toHaveBeenCalledWith(
expect.objectContaining({ serviceURL: "https://sp.example.com", pieceCid: mockPieceCid, poll: true }),
);
expect(createDataSetAndAddPieces).toHaveBeenCalledWith(
mockClient,
expect.objectContaining({
cdn: false,
payee: "0xpayee",
serviceURL: "https://sp.example.com",
pieces: [expect.objectContaining({ pieceCid: mockPieceCid })],
metadata: { dealbotLifecycleCheck: "nonce-123" },
}),
);
expect(waitForCreateDataSetAddPieces).toHaveBeenCalledWith(
expect.objectContaining({ statusUrl: "https://sp.example.com/status/2" }),
);
// terminateService (provider-relayed) called for both data sets, each followed by a status poll
expect(terminateService).toHaveBeenCalledTimes(2);
expect(terminateService).toHaveBeenCalledWith(
mockClient,
expect.objectContaining({ dataSetId: 42n, serviceURL: "https://sp.example.com" }),
);
expect(terminateService).toHaveBeenCalledWith(
mockClient,
expect.objectContaining({ dataSetId: 77n, serviceURL: "https://sp.example.com" }),
);
expect(waitForTerminateService).toHaveBeenCalledTimes(2);
expect(waitForTerminateService).toHaveBeenCalledWith(
expect.objectContaining({ statusUrl: "https://sp.example.com/terminate/status" }),
);
expect(mockMetrics.recordStatus).toHaveBeenCalledOnce();
expect(mockMetrics.recordStatus).toHaveBeenCalledWith(
expect.objectContaining({ checkType: "dataSetLifecycleCheck" }),
"success",
);
expect(mockMetrics.observeCheckDuration).toHaveBeenCalledOnce();
});
// ─── Abort before any work ─────────────────────────────────────────────────
it("records failure.timedout for both variants when signal is aborted before any call", async () => {
const controller = new AbortController();
controller.abort(new Error("job timeout"));
await expect(
service.runLifecycleCheck("0xsp", "calibration", { dealbotLifecycleCheck: "nonce-abort" }, controller.signal),
).rejects.toThrow();
expect(createDataSet).not.toHaveBeenCalled();
expect(uploadPieceStreaming).not.toHaveBeenCalled();
expect(mockMetrics.recordStatus).toHaveBeenCalledOnce();
expect(mockMetrics.recordStatus).toHaveBeenCalledWith(
expect.objectContaining({ checkType: "dataSetLifecycleCheck" }),
"failure.timedout",
);
});
// ─── Dependency outages: partial failures are not swallowed ───────────────
it("throws when empty variant fails even if with-pieces variant succeeds", async () => {
setupWithPiecesVariantMocks();
setupTerminateMocks();
vi.mocked(createDataSet).mockRejectedValue(new Error("empty variant: SP unreachable"));
await expect(
service.runLifecycleCheck("0xsp", "calibration", { dealbotLifecycleCheck: "nonce-partial-1" }),
).rejects.toThrow("empty variant: SP unreachable");
expect(mockMetrics.recordStatus).toHaveBeenCalledOnce();
expect(mockMetrics.recordStatus).toHaveBeenCalledWith(
expect.objectContaining({ checkType: "dataSetLifecycleCheck" }),
"failure.other",
);
});
it("throws when with-pieces variant fails even if empty variant succeeds", async () => {
setupEmptyVariantMocks();
setupTerminateMocks();
vi.mocked(uploadPieceStreaming).mockRejectedValue(new Error("with-pieces variant: upload failed"));
await expect(
service.runLifecycleCheck("0xsp", "calibration", { dealbotLifecycleCheck: "nonce-partial-2" }),
).rejects.toThrow("with-pieces variant: upload failed");
expect(mockMetrics.recordStatus).toHaveBeenCalledOnce();
expect(mockMetrics.recordStatus).toHaveBeenCalledWith(
expect.objectContaining({ checkType: "dataSetLifecycleCheck" }),
"failure.other",
);
});
it("throws AggregateError when both variants fail", async () => {
vi.mocked(createDataSet).mockRejectedValue(new Error("empty failed"));
vi.mocked(uploadPieceStreaming).mockRejectedValue(new Error("with-pieces failed"));
const error = await service
.runLifecycleCheck("0xsp", "calibration", { dealbotLifecycleCheck: "nonce-both-fail" })
.catch((e) => e);
expect(error).toBeInstanceOf(AggregateError);
expect((error as AggregateError).errors).toHaveLength(2);
expect(mockMetrics.recordStatus).toHaveBeenCalledOnce();
expect(mockMetrics.recordStatus).toHaveBeenCalledWith(
expect.objectContaining({ checkType: "dataSetLifecycleCheck" }),
"failure.other",
);
});
// ─── Individual variant failure cases ─────────────────────────────────────
it("records failure.other for empty variant when createDataSet rejects", async () => {
setupWithPiecesVariantMocks();
setupTerminateMocks();
vi.mocked(createDataSet).mockRejectedValue(new Error("SP unreachable"));
await expect(
service.runLifecycleCheck("0xsp", "calibration", { dealbotLifecycleCheck: "nonce-e-1" }),
).rejects.toThrow("SP unreachable");
expect(terminateService).toHaveBeenCalledOnce(); // only with-pieces succeeded
expect(mockMetrics.recordStatus).toHaveBeenCalledOnce();
expect(mockMetrics.recordStatus).toHaveBeenCalledWith(
expect.objectContaining({ checkType: "dataSetLifecycleCheck" }),
"failure.other",
);
});
it("records failure.other for empty variant when termination fails after creation", async () => {
setupAllVariantMocks();
vi.mocked(terminateService)
.mockRejectedValueOnce(new Error("terminate failed"))
.mockResolvedValueOnce({ statusUrl: "https://sp.example.com/terminate/status" });
await expect(
service.runLifecycleCheck("0xsp", "calibration", { dealbotLifecycleCheck: "nonce-e-2" }),
).rejects.toThrow("terminate failed");
expect(mockMetrics.recordStatus).toHaveBeenCalledOnce();
expect(mockMetrics.recordStatus).toHaveBeenCalledWith(
expect.objectContaining({ checkType: "dataSetLifecycleCheck" }),
"failure.other",
);
});
it("records failure.other for with-pieces variant when findPiece rejects", async () => {
setupEmptyVariantMocks();
setupTerminateMocks();
vi.mocked(uploadPieceStreaming).mockResolvedValue({ pieceCid: mockPieceCid as any, size: 256 });
vi.mocked(findPiece).mockRejectedValue(new Error("piece not found"));
await expect(
service.runLifecycleCheck("0xsp", "calibration", { dealbotLifecycleCheck: "nonce-wp-1" }),
).rejects.toThrow("piece not found");
expect(createDataSetAndAddPieces).not.toHaveBeenCalled();
expect(mockMetrics.recordStatus).toHaveBeenCalledOnce();
expect(mockMetrics.recordStatus).toHaveBeenCalledWith(
expect.objectContaining({ checkType: "dataSetLifecycleCheck" }),
"failure.other",
);
});
it("records failure.other for with-pieces variant when createDataSetAndAddPieces rejects", async () => {
setupEmptyVariantMocks();
setupTerminateMocks();
vi.mocked(uploadPieceStreaming).mockResolvedValue({ pieceCid: mockPieceCid as any, size: 256 });
vi.mocked(findPiece).mockResolvedValue(mockPieceCid as any);
vi.mocked(createDataSetAndAddPieces).mockRejectedValue(new Error("on-chain create failed"));
await expect(
service.runLifecycleCheck("0xsp", "calibration", { dealbotLifecycleCheck: "nonce-wp-2" }),
).rejects.toThrow("on-chain create failed");
expect(waitForCreateDataSetAddPieces).not.toHaveBeenCalled();
expect(mockMetrics.recordStatus).toHaveBeenCalledOnce();
expect(mockMetrics.recordStatus).toHaveBeenCalledWith(
expect.objectContaining({ checkType: "dataSetLifecycleCheck" }),
"failure.other",
);
});
it("records failure.other for with-pieces variant when termination fails after creation", async () => {
setupAllVariantMocks();
vi.mocked(terminateService)
.mockResolvedValueOnce({ statusUrl: "https://sp.example.com/terminate/status" })
.mockRejectedValueOnce(new Error("terminate failed"));
await expect(
service.runLifecycleCheck("0xsp", "calibration", { dealbotLifecycleCheck: "nonce-wp-3" }),
).rejects.toThrow("terminate failed");
expect(mockMetrics.recordStatus).toHaveBeenCalledOnce();
expect(mockMetrics.recordStatus).toHaveBeenCalledWith(
expect.objectContaining({ checkType: "dataSetLifecycleCheck" }),
"failure.other",
);
});
// ─── Shared pre-flight guards ─────────────────────────────────────────────
it("throws when provider is not found in registry", async () => {
vi.mocked(mockStorageProviderRepository.findByAddress).mockResolvedValueOnce(undefined);
await expect(service.runLifecycleCheck("0xunknown", "calibration", {})).rejects.toThrow("not found in registry");
});
it("throws when synapse client is not initialized", async () => {
vi.mocked(mockWalletSdkService.getSynapseClient).mockReturnValueOnce(null);
await expect(service.runLifecycleCheck("0xsp", "calibration", {})).rejects.toThrow("not initialized");
});
});