rivetkit 0.0.0-feat-sqlite-sync.32022a9 → 0.0.0-feat-sqlite-sync-3x.a00b07f

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.
Files changed (154) hide show
  1. package/dist/browser/client.d.ts +58 -9
  2. package/dist/browser/client.js +193 -23
  3. package/dist/browser/client.js.map +1 -1
  4. package/dist/browser/inspector/client.js +1 -5
  5. package/dist/browser/inspector/client.js.map +1 -1
  6. package/dist/tsup/agent-os/index.cjs +3 -2652
  7. package/dist/tsup/agent-os/index.cjs.map +1 -1
  8. package/dist/tsup/agent-os/index.d.cts +1 -2758
  9. package/dist/tsup/agent-os/index.d.ts +1 -2758
  10. package/dist/tsup/agent-os/index.js +3 -2652
  11. package/dist/tsup/agent-os/index.js.map +1 -1
  12. package/dist/tsup/{chunk-6ZHUGY2E.cjs → chunk-3JPXBFED.cjs} +4 -4
  13. package/dist/tsup/{chunk-6ZHUGY2E.cjs.map → chunk-3JPXBFED.cjs.map} +1 -1
  14. package/dist/tsup/{chunk-6VNQCLKF.js → chunk-5X35FD2T.js} +4 -4
  15. package/dist/tsup/{chunk-BJGWUWNE.js → chunk-6QNBANXM.js} +3 -3
  16. package/dist/tsup/{chunk-Z6IVW2Y5.js → chunk-7HILY7QU.js} +188 -28
  17. package/dist/tsup/chunk-7HILY7QU.js.map +1 -0
  18. package/dist/tsup/{chunk-JI6GZ2C2.js → chunk-A2CF6PFH.js} +18 -1
  19. package/dist/tsup/chunk-A2CF6PFH.js.map +1 -0
  20. package/dist/tsup/{chunk-7XBDAFGQ.cjs → chunk-CT6GQRUN.cjs} +3 -3
  21. package/dist/tsup/{chunk-7XBDAFGQ.cjs.map → chunk-CT6GQRUN.cjs.map} +1 -1
  22. package/dist/tsup/{chunk-M27XAZNI.js → chunk-DRZTELHJ.js} +10 -14
  23. package/dist/tsup/chunk-DRZTELHJ.js.map +1 -0
  24. package/dist/tsup/{chunk-EDJKDOI2.js → chunk-DUGEPEJO.js} +2 -2
  25. package/dist/tsup/{chunk-PMU5EDGT.cjs → chunk-EA6P3N4I.cjs} +3 -3
  26. package/dist/tsup/{chunk-PMU5EDGT.cjs.map → chunk-EA6P3N4I.cjs.map} +1 -1
  27. package/dist/tsup/{chunk-WE7D64FW.cjs → chunk-ERZDCOQ3.cjs} +10 -10
  28. package/dist/tsup/{chunk-WE7D64FW.cjs.map → chunk-ERZDCOQ3.cjs.map} +1 -1
  29. package/dist/tsup/{chunk-UZ5O42HF.js → chunk-GHTA76HT.js} +2 -2
  30. package/dist/tsup/{chunk-IAVZH6HH.js → chunk-ID5VJNZY.js} +2 -2
  31. package/dist/tsup/{chunk-2LRKNNQJ.cjs → chunk-PXIOH445.cjs} +327 -167
  32. package/dist/tsup/chunk-PXIOH445.cjs.map +1 -0
  33. package/dist/tsup/{chunk-MYWZCDML.cjs → chunk-RTWH3AX3.cjs} +19 -2
  34. package/dist/tsup/chunk-RTWH3AX3.cjs.map +1 -0
  35. package/dist/tsup/{chunk-5LBUG2IK.cjs → chunk-UV4KDO4W.cjs} +3 -3
  36. package/dist/tsup/{chunk-5LBUG2IK.cjs.map → chunk-UV4KDO4W.cjs.map} +1 -1
  37. package/dist/tsup/{chunk-HHALUH4L.js → chunk-VI2S336K.js} +2 -2
  38. package/dist/tsup/{chunk-6BRDJRCZ.cjs → chunk-W7NPIYWC.cjs} +10 -14
  39. package/dist/tsup/chunk-W7NPIYWC.cjs.map +1 -0
  40. package/dist/tsup/{chunk-4SIQMCXI.cjs → chunk-ZZQM77UA.cjs} +7 -7
  41. package/dist/tsup/{chunk-4SIQMCXI.cjs.map → chunk-ZZQM77UA.cjs.map} +1 -1
  42. package/dist/tsup/client/mod.cjs +6 -6
  43. package/dist/tsup/client/mod.d.cts +15 -3
  44. package/dist/tsup/client/mod.d.ts +15 -3
  45. package/dist/tsup/client/mod.js +5 -5
  46. package/dist/tsup/common/log.cjs +2 -2
  47. package/dist/tsup/common/log.js +1 -1
  48. package/dist/tsup/common/websocket.cjs +3 -3
  49. package/dist/tsup/common/websocket.js +2 -2
  50. package/dist/tsup/{config-DfVrAigH.d.ts → config-B4erdavw.d.ts} +43 -12
  51. package/dist/tsup/{config-BaUctMNL.d.cts → config-BfG1qpYz.d.cts} +7 -1
  52. package/dist/tsup/{config-BaUctMNL.d.ts → config-BfG1qpYz.d.ts} +7 -1
  53. package/dist/tsup/{config-D6T6Ud_V.d.cts → config-DK144OAw.d.cts} +43 -12
  54. package/dist/tsup/{context-CowF_zyQ.d.cts → context-Cvnf3BRe.d.cts} +2 -2
  55. package/dist/tsup/{context-BsFWwi3i.d.ts → context-Oxi6YWcG.d.ts} +2 -2
  56. package/dist/tsup/db/drizzle.cjs +2 -2
  57. package/dist/tsup/db/drizzle.d.cts +1 -1
  58. package/dist/tsup/db/drizzle.d.ts +1 -1
  59. package/dist/tsup/db/drizzle.js +1 -1
  60. package/dist/tsup/db/mod.cjs +3 -3
  61. package/dist/tsup/db/mod.d.cts +2 -2
  62. package/dist/tsup/db/mod.d.ts +2 -2
  63. package/dist/tsup/db/mod.js +2 -2
  64. package/dist/tsup/dynamic/mod.d.cts +2 -2
  65. package/dist/tsup/dynamic/mod.d.ts +2 -2
  66. package/dist/tsup/inspector/mod.cjs +5 -5
  67. package/dist/tsup/inspector/mod.js +4 -4
  68. package/dist/tsup/inspector/workflow.cjs +4 -4
  69. package/dist/tsup/inspector/workflow.js +3 -3
  70. package/dist/tsup/inspector-tab/mod.d.cts +3 -3
  71. package/dist/tsup/inspector-tab/mod.d.ts +3 -3
  72. package/dist/tsup/mod.cjs +256 -174
  73. package/dist/tsup/mod.cjs.map +1 -1
  74. package/dist/tsup/mod.d.cts +4 -4
  75. package/dist/tsup/mod.d.ts +4 -4
  76. package/dist/tsup/mod.js +170 -88
  77. package/dist/tsup/mod.js.map +1 -1
  78. package/dist/tsup/test/mod.cjs +7 -7
  79. package/dist/tsup/test/mod.d.cts +2 -2
  80. package/dist/tsup/test/mod.d.ts +2 -2
  81. package/dist/tsup/test/mod.js +5 -5
  82. package/dist/tsup/unstable/migrations.d.cts +1 -1
  83. package/dist/tsup/unstable/migrations.d.ts +1 -1
  84. package/dist/tsup/utils.cjs +2 -2
  85. package/dist/tsup/utils.js +1 -1
  86. package/dist/tsup/workflow/mod.cjs +17 -17
  87. package/dist/tsup/workflow/mod.d.cts +4 -4
  88. package/dist/tsup/workflow/mod.d.ts +4 -4
  89. package/dist/tsup/workflow/mod.js +5 -5
  90. package/package.json +9 -13
  91. package/src/actor/keys.ts +86 -0
  92. package/src/agent-os/index.ts +5 -65
  93. package/src/client/actor-conn.ts +92 -6
  94. package/src/client/actor-handle.ts +24 -4
  95. package/src/client/client.ts +14 -4
  96. package/src/client/config.ts +12 -1
  97. package/src/client/lifecycle-errors.ts +16 -1
  98. package/src/client/retry-budget.ts +78 -0
  99. package/src/client/utils.ts +13 -2
  100. package/src/common/database/config.ts +7 -1
  101. package/src/common/database/native-database.ts +17 -9
  102. package/src/common/router.ts +1 -1
  103. package/src/engine-api/actors.ts +3 -3
  104. package/src/engine-client/actor-websocket-client.ts +4 -2
  105. package/src/engine-client/api-endpoints.ts +44 -14
  106. package/src/engine-client/mod.ts +2 -2
  107. package/src/registry/config/envoy.ts +1 -1
  108. package/src/registry/config/index.ts +28 -31
  109. package/src/registry/index.ts +51 -19
  110. package/src/registry/napi-runtime.ts +3 -2
  111. package/src/registry/native.ts +129 -40
  112. package/src/registry/runtime.ts +27 -10
  113. package/src/registry/wasm-runtime.ts +6 -3
  114. package/src/utils/env-vars.ts +15 -10
  115. package/dist/tsup/chunk-2LRKNNQJ.cjs.map +0 -1
  116. package/dist/tsup/chunk-6BRDJRCZ.cjs.map +0 -1
  117. package/dist/tsup/chunk-JI6GZ2C2.js.map +0 -1
  118. package/dist/tsup/chunk-M27XAZNI.js.map +0 -1
  119. package/dist/tsup/chunk-MYWZCDML.cjs.map +0 -1
  120. package/dist/tsup/chunk-Z6IVW2Y5.js.map +0 -1
  121. package/src/actor/config.test.ts +0 -78
  122. package/src/actor/keys.test.ts +0 -275
  123. package/src/actor/utils.test.ts +0 -48
  124. package/src/agent-os/actor/cron.ts +0 -84
  125. package/src/agent-os/actor/db.ts +0 -54
  126. package/src/agent-os/actor/filesystem.ts +0 -131
  127. package/src/agent-os/actor/index.ts +0 -285
  128. package/src/agent-os/actor/network.ts +0 -58
  129. package/src/agent-os/actor/preview.ts +0 -190
  130. package/src/agent-os/actor/process.ts +0 -177
  131. package/src/agent-os/actor/session.ts +0 -518
  132. package/src/agent-os/actor/shell.ts +0 -60
  133. package/src/agent-os/config.ts +0 -79
  134. package/src/agent-os/fs/database-vfs.ts +0 -624
  135. package/src/agent-os/types.ts +0 -148
  136. package/src/client/test.ts +0 -44
  137. package/src/client/token-provider.test.ts +0 -86
  138. package/src/common/database/mod.test.ts +0 -364
  139. package/src/common/database/native-database.test.ts +0 -241
  140. package/src/common/inline-websocket-adapter.test.ts +0 -51
  141. package/src/db/drizzle.test.ts +0 -164
  142. package/src/registry/config/index.test.ts +0 -25
  143. package/src/registry/run-handler-coordinator.test.ts +0 -252
  144. package/src/registry/runtime.test.ts +0 -124
  145. package/src/unstable/migrations.test.ts +0 -170
  146. package/src/utils/endpoint-parser.test.ts +0 -206
  147. package/src/utils.test.ts +0 -34
  148. package/src/workflow/driver.test.ts +0 -62
  149. /package/dist/tsup/{chunk-6VNQCLKF.js.map → chunk-5X35FD2T.js.map} +0 -0
  150. /package/dist/tsup/{chunk-BJGWUWNE.js.map → chunk-6QNBANXM.js.map} +0 -0
  151. /package/dist/tsup/{chunk-EDJKDOI2.js.map → chunk-DUGEPEJO.js.map} +0 -0
  152. /package/dist/tsup/{chunk-UZ5O42HF.js.map → chunk-GHTA76HT.js.map} +0 -0
  153. /package/dist/tsup/{chunk-IAVZH6HH.js.map → chunk-ID5VJNZY.js.map} +0 -0
  154. /package/dist/tsup/{chunk-HHALUH4L.js.map → chunk-VI2S336K.js.map} +0 -0
