@axiom-lattice/local-stores 5.0.0 → 5.0.2

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.0.0",
3
+ "version": "5.0.2",
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.0.0",
29
- "@axiom-lattice/protocols": "7.0.0"
28
+ "@axiom-lattice/core": "12.1.0",
29
+ "@axiom-lattice/protocols": "7.1.0"
30
30
  },
31
31
  "devDependencies": {
32
32
  "@types/jest": "^29.5.14",
@@ -0,0 +1,235 @@
1
+ /**
2
+ * LocalAssistantStore behavior tests, mirroring the PostgreSQL assistant
3
+ * integration suite: CRUD, ownership lookup, tenant isolation, and
4
+ * graphDefinition/metadata round-trips.
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 { LocalAssistantStore } from "../stores/LocalAssistantStore";
14
+
15
+ const nextDbPath = () =>
16
+ path.join(tmpdir(), `lt-assistant-test-${Date.now()}-${Math.random().toString(36).slice(2)}.db`);
17
+
18
+ function graphDefinition(label: string) {
19
+ return {
20
+ entryPoint: "orchestrator",
21
+ nodes: [
22
+ { id: "orchestrator", type: "agent", metadata: { tags: ["a", "b"], nested: { level: 2 } } },
23
+ { id: "worker", type: "tool", metadata: { disabled: false, note: null } },
24
+ ],
25
+ edges: [{ from: "orchestrator", to: "worker", condition: null }],
26
+ metadata: { label, flags: { beta: true } },
27
+ };
28
+ }
29
+
30
+ describe("LocalAssistantStore", () => {
31
+ let db: DatabaseWrapper;
32
+ let store: LocalAssistantStore;
33
+
34
+ beforeEach(async () => {
35
+ db = await initDatabase({ dbPath: nextDbPath() });
36
+ store = new LocalAssistantStore(db);
37
+ });
38
+
39
+ afterEach(() => {
40
+ closeDatabase();
41
+ });
42
+
43
+ function tenantId(): string {
44
+ return `assistant-${randomUUID()}`;
45
+ }
46
+
47
+ it("creates an assistant and reads it back by id", async () => {
48
+ const tenant = tenantId();
49
+ const id = randomUUID();
50
+
51
+ const created = await store.createAssistant(tenant, id, {
52
+ name: "Planner",
53
+ description: "Plans work",
54
+ ownerUserId: "user-1",
55
+ graphDefinition: graphDefinition("v1"),
56
+ });
57
+
58
+ expect(created).toMatchObject({
59
+ id,
60
+ tenantId: tenant,
61
+ name: "Planner",
62
+ description: "Plans work",
63
+ ownerUserId: "user-1",
64
+ });
65
+ expect(created.graphDefinition).toEqual(graphDefinition("v1"));
66
+ expect(created.createdAt).toBeInstanceOf(Date);
67
+ expect(created.updatedAt).toBeInstanceOf(Date);
68
+
69
+ const read = await store.getAssistantById(tenant, id);
70
+ expect(read).toMatchObject({
71
+ id,
72
+ tenantId: tenant,
73
+ name: "Planner",
74
+ description: "Plans work",
75
+ ownerUserId: "user-1",
76
+ });
77
+ expect(read!.graphDefinition.metadata).toEqual({ label: "v1", flags: { beta: true } });
78
+ });
79
+
80
+ it("lists only the requested tenant's assistants", async () => {
81
+ const tenantA = tenantId();
82
+ const tenantB = tenantId();
83
+ const a1 = randomUUID();
84
+ const a2 = randomUUID();
85
+ const b1 = randomUUID();
86
+ await store.createAssistant(tenantA, a1, { name: "A1", graphDefinition: graphDefinition("a1") });
87
+ await store.createAssistant(tenantA, a2, { name: "A2", graphDefinition: graphDefinition("a2") });
88
+ await store.createAssistant(tenantB, b1, { name: "B1", graphDefinition: graphDefinition("b1") });
89
+
90
+ const listedA = await store.getAllAssistants(tenantA);
91
+ expect(listedA.map((a) => a.id).sort()).toEqual([a1, a2].sort());
92
+ expect(listedA.every((a) => a.tenantId === tenantA)).toBe(true);
93
+
94
+ const listedB = await store.getAllAssistants(tenantB);
95
+ expect(listedB.map((a) => a.id)).toEqual([b1]);
96
+ });
97
+
98
+ it("updates only the provided fields", async () => {
99
+ const tenant = tenantId();
100
+ const id = randomUUID();
101
+ await store.createAssistant(tenant, id, {
102
+ name: "Before",
103
+ description: "keep me",
104
+ graphDefinition: graphDefinition("before"),
105
+ });
106
+
107
+ const updated = await store.updateAssistant(tenant, id, {
108
+ name: "After",
109
+ ownerUserId: "user-2",
110
+ graphDefinition: graphDefinition("after"),
111
+ });
112
+
113
+ expect(updated).toMatchObject({
114
+ id,
115
+ name: "After",
116
+ description: "keep me",
117
+ ownerUserId: "user-2",
118
+ });
119
+ expect(updated!.graphDefinition.metadata).toEqual({ label: "after", flags: { beta: true } });
120
+ });
121
+
122
+ it("returns null / false for an absent assistant id", async () => {
123
+ const tenant = tenantId();
124
+ const missing = randomUUID();
125
+
126
+ expect(await store.getAssistantById(tenant, missing)).toBeNull();
127
+ expect(await store.hasAssistant(tenant, missing)).toBe(false);
128
+ expect(await store.updateAssistant(tenant, missing, { name: "nope" })).toBeNull();
129
+ expect(await store.deleteAssistant(tenant, missing)).toBe(false);
130
+ });
131
+
132
+ it("deletes an assistant and reports existence", async () => {
133
+ const tenant = tenantId();
134
+ const id = randomUUID();
135
+ await store.createAssistant(tenant, id, { name: "Temp", graphDefinition: graphDefinition("temp") });
136
+
137
+ expect(await store.hasAssistant(tenant, id)).toBe(true);
138
+ expect(await store.deleteAssistant(tenant, id)).toBe(true);
139
+ expect(await store.hasAssistant(tenant, id)).toBe(false);
140
+ expect(await store.getAssistantById(tenant, id)).toBeNull();
141
+ });
142
+
143
+ it("maps absent optional description and owner to undefined", async () => {
144
+ const tenant = tenantId();
145
+ const id = randomUUID();
146
+ await store.createAssistant(tenant, id, {
147
+ name: "Shared",
148
+ graphDefinition: { nested: { withNull: null }, list: [1, "two", false] },
149
+ });
150
+
151
+ const read = await store.getAssistantById(tenant, id);
152
+ expect(read).toMatchObject({ id, name: "Shared" });
153
+ expect(read!.description).toBeUndefined();
154
+ expect(read!.ownerUserId).toBeUndefined();
155
+ expect(read!.graphDefinition).toEqual({ nested: { withNull: null }, list: [1, "two", false] });
156
+ });
157
+
158
+ it("finds a personal assistant by owner, scoped to the tenant", async () => {
159
+ const tenant = tenantId();
160
+ const otherTenant = tenantId();
161
+ const personal = randomUUID();
162
+ const shared = randomUUID();
163
+ await store.createAssistant(tenant, personal, {
164
+ name: "Personal",
165
+ ownerUserId: "user-1",
166
+ graphDefinition: graphDefinition("personal"),
167
+ });
168
+ await store.createAssistant(tenant, shared, {
169
+ name: "Shared",
170
+ graphDefinition: graphDefinition("shared"),
171
+ });
172
+ await store.createAssistant(otherTenant, randomUUID(), {
173
+ name: "Other tenant personal",
174
+ ownerUserId: "user-1",
175
+ graphDefinition: graphDefinition("other"),
176
+ });
177
+
178
+ const found = await store.getByOwner(tenant, "user-1");
179
+ expect(found).toMatchObject({ id: personal, tenantId: tenant, ownerUserId: "user-1" });
180
+
181
+ expect(await store.getByOwner(tenant, "user-2")).toBeNull();
182
+ expect(await store.getByOwner(otherTenant, "user-2")).toBeNull();
183
+ });
184
+
185
+ it("keeps the same assistant id independent across tenants", async () => {
186
+ const tenantA = tenantId();
187
+ const tenantB = tenantId();
188
+ const id = randomUUID();
189
+ await store.createAssistant(tenantA, id, { name: "A", graphDefinition: graphDefinition("a") });
190
+ await store.createAssistant(tenantB, id, { name: "B", graphDefinition: graphDefinition("b") });
191
+
192
+ expect((await store.getAssistantById(tenantA, id))?.name).toBe("A");
193
+ expect((await store.getAssistantById(tenantB, id))?.name).toBe("B");
194
+ expect((await store.getAllAssistants(tenantA)).map((a) => a.id)).toEqual([id]);
195
+
196
+ expect(await store.deleteAssistant(tenantA, id)).toBe(true);
197
+ expect(await store.hasAssistant(tenantA, id)).toBe(false);
198
+ expect(await store.hasAssistant(tenantB, id)).toBe(true);
199
+ });
200
+
201
+ it("upserts when create is called again for the same id and tenant", async () => {
202
+ const tenant = tenantId();
203
+ const id = randomUUID();
204
+ await store.createAssistant(tenant, id, { name: "First", graphDefinition: graphDefinition("first") });
205
+
206
+ const second = await store.createAssistant(tenant, id, {
207
+ name: "Second",
208
+ description: "replaced",
209
+ graphDefinition: graphDefinition("second"),
210
+ });
211
+
212
+ expect(second.name).toBe("Second");
213
+ expect(await store.getAllAssistants(tenant)).toHaveLength(1);
214
+ expect((await store.getAssistantById(tenant, id))?.name).toBe("Second");
215
+ });
216
+
217
+ it("clears optional fields when an update sets them empty", async () => {
218
+ const tenant = tenantId();
219
+ const id = randomUUID();
220
+ await store.createAssistant(tenant, id, {
221
+ name: "Owner",
222
+ description: "described",
223
+ ownerUserId: "user-1",
224
+ graphDefinition: graphDefinition("v1"),
225
+ });
226
+
227
+ const updated = await store.updateAssistant(tenant, id, {
228
+ description: "",
229
+ ownerUserId: "",
230
+ });
231
+
232
+ expect(updated!.description).toBeUndefined();
233
+ expect(updated!.ownerUserId).toBeUndefined();
234
+ });
235
+ });
@@ -0,0 +1,194 @@
1
+ /**
2
+ * LocalDatabaseConfigStore behavior tests, mirroring the PostgreSQL database
3
+ * config integration suite: CRUD, tenant isolation, cross-tenant listing, and
4
+ * password encryption at rest / decryption on read.
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 { DatabaseConfig } from "@axiom-lattice/protocols";
19
+ import { decrypt } from "@axiom-lattice/core";
20
+ import { closeDatabase, initDatabase, type DatabaseWrapper } from "../database";
21
+ import { LocalDatabaseConfigStore } from "../stores/LocalDatabaseConfigStore";
22
+
23
+ const nextDbPath = () =>
24
+ path.join(tmpdir(), `lt-db-config-test-${Date.now()}-${Math.random().toString(36).slice(2)}.db`);
25
+
26
+ function config(overrides: Partial<DatabaseConfig> = {}): DatabaseConfig {
27
+ return {
28
+ type: "postgres",
29
+ host: "db.example.com",
30
+ port: 5432,
31
+ database: "app",
32
+ user: "app_user",
33
+ password: "super-secret",
34
+ ssl: true,
35
+ ...overrides,
36
+ };
37
+ }
38
+
39
+ describe("LocalDatabaseConfigStore", () => {
40
+ let db: DatabaseWrapper;
41
+ let store: LocalDatabaseConfigStore;
42
+
43
+ beforeEach(async () => {
44
+ db = await initDatabase({ dbPath: nextDbPath() });
45
+ store = new LocalDatabaseConfigStore(db);
46
+ });
47
+
48
+ afterEach(() => {
49
+ closeDatabase();
50
+ });
51
+
52
+ function tenantId(): string {
53
+ return `db-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: "Primary",
62
+ description: "Primary database",
63
+ config: config(),
64
+ });
65
+ return { id, key, entry };
66
+ }
67
+
68
+ function rawPassword(tenant: string, id: string): string {
69
+ const row = db
70
+ .prepare("SELECT config FROM lt_database_configs WHERE tenant_id = ? AND id = ?")
71
+ .get(tenant, id) as { config: string } | undefined;
72
+ return (JSON.parse(row!.config) as DatabaseConfig).password!;
73
+ }
74
+
75
+ it("creates and reads a config by id and by key", async () => {
76
+ const tenant = tenantId();
77
+ const { id, key, entry } = await createConfig(tenant);
78
+
79
+ expect(entry).toMatchObject({
80
+ id,
81
+ tenantId: tenant,
82
+ key,
83
+ name: "Primary",
84
+ description: "Primary database",
85
+ });
86
+ expect(entry.config.database).toBe("app");
87
+
88
+ const byId = await store.getConfigById(tenant, id);
89
+ expect(byId?.id).toBe(id);
90
+ expect(byId?.config.host).toBe("db.example.com");
91
+
92
+ const byKey = await store.getConfigByKey(tenant, key);
93
+ expect(byKey?.id).toBe(id);
94
+ });
95
+
96
+ it("encrypts the password at rest and decrypts it on read", async () => {
97
+ const tenant = tenantId();
98
+ const { id } = await createConfig(tenant);
99
+
100
+ const stored = rawPassword(tenant, id);
101
+ expect(stored).not.toBe("super-secret");
102
+ expect(decrypt(stored)).toBe("super-secret");
103
+
104
+ const read = await store.getConfigById(tenant, id);
105
+ expect(read?.config.password).toBe("super-secret");
106
+ });
107
+
108
+ it("lists a tenant's configs and includes them in the cross-tenant listing", async () => {
109
+ const tenant = tenantId();
110
+ const first = await createConfig(tenant);
111
+ const second = await createConfig(tenant);
112
+
113
+ const listed = await store.getAllConfigs(tenant);
114
+ expect(listed.map((entry) => entry.id).sort()).toEqual([first.id, second.id].sort());
115
+
116
+ const all = await store.getAllConfigsWithoutTenant();
117
+ const ids = all.map((entry) => entry.id);
118
+ expect(ids).toContain(first.id);
119
+ expect(ids).toContain(second.id);
120
+ expect(all.find((entry) => entry.id === first.id)?.config.password).toBe("super-secret");
121
+ });
122
+
123
+ it("updates fields and re-encrypts a new password", async () => {
124
+ const tenant = tenantId();
125
+ const { id, key } = await createConfig(tenant);
126
+
127
+ const updated = await store.updateConfig(tenant, id, {
128
+ name: "Renamed",
129
+ description: "Updated description",
130
+ config: config({ host: "new.example.com", password: "rotated-secret" }),
131
+ });
132
+
133
+ expect(updated).toMatchObject({
134
+ id,
135
+ key,
136
+ name: "Renamed",
137
+ description: "Updated description",
138
+ });
139
+ expect(updated?.config.host).toBe("new.example.com");
140
+ expect(updated?.config.password).toBe("rotated-secret");
141
+ expect(decrypt(rawPassword(tenant, id))).toBe("rotated-secret");
142
+ });
143
+
144
+ it("keeps the encrypted password when a partial update omits config", async () => {
145
+ const tenant = tenantId();
146
+ const { id } = await createConfig(tenant);
147
+
148
+ const updated = await store.updateConfig(tenant, id, { name: "Only renamed" });
149
+
150
+ expect(updated?.name).toBe("Only renamed");
151
+ expect(updated?.config.password).toBe("super-secret");
152
+
153
+ const stored = rawPassword(tenant, id);
154
+ expect(stored).not.toBe("super-secret");
155
+ expect(decrypt(stored)).toBe("super-secret");
156
+ });
157
+
158
+ it("reports has and deletes a config", async () => {
159
+ const tenant = tenantId();
160
+ const { id } = await createConfig(tenant);
161
+
162
+ expect(await store.hasConfig(tenant, id)).toBe(true);
163
+ expect(await store.deleteConfig(tenant, id)).toBe(true);
164
+ expect(await store.hasConfig(tenant, id)).toBe(false);
165
+ expect(await store.getConfigById(tenant, id)).toBeNull();
166
+ expect(await store.deleteConfig(tenant, id)).toBe(false);
167
+ });
168
+
169
+ it("isolates configs by tenant", async () => {
170
+ const tenantA = tenantId();
171
+ const tenantB = tenantId();
172
+ const key = `shared-${randomUUID()}`;
173
+ const idA = randomUUID();
174
+ const idB = randomUUID();
175
+
176
+ await store.createConfig(tenantA, idA, { key, config: config() });
177
+ await store.createConfig(tenantB, idB, { key, config: config() });
178
+
179
+ expect(await store.getConfigById(tenantA, idB)).toBeNull();
180
+ expect((await store.getConfigByKey(tenantA, key))?.id).toBe(idA);
181
+ expect((await store.getAllConfigs(tenantA)).map((entry) => entry.id)).toEqual([idA]);
182
+ });
183
+
184
+ it("returns absent results for an unknown config", async () => {
185
+ const tenant = tenantId();
186
+ const missing = randomUUID();
187
+
188
+ expect(await store.getConfigById(tenant, missing)).toBeNull();
189
+ expect(await store.getConfigByKey(tenant, "missing")).toBeNull();
190
+ expect(await store.updateConfig(tenant, missing, { name: "x" })).toBeNull();
191
+ expect(await store.hasConfig(tenant, missing)).toBe(false);
192
+ expect(await store.deleteConfig(tenant, missing)).toBe(false);
193
+ });
194
+ });