@axiom-lattice/local-stores 5.1.1 → 5.2.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@axiom-lattice/local-stores",
3
- "version": "5.1.1",
3
+ "version": "5.2.0",
4
4
  "description": "Local SQLite-based stores for Axiom Lattice framework",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -25,8 +25,8 @@
25
25
  "@types/sql.js": "^1.4.11",
26
26
  "sql.js": "^1.14.1",
27
27
  "uuid": "^14.0.1",
28
- "@axiom-lattice/core": "12.2.1",
29
- "@axiom-lattice/protocols": "7.2.0"
28
+ "@axiom-lattice/core": "12.3.0",
29
+ "@axiom-lattice/protocols": "7.3.0"
30
30
  },
31
31
  "devDependencies": {
32
32
  "@types/jest": "^29.5.14",
@@ -103,3 +103,104 @@ describe("LocalMcpServerConfigStore secret handling", () => {
103
103
  }
104
104
  });
105
105
  });
106
+
107
+ describe("LocalMcpServerConfigStore personal data domain", () => {
108
+ let store: LocalMcpServerConfigStore;
109
+ let db: Awaited<ReturnType<typeof initDatabase>>;
110
+
111
+ beforeEach(async () => {
112
+ db = await initDatabase({ dbPath: nextDbPath() });
113
+ store = new LocalMcpServerConfigStore(db);
114
+ });
115
+ afterEach(() => closeDatabase());
116
+
117
+ it("creates the personal table alongside the tenant table", () => {
118
+ const row = db.prepare(
119
+ "SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'lt_personal_mcp_configs'",
120
+ ).get();
121
+ expect(row).toBeDefined();
122
+ });
123
+
124
+ it("keeps a personal and a tenant config with the same key isolated", async () => {
125
+ await store.createConfig("tenant-1", "tenant-cfg", {
126
+ key: "shared",
127
+ config: { ...httpConfig },
128
+ });
129
+ await store.createConfigForAssistant("tenant-1", "pa-1", "personal-cfg", {
130
+ key: "shared",
131
+ config: { ...httpConfig, url: "https://personal.example.com/mcp" },
132
+ });
133
+
134
+ const tenantConfig = await store.getConfigByKey("tenant-1", "shared");
135
+ const personalConfig = await store.getConfigByKeyForAssistant(
136
+ "tenant-1",
137
+ "pa-1",
138
+ "shared",
139
+ );
140
+
141
+ expect(tenantConfig?.config.url).toBe("https://mcp.example.com/mcp");
142
+ expect(personalConfig?.config.url).toBe("https://personal.example.com/mcp");
143
+ expect(personalConfig?.ownerAssistantId).toBe("pa-1");
144
+
145
+ // Tenant-scoped reads never see the personal row.
146
+ expect((await store.getAllConfigs("tenant-1")).map((c) => c.id)).toEqual([
147
+ "tenant-cfg",
148
+ ]);
149
+ expect((await store.getAllConfigsWithoutTenant()).map((c) => c.id)).toEqual([
150
+ "tenant-cfg",
151
+ ]);
152
+ });
153
+
154
+ it("scopes personal reads and writes to the owning assistant", async () => {
155
+ await store.createConfigForAssistant("tenant-1", "pa-1", "cfg", {
156
+ key: "mine",
157
+ config: { ...httpConfig },
158
+ });
159
+
160
+ expect(await store.getConfigByIdForAssistant("tenant-1", "pa-2", "cfg")).toBeNull();
161
+ expect(await store.getConfigByKeyForAssistant("tenant-1", "pa-2", "mine")).toBeNull();
162
+ expect(await store.hasConfigForAssistant("tenant-1", "pa-2", "cfg")).toBe(false);
163
+ expect(await store.hasConfigForAssistant("tenant-1", "pa-1", "cfg")).toBe(true);
164
+
165
+ expect(
166
+ await store.updateConfigForAssistant("tenant-1", "pa-2", "cfg", {
167
+ name: "hijack",
168
+ }),
169
+ ).toBeNull();
170
+ });
171
+
172
+ it("encrypts personal env and headers at rest", async () => {
173
+ await store.createConfigForAssistant("tenant-1", "pa-1", "cfg", {
174
+ key: "remote",
175
+ config: { ...httpConfig, env: { MCP_TOKEN: "env-secret" } },
176
+ });
177
+
178
+ const row = db.prepare(
179
+ "SELECT config, is_env_encrypted FROM lt_personal_mcp_configs WHERE id = ?",
180
+ ).get("cfg") as unknown as { config: string; is_env_encrypted: number };
181
+ expect(row.is_env_encrypted).toBe(1);
182
+ expect(JSON.parse(row.config).env.MCP_TOKEN).not.toBe("env-secret");
183
+
184
+ const read = await store.getConfigByIdForAssistant("tenant-1", "pa-1", "cfg");
185
+ expect(read?.config.env?.MCP_TOKEN).toBe("env-secret");
186
+ });
187
+
188
+ it("deletes every personal config for one assistant only", async () => {
189
+ await store.createConfigForAssistant("tenant-1", "pa-1", "a", {
190
+ key: "a",
191
+ config: { ...httpConfig },
192
+ });
193
+ await store.createConfigForAssistant("tenant-1", "pa-1", "b", {
194
+ key: "b",
195
+ config: { ...httpConfig },
196
+ });
197
+ await store.createConfigForAssistant("tenant-1", "pa-2", "c", {
198
+ key: "c",
199
+ config: { ...httpConfig },
200
+ });
201
+
202
+ expect(await store.deleteConfigsForAssistant("tenant-1", "pa-1")).toBe(2);
203
+ expect(await store.getAllConfigsForAssistant("tenant-1", "pa-1")).toEqual([]);
204
+ expect(await store.getAllConfigsForAssistant("tenant-1", "pa-2")).toHaveLength(1);
205
+ });
206
+ });
@@ -0,0 +1,509 @@
1
+ import { afterEach, beforeEach, describe, expect, it } from "@jest/globals";
2
+ import { existsSync, unlinkSync } from "node:fs";
3
+ import { tmpdir } from "node:os";
4
+ import path from "node:path";
5
+ import type {
6
+ PlatformAuditAppendInput,
7
+ PlatformAuditInitiator,
8
+ PlatformAuditRecord,
9
+ } from "@axiom-lattice/protocols";
10
+ import { closeDatabase, initDatabase, type DatabaseWrapper } from "../database";
11
+ import { LocalPlatformAuditStore } from "../stores/LocalPlatformAuditStore";
12
+
13
+ const nextDbPath = () =>
14
+ path.join(
15
+ tmpdir(),
16
+ `lt-platform-audit-${Date.now()}-${Math.random().toString(36).slice(2)}.db`,
17
+ );
18
+
19
+ const humanInput = (
20
+ overrides: Partial<PlatformAuditAppendInput> = {},
21
+ ): PlatformAuditAppendInput => ({
22
+ initiator: { type: "human", userId: "user-1", role: "admin" },
23
+ source: "admin_center",
24
+ executor: { type: "admin_center", route: "PATCH /api/platform/settings" },
25
+ action: "write",
26
+ status: "ok",
27
+ summary: { changed: ["invite.defaultMaxUses"] },
28
+ durationMs: 12,
29
+ ...overrides,
30
+ });
31
+
32
+ interface RawRow {
33
+ id: string;
34
+ createdAt: string;
35
+ initiator?: PlatformAuditInitiator;
36
+ source?: PlatformAuditRecord["source"];
37
+ executor?: PlatformAuditRecord["executor"];
38
+ tenantId?: string;
39
+ action?: PlatformAuditRecord["action"];
40
+ status?: PlatformAuditRecord["status"];
41
+ summary?: Record<string, unknown>;
42
+ before?: Record<string, unknown>;
43
+ after?: Record<string, unknown>;
44
+ errorCode?: string;
45
+ durationMs?: number;
46
+ }
47
+
48
+ function insertRow(db: DatabaseWrapper, row: RawRow): void {
49
+ const initiator: PlatformAuditInitiator =
50
+ row.initiator ?? { type: "system", component: "test" };
51
+ const userId = initiator.type === "human" ? initiator.userId : null;
52
+ const role = initiator.type === "human" ? initiator.role : null;
53
+ db.prepare(
54
+ `INSERT INTO lattice_platform_audit
55
+ (id, initiator_user_id, initiator_role, initiator_json, source,
56
+ executor_json, tenant_id, action, status, summary_json, before_json,
57
+ after_json, error_code, duration_ms, created_at)
58
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
59
+ ).run(
60
+ row.id,
61
+ userId,
62
+ role,
63
+ JSON.stringify(initiator),
64
+ row.source ?? "system",
65
+ JSON.stringify(row.executor ?? { type: "system", component: "test" }),
66
+ row.tenantId ?? null,
67
+ row.action ?? "write",
68
+ row.status ?? "ok",
69
+ JSON.stringify(row.summary ?? {}),
70
+ row.before === undefined ? null : JSON.stringify(row.before),
71
+ row.after === undefined ? null : JSON.stringify(row.after),
72
+ row.errorCode ?? null,
73
+ row.durationMs ?? 1,
74
+ row.createdAt,
75
+ );
76
+ }
77
+
78
+ describe("LocalPlatformAuditStore append/get", () => {
79
+ let dbPath: string;
80
+ let db: DatabaseWrapper;
81
+ let store: LocalPlatformAuditStore;
82
+
83
+ beforeEach(async () => {
84
+ dbPath = nextDbPath();
85
+ db = await initDatabase({ dbPath });
86
+ store = new LocalPlatformAuditStore(db);
87
+ });
88
+
89
+ afterEach(() => {
90
+ closeDatabase();
91
+ if (dbPath && existsSync(dbPath)) unlinkSync(dbPath);
92
+ });
93
+
94
+ it("returns a persisted record with generated id and createdAt", async () => {
95
+ const input = humanInput({ tenantId: "tenant-1", errorCode: "NOPE" });
96
+ const record = await store.append(input);
97
+
98
+ expect(typeof record.id).toBe("string");
99
+ expect(record.id.length).toBeGreaterThan(0);
100
+ expect(record.createdAt).toBeInstanceOf(Date);
101
+ expect(record.initiator).toEqual(input.initiator);
102
+ expect(record.source).toBe(input.source);
103
+ expect(record.executor).toEqual(input.executor);
104
+ expect(record.tenantId).toBe("tenant-1");
105
+ expect(record.action).toBe(input.action);
106
+ expect(record.status).toBe(input.status);
107
+ expect(record.summary).toEqual(input.summary);
108
+ expect(record.errorCode).toBe("NOPE");
109
+ expect(record.durationMs).toBe(12);
110
+
111
+ await expect(store.get(record.id)).resolves.toEqual(record);
112
+ });
113
+
114
+ it("returns null for a missing id", async () => {
115
+ await expect(store.get("does-not-exist")).resolves.toBeNull();
116
+ });
117
+
118
+ it("round-trips nested JSON in initiator, executor, summary, before and after", async () => {
119
+ const input = humanInput({
120
+ initiator: { type: "human", userId: "user-7", role: "owner" },
121
+ executor: {
122
+ type: "ops_agent",
123
+ agentId: "platform-ops",
124
+ threadId: "thread-1",
125
+ runId: "run-1",
126
+ toolName: "update_platform_settings",
127
+ },
128
+ summary: { nested: { list: [1, "two", { three: true }] } },
129
+ before: { enabled: false, tags: ["a", "b"] },
130
+ after: { enabled: true, tags: [] },
131
+ });
132
+ const record = await store.append(input);
133
+ const reloaded = await store.get(record.id);
134
+
135
+ expect(reloaded).not.toBeNull();
136
+ expect(reloaded?.initiator).toEqual(input.initiator);
137
+ expect(reloaded?.executor).toEqual(input.executor);
138
+ expect(reloaded?.summary).toEqual(input.summary);
139
+ expect(reloaded?.before).toEqual(input.before);
140
+ expect(reloaded?.after).toEqual(input.after);
141
+ });
142
+
143
+ it("omits optional fields that were not provided", async () => {
144
+ const record = await store.append(humanInput());
145
+ expect(record).not.toHaveProperty("tenantId");
146
+ expect(record).not.toHaveProperty("before");
147
+ expect(record).not.toHaveProperty("after");
148
+ expect(record).not.toHaveProperty("errorCode");
149
+ });
150
+
151
+ it("isolates stored state from mutations to returned records", async () => {
152
+ const record = await store.append(
153
+ humanInput({ summary: { key: "original" } }),
154
+ );
155
+
156
+ record.summary.key = "mutated";
157
+ (record.initiator as { userId: string }).userId = "mutated";
158
+
159
+ const reloaded = await store.get(record.id);
160
+ expect(reloaded?.summary).toEqual({ key: "original" });
161
+ expect(reloaded?.initiator).toEqual({
162
+ type: "human",
163
+ userId: "user-1",
164
+ role: "admin",
165
+ });
166
+ });
167
+
168
+ it("isolates stored state from mutations to the append input", async () => {
169
+ const input = humanInput({ summary: { key: "original" } });
170
+ const record = await store.append(input);
171
+
172
+ input.summary.key = "mutated";
173
+
174
+ const reloaded = await store.get(record.id);
175
+ expect(reloaded?.summary).toEqual({ key: "original" });
176
+ });
177
+
178
+ it("normalizes durationMs to a non-negative integer", async () => {
179
+ const rounded = await store.append(humanInput({ durationMs: 4.6 }));
180
+ expect(rounded.durationMs).toBe(5);
181
+
182
+ const clamped = await store.append(humanInput({ durationMs: -3 }));
183
+ expect(clamped.durationMs).toBe(0);
184
+ });
185
+ });
186
+
187
+ describe("LocalPlatformAuditStore persistence", () => {
188
+ let dbPath: string;
189
+
190
+ afterEach(() => {
191
+ closeDatabase();
192
+ if (dbPath && existsSync(dbPath)) unlinkSync(dbPath);
193
+ });
194
+
195
+ it("survives closeDatabase and reopen with the same path", async () => {
196
+ dbPath = nextDbPath();
197
+ let db = await initDatabase({ dbPath });
198
+ let store = new LocalPlatformAuditStore(db);
199
+ const record = await store.append(
200
+ humanInput({ summary: { persisted: true } }),
201
+ );
202
+
203
+ closeDatabase();
204
+
205
+ db = await initDatabase({ dbPath });
206
+ store = new LocalPlatformAuditStore(db);
207
+ const reloaded = await store.get(record.id);
208
+
209
+ expect(reloaded).toEqual(record);
210
+ await expect(store.query({ initiatorUserId: "user-1" })).resolves.toHaveLength(
211
+ 1,
212
+ );
213
+ });
214
+
215
+ it("is idempotent against an existing database", async () => {
216
+ dbPath = nextDbPath();
217
+ const first = await initDatabase({ dbPath });
218
+ new LocalPlatformAuditStore(first);
219
+ closeDatabase();
220
+
221
+ const reopened = await initDatabase({ dbPath });
222
+ expect(() => new LocalPlatformAuditStore(reopened)).not.toThrow();
223
+ const store = new LocalPlatformAuditStore(reopened);
224
+ await expect(store.query({})).resolves.toEqual([]);
225
+ });
226
+ });
227
+
228
+ describe("LocalPlatformAuditStore query filters", () => {
229
+ let dbPath: string;
230
+ let db: DatabaseWrapper;
231
+ let store: LocalPlatformAuditStore;
232
+
233
+ beforeEach(async () => {
234
+ dbPath = nextDbPath();
235
+ db = await initDatabase({ dbPath });
236
+ store = new LocalPlatformAuditStore(db);
237
+ insertRow(db, {
238
+ id: "1000",
239
+ createdAt: "2024-01-01T00:00:00.000Z",
240
+ initiator: { type: "human", userId: "user-1", role: "admin" },
241
+ source: "admin_center",
242
+ tenantId: "tenant-1",
243
+ action: "write",
244
+ status: "ok",
245
+ });
246
+ insertRow(db, {
247
+ id: "1001",
248
+ createdAt: "2024-01-01T00:00:01.000Z",
249
+ initiator: { type: "human", userId: "user-2", role: "owner" },
250
+ source: "admin_center",
251
+ tenantId: "tenant-2",
252
+ action: "playbook",
253
+ status: "error",
254
+ });
255
+ insertRow(db, {
256
+ id: "1002",
257
+ createdAt: "2024-01-01T00:00:02.000Z",
258
+ initiator: { type: "system", component: "scheduler" },
259
+ source: "system",
260
+ tenantId: "tenant-1",
261
+ action: "write",
262
+ status: "ok",
263
+ });
264
+ insertRow(db, {
265
+ id: "1003",
266
+ createdAt: "2024-01-01T00:00:03.000Z",
267
+ initiator: { type: "bootstrap" },
268
+ source: "bootstrap",
269
+ action: "playbook",
270
+ status: "cancelled",
271
+ });
272
+ });
273
+
274
+ afterEach(() => {
275
+ closeDatabase();
276
+ if (dbPath && existsSync(dbPath)) unlinkSync(dbPath);
277
+ });
278
+
279
+ const ids = (rows: PlatformAuditRecord[]) => rows.map((row) => row.id);
280
+
281
+ it("returns everything without filters", async () => {
282
+ expect(ids(await store.query({}))).toEqual(["1003", "1002", "1001", "1000"]);
283
+ });
284
+
285
+ it("filters by human initiator user id only", async () => {
286
+ expect(ids(await store.query({ initiatorUserId: "user-1" }))).toEqual([
287
+ "1000",
288
+ ]);
289
+ expect(await store.query({ initiatorUserId: "nobody" })).toEqual([]);
290
+ });
291
+
292
+ it("keeps non-human records visible when initiatorUserId is absent", async () => {
293
+ const rows = await store.query({});
294
+ expect(rows.some((row) => row.initiator.type === "system")).toBe(true);
295
+ expect(rows.some((row) => row.initiator.type === "bootstrap")).toBe(true);
296
+ });
297
+
298
+ it("filters by source", async () => {
299
+ expect(ids(await store.query({ source: "system" }))).toEqual(["1002"]);
300
+ expect(ids(await store.query({ source: "admin_center" }))).toEqual([
301
+ "1001",
302
+ "1000",
303
+ ]);
304
+ });
305
+
306
+ it("filters by tenantId", async () => {
307
+ expect(ids(await store.query({ tenantId: "tenant-1" }))).toEqual([
308
+ "1002",
309
+ "1000",
310
+ ]);
311
+ });
312
+
313
+ it("filters by action", async () => {
314
+ expect(ids(await store.query({ action: "playbook" }))).toEqual([
315
+ "1003",
316
+ "1001",
317
+ ]);
318
+ });
319
+
320
+ it("filters by status", async () => {
321
+ expect(ids(await store.query({ status: "error" }))).toEqual(["1001"]);
322
+ });
323
+
324
+ it("combines filters", async () => {
325
+ expect(
326
+ ids(await store.query({ tenantId: "tenant-1", action: "write" })),
327
+ ).toEqual(["1002", "1000"]);
328
+ });
329
+ });
330
+
331
+ describe("LocalPlatformAuditStore query ordering and pagination", () => {
332
+ let dbPath: string;
333
+ let db: DatabaseWrapper;
334
+ let store: LocalPlatformAuditStore;
335
+
336
+ beforeEach(async () => {
337
+ dbPath = nextDbPath();
338
+ db = await initDatabase({ dbPath });
339
+ store = new LocalPlatformAuditStore(db);
340
+ });
341
+
342
+ afterEach(() => {
343
+ closeDatabase();
344
+ if (dbPath && existsSync(dbPath)) unlinkSync(dbPath);
345
+ });
346
+
347
+ it("orders by createdAt descending then id descending", async () => {
348
+ insertRow(db, { id: "b", createdAt: "2024-01-01T00:00:00.000Z" });
349
+ insertRow(db, { id: "a", createdAt: "2024-01-01T00:00:00.000Z" });
350
+ insertRow(db, { id: "c", createdAt: "2024-01-01T00:00:00.000Z" });
351
+ insertRow(db, { id: "z", createdAt: "2024-01-02T00:00:00.000Z" });
352
+
353
+ expect((await store.query({})).map((row) => row.id)).toEqual([
354
+ "z",
355
+ "c",
356
+ "b",
357
+ "a",
358
+ ]);
359
+ });
360
+
361
+ it("treats from as inclusive on createdAt", async () => {
362
+ insertRow(db, { id: "early", createdAt: "2024-01-01T00:00:00.000Z" });
363
+ insertRow(db, { id: "boundary", createdAt: "2024-01-01T00:00:01.000Z" });
364
+ insertRow(db, { id: "late", createdAt: "2024-01-01T00:00:02.000Z" });
365
+
366
+ expect(
367
+ (await store.query({ from: "2024-01-01T00:00:01.000Z" })).map(
368
+ (row) => row.id,
369
+ ),
370
+ ).toEqual(["late", "boundary"]);
371
+ });
372
+
373
+ it("treats to as exclusive on createdAt", async () => {
374
+ insertRow(db, { id: "early", createdAt: "2024-01-01T00:00:00.000Z" });
375
+ insertRow(db, { id: "boundary", createdAt: "2024-01-01T00:00:01.000Z" });
376
+ insertRow(db, { id: "late", createdAt: "2024-01-01T00:00:02.000Z" });
377
+
378
+ expect(
379
+ (await store.query({ to: "2024-01-01T00:00:01.000Z" })).map(
380
+ (row) => row.id,
381
+ ),
382
+ ).toEqual(["early"]);
383
+ });
384
+
385
+ it("applies a from/to window", async () => {
386
+ insertRow(db, { id: "early", createdAt: "2024-01-01T00:00:00.000Z" });
387
+ insertRow(db, { id: "boundary", createdAt: "2024-01-01T00:00:01.000Z" });
388
+ insertRow(db, { id: "late", createdAt: "2024-01-01T00:00:02.000Z" });
389
+
390
+ expect(
391
+ (
392
+ await store.query({
393
+ from: "2024-01-01T00:00:00.000Z",
394
+ to: "2024-01-01T00:00:02.000Z",
395
+ })
396
+ ).map((row) => row.id),
397
+ ).toEqual(["boundary", "early"]);
398
+ });
399
+
400
+ it("defaults to a limit of 100", async () => {
401
+ for (let i = 0; i < 101; i += 1) {
402
+ insertRow(db, {
403
+ id: `r-${String(i).padStart(3, "0")}`,
404
+ createdAt: "2024-01-01T00:00:00.000Z",
405
+ });
406
+ }
407
+
408
+ const rows = await store.query({});
409
+ expect(rows).toHaveLength(100);
410
+ expect(rows[0].id).toBe("r-100");
411
+ expect(rows[99].id).toBe("r-001");
412
+ });
413
+
414
+ it("honors explicit limit and offset", async () => {
415
+ for (let i = 0; i < 5; i += 1) {
416
+ insertRow(db, {
417
+ id: `r-${i}`,
418
+ createdAt: `2024-01-01T00:00:0${i}.000Z`,
419
+ });
420
+ }
421
+
422
+ const rows = await store.query({ limit: 2, offset: 1 });
423
+ expect(rows.map((row) => row.id)).toEqual(["r-3", "r-2"]);
424
+ });
425
+
426
+ it("clamps negative offset and limit to zero", async () => {
427
+ insertRow(db, { id: "a", createdAt: "2024-01-01T00:00:00.000Z" });
428
+ insertRow(db, { id: "b", createdAt: "2024-01-01T00:00:01.000Z" });
429
+
430
+ expect(await store.query({ limit: -1 })).toEqual([]);
431
+ expect((await store.query({ offset: -5 })).map((row) => row.id)).toEqual([
432
+ "b",
433
+ "a",
434
+ ]);
435
+ });
436
+
437
+ it("normalizes equivalent non-canonical boundaries", async () => {
438
+ insertRow(db, { id: "early", createdAt: "2024-01-01T00:00:00.000Z" });
439
+ insertRow(db, { id: "boundary", createdAt: "2024-01-01T00:00:01.000Z" });
440
+ insertRow(db, { id: "late", createdAt: "2024-01-01T00:00:02.000Z" });
441
+
442
+ expect(
443
+ (await store.query({ from: "2024-01-01T08:00:01+08:00" })).map(
444
+ (row) => row.id,
445
+ ),
446
+ ).toEqual(["late", "boundary"]);
447
+ expect(
448
+ (await store.query({ to: "2024-01-01T08:00:02+08:00" })).map(
449
+ (row) => row.id,
450
+ ),
451
+ ).toEqual(["boundary", "early"]);
452
+ });
453
+
454
+ it("normalizes date-only boundaries", async () => {
455
+ insertRow(db, { id: "early", createdAt: "2024-01-01T00:00:00.000Z" });
456
+ insertRow(db, { id: "late", createdAt: "2024-01-02T00:00:00.000Z" });
457
+
458
+ expect((await store.query({ from: "2024-01-01" })).map((row) => row.id)).toEqual(
459
+ ["late", "early"],
460
+ );
461
+ expect(await store.query({ to: "2024-01-01" })).toEqual([]);
462
+ });
463
+
464
+ it("ignores invalid or empty boundaries", async () => {
465
+ insertRow(db, { id: "a", createdAt: "2024-01-01T00:00:00.000Z" });
466
+ insertRow(db, { id: "b", createdAt: "2024-01-01T00:00:01.000Z" });
467
+
468
+ expect((await store.query({ from: "not-a-date" })).map((row) => row.id)).toEqual(
469
+ ["b", "a"],
470
+ );
471
+ expect((await store.query({ from: "" })).map((row) => row.id)).toEqual([
472
+ "b",
473
+ "a",
474
+ ]);
475
+ expect((await store.query({ to: "" })).map((row) => row.id)).toEqual([
476
+ "b",
477
+ "a",
478
+ ]);
479
+ expect(
480
+ (await store.query({ to: "also-not-a-date" })).map((row) => row.id),
481
+ ).toEqual(["b", "a"]);
482
+ });
483
+
484
+ it("falls back to defaults for non-finite limit and offset", async () => {
485
+ insertRow(db, { id: "a", createdAt: "2024-01-01T00:00:00.000Z" });
486
+ insertRow(db, { id: "b", createdAt: "2024-01-01T00:00:01.000Z" });
487
+
488
+ expect(await store.query({ limit: Number.NaN })).toHaveLength(2);
489
+ expect(await store.query({ limit: Number.POSITIVE_INFINITY })).toHaveLength(
490
+ 2,
491
+ );
492
+ expect(await store.query({ offset: Number.NaN })).toHaveLength(2);
493
+ expect(await store.query({ offset: Number.NEGATIVE_INFINITY })).toHaveLength(
494
+ 2,
495
+ );
496
+ });
497
+
498
+ it("truncates fractional limit and offset to integers", async () => {
499
+ for (let i = 0; i < 5; i += 1) {
500
+ insertRow(db, {
501
+ id: `r-${i}`,
502
+ createdAt: `2024-01-01T00:00:0${i}.000Z`,
503
+ });
504
+ }
505
+
506
+ const rows = await store.query({ limit: 2.9, offset: 1.9 });
507
+ expect(rows.map((row) => row.id)).toEqual(["r-3", "r-2"]);
508
+ });
509
+ });
@@ -0,0 +1,46 @@
1
+ import { afterEach, describe, expect, it } from "@jest/globals";
2
+ import { existsSync, unlinkSync } from "node:fs";
3
+ import { tmpdir } from "node:os";
4
+ import path from "node:path";
5
+ import {
6
+ PLATFORM_TENANT_ID,
7
+ PLATFORM_TENANT_PROTECTED_ERROR,
8
+ } from "@axiom-lattice/protocols";
9
+ import { closeDatabase, initDatabase } from "../database";
10
+ import { LocalUserTenantLinkStore } from "../stores/LocalUserTenantLinkStore";
11
+
12
+ const nextDbPath = () =>
13
+ path.join(
14
+ tmpdir(),
15
+ `lt-user-tenant-link-${Date.now()}-${Math.random().toString(36).slice(2)}.db`,
16
+ );
17
+
18
+ describe("LocalUserTenantLinkStore platform tenant guard", () => {
19
+ let dbPath: string;
20
+
21
+ afterEach(() => {
22
+ closeDatabase();
23
+ if (dbPath && existsSync(dbPath)) unlinkSync(dbPath);
24
+ });
25
+
26
+ it("still creates links for ordinary tenants", async () => {
27
+ dbPath = nextDbPath();
28
+ const store = new LocalUserTenantLinkStore(await initDatabase({ dbPath }));
29
+
30
+ await expect(
31
+ store.createLink({ userId: "u1", tenantId: "acme", role: "member" }),
32
+ ).resolves.toMatchObject({ userId: "u1", tenantId: "acme", role: "member" });
33
+ });
34
+
35
+ it("rejects creating a link to the hidden platform tenant", async () => {
36
+ dbPath = nextDbPath();
37
+ const store = new LocalUserTenantLinkStore(await initDatabase({ dbPath }));
38
+
39
+ await expect(
40
+ store.createLink({ userId: "u1", tenantId: PLATFORM_TENANT_ID }),
41
+ ).rejects.toMatchObject({ code: PLATFORM_TENANT_PROTECTED_ERROR });
42
+
43
+ await expect(store.getLink("u1", PLATFORM_TENANT_ID)).resolves.toBeNull();
44
+ await expect(store.hasLink("u1", PLATFORM_TENANT_ID)).resolves.toBe(false);
45
+ });
46
+ });
@@ -36,6 +36,7 @@ import { LocalPlatformInvitationStore } from "./stores/LocalPlatformInvitationSt
36
36
  import { LocalOidcIdentityClaimStore } from "./stores/LocalOidcIdentityClaimStore";
37
37
  import { LocalInviteLinkStore } from "./stores/LocalInviteLinkStore";
38
38
  import { LocalPlatformSettingsStore } from "./stores/LocalPlatformSettingsStore";
39
+ import { LocalPlatformAuditStore } from "./stores/LocalPlatformAuditStore";
39
40
  import { LocalDatabaseConfigStore } from "./stores/LocalDatabaseConfigStore";
40
41
  import { LocalConnectionStore } from "./stores/LocalConnectionStore";
41
42
  import { LocalMetricsServerConfigStore } from "./stores/LocalMetricsServerConfigStore";
@@ -97,6 +98,7 @@ export async function createLocalStoreConfig(options: LocalStoreConfigOptions =
97
98
  oidcIdentityClaim: new LocalOidcIdentityClaimStore(db),
98
99
  inviteLink: new LocalInviteLinkStore(db),
99
100
  platformSettings: new LocalPlatformSettingsStore(db),
101
+ platformAudit: new LocalPlatformAuditStore(db),
100
102
  database: new LocalDatabaseConfigStore(db),
101
103
  connection: new LocalConnectionStore(db),
102
104
  metrics: new LocalMetricsServerConfigStore(db),