@@ -1,252 +0,0 @@
1
- import { describe, expect, test, vi } from "vitest";
2
- import { defineRunHandler, type RunControl } from "@/actor/config";
3
- import { RunHandlerCoordinator } from "./run-handler-coordinator";
4
-
5
- class Deferred<T = void> {
6
- readonly promise: Promise<T>;
7
- resolve!: (value: T | PromiseLike<T>) => void;
8
- reject!: (reason?: unknown) => void;
9
-
10
- constructor() {
11
- this.promise = new Promise<T>((resolve, reject) => {
12
- this.resolve = resolve;
13
- this.reject = reject;
14
- });
15
- }
16
- }
17
-
18
- function createCoordinator() {
19
- let control: RunControl | undefined;
20
- const dispose = vi.fn();
21
- const createInspector = vi.fn((context: { control: RunControl }) => {
22
- control = context.control;
23
- return {
24
- inspector: {
25
- workflow: {
26
- getHistory: () => null,
27
- getState: async () => null,
28
- onHistoryUpdated: () => () => {},
29
- replayFromStep: async () => null,
30
- },
31
- },
32
- dispose,
33
- };
34
- });
35
- const run = defineRunHandler(async () => {}, {
36
- inspectorKind: "workflow",
37
- createInspector,
38
- });
39
- const coordinator = new RunHandlerCoordinator(run);
40
- const restart = vi.fn();
41
- const inspector = coordinator.getInspector("actor-1", restart);
42
-
43
- return {
44
- control: control as unknown as RunControl,
45
- coordinator,
46
- createInspector,
47
- dispose,
48
- inspector,
49
- restart,
50
- };
51
- }
52
-
53
- describe("RunHandlerCoordinator", () => {
54
- test("fails explicitly when a JavaScript factory omits required workflow controls", async () => {
55
- const coordinator = new RunHandlerCoordinator(
56
- defineRunHandler(async () => {}, {
57
- inspectorKind: "workflow",
58
- createInspector: (() => ({
59
- inspector: { workflow: { getHistory: () => null } },
60
- })) as never,
61
- }),
62
- );
63
-
64
- await expect(
65
- coordinator.run(
66
- "actor-1",
67
- () => {},
68
- async () => {},
69
- ),
70
- ).rejects.toThrow(
71
- "createInspector returned an invalid workflow adapter for actor actor-1",
72
- );
73
- });
74
-
75
- test("initializes and disposes Inspector state even when it is never queried", async () => {
76
- const dispose = vi.fn();
77
- const createInspector = vi.fn(() => ({
78
- inspector: {
79
- workflow: {
80
- getHistory: () => null,
81
- getState: async () => null,
82
- onHistoryUpdated: () => () => {},
83
- replayFromStep: async () => null,
84
- },
85
- },
86
- dispose,
87
- }));
88
- const coordinator = new RunHandlerCoordinator(
89
- defineRunHandler(async () => {}, {
90
- inspectorKind: "workflow",
91
- createInspector,
92
- }),
93
- );
94
-
95
- await coordinator.run(
96
- "actor-1",
97
- () => {},
98
- async () => {},
99
- );
100
- expect(createInspector).toHaveBeenCalledOnce();
101
- coordinator.destroy("actor-1");
102
- expect(dispose).toHaveBeenCalledOnce();
103
- });
104
-
105
- test("creates one inspector per live actor and disposes it exactly once", () => {
106
- const subject = createCoordinator();
107
- expect(
108
- subject.coordinator.getInspector("actor-1", subject.restart),
109
- ).toBe(subject.inspector);
110
- expect(subject.createInspector).toHaveBeenCalledOnce();
111
-
112
- subject.coordinator.destroy("actor-1");
113
- subject.coordinator.destroy("actor-1");
114
- expect(subject.dispose).toHaveBeenCalledOnce();
115
-
116
- const recreated = subject.coordinator.getInspector(
117
- "actor-1",
118
- subject.restart,
119
- );
120
- expect(recreated).not.toBe(subject.inspector);
121
- expect(subject.createInspector).toHaveBeenCalledTimes(2);
122
- });
123
-
124
- test("rejects replay while a run is active", async () => {
125
- const subject = createCoordinator();
126
- const active = new Deferred();
127
- const started = new Deferred();
128
- const running = subject.coordinator.run(
129
- "actor-1",
130
- subject.restart,
131
- async () => {
132
- started.resolve();
133
- await active.promise;
134
- },
135
- );
136
- await started.promise;
137
-
138
- await expect(
139
- subject.control.run.withInactive(
140
- { restartOnSuccess: true },
141
- async () => {},
142
- ),
143
- ).rejects.toMatchObject({
144
- group: "actor",
145
- code: "run_handler_unavailable",
146
- });
147
-
148
- active.resolve();
149
- await running;
150
- });
151
-
152
- test("uses a start queued during replay as the single restart", async () => {
153
- const subject = createCoordinator();
154
- const replay = new Deferred();
155
- const replayStarted = new Deferred();
156
- const events: string[] = [];
157
- subject.restart.mockImplementation(() => {
158
- events.push("restart");
159
- });
160
-
161
- const exclusive = subject.control.run.withInactive(
162
- { restartOnSuccess: true },
163
- async () => {
164
- events.push("replay");
165
- replayStarted.resolve();
166
- await replay.promise;
167
- },
168
- );
169
- await replayStarted.promise;
170
-
171
- const queued = subject.coordinator.run(
172
- "actor-1",
173
- subject.restart,
174
- async () => {
175
- events.push("run");
176
- },
177
- );
178
- await Promise.resolve();
179
- expect(events).toEqual(["replay"]);
180
-
181
- replay.resolve();
182
- await exclusive;
183
- await queued;
184
- expect(events).toEqual(["replay", "run"]);
185
- expect(subject.restart).not.toHaveBeenCalled();
186
- });
187
-
188
- test("drops starts queued behind a failed replay and leaves the run inactive", async () => {
189
- const subject = createCoordinator();
190
- const replay = new Deferred();
191
- const replayStarted = new Deferred();
192
- const run = vi.fn();
193
- const expected = new Error("rewrite failed");
194
-
195
- const exclusive = subject.control.run.withInactive(
196
- { restartOnSuccess: true },
197
- async () => {
198
- replayStarted.resolve();
199
- await replay.promise;
200
- throw expected;
201
- },
202
- );
203
- await replayStarted.promise;
204
- const queued = subject.coordinator.run("actor-1", subject.restart, run);
205
-
206
- replay.resolve();
207
- await expect(exclusive).rejects.toBe(expected);
208
- await expect(queued).resolves.toBe("suppressed");
209
- expect(run).not.toHaveBeenCalled();
210
- expect(subject.restart).not.toHaveBeenCalled();
211
- });
212
-
213
- test("restores a durable wake suppressed by a failed replay", async () => {
214
- const subject = createCoordinator();
215
- const replay = new Deferred();
216
- const replayStarted = new Deferred();
217
- const run = vi.fn();
218
-
219
- const exclusive = subject.control.run.withInactive({}, async () => {
220
- replayStarted.resolve();
221
- await replay.promise;
222
- throw new Error("rewrite failed");
223
- });
224
- await replayStarted.promise;
225
- const queued = subject.coordinator.run("actor-1", subject.restart, run);
226
-
227
- replay.resolve();
228
- await expect(exclusive).rejects.toThrow("rewrite failed");
229
- await expect(queued).resolves.toBe("suppressed");
230
- expect(run).not.toHaveBeenCalled();
231
- });
232
-
233
- test("allows only one concurrent replay and closes old controls on destroy", async () => {
234
- const subject = createCoordinator();
235
- const replay = new Deferred();
236
- const first = subject.control.run.withInactive({}, async () => {
237
- await replay.promise;
238
- });
239
-
240
- await expect(
241
- subject.control.run.withInactive({}, async () => {}),
242
- ).rejects.toMatchObject({ code: "run_handler_unavailable" });
243
- subject.coordinator.destroy("actor-1");
244
- replay.resolve();
245
- await expect(first).rejects.toMatchObject({
246
- code: "run_handler_unavailable",
247
- });
248
- await expect(
249
- subject.control.run.withInactive({}, async () => {}),
250
- ).rejects.toMatchObject({ code: "run_handler_unavailable" });
251
- });
252
- });
@@ -1,124 +0,0 @@
1
- import { describe, expect, test } from "vitest";
2
- import type { RegistryConfig } from "./config";
3
- import { loadAutoRuntime, type RuntimeLoaders } from "./native";
4
- import {
5
- type CoreRuntime,
6
- normalizeRuntimeSqlExecuteResult,
7
- type RuntimeSqlBindParam,
8
- type RuntimeSqlBindParams,
9
- } from "./runtime";
10
-
11
- describe("runtime SQL boundary", () => {
12
- test("accepts exact bind param variants", () => {
13
- const blob = new Uint8Array([1, 2, 3]);
14
- const params = [
15
- { kind: "null" },
16
- { kind: "int", intValue: 1 },
17
- { kind: "float", floatValue: 1.5 },
18
- { kind: "text", textValue: "text" },
19
- { kind: "blob", blobValue: blob },
20
- ] satisfies RuntimeSqlBindParams;
21
-
22
- expect(params).toEqual([
23
- { kind: "null" },
24
- { kind: "int", intValue: 1 },
25
- { kind: "float", floatValue: 1.5 },
26
- { kind: "text", textValue: "text" },
27
- { kind: "blob", blobValue: blob },
28
- ]);
29
- });
30
-
31
- test("rejects bind params with mismatched value fields at typecheck time", () => {
32
- const invalidIntParamCandidate = {
33
- kind: "int",
34
- intValue: 1,
35
- textValue: "extra",
36
- } as const;
37
- // @ts-expect-error Runtime SQL int params must only carry intValue.
38
- const invalidIntParam: RuntimeSqlBindParam = invalidIntParamCandidate;
39
-
40
- expect(invalidIntParam.kind).toBe("int");
41
- });
42
-
43
- test("normalizes execute result metadata", () => {
44
- const base = {
45
- columns: ["value"],
46
- rows: [[1]],
47
- changes: 1,
48
- lastInsertRowId: null,
49
- };
50
-
51
- expect(normalizeRuntimeSqlExecuteResult(base)).toEqual(base);
52
- });
53
- });
54
-
55
- describe("loadAutoRuntime failure reporting", () => {
56
- const wasmRuntime = { kind: "wasm" } as unknown as CoreRuntime;
57
- const nativeRuntime = { kind: "napi" } as unknown as CoreRuntime;
58
- const config = {} as RegistryConfig;
59
-
60
- function loaders(overrides: Partial<RuntimeLoaders>): RuntimeLoaders {
61
- return {
62
- detectHost: () => "node-like",
63
- loadNative: async () => ({ runtime: nativeRuntime }) as never,
64
- loadWasm: async () => ({ runtime: wasmRuntime }) as never,
65
- ...overrides,
66
- };
67
- }
68
-
69
- test("prefers native when it loads", async () => {
70
- const runtime = await loadAutoRuntime(config, loaders({}));
71
- expect(runtime).toBe(nativeRuntime);
72
- });
73
-
74
- test("falls back to wasm when native fails", async () => {
75
- const runtime = await loadAutoRuntime(
76
- config,
77
- loaders({
78
- loadNative: async () => {
79
- throw new Error("missing platform binding");
80
- },
81
- }),
82
- );
83
- expect(runtime).toBe(wasmRuntime);
84
- });
85
-
86
- test("reports the native cause when both runtimes fail", async () => {
87
- // The native failure is the actionable one. Before this was reported,
88
- // a skipped platform binding surfaced only as the wasm loader's
89
- // unrelated `file://` fetch error.
90
- const promise = loadAutoRuntime(
91
- config,
92
- loaders({
93
- loadNative: async () => {
94
- throw new Error(
95
- "Cannot find module '@rivetkit/rivetkit-napi-linux-x64-musl'",
96
- );
97
- },
98
- loadWasm: async () => {
99
- throw new Error("fetch failed");
100
- },
101
- }),
102
- );
103
- await expect(promise).rejects.toThrow(/rivetkit-napi-linux-x64-musl/);
104
- await expect(promise).rejects.toThrow(/fetch failed/);
105
- });
106
-
107
- test("uses wasm directly on an edge-like host without touching native", async () => {
108
- let nativeCalls = 0;
109
- const runtime = await loadAutoRuntime(
110
- config,
111
- loaders({
112
- detectHost: () => "edge-like",
113
- loadNative: async () => {
114
- nativeCalls += 1;
115
- throw new Error(
116
- "native must not be attempted on edge hosts",
117
- );
118
- },
119
- }),
120
- );
121
- expect(runtime).toBe(wasmRuntime);
122
- expect(nativeCalls).toBe(0);
123
- });
124
- });
@@ -1,170 +0,0 @@
1
- import { DatabaseSync } from "node:sqlite";
2
- import { afterEach, describe, expect, test } from "vitest";
3
- import {
4
- DEFAULT_MIGRATION_TABLE_NAME,
5
- migrations,
6
- type MigrationDatabase,
7
- } from "./migrations";
8
-
9
- const databases: DatabaseSync[] = [];
10
-
11
- afterEach(() => {
12
- for (const database of databases.splice(0)) database.close();
13
- });
14
-
15
- function createDatabase(): { raw: DatabaseSync; db: MigrationDatabase } {
16
- const raw = new DatabaseSync(":memory:");
17
- databases.push(raw);
18
- return {
19
- raw,
20
- db: {
21
- execute: async <TRow extends Record<string, unknown>>(
22
- sql: string,
23
- ...args: unknown[]
24
- ): Promise<TRow[]> => {
25
- if (args.length > 0 || /^\s*SELECT\b/i.test(sql)) {
26
- return raw.prepare(sql).all(...(args as never[])) as TRow[];
27
- }
28
- raw.exec(sql);
29
- return [];
30
- },
31
- },
32
- };
33
- }
34
-
35
- describe("migrations", () => {
36
- test("applies SQL and function migrations once in order", async () => {
37
- const { raw, db } = createDatabase();
38
- const applied: number[] = [];
39
- const migrate = migrations({
40
- tableName: "app_schema_version",
41
- migrations: [
42
- {
43
- version: 1,
44
- sql: "CREATE TABLE items (id INTEGER PRIMARY KEY, name TEXT NOT NULL)",
45
- },
46
- {
47
- version: 2,
48
- up: async (database) => {
49
- applied.push(2);
50
- await database.execute(
51
- "ALTER TABLE items ADD COLUMN active INTEGER NOT NULL DEFAULT 1",
52
- );
53
- },
54
- },
55
- ],
56
- });
57
-
58
- await migrate(db);
59
- await migrate(db);
60
-
61
- expect(applied).toEqual([2]);
62
- expect(
63
- raw
64
- .prepare(
65
- "SELECT schema_version FROM app_schema_version WHERE singleton = 1",
66
- )
67
- .get(),
68
- ).toEqual({ schema_version: 2 });
69
- expect(
70
- raw
71
- .prepare("PRAGMA table_info(items)")
72
- .all()
73
- .map((row) => row.name),
74
- ).toEqual(["id", "name", "active"]);
75
- });
76
-
77
- test("uses the default migration table", async () => {
78
- const { raw, db } = createDatabase();
79
- await migrations({
80
- migrations: [{ version: 1, sql: "CREATE TABLE item (id INTEGER)" }],
81
- })(db);
82
-
83
- expect(
84
- raw
85
- .prepare(
86
- `SELECT schema_version FROM ${DEFAULT_MIGRATION_TABLE_NAME} WHERE singleton = 1`,
87
- )
88
- .get(),
89
- ).toEqual({ schema_version: 1 });
90
- });
91
-
92
- test("adopts an unversioned schema before applying later migrations", async () => {
93
- const { raw, db } = createDatabase();
94
- raw.exec(
95
- "CREATE TABLE items (id INTEGER PRIMARY KEY, name TEXT NOT NULL)",
96
- );
97
- const applied: number[] = [];
98
-
99
- await migrations({
100
- tableName: "app_schema_version",
101
- adopt: async (database) => {
102
- const tables = await database.execute<{ name: string }>(
103
- "SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'items'",
104
- );
105
- return tables.length === 0 ? 0 : 1;
106
- },
107
- migrations: [
108
- {
109
- version: 1,
110
- up: () => {
111
- throw new Error("already applied migration ran");
112
- },
113
- },
114
- {
115
- version: 2,
116
- up: async (database) => {
117
- applied.push(2);
118
- await database.execute(
119
- "ALTER TABLE items ADD COLUMN active INTEGER NOT NULL DEFAULT 1",
120
- );
121
- },
122
- },
123
- ],
124
- })(db);
125
-
126
- expect(applied).toEqual([2]);
127
- expect(
128
- raw
129
- .prepare(
130
- "SELECT schema_version FROM app_schema_version WHERE singleton = 1",
131
- )
132
- .get(),
133
- ).toEqual({ schema_version: 2 });
134
- });
135
-
136
- test("rejects invalid ladders and table names before executing", () => {
137
- expect(() => migrations({ migrations: [] })).toThrow(
138
- "must not be empty",
139
- );
140
- expect(() =>
141
- migrations({
142
- migrations: [{ version: 2, sql: "SELECT 1" }],
143
- }),
144
- ).toThrow("expected version 1");
145
- expect(() =>
146
- migrations({
147
- tableName: "schema; DROP TABLE items",
148
- migrations: [{ version: 1, sql: "SELECT 1" }],
149
- }),
150
- ).toThrow("invalid migration table name");
151
- });
152
-
153
- test("rejects a database newer than the migration ladder", async () => {
154
- const { raw, db } = createDatabase();
155
- raw.exec(`
156
- CREATE TABLE app_schema_version (
157
- singleton INTEGER PRIMARY KEY,
158
- schema_version INTEGER NOT NULL
159
- ) STRICT;
160
- INSERT INTO app_schema_version VALUES (1, 2);
161
- `);
162
-
163
- await expect(
164
- migrations({
165
- tableName: "app_schema_version",
166
- migrations: [{ version: 1, sql: "SELECT 1" }],
167
- })(db),
168
- ).rejects.toThrow("newer than supported version 1");
169
- });
170
- });