@axiom-lattice/local-stores 5.0.0 → 5.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +10 -10
- package/CHANGELOG.md +172 -0
- package/dist/index.d.mts +2 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +46 -21
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +66 -41
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
- package/src/__tests__/LocalAssistantStore.test.ts +235 -0
- package/src/__tests__/LocalDatabaseConfigStore.test.ts +194 -0
- package/src/__tests__/LocalEvalStore.crud.test.ts +355 -0
- package/src/__tests__/LocalMetricsServerConfigStore.test.ts +276 -0
- package/src/__tests__/LocalModelProviderStore.test.ts +203 -0
- package/src/__tests__/LocalScheduleStorage.crud.test.ts +304 -0
- package/src/__tests__/LocalSkillStore.test.ts +284 -0
- package/src/__tests__/LocalWorkflowTrackingStore.test.ts +510 -0
- package/src/stores/LocalMetricsServerConfigStore.ts +35 -3
- package/src/stores/LocalScheduleStorage.ts +6 -1
- package/src/stores/LocalWorkflowTrackingStore.ts +13 -3
|
@@ -62,4 +62,207 @@ describe("LocalModelProviderStore secret handling", () => {
|
|
|
62
62
|
clearEncryptionKeyCache();
|
|
63
63
|
}
|
|
64
64
|
});
|
|
65
|
+
|
|
66
|
+
describe("catalog behavior", () => {
|
|
67
|
+
const tenant = "tenant-a";
|
|
68
|
+
|
|
69
|
+
async function seedProvider(
|
|
70
|
+
id: string,
|
|
71
|
+
overrides: Partial<Parameters<LocalModelProviderStore["createProvider"]>[2]> = {},
|
|
72
|
+
) {
|
|
73
|
+
return store.createProvider(tenant, id, {
|
|
74
|
+
name: `provider-${id}`,
|
|
75
|
+
displayName: `Provider ${id}`,
|
|
76
|
+
protocol: "openai-compatible",
|
|
77
|
+
baseURL: `https://${id}.example.com/v1`,
|
|
78
|
+
apiKey: "sk-secret-abcd",
|
|
79
|
+
...overrides,
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
it("creates a provider with an encrypted key and its hint", async () => {
|
|
84
|
+
const created = await seedProvider("p1");
|
|
85
|
+
|
|
86
|
+
expect(created).toMatchObject({
|
|
87
|
+
id: "p1",
|
|
88
|
+
tenantId: tenant,
|
|
89
|
+
name: "provider-p1",
|
|
90
|
+
displayName: "Provider p1",
|
|
91
|
+
protocol: "openai-compatible",
|
|
92
|
+
apiStyle: "chat-completions",
|
|
93
|
+
llmProvider: "openai",
|
|
94
|
+
baseURL: "https://p1.example.com/v1",
|
|
95
|
+
apiKey: "sk-secret-abcd",
|
|
96
|
+
apiKeyHint: "abcd",
|
|
97
|
+
enabled: true,
|
|
98
|
+
status: "unknown",
|
|
99
|
+
});
|
|
100
|
+
expect(created.createdAt).toBeInstanceOf(Date);
|
|
101
|
+
expect(created.updatedAt).toBeInstanceOf(Date);
|
|
102
|
+
|
|
103
|
+
expect(await store.getProviderById(tenant, "p1")).toEqual(created);
|
|
104
|
+
expect(await store.getProviderByName(tenant, "provider-p1")).toEqual(created);
|
|
105
|
+
expect((await store.listProviders(tenant)).map((p) => p.id)).toEqual(["p1"]);
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it("supports keyless providers and a responses api style", async () => {
|
|
109
|
+
const created = await seedProvider("p2", {
|
|
110
|
+
apiKey: undefined,
|
|
111
|
+
apiStyle: "responses",
|
|
112
|
+
enabled: false,
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
expect(created.apiKey).toBeUndefined();
|
|
116
|
+
expect(created.apiKeyHint).toBeUndefined();
|
|
117
|
+
expect(created.apiStyle).toBe("responses");
|
|
118
|
+
expect(created.enabled).toBe(false);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it("returns absent results for an unknown provider", async () => {
|
|
122
|
+
expect(await store.getProviderById(tenant, "missing")).toBeNull();
|
|
123
|
+
expect(await store.getProviderByName(tenant, "missing")).toBeNull();
|
|
124
|
+
expect(await store.updateProvider(tenant, "missing", { name: "x" })).toBeNull();
|
|
125
|
+
expect(await store.deleteProvider(tenant, "missing")).toBe(false);
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it("updates fields while keeping an omitted key and clearing on request", async () => {
|
|
129
|
+
await seedProvider("p1");
|
|
130
|
+
|
|
131
|
+
const updated = await store.updateProvider(tenant, "p1", {
|
|
132
|
+
name: "renamed",
|
|
133
|
+
displayName: "Renamed",
|
|
134
|
+
baseURL: "https://new.example.com/v1",
|
|
135
|
+
apiStyle: "responses",
|
|
136
|
+
enabled: false,
|
|
137
|
+
});
|
|
138
|
+
expect(updated).toMatchObject({
|
|
139
|
+
name: "renamed",
|
|
140
|
+
displayName: "Renamed",
|
|
141
|
+
baseURL: "https://new.example.com/v1",
|
|
142
|
+
apiStyle: "responses",
|
|
143
|
+
enabled: false,
|
|
144
|
+
apiKey: "sk-secret-abcd",
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
const kept = await store.updateProvider(tenant, "p1", { apiKey: "" });
|
|
148
|
+
expect(kept?.apiKey).toBe("sk-secret-abcd");
|
|
149
|
+
|
|
150
|
+
const cleared = await store.updateProvider(tenant, "p1", { clearApiKey: true });
|
|
151
|
+
expect(cleared?.apiKey).toBeUndefined();
|
|
152
|
+
expect(cleared?.apiKeyHint).toBeUndefined();
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
it("records discovery status and clears the error on success", async () => {
|
|
156
|
+
await seedProvider("p1");
|
|
157
|
+
const discoveredAt = new Date("2026-04-05T06:07:08.000Z");
|
|
158
|
+
|
|
159
|
+
await store.markProviderStatus(tenant, "p1", "error", "UPSTREAM_UNREACHABLE");
|
|
160
|
+
let read = await store.getProviderById(tenant, "p1");
|
|
161
|
+
expect(read?.status).toBe("error");
|
|
162
|
+
expect(read?.lastError).toBe("UPSTREAM_UNREACHABLE");
|
|
163
|
+
|
|
164
|
+
await store.markProviderStatus(tenant, "p1", "ok", undefined, discoveredAt);
|
|
165
|
+
read = await store.getProviderById(tenant, "p1");
|
|
166
|
+
expect(read?.status).toBe("ok");
|
|
167
|
+
expect(read?.lastError).toBeUndefined();
|
|
168
|
+
expect(read?.lastDiscoveredAt?.toISOString()).toBe(discoveredAt.toISOString());
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it("isolates providers by tenant, including the same id and name", async () => {
|
|
172
|
+
await seedProvider("p1");
|
|
173
|
+
await store.createProvider("tenant-b", "p1", {
|
|
174
|
+
name: "provider-p1",
|
|
175
|
+
protocol: "openai-compatible",
|
|
176
|
+
baseURL: "https://b.example.com/v1",
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
expect((await store.getProviderById(tenant, "p1"))?.baseURL).toBe(
|
|
180
|
+
"https://p1.example.com/v1",
|
|
181
|
+
);
|
|
182
|
+
expect((await store.getProviderById("tenant-b", "p1"))?.baseURL).toBe(
|
|
183
|
+
"https://b.example.com/v1",
|
|
184
|
+
);
|
|
185
|
+
expect(await store.listProviders(tenant)).toHaveLength(1);
|
|
186
|
+
|
|
187
|
+
expect(await store.deleteProvider(tenant, "p1")).toBe(true);
|
|
188
|
+
expect(await store.getProviderById(tenant, "p1")).toBeNull();
|
|
189
|
+
expect(await store.getProviderById("tenant-b", "p1")).not.toBeNull();
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
it("replaces the model snapshot, preserves enabled/kind, and drops missing models", async () => {
|
|
193
|
+
await seedProvider("p1");
|
|
194
|
+
|
|
195
|
+
const first = await store.replaceProviderModels(tenant, "p1", [
|
|
196
|
+
{ id: "m1", displayName: "Model One", kind: "embedding" },
|
|
197
|
+
{ id: "m2" },
|
|
198
|
+
]);
|
|
199
|
+
expect(first).toEqual(["m1", "m2"]);
|
|
200
|
+
expect((await store.listModels(tenant, "p1")).map((m) => m.modelId)).toEqual(["m1", "m2"]);
|
|
201
|
+
|
|
202
|
+
const disabled = await store.setModelEnabled(tenant, "p1", "m1", false);
|
|
203
|
+
expect(disabled?.enabled).toBe(false);
|
|
204
|
+
|
|
205
|
+
const second = await store.replaceProviderModels(tenant, "p1", [
|
|
206
|
+
{ id: "m1", displayName: "Model One" },
|
|
207
|
+
{ id: "m3" },
|
|
208
|
+
]);
|
|
209
|
+
expect(second).toEqual(["m1", "m3"]);
|
|
210
|
+
|
|
211
|
+
const models = await store.listModels(tenant, "p1");
|
|
212
|
+
expect(models.map((m) => m.modelId)).toEqual(["m1", "m3"]);
|
|
213
|
+
expect(models[0]).toMatchObject({ enabled: false, kind: "embedding" });
|
|
214
|
+
expect(models[1]).toMatchObject({ enabled: true, kind: "chat" });
|
|
215
|
+
expect(await store.getModel(tenant, "p1", "m2")).toBeNull();
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
it("de-duplicates repeated model ids and clears the snapshot for an empty list", async () => {
|
|
219
|
+
await seedProvider("p1");
|
|
220
|
+
|
|
221
|
+
const ids = await store.replaceProviderModels(tenant, "p1", [
|
|
222
|
+
{ id: "m1", displayName: "First" },
|
|
223
|
+
{ id: "m1", displayName: "Last" },
|
|
224
|
+
]);
|
|
225
|
+
expect(ids).toEqual(["m1"]);
|
|
226
|
+
expect((await store.listModels(tenant, "p1"))[0].displayName).toBe("Last");
|
|
227
|
+
|
|
228
|
+
expect(await store.replaceProviderModels(tenant, "p1", [])).toEqual([]);
|
|
229
|
+
expect(await store.listModels(tenant, "p1")).toEqual([]);
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
it("returns null when enabling an absent model", async () => {
|
|
233
|
+
expect(await store.setModelEnabled(tenant, "p1", "missing", true)).toBeNull();
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
it("isolates model snapshots by tenant", async () => {
|
|
237
|
+
await store.createProvider(tenant, "p1", {
|
|
238
|
+
name: "p",
|
|
239
|
+
protocol: "openai-compatible",
|
|
240
|
+
baseURL: "https://a.example.com/v1",
|
|
241
|
+
});
|
|
242
|
+
await store.createProvider("tenant-b", "p1", {
|
|
243
|
+
name: "p",
|
|
244
|
+
protocol: "openai-compatible",
|
|
245
|
+
baseURL: "https://b.example.com/v1",
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
await store.replaceProviderModels(tenant, "p1", [{ id: "shared" }]);
|
|
249
|
+
await store.replaceProviderModels("tenant-b", "p1", [{ id: "shared" }, { id: "b-only" }]);
|
|
250
|
+
|
|
251
|
+
expect((await store.listModels(tenant, "p1")).map((m) => m.modelId)).toEqual(["shared"]);
|
|
252
|
+
expect((await store.listModels("tenant-b", "p1")).map((m) => m.modelId)).toEqual([
|
|
253
|
+
"b-only",
|
|
254
|
+
"shared",
|
|
255
|
+
]);
|
|
256
|
+
expect((await store.listModels(tenant)).map((m) => m.modelId)).toEqual(["shared"]);
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
it("deletes a provider's models with it", async () => {
|
|
260
|
+
await seedProvider("p1");
|
|
261
|
+
await store.replaceProviderModels(tenant, "p1", [{ id: "m1" }]);
|
|
262
|
+
|
|
263
|
+
expect(await store.deleteProvider(tenant, "p1")).toBe(true);
|
|
264
|
+
expect(await store.getProviderById(tenant, "p1")).toBeNull();
|
|
265
|
+
expect(await store.listModels(tenant, "p1")).toEqual([]);
|
|
266
|
+
});
|
|
267
|
+
});
|
|
65
268
|
});
|
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LocalScheduleStorage CRUD/query coverage. The claim/requeue suite lives in
|
|
3
|
+
* LocalScheduleStorage.test.ts; this file exercises persistence, updates,
|
|
4
|
+
* filtered listings, pagination, and retention.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { afterEach, beforeEach, describe, expect, it } from "@jest/globals";
|
|
8
|
+
import { tmpdir } from "os";
|
|
9
|
+
import path from "path";
|
|
10
|
+
|
|
11
|
+
import {
|
|
12
|
+
ScheduleExecutionType,
|
|
13
|
+
type ScheduledTaskDefinition,
|
|
14
|
+
ScheduledTaskStatus,
|
|
15
|
+
} from "@axiom-lattice/protocols";
|
|
16
|
+
|
|
17
|
+
import { closeDatabase, initDatabase, type DatabaseWrapper } from "../database";
|
|
18
|
+
import { LocalScheduleStorage } from "../stores/LocalScheduleStorage";
|
|
19
|
+
|
|
20
|
+
const nextDbPath = () =>
|
|
21
|
+
path.join(tmpdir(), `lt-schedule-crud-${Date.now()}-${Math.random().toString(36).slice(2)}.db`);
|
|
22
|
+
|
|
23
|
+
function buildTask(
|
|
24
|
+
taskId: string,
|
|
25
|
+
overrides: Partial<ScheduledTaskDefinition> = {},
|
|
26
|
+
): ScheduledTaskDefinition {
|
|
27
|
+
const now = Date.now();
|
|
28
|
+
return {
|
|
29
|
+
taskId,
|
|
30
|
+
taskType: "test",
|
|
31
|
+
payload: {},
|
|
32
|
+
tenantId: "tenant-1",
|
|
33
|
+
executionType: ScheduleExecutionType.ONCE,
|
|
34
|
+
executeAt: now + 60_000,
|
|
35
|
+
status: ScheduledTaskStatus.PENDING,
|
|
36
|
+
runCount: 0,
|
|
37
|
+
retryCount: 0,
|
|
38
|
+
maxRetries: 0,
|
|
39
|
+
createdAt: now,
|
|
40
|
+
updatedAt: now,
|
|
41
|
+
...overrides,
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
describe("LocalScheduleStorage CRUD", () => {
|
|
46
|
+
let db: DatabaseWrapper;
|
|
47
|
+
let store: LocalScheduleStorage;
|
|
48
|
+
|
|
49
|
+
beforeEach(async () => {
|
|
50
|
+
db = await initDatabase({ dbPath: nextDbPath() });
|
|
51
|
+
store = new LocalScheduleStorage(db);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
afterEach(() => closeDatabase());
|
|
55
|
+
|
|
56
|
+
it("round-trips every persistable field", async () => {
|
|
57
|
+
const executeAt = Date.now() + 120_000;
|
|
58
|
+
const nextRunAt = Date.now() + 180_000;
|
|
59
|
+
const lastRunAt = Date.now() - 60_000;
|
|
60
|
+
const expiresAt = Date.now() + 600_000;
|
|
61
|
+
|
|
62
|
+
await store.save(
|
|
63
|
+
buildTask("task-1", {
|
|
64
|
+
taskType: "report",
|
|
65
|
+
payload: { query: "select 1", nested: { values: [1, 2] } },
|
|
66
|
+
tenantId: "tenant-9",
|
|
67
|
+
assistantId: "assistant-1",
|
|
68
|
+
threadId: "thread-1",
|
|
69
|
+
executionType: ScheduleExecutionType.CRON,
|
|
70
|
+
executeAt,
|
|
71
|
+
delayMs: 5_000,
|
|
72
|
+
cronExpression: "0 9 * * *",
|
|
73
|
+
timezone: "Asia/Shanghai",
|
|
74
|
+
nextRunAt,
|
|
75
|
+
lastRunAt,
|
|
76
|
+
status: ScheduledTaskStatus.PAUSED,
|
|
77
|
+
runCount: 4,
|
|
78
|
+
maxRuns: 10,
|
|
79
|
+
retryCount: 1,
|
|
80
|
+
maxRetries: 3,
|
|
81
|
+
lastError: "transient",
|
|
82
|
+
expiresAt,
|
|
83
|
+
metadata: { owner: "ops", tags: ["a", "b"] },
|
|
84
|
+
}),
|
|
85
|
+
);
|
|
86
|
+
|
|
87
|
+
const read = await store.get("task-1");
|
|
88
|
+
expect(read).toMatchObject({
|
|
89
|
+
taskId: "task-1",
|
|
90
|
+
taskType: "report",
|
|
91
|
+
tenantId: "tenant-9",
|
|
92
|
+
assistantId: "assistant-1",
|
|
93
|
+
threadId: "thread-1",
|
|
94
|
+
executionType: ScheduleExecutionType.CRON,
|
|
95
|
+
executeAt,
|
|
96
|
+
delayMs: 5_000,
|
|
97
|
+
cronExpression: "0 9 * * *",
|
|
98
|
+
timezone: "Asia/Shanghai",
|
|
99
|
+
nextRunAt,
|
|
100
|
+
lastRunAt,
|
|
101
|
+
status: ScheduledTaskStatus.PAUSED,
|
|
102
|
+
runCount: 4,
|
|
103
|
+
maxRuns: 10,
|
|
104
|
+
retryCount: 1,
|
|
105
|
+
maxRetries: 3,
|
|
106
|
+
lastError: "transient",
|
|
107
|
+
expiresAt,
|
|
108
|
+
metadata: { owner: "ops", tags: ["a", "b"] },
|
|
109
|
+
});
|
|
110
|
+
expect(read!.payload).toEqual({ query: "select 1", nested: { values: [1, 2] } });
|
|
111
|
+
expect(await store.get("missing")).toBeNull();
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it("upserts on save and updates fields in place", async () => {
|
|
115
|
+
await store.save(buildTask("task-1"));
|
|
116
|
+
await store.save(
|
|
117
|
+
buildTask("task-1", {
|
|
118
|
+
status: ScheduledTaskStatus.COMPLETED,
|
|
119
|
+
payload: { attempt: 2 },
|
|
120
|
+
runCount: 2,
|
|
121
|
+
}),
|
|
122
|
+
);
|
|
123
|
+
|
|
124
|
+
const upserted = await store.get("task-1");
|
|
125
|
+
expect(upserted).toMatchObject({ status: ScheduledTaskStatus.COMPLETED, runCount: 2 });
|
|
126
|
+
expect(upserted!.payload).toEqual({ attempt: 2 });
|
|
127
|
+
|
|
128
|
+
await store.update("task-1", {
|
|
129
|
+
status: ScheduledTaskStatus.FAILED,
|
|
130
|
+
lastError: "boom",
|
|
131
|
+
assistantId: "assistant-2",
|
|
132
|
+
metadata: { retried: true },
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
const updated = await store.get("task-1");
|
|
136
|
+
expect(updated).toMatchObject({
|
|
137
|
+
status: ScheduledTaskStatus.FAILED,
|
|
138
|
+
lastError: "boom",
|
|
139
|
+
assistantId: "assistant-2",
|
|
140
|
+
});
|
|
141
|
+
expect(updated!.metadata).toEqual({ retried: true });
|
|
142
|
+
expect(updated!.payload).toEqual({ attempt: 2 });
|
|
143
|
+
|
|
144
|
+
// Updating an absent task is a no-op rather than an insert.
|
|
145
|
+
await store.update("missing", { status: ScheduledTaskStatus.FAILED });
|
|
146
|
+
expect(await store.get("missing")).toBeNull();
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it("lists by execution type, assistant, thread, and active status", async () => {
|
|
150
|
+
const base = Date.now();
|
|
151
|
+
await store.save(
|
|
152
|
+
buildTask("once", {
|
|
153
|
+
taskType: "alpha",
|
|
154
|
+
executionType: ScheduleExecutionType.ONCE,
|
|
155
|
+
assistantId: "a1",
|
|
156
|
+
threadId: "t1",
|
|
157
|
+
createdAt: base,
|
|
158
|
+
updatedAt: base,
|
|
159
|
+
}),
|
|
160
|
+
);
|
|
161
|
+
await store.save(
|
|
162
|
+
buildTask("cron", {
|
|
163
|
+
taskType: "alpha",
|
|
164
|
+
executionType: ScheduleExecutionType.CRON,
|
|
165
|
+
assistantId: "a1",
|
|
166
|
+
threadId: "t2",
|
|
167
|
+
status: ScheduledTaskStatus.PAUSED,
|
|
168
|
+
createdAt: base + 1,
|
|
169
|
+
updatedAt: base + 1,
|
|
170
|
+
}),
|
|
171
|
+
);
|
|
172
|
+
await store.save(
|
|
173
|
+
buildTask("done", {
|
|
174
|
+
taskType: "beta",
|
|
175
|
+
executionType: ScheduleExecutionType.CRON,
|
|
176
|
+
assistantId: "a2",
|
|
177
|
+
threadId: "t1",
|
|
178
|
+
status: ScheduledTaskStatus.COMPLETED,
|
|
179
|
+
createdAt: base + 2,
|
|
180
|
+
updatedAt: base + 2,
|
|
181
|
+
}),
|
|
182
|
+
);
|
|
183
|
+
|
|
184
|
+
expect((await store.getTasksByExecutionType(ScheduleExecutionType.ONCE)).map((t) => t.taskId)).toEqual([
|
|
185
|
+
"once",
|
|
186
|
+
]);
|
|
187
|
+
expect(
|
|
188
|
+
(await store.getTasksByExecutionType(ScheduleExecutionType.CRON)).map((t) => t.taskId).sort(),
|
|
189
|
+
).toEqual(["cron", "done"]);
|
|
190
|
+
expect((await store.getTasksByAssistantId("a1")).map((t) => t.taskId).sort()).toEqual([
|
|
191
|
+
"cron",
|
|
192
|
+
"once",
|
|
193
|
+
]);
|
|
194
|
+
expect((await store.getTasksByThreadId("t1")).map((t) => t.taskId).sort()).toEqual([
|
|
195
|
+
"done",
|
|
196
|
+
"once",
|
|
197
|
+
]);
|
|
198
|
+
expect((await store.getTasksByType("alpha")).map((t) => t.taskId).sort()).toEqual([
|
|
199
|
+
"cron",
|
|
200
|
+
"once",
|
|
201
|
+
]);
|
|
202
|
+
expect((await store.getTasksByType("beta")).map((t) => t.taskId)).toEqual(["done"]);
|
|
203
|
+
expect((await store.getTasksByStatus(ScheduledTaskStatus.PAUSED)).map((t) => t.taskId)).toEqual([
|
|
204
|
+
"cron",
|
|
205
|
+
]);
|
|
206
|
+
expect(
|
|
207
|
+
(await store.getTasksByStatus(ScheduledTaskStatus.COMPLETED)).map((t) => t.taskId),
|
|
208
|
+
).toEqual(["done"]);
|
|
209
|
+
|
|
210
|
+
// Active = pending or paused, oldest first.
|
|
211
|
+
expect((await store.getActiveTasks()).map((t) => t.taskId)).toEqual(["once", "cron"]);
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
it("filters, counts, and paginates getAllTasks", async () => {
|
|
215
|
+
const base = Date.now();
|
|
216
|
+
for (let i = 0; i < 4; i += 1) {
|
|
217
|
+
await store.save(
|
|
218
|
+
buildTask(`task-${i}`, {
|
|
219
|
+
taskType: i % 2 === 0 ? "even" : "odd",
|
|
220
|
+
tenantId: i < 3 ? "tenant-a" : "tenant-b",
|
|
221
|
+
status: i === 0 ? ScheduledTaskStatus.COMPLETED : ScheduledTaskStatus.PENDING,
|
|
222
|
+
createdAt: base + i,
|
|
223
|
+
updatedAt: base + i,
|
|
224
|
+
}),
|
|
225
|
+
);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
const tenantA = await store.getAllTasks({ tenantId: "tenant-a" });
|
|
229
|
+
expect(tenantA.map((t) => t.taskId).sort()).toEqual(["task-0", "task-1", "task-2"]);
|
|
230
|
+
|
|
231
|
+
expect((await store.getAllTasks({ taskType: "even" })).map((t) => t.taskId).sort()).toEqual([
|
|
232
|
+
"task-0",
|
|
233
|
+
"task-2",
|
|
234
|
+
]);
|
|
235
|
+
expect(
|
|
236
|
+
(await store.getAllTasks({ status: ScheduledTaskStatus.COMPLETED })).map((t) => t.taskId),
|
|
237
|
+
).toEqual(["task-0"]);
|
|
238
|
+
|
|
239
|
+
expect(await store.countTasks()).toBe(4);
|
|
240
|
+
expect(await store.countTasks({ tenantId: "tenant-a" })).toBe(3);
|
|
241
|
+
expect(await store.countTasks({ status: ScheduledTaskStatus.PENDING })).toBe(3);
|
|
242
|
+
expect(await store.countTasks({ tenantId: "tenant-b", taskType: "odd" })).toBe(1);
|
|
243
|
+
|
|
244
|
+
// ORDER BY created_at DESC: task-3 first.
|
|
245
|
+
const firstPage = await store.getAllTasks({ limit: 2, offset: 0 });
|
|
246
|
+
const secondPage = await store.getAllTasks({ limit: 2, offset: 2 });
|
|
247
|
+
expect(firstPage.map((t) => t.taskId)).toEqual(["task-3", "task-2"]);
|
|
248
|
+
expect(secondPage.map((t) => t.taskId)).toEqual(["task-1", "task-0"]);
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
it("applies an offset without a limit", async () => {
|
|
252
|
+
const base = Date.now();
|
|
253
|
+
await store.save(buildTask("oldest", { createdAt: base, updatedAt: base }));
|
|
254
|
+
await store.save(buildTask("newest", { createdAt: base + 1, updatedAt: base + 1 }));
|
|
255
|
+
|
|
256
|
+
const results = await store.getAllTasks({ offset: 1 });
|
|
257
|
+
|
|
258
|
+
expect(results.map((t) => t.taskId)).toEqual(["oldest"]);
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
it("deletes tasks and prunes old terminal tasks only", async () => {
|
|
262
|
+
const now = Date.now();
|
|
263
|
+
const old = now - 120_000;
|
|
264
|
+
|
|
265
|
+
await store.save(
|
|
266
|
+
buildTask("completed-old", {
|
|
267
|
+
status: ScheduledTaskStatus.COMPLETED,
|
|
268
|
+
createdAt: old,
|
|
269
|
+
updatedAt: old,
|
|
270
|
+
}),
|
|
271
|
+
);
|
|
272
|
+
await store.save(
|
|
273
|
+
buildTask("failed-old", {
|
|
274
|
+
status: ScheduledTaskStatus.FAILED,
|
|
275
|
+
createdAt: old,
|
|
276
|
+
updatedAt: old,
|
|
277
|
+
}),
|
|
278
|
+
);
|
|
279
|
+
await store.save(
|
|
280
|
+
buildTask("pending-old", {
|
|
281
|
+
status: ScheduledTaskStatus.PENDING,
|
|
282
|
+
createdAt: old,
|
|
283
|
+
updatedAt: old,
|
|
284
|
+
}),
|
|
285
|
+
);
|
|
286
|
+
await store.save(
|
|
287
|
+
buildTask("completed-new", {
|
|
288
|
+
status: ScheduledTaskStatus.COMPLETED,
|
|
289
|
+
createdAt: now,
|
|
290
|
+
updatedAt: now,
|
|
291
|
+
}),
|
|
292
|
+
);
|
|
293
|
+
|
|
294
|
+
expect(await store.deleteOldTasks(60_000)).toBe(2);
|
|
295
|
+
expect(await store.get("completed-old")).toBeNull();
|
|
296
|
+
expect(await store.get("failed-old")).toBeNull();
|
|
297
|
+
expect(await store.get("pending-old")).not.toBeNull();
|
|
298
|
+
expect(await store.get("completed-new")).not.toBeNull();
|
|
299
|
+
|
|
300
|
+
await store.delete("pending-old");
|
|
301
|
+
expect(await store.get("pending-old")).toBeNull();
|
|
302
|
+
await expect(store.delete("missing")).resolves.toBeUndefined();
|
|
303
|
+
});
|
|
304
|
+
});
|