@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
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LocalEvalStore CRUD coverage: projects, suites, cases, runs, results, and
|
|
3
|
+
* the project report. These complement the uniqueness/cleanup suite in
|
|
4
|
+
* LocalEvalStore.test.ts.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { afterEach, beforeEach, describe, expect, it } from "@jest/globals";
|
|
8
|
+
import { tmpdir } from "os";
|
|
9
|
+
import path from "path";
|
|
10
|
+
import { randomUUID } from "crypto";
|
|
11
|
+
|
|
12
|
+
import { closeDatabase, initDatabase, type DatabaseWrapper } from "../database";
|
|
13
|
+
import { LocalEvalStore } from "../stores/LocalEvalStore";
|
|
14
|
+
|
|
15
|
+
const nextDbPath = () =>
|
|
16
|
+
path.join(tmpdir(), `lt-eval-crud-${Date.now()}-${Math.random().toString(36).slice(2)}.db`);
|
|
17
|
+
|
|
18
|
+
describe("LocalEvalStore CRUD", () => {
|
|
19
|
+
let db: DatabaseWrapper;
|
|
20
|
+
let store: LocalEvalStore;
|
|
21
|
+
|
|
22
|
+
beforeEach(async () => {
|
|
23
|
+
db = await initDatabase({ dbPath: nextDbPath() });
|
|
24
|
+
store = new LocalEvalStore(db);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
afterEach(() => closeDatabase());
|
|
28
|
+
|
|
29
|
+
async function createProject(
|
|
30
|
+
tenant: string,
|
|
31
|
+
id: string,
|
|
32
|
+
overrides: { name?: string; description?: string } = {},
|
|
33
|
+
) {
|
|
34
|
+
return store.createProject(tenant, id, {
|
|
35
|
+
name: overrides.name ?? `eval-${id}`,
|
|
36
|
+
description: overrides.description,
|
|
37
|
+
judgeModelConfig: { model: "judge" },
|
|
38
|
+
targetServerConfig: { url: "https://target.example.com" },
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
it("round-trips a project and updates it", async () => {
|
|
43
|
+
const tenant = `eval-${randomUUID()}`;
|
|
44
|
+
const id = randomUUID();
|
|
45
|
+
|
|
46
|
+
const created = await createProject(tenant, id, { description: "first" });
|
|
47
|
+
expect(created).toMatchObject({
|
|
48
|
+
id,
|
|
49
|
+
tenantId: tenant,
|
|
50
|
+
name: `eval-${id}`,
|
|
51
|
+
description: "first",
|
|
52
|
+
concurrency: 3,
|
|
53
|
+
});
|
|
54
|
+
expect(created.judgeModelConfig).toEqual({ model: "judge" });
|
|
55
|
+
expect(created.targetServerConfig).toEqual({ url: "https://target.example.com" });
|
|
56
|
+
|
|
57
|
+
const updated = await store.updateProject(tenant, id, {
|
|
58
|
+
name: "renamed",
|
|
59
|
+
description: "second",
|
|
60
|
+
concurrency: 5,
|
|
61
|
+
reportConfig: { showDetails: true },
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
expect(updated).toMatchObject({
|
|
65
|
+
id,
|
|
66
|
+
name: "renamed",
|
|
67
|
+
description: "second",
|
|
68
|
+
concurrency: 5,
|
|
69
|
+
});
|
|
70
|
+
expect(updated!.reportConfig).toEqual({ showDetails: true });
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it("lists projects per tenant and returns absent results", async () => {
|
|
74
|
+
const tenantA = `eval-${randomUUID()}`;
|
|
75
|
+
const tenantB = `eval-${randomUUID()}`;
|
|
76
|
+
const a = await createProject(tenantA, randomUUID());
|
|
77
|
+
const b = await createProject(tenantA, randomUUID());
|
|
78
|
+
const c = await createProject(tenantB, randomUUID());
|
|
79
|
+
|
|
80
|
+
expect((await store.getProjectsByTenant(tenantA)).map((p) => p.id).sort()).toEqual(
|
|
81
|
+
[a.id, b.id].sort(),
|
|
82
|
+
);
|
|
83
|
+
expect((await store.getProjectsByTenant(tenantB)).map((p) => p.id)).toEqual([c.id]);
|
|
84
|
+
expect(await store.getProjectById(tenantB, a.id)).toBeNull();
|
|
85
|
+
expect(await store.updateProject(tenantA, randomUUID(), { name: "missing" })).toBeNull();
|
|
86
|
+
expect(await store.getProjectReport(tenantA, randomUUID())).toBeNull();
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it("round-trips a suite and counts its cases", async () => {
|
|
90
|
+
const tenant = `eval-${randomUUID()}`;
|
|
91
|
+
const projectId = randomUUID();
|
|
92
|
+
await createProject(tenant, projectId);
|
|
93
|
+
const suiteId = randomUUID();
|
|
94
|
+
|
|
95
|
+
const suite = await store.createSuite(tenant, projectId, suiteId, { name: "refunds" });
|
|
96
|
+
expect(suite).toMatchObject({ id: suiteId, tenantId: tenant, projectId, name: "refunds" });
|
|
97
|
+
expect(suite.caseCount).toBe(0);
|
|
98
|
+
|
|
99
|
+
await store.createCase(tenant, suiteId, randomUUID(), {
|
|
100
|
+
inputMessage: "I want a refund",
|
|
101
|
+
steps: [],
|
|
102
|
+
outputType: "message_content",
|
|
103
|
+
contentAssertion: "refund",
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
expect((await store.getSuiteById(tenant, suiteId))?.caseCount).toBe(1);
|
|
107
|
+
expect((await store.getSuitesByProject(tenant, projectId)).map((s) => s.id)).toEqual([suiteId]);
|
|
108
|
+
|
|
109
|
+
const updated = await store.updateSuite(tenant, suiteId, { name: "refunds-v2" });
|
|
110
|
+
expect(updated?.name).toBe("refunds-v2");
|
|
111
|
+
|
|
112
|
+
expect(await store.updateSuite(tenant, randomUUID(), { name: "x" })).toBeNull();
|
|
113
|
+
expect(await store.deleteSuite(tenant, randomUUID())).toBe(false);
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it("round-trips an eval case including JSON fields", async () => {
|
|
117
|
+
const tenant = `eval-${randomUUID()}`;
|
|
118
|
+
const projectId = randomUUID();
|
|
119
|
+
await createProject(tenant, projectId);
|
|
120
|
+
const suiteId = randomUUID();
|
|
121
|
+
await store.createSuite(tenant, projectId, suiteId, { name: "suite" });
|
|
122
|
+
const caseId = randomUUID();
|
|
123
|
+
|
|
124
|
+
const created = await store.createCase(tenant, suiteId, caseId, {
|
|
125
|
+
inputMessage: "hello",
|
|
126
|
+
inputFiles: { "a.txt": "content" },
|
|
127
|
+
steps: [{ agent_id: "agent-1", override_message: "go" }],
|
|
128
|
+
outputType: "file_content",
|
|
129
|
+
contentAssertion: "contains hello",
|
|
130
|
+
rubrics: [{ name: "accuracy", weight: 1, description: "is accurate" }],
|
|
131
|
+
interruptPolicy: { mode: "auto-approve" },
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
expect(created).toMatchObject({
|
|
135
|
+
id: caseId,
|
|
136
|
+
tenantId: tenant,
|
|
137
|
+
suiteId,
|
|
138
|
+
inputMessage: "hello",
|
|
139
|
+
outputType: "file_content",
|
|
140
|
+
contentAssertion: "contains hello",
|
|
141
|
+
inputFiles: { "a.txt": "content" },
|
|
142
|
+
steps: [{ agent_id: "agent-1", override_message: "go" }],
|
|
143
|
+
rubrics: [{ name: "accuracy", weight: 1, description: "is accurate" }],
|
|
144
|
+
interruptPolicy: { mode: "auto-approve" },
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
const updated = await store.updateCase(tenant, caseId, {
|
|
148
|
+
inputMessage: "changed",
|
|
149
|
+
interruptPolicy: { mode: "stop" },
|
|
150
|
+
});
|
|
151
|
+
expect(updated?.inputMessage).toBe("changed");
|
|
152
|
+
expect(updated?.interruptPolicy).toEqual({ mode: "stop" });
|
|
153
|
+
expect(updated?.steps).toEqual([{ agent_id: "agent-1", override_message: "go" }]);
|
|
154
|
+
|
|
155
|
+
expect((await store.getCasesBySuite(tenant, suiteId)).map((c) => c.id)).toEqual([caseId]);
|
|
156
|
+
expect((await store.getCaseById(tenant, caseId))?.inputMessage).toBe("changed");
|
|
157
|
+
expect(await store.getCaseById(tenant, randomUUID())).toBeNull();
|
|
158
|
+
expect(await store.updateCase(tenant, randomUUID(), { inputMessage: "x" })).toBeNull();
|
|
159
|
+
expect(await store.deleteCase(tenant, randomUUID())).toBe(false);
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
it("deletes a suite, case, run, and result", async () => {
|
|
163
|
+
const tenant = `eval-${randomUUID()}`;
|
|
164
|
+
const projectId = randomUUID();
|
|
165
|
+
await createProject(tenant, projectId);
|
|
166
|
+
const suiteId = randomUUID();
|
|
167
|
+
const caseId = randomUUID();
|
|
168
|
+
const runId = randomUUID();
|
|
169
|
+
const resultId = randomUUID();
|
|
170
|
+
await store.createSuite(tenant, projectId, suiteId, { name: "suite" });
|
|
171
|
+
await store.createCase(tenant, suiteId, caseId, {
|
|
172
|
+
inputMessage: "hello",
|
|
173
|
+
steps: [],
|
|
174
|
+
outputType: "message_content",
|
|
175
|
+
contentAssertion: "hello",
|
|
176
|
+
});
|
|
177
|
+
await store.createRun(tenant, projectId, runId, { concurrency: 1, totalCases: 1 });
|
|
178
|
+
await store.createRunResult(tenant, runId, resultId, {
|
|
179
|
+
suiteName: "suite",
|
|
180
|
+
caseId,
|
|
181
|
+
pass: true,
|
|
182
|
+
score: 100,
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
expect(await store.deleteRunResult(tenant, resultId)).toBe(true);
|
|
186
|
+
expect(await store.deleteRunResult(tenant, resultId)).toBe(false);
|
|
187
|
+
expect(await store.getResultsByRun(tenant, runId)).toEqual([]);
|
|
188
|
+
|
|
189
|
+
expect(await store.deleteCase(tenant, caseId)).toBe(true);
|
|
190
|
+
expect(await store.deleteCase(tenant, caseId)).toBe(false);
|
|
191
|
+
expect(await store.getCaseById(tenant, caseId)).toBeNull();
|
|
192
|
+
|
|
193
|
+
expect(await store.deleteRun(tenant, runId)).toBe(true);
|
|
194
|
+
expect(await store.deleteRun(tenant, runId)).toBe(false);
|
|
195
|
+
expect(await store.getRunById(tenant, runId)).toBeNull();
|
|
196
|
+
|
|
197
|
+
expect(await store.deleteSuite(tenant, suiteId)).toBe(true);
|
|
198
|
+
expect(await store.deleteSuite(tenant, suiteId)).toBe(false);
|
|
199
|
+
expect(await store.getSuiteById(tenant, suiteId)).toBeNull();
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
it("creates runs, updates counts, and filters listings", async () => {
|
|
203
|
+
const tenant = `eval-${randomUUID()}`;
|
|
204
|
+
const otherTenant = `eval-${randomUUID()}`;
|
|
205
|
+
const projectId = randomUUID();
|
|
206
|
+
await createProject(tenant, projectId);
|
|
207
|
+
const runId = randomUUID();
|
|
208
|
+
const otherRunId = randomUUID();
|
|
209
|
+
|
|
210
|
+
const run = await store.createRun(tenant, projectId, runId, {
|
|
211
|
+
concurrency: 2,
|
|
212
|
+
totalCases: 4,
|
|
213
|
+
holdout: true,
|
|
214
|
+
envProjectId: "env-project",
|
|
215
|
+
taskId: "task-1",
|
|
216
|
+
});
|
|
217
|
+
expect(run).toMatchObject({
|
|
218
|
+
id: runId,
|
|
219
|
+
tenantId: tenant,
|
|
220
|
+
projectId,
|
|
221
|
+
status: "running",
|
|
222
|
+
concurrency: 2,
|
|
223
|
+
totalCases: 4,
|
|
224
|
+
passedCases: 0,
|
|
225
|
+
failedCases: 0,
|
|
226
|
+
avgScore: 0,
|
|
227
|
+
holdout: true,
|
|
228
|
+
taskId: "task-1",
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
await store.createRun(otherTenant, projectId, otherRunId, { concurrency: 1, totalCases: 1 });
|
|
232
|
+
|
|
233
|
+
const completedAt = new Date("2026-05-06T07:08:09.000Z");
|
|
234
|
+
const updated = await store.updateRunStatus(tenant, runId, {
|
|
235
|
+
status: "completed",
|
|
236
|
+
passedCases: 3,
|
|
237
|
+
failedCases: 1,
|
|
238
|
+
interruptedCases: 0,
|
|
239
|
+
avgScore: 87.5,
|
|
240
|
+
completedAt,
|
|
241
|
+
});
|
|
242
|
+
expect(updated).toMatchObject({
|
|
243
|
+
status: "completed",
|
|
244
|
+
passedCases: 3,
|
|
245
|
+
failedCases: 1,
|
|
246
|
+
interruptedCases: 0,
|
|
247
|
+
avgScore: 87.5,
|
|
248
|
+
});
|
|
249
|
+
expect(updated!.completedAt?.toISOString()).toBe(completedAt.toISOString());
|
|
250
|
+
|
|
251
|
+
expect((await store.getRunsByTenant(tenant)).map((r) => r.id)).toEqual([runId]);
|
|
252
|
+
expect((await store.getRunsByTenant(tenant, { status: "completed" })).map((r) => r.id)).toEqual([
|
|
253
|
+
runId,
|
|
254
|
+
]);
|
|
255
|
+
expect((await store.getRunsByTenant(tenant, { status: "running" }))).toEqual([]);
|
|
256
|
+
expect((await store.getRunsByTenant(tenant, { projectId })).map((r) => r.id)).toEqual([runId]);
|
|
257
|
+
expect((await store.getRunsByTenant(otherTenant)).map((r) => r.id)).toEqual([otherRunId]);
|
|
258
|
+
|
|
259
|
+
expect(await store.updateRunStatus(tenant, randomUUID(), { status: "failed" })).toBeNull();
|
|
260
|
+
expect(await store.deleteRun(tenant, randomUUID())).toBe(false);
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
it("round-trips a run result and rejects a runId update", async () => {
|
|
264
|
+
const tenant = `eval-${randomUUID()}`;
|
|
265
|
+
const projectId = randomUUID();
|
|
266
|
+
await createProject(tenant, projectId);
|
|
267
|
+
const runId = randomUUID();
|
|
268
|
+
await store.createRun(tenant, projectId, runId, { concurrency: 1, totalCases: 1 });
|
|
269
|
+
const resultId = randomUUID();
|
|
270
|
+
|
|
271
|
+
const created = await store.createRunResult(tenant, runId, resultId, {
|
|
272
|
+
suiteName: "suite",
|
|
273
|
+
caseId: "case-1",
|
|
274
|
+
pass: false,
|
|
275
|
+
score: 0,
|
|
276
|
+
summary: "interrupted",
|
|
277
|
+
dimensionResults: [{ name: "accuracy", score: 0, reason: "no answer" }],
|
|
278
|
+
durationMs: 42,
|
|
279
|
+
messages: [{ role: "human", content: "hi" }],
|
|
280
|
+
logs: [{ timestamp: "2026-01-01T00:00:00.000Z", level: "info", message: "started" }],
|
|
281
|
+
interrupted: true,
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
expect(created).toMatchObject({
|
|
285
|
+
id: resultId,
|
|
286
|
+
runId,
|
|
287
|
+
suiteName: "suite",
|
|
288
|
+
caseId: "case-1",
|
|
289
|
+
pass: false,
|
|
290
|
+
score: 0,
|
|
291
|
+
interrupted: true,
|
|
292
|
+
durationMs: 42,
|
|
293
|
+
});
|
|
294
|
+
expect(created.dimensionResults).toEqual([
|
|
295
|
+
{ name: "accuracy", score: 0, reason: "no answer" },
|
|
296
|
+
]);
|
|
297
|
+
|
|
298
|
+
const updated = await store.updateRunResult(tenant, resultId, {
|
|
299
|
+
pass: true,
|
|
300
|
+
score: 100,
|
|
301
|
+
interrupted: false,
|
|
302
|
+
summary: "ok",
|
|
303
|
+
});
|
|
304
|
+
expect(updated).toMatchObject({ pass: true, score: 100, interrupted: false, summary: "ok" });
|
|
305
|
+
|
|
306
|
+
await expect(
|
|
307
|
+
store.updateRunResult(tenant, resultId, { runId: "other" }),
|
|
308
|
+
).rejects.toThrow("runId cannot be updated");
|
|
309
|
+
|
|
310
|
+
expect((await store.getResultsByRun(tenant, runId)).map((r) => r.id)).toEqual([resultId]);
|
|
311
|
+
expect(await store.updateRunResult(tenant, randomUUID(), { score: 1 })).toBeNull();
|
|
312
|
+
expect(await store.deleteRunResult(tenant, randomUUID())).toBe(false);
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
it("averages scores and takes the pass rate from the newest run", async () => {
|
|
316
|
+
const tenant = `eval-${randomUUID()}`;
|
|
317
|
+
const projectId = randomUUID();
|
|
318
|
+
await createProject(tenant, projectId, { name: "my-eval" });
|
|
319
|
+
|
|
320
|
+
const olderRun = randomUUID();
|
|
321
|
+
const newerRun = randomUUID();
|
|
322
|
+
await store.createRun(tenant, projectId, olderRun, { concurrency: 1, totalCases: 4 });
|
|
323
|
+
await store.updateRunStatus(tenant, olderRun, {
|
|
324
|
+
status: "completed",
|
|
325
|
+
passedCases: 1,
|
|
326
|
+
failedCases: 3,
|
|
327
|
+
avgScore: 0.25,
|
|
328
|
+
});
|
|
329
|
+
await store.createRun(tenant, projectId, newerRun, { concurrency: 1, totalCases: 4 });
|
|
330
|
+
await store.updateRunStatus(tenant, newerRun, {
|
|
331
|
+
status: "completed",
|
|
332
|
+
passedCases: 3,
|
|
333
|
+
failedCases: 1,
|
|
334
|
+
avgScore: 0.75,
|
|
335
|
+
});
|
|
336
|
+
db.prepare("UPDATE lt_eval_runs SET created_at = ? WHERE id = ?").run(
|
|
337
|
+
"2026-01-01T00:00:00.000Z",
|
|
338
|
+
olderRun,
|
|
339
|
+
);
|
|
340
|
+
db.prepare("UPDATE lt_eval_runs SET created_at = ? WHERE id = ?").run(
|
|
341
|
+
"2026-01-02T00:00:00.000Z",
|
|
342
|
+
newerRun,
|
|
343
|
+
);
|
|
344
|
+
|
|
345
|
+
const report = await store.getProjectReport(tenant, projectId);
|
|
346
|
+
expect(report).toMatchObject({
|
|
347
|
+
projectId,
|
|
348
|
+
projectName: "my-eval",
|
|
349
|
+
totalRuns: 2,
|
|
350
|
+
latestPassRate: 0.75,
|
|
351
|
+
avgScore: 0.5,
|
|
352
|
+
});
|
|
353
|
+
expect(report!.runs.map((r) => r.id)).toEqual([newerRun, olderRun]);
|
|
354
|
+
});
|
|
355
|
+
});
|
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LocalMetricsServerConfigStore behavior tests, mirroring the PostgreSQL
|
|
3
|
+
* metrics config integration suite: CRUD, tenant isolation, cross-tenant
|
|
4
|
+
* listing, config round-trips, and apiKey/password encryption at rest.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
// The @axiom-lattice/core barrel cannot load under jest in this package (ESM-only
|
|
8
|
+
// transitive deps), so mock it with the real encryption module — node:crypto only.
|
|
9
|
+
jest.mock("@axiom-lattice/core", () => ({
|
|
10
|
+
...jest.requireActual("../../../core/src/util/encryption"),
|
|
11
|
+
}));
|
|
12
|
+
|
|
13
|
+
import { afterEach, beforeEach, describe, expect, it } from "@jest/globals";
|
|
14
|
+
import { tmpdir } from "os";
|
|
15
|
+
import path from "path";
|
|
16
|
+
import { randomUUID } from "crypto";
|
|
17
|
+
|
|
18
|
+
import type { MetricsServerConfig } from "@axiom-lattice/protocols";
|
|
19
|
+
import { clearEncryptionKeyCache, decrypt } from "@axiom-lattice/core";
|
|
20
|
+
import { closeDatabase, initDatabase, type DatabaseWrapper } from "../database";
|
|
21
|
+
import { LocalMetricsServerConfigStore } from "../stores/LocalMetricsServerConfigStore";
|
|
22
|
+
|
|
23
|
+
const nextDbPath = () =>
|
|
24
|
+
path.join(tmpdir(), `lt-metrics-config-test-${Date.now()}-${Math.random().toString(36).slice(2)}.db`);
|
|
25
|
+
|
|
26
|
+
function config(overrides: Partial<MetricsServerConfig> = {}): MetricsServerConfig {
|
|
27
|
+
return {
|
|
28
|
+
type: "prometheus",
|
|
29
|
+
serverUrl: "https://metrics.example.com",
|
|
30
|
+
apiKey: "metrics-api-key",
|
|
31
|
+
username: "metrics-user",
|
|
32
|
+
password: "metrics-password",
|
|
33
|
+
headers: { "X-Scope": "tenant", "X-Trace": "on" },
|
|
34
|
+
timeout: 15000,
|
|
35
|
+
...overrides,
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
describe("LocalMetricsServerConfigStore", () => {
|
|
40
|
+
let db: DatabaseWrapper;
|
|
41
|
+
let store: LocalMetricsServerConfigStore;
|
|
42
|
+
|
|
43
|
+
beforeEach(async () => {
|
|
44
|
+
db = await initDatabase({ dbPath: nextDbPath() });
|
|
45
|
+
store = new LocalMetricsServerConfigStore(db);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
afterEach(() => {
|
|
49
|
+
closeDatabase();
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
function tenantId(): string {
|
|
53
|
+
return `metrics-config-${randomUUID()}`;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
async function createConfig(tenant: string) {
|
|
57
|
+
const id = randomUUID();
|
|
58
|
+
const key = `key-${randomUUID()}`;
|
|
59
|
+
const entry = await store.createConfig(tenant, id, {
|
|
60
|
+
key,
|
|
61
|
+
name: "Prometheus",
|
|
62
|
+
description: "Primary metrics server",
|
|
63
|
+
config: config(),
|
|
64
|
+
});
|
|
65
|
+
return { id, key, entry };
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function rawConfig(tenant: string, id: string): MetricsServerConfig {
|
|
69
|
+
const row = db
|
|
70
|
+
.prepare("SELECT config FROM lt_metrics_configs WHERE tenant_id = ? AND id = ?")
|
|
71
|
+
.get(tenant, id) as { config: string } | undefined;
|
|
72
|
+
return JSON.parse(row!.config) as MetricsServerConfig;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
it("encrypts apiKey and password at rest and decrypts them on read", async () => {
|
|
76
|
+
const tenant = tenantId();
|
|
77
|
+
const { id } = await createConfig(tenant);
|
|
78
|
+
|
|
79
|
+
const stored = rawConfig(tenant, id);
|
|
80
|
+
expect(stored.apiKey).not.toBe("metrics-api-key");
|
|
81
|
+
expect(stored.password).not.toBe("metrics-password");
|
|
82
|
+
expect(decrypt(stored.apiKey!)).toBe("metrics-api-key");
|
|
83
|
+
expect(decrypt(stored.password!)).toBe("metrics-password");
|
|
84
|
+
|
|
85
|
+
const read = await store.getConfigById(tenant, id);
|
|
86
|
+
expect(read?.config.apiKey).toBe("metrics-api-key");
|
|
87
|
+
expect(read?.config.password).toBe("metrics-password");
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it("reads an undecryptable secret back as an empty string", async () => {
|
|
91
|
+
const previous = process.env.LATTICE_ENCRYPTION_KEY;
|
|
92
|
+
try {
|
|
93
|
+
process.env.LATTICE_ENCRYPTION_KEY = "metrics-key-one";
|
|
94
|
+
clearEncryptionKeyCache();
|
|
95
|
+
const tenant = tenantId();
|
|
96
|
+
const { id } = await createConfig(tenant);
|
|
97
|
+
|
|
98
|
+
process.env.LATTICE_ENCRYPTION_KEY = "metrics-key-two";
|
|
99
|
+
clearEncryptionKeyCache();
|
|
100
|
+
|
|
101
|
+
const read = await store.getConfigById(tenant, id);
|
|
102
|
+
expect(read?.config.apiKey).toBe("");
|
|
103
|
+
expect(read?.config.password).toBe("");
|
|
104
|
+
expect(read?.config.serverUrl).toBe("https://metrics.example.com");
|
|
105
|
+
expect(read?.config.username).toBe("metrics-user");
|
|
106
|
+
|
|
107
|
+
const listed = await store.getAllConfigs(tenant);
|
|
108
|
+
expect(listed).toHaveLength(1);
|
|
109
|
+
expect(listed[0].config.apiKey).toBe("");
|
|
110
|
+
} finally {
|
|
111
|
+
if (previous === undefined) delete process.env.LATTICE_ENCRYPTION_KEY;
|
|
112
|
+
else process.env.LATTICE_ENCRYPTION_KEY = previous;
|
|
113
|
+
clearEncryptionKeyCache();
|
|
114
|
+
}
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it("preserves legacy plaintext secrets on read", async () => {
|
|
118
|
+
const tenant = tenantId();
|
|
119
|
+
const id = randomUUID();
|
|
120
|
+
const key = `key-${randomUUID()}`;
|
|
121
|
+
const legacyConfig = config({ apiKey: "legacy-plain-key", password: "legacy-plain-pass" });
|
|
122
|
+
|
|
123
|
+
db.prepare(
|
|
124
|
+
`INSERT INTO lt_metrics_configs
|
|
125
|
+
(id, tenant_id, key, name, description, config, created_at, updated_at)
|
|
126
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
127
|
+
).run(
|
|
128
|
+
id,
|
|
129
|
+
tenant,
|
|
130
|
+
key,
|
|
131
|
+
"Legacy",
|
|
132
|
+
"Stored before encryption existed",
|
|
133
|
+
JSON.stringify(legacyConfig),
|
|
134
|
+
"2026-01-01T00:00:00.000Z",
|
|
135
|
+
"2026-01-01T00:00:00.000Z",
|
|
136
|
+
);
|
|
137
|
+
|
|
138
|
+
const byId = await store.getConfigById(tenant, id);
|
|
139
|
+
expect(byId?.config.apiKey).toBe("legacy-plain-key");
|
|
140
|
+
expect(byId?.config.password).toBe("legacy-plain-pass");
|
|
141
|
+
|
|
142
|
+
const listed = await store.getAllConfigs(tenant);
|
|
143
|
+
expect(listed).toHaveLength(1);
|
|
144
|
+
expect(listed[0].config.apiKey).toBe("legacy-plain-key");
|
|
145
|
+
expect(listed[0].config.password).toBe("legacy-plain-pass");
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
it("creates and reads a config by id and by key", async () => {
|
|
149
|
+
const tenant = tenantId();
|
|
150
|
+
const { id, key, entry } = await createConfig(tenant);
|
|
151
|
+
|
|
152
|
+
expect(entry).toMatchObject({
|
|
153
|
+
id,
|
|
154
|
+
tenantId: tenant,
|
|
155
|
+
key,
|
|
156
|
+
name: "Prometheus",
|
|
157
|
+
description: "Primary metrics server",
|
|
158
|
+
});
|
|
159
|
+
expect(entry.config.serverUrl).toBe("https://metrics.example.com");
|
|
160
|
+
|
|
161
|
+
const byId = await store.getConfigById(tenant, id);
|
|
162
|
+
expect(byId?.id).toBe(id);
|
|
163
|
+
expect(byId?.config.timeout).toBe(15000);
|
|
164
|
+
|
|
165
|
+
const byKey = await store.getConfigByKey(tenant, key);
|
|
166
|
+
expect(byKey?.id).toBe(id);
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
it("round-trips nested headers and optional auth fields", async () => {
|
|
170
|
+
const tenant = tenantId();
|
|
171
|
+
const { id } = await createConfig(tenant);
|
|
172
|
+
|
|
173
|
+
const read = await store.getConfigById(tenant, id);
|
|
174
|
+
expect(read?.config.headers).toEqual({ "X-Scope": "tenant", "X-Trace": "on" });
|
|
175
|
+
expect(read?.config.apiKey).toBe("metrics-api-key");
|
|
176
|
+
expect(read?.config.password).toBe("metrics-password");
|
|
177
|
+
expect(read?.config.username).toBe("metrics-user");
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
it("lists a tenant's configs and includes them in the cross-tenant listing", async () => {
|
|
181
|
+
const tenant = tenantId();
|
|
182
|
+
const first = await createConfig(tenant);
|
|
183
|
+
const second = await createConfig(tenant);
|
|
184
|
+
|
|
185
|
+
const listed = await store.getAllConfigs(tenant);
|
|
186
|
+
expect(listed.map((entry) => entry.id).sort()).toEqual([first.id, second.id].sort());
|
|
187
|
+
|
|
188
|
+
const all = await store.getAllConfigsWithoutTenant();
|
|
189
|
+
const ids = all.map((entry) => entry.id);
|
|
190
|
+
expect(ids).toContain(first.id);
|
|
191
|
+
expect(ids).toContain(second.id);
|
|
192
|
+
expect(all.find((entry) => entry.id === first.id)?.config.serverUrl).toBe(
|
|
193
|
+
"https://metrics.example.com",
|
|
194
|
+
);
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
it("updates fields and replaces the config", async () => {
|
|
198
|
+
const tenant = tenantId();
|
|
199
|
+
const { id, key } = await createConfig(tenant);
|
|
200
|
+
|
|
201
|
+
const updated = await store.updateConfig(tenant, id, {
|
|
202
|
+
name: "Renamed",
|
|
203
|
+
description: "Updated description",
|
|
204
|
+
config: config({ type: "semantic", serverUrl: "https://semantic.example.com", timeout: 5000 }),
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
expect(updated).toMatchObject({
|
|
208
|
+
id,
|
|
209
|
+
key,
|
|
210
|
+
name: "Renamed",
|
|
211
|
+
description: "Updated description",
|
|
212
|
+
});
|
|
213
|
+
expect(updated?.config.type).toBe("semantic");
|
|
214
|
+
expect(updated?.config.serverUrl).toBe("https://semantic.example.com");
|
|
215
|
+
expect(updated?.config.timeout).toBe(5000);
|
|
216
|
+
|
|
217
|
+
const stored = rawConfig(tenant, id);
|
|
218
|
+
expect(stored.apiKey).not.toBe("metrics-api-key");
|
|
219
|
+
expect(decrypt(stored.apiKey!)).toBe("metrics-api-key");
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
it("keeps the existing config when a partial update omits it", async () => {
|
|
223
|
+
const tenant = tenantId();
|
|
224
|
+
const { id } = await createConfig(tenant);
|
|
225
|
+
|
|
226
|
+
const updated = await store.updateConfig(tenant, id, { name: "Only renamed" });
|
|
227
|
+
|
|
228
|
+
expect(updated?.name).toBe("Only renamed");
|
|
229
|
+
expect(updated?.config).toEqual(config());
|
|
230
|
+
|
|
231
|
+
const stored = rawConfig(tenant, id);
|
|
232
|
+
expect(stored.apiKey).not.toBe("metrics-api-key");
|
|
233
|
+
expect(stored.password).not.toBe("metrics-password");
|
|
234
|
+
expect(decrypt(stored.apiKey!)).toBe("metrics-api-key");
|
|
235
|
+
expect(decrypt(stored.password!)).toBe("metrics-password");
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
it("reports has and deletes a config", async () => {
|
|
239
|
+
const tenant = tenantId();
|
|
240
|
+
const { id } = await createConfig(tenant);
|
|
241
|
+
|
|
242
|
+
expect(await store.hasConfig(tenant, id)).toBe(true);
|
|
243
|
+
expect(await store.deleteConfig(tenant, id)).toBe(true);
|
|
244
|
+
expect(await store.hasConfig(tenant, id)).toBe(false);
|
|
245
|
+
expect(await store.getConfigById(tenant, id)).toBeNull();
|
|
246
|
+
expect(await store.deleteConfig(tenant, id)).toBe(false);
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
it("isolates configs by tenant", async () => {
|
|
250
|
+
const tenantA = tenantId();
|
|
251
|
+
const tenantB = tenantId();
|
|
252
|
+
const key = `shared-${randomUUID()}`;
|
|
253
|
+
const idA = randomUUID();
|
|
254
|
+
const idB = randomUUID();
|
|
255
|
+
|
|
256
|
+
await store.createConfig(tenantA, idA, { key, config: config() });
|
|
257
|
+
await store.createConfig(tenantB, idB, { key, config: config({ serverUrl: "https://b.example.com" }) });
|
|
258
|
+
|
|
259
|
+
expect(await store.getConfigById(tenantA, idB)).toBeNull();
|
|
260
|
+
const byKey = await store.getConfigByKey(tenantA, key);
|
|
261
|
+
expect(byKey?.id).toBe(idA);
|
|
262
|
+
expect(byKey?.config.serverUrl).toBe("https://metrics.example.com");
|
|
263
|
+
expect((await store.getAllConfigs(tenantA)).map((entry) => entry.id)).toEqual([idA]);
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
it("returns absent results for an unknown config", async () => {
|
|
267
|
+
const tenant = tenantId();
|
|
268
|
+
const missing = randomUUID();
|
|
269
|
+
|
|
270
|
+
expect(await store.getConfigById(tenant, missing)).toBeNull();
|
|
271
|
+
expect(await store.getConfigByKey(tenant, "missing")).toBeNull();
|
|
272
|
+
expect(await store.updateConfig(tenant, missing, { name: "x" })).toBeNull();
|
|
273
|
+
expect(await store.hasConfig(tenant, missing)).toBe(false);
|
|
274
|
+
expect(await store.deleteConfig(tenant, missing)).toBe(false);
|
|
275
|
+
});
|
|
276
|
+
});
|