1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
|
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import type { Migration } from "./migrate.js";
import { computePending } from "./migrate.js";
import type { SqliteStorageBackend } from "./storage.js";
import { createSqliteStorage } from "./storage.js";
describe("computePending (pure)", () => {
it("returns all migrations when none applied", () => {
const migrations: Migration[] = [
{ version: 2, name: "b", up: "" },
{ version: 1, name: "a", up: "" },
];
const result = computePending(new Set(), migrations);
expect(result.map((m) => m.version)).toEqual([1, 2]);
});
it("skips already-applied versions", () => {
const migrations: Migration[] = [
{ version: 1, name: "a", up: "" },
{ version: 2, name: "b", up: "" },
{ version: 3, name: "c", up: "" },
];
const result = computePending(new Set([1, 3]), migrations);
expect(result.map((m) => m.version)).toEqual([2]);
});
it("returns empty when all applied", () => {
const migrations: Migration[] = [{ version: 1, name: "a", up: "" }];
const result = computePending(new Set([1]), migrations);
expect(result).toEqual([]);
});
it("sorts by version ascending", () => {
const migrations: Migration[] = [
{ version: 3, name: "c", up: "" },
{ version: 1, name: "a", up: "" },
{ version: 2, name: "b", up: "" },
];
const result = computePending(new Set(), migrations);
expect(result.map((m) => m.version)).toEqual([1, 2, 3]);
});
});
describe("createSqliteStorage", () => {
let backend: SqliteStorageBackend;
beforeEach(() => {
backend = createSqliteStorage({ path: ":memory:" });
});
afterEach(() => {
backend.close();
});
describe("StorageNamespace get/set/delete/has", () => {
it("returns null for missing key", async () => {
const ns = backend.storage("test");
expect(await ns.get("missing")).toBeNull();
});
it("roundtrips set then get", async () => {
const ns = backend.storage("test");
await ns.set("key1", "value1");
expect(await ns.get("key1")).toBe("value1");
});
it("overwrites existing value", async () => {
const ns = backend.storage("test");
await ns.set("key1", "v1");
await ns.set("key1", "v2");
expect(await ns.get("key1")).toBe("v2");
});
it("has returns false for missing, true for existing", async () => {
const ns = backend.storage("test");
expect(await ns.has("key1")).toBe(false);
await ns.set("key1", "val");
expect(await ns.has("key1")).toBe(true);
});
it("delete removes the key", async () => {
const ns = backend.storage("test");
await ns.set("key1", "val");
await ns.delete("key1");
expect(await ns.get("key1")).toBeNull();
expect(await ns.has("key1")).toBe(false);
});
it("delete on missing key is a no-op", async () => {
const ns = backend.storage("test");
await ns.delete("nonexistent");
});
});
describe("keys", () => {
it("returns all keys in namespace", async () => {
const ns = backend.storage("test");
await ns.set("a", "1");
await ns.set("b", "2");
await ns.set("c", "3");
const keys = await ns.keys();
expect(keys.toSorted()).toEqual(["a", "b", "c"]);
});
it("filters by prefix", async () => {
const ns = backend.storage("test");
await ns.set("foo:1", "a");
await ns.set("foo:2", "b");
await ns.set("bar:1", "c");
const keys = await ns.keys("foo");
expect(keys.toSorted()).toEqual(["foo:1", "foo:2"]);
});
it("returns empty for no matches", async () => {
const ns = backend.storage("test");
await ns.set("a", "1");
expect(await ns.keys("zzz")).toEqual([]);
});
});
describe("namespace isolation", () => {
it("same key in different namespaces does not collide", async () => {
const ns1 = backend.storage("ns1");
const ns2 = backend.storage("ns2");
await ns1.set("key", "value1");
await ns2.set("key", "value2");
expect(await ns1.get("key")).toBe("value1");
expect(await ns2.get("key")).toBe("value2");
});
it("delete in one namespace does not affect another", async () => {
const ns1 = backend.storage("ns1");
const ns2 = backend.storage("ns2");
await ns1.set("key", "v1");
await ns2.set("key", "v2");
await ns1.delete("key");
expect(await ns1.has("key")).toBe(false);
expect(await ns2.get("key")).toBe("v2");
});
it("keys are scoped to namespace", async () => {
const ns1 = backend.storage("ns1");
const ns2 = backend.storage("ns2");
await ns1.set("a", "1");
await ns1.set("b", "2");
await ns2.set("c", "3");
expect((await ns1.keys()).toSorted()).toEqual(["a", "b"]);
expect(await ns2.keys()).toEqual(["c"]);
});
});
describe("migrate", () => {
it("runs pending migrations in order", async () => {
const migrations: Migration[] = [
{ version: 1, name: "create_foo", up: "CREATE TABLE foo (id INTEGER PRIMARY KEY);" },
{ version: 2, name: "create_bar", up: "CREATE TABLE bar (id INTEGER PRIMARY KEY);" },
];
await backend.migrate("ext1", migrations);
const ns = backend.storage("ext1");
await ns.set("test", "val");
expect(await ns.get("test")).toBe("val");
});
it("skips already-applied migrations", async () => {
const migrations: Migration[] = [
{ version: 1, name: "create_foo", up: "CREATE TABLE foo (id INTEGER PRIMARY KEY);" },
];
await backend.migrate("ext1", migrations);
await backend.migrate("ext1", [
...migrations,
{ version: 2, name: "create_bar", up: "CREATE TABLE bar (id INTEGER PRIMARY KEY);" },
]);
});
it("is idempotent — calling migrate twice with same migrations is safe", async () => {
const migrations: Migration[] = [
{ version: 1, name: "create_foo", up: "CREATE TABLE foo (id INTEGER PRIMARY KEY);" },
];
await backend.migrate("ext1", migrations);
await backend.migrate("ext1", migrations);
});
it("migrations are per-namespace", async () => {
const m1: Migration[] = [
{ version: 1, name: "create_t1", up: "CREATE TABLE t1 (id INTEGER PRIMARY KEY);" },
];
const m2: Migration[] = [
{ version: 1, name: "create_t2", up: "CREATE TABLE t2 (id INTEGER PRIMARY KEY);" },
];
await backend.migrate("ext1", m1);
await backend.migrate("ext2", m2);
});
});
});
describe("createSqliteStorage — persistence across reopen", () => {
it("migrations survive close and reopen", async () => {
const tmpPath = `/tmp/storage-sqlite-test-${Date.now()}.db`;
const migrations: Migration[] = [
{ version: 1, name: "create_foo", up: "CREATE TABLE foo (id INTEGER PRIMARY KEY);" },
];
const first = createSqliteStorage({ path: tmpPath });
await first.migrate("ext1", migrations);
first.close();
const second = createSqliteStorage({ path: tmpPath });
await second.migrate("ext1", [
...migrations,
{ version: 2, name: "create_bar", up: "CREATE TABLE bar (id INTEGER PRIMARY KEY);" },
]);
second.close();
const { unlinkSync } = await import("node:fs");
unlinkSync(tmpPath);
});
});
|