toiljs 0.0.115 → 0.0.117

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 (326) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/README.md +16 -7
  3. package/TYPESCRIPT_LAW.md +1946 -2073
  4. package/build/backend/.tsbuildinfo +1 -1
  5. package/build/cli/.tsbuildinfo +1 -1
  6. package/build/cli/index.js +2375 -258
  7. package/build/client/.tsbuildinfo +1 -1
  8. package/build/client/dev/devtools.js +1 -1
  9. package/build/client/dev/error-overlay.js +1 -1
  10. package/build/client/routing/mount.js +1 -1
  11. package/build/compiler/.tsbuildinfo +1 -1
  12. package/build/compiler/auth-emails.js +7 -3
  13. package/build/compiler/emails.js +1 -1
  14. package/build/compiler/index.d.ts +1 -0
  15. package/build/compiler/index.js +25 -4
  16. package/build/compiler/pages.js +2 -3
  17. package/build/compiler/prerender.d.ts +3 -8
  18. package/build/compiler/prerender.js +88 -127
  19. package/build/compiler/ssg.js +3 -4
  20. package/build/compiler/template-build.js +3 -4
  21. package/build/compiler/toil-docs.generated.js +6 -5
  22. package/build/compiler/vite.js +3 -6
  23. package/build/devserver/.tsbuildinfo +1 -1
  24. package/build/devserver/db/database.js +7 -3
  25. package/build/devserver/db/types.js +1 -1
  26. package/build/devserver/production.js +4 -3
  27. package/build/devserver/runtime/crypto.d.ts +1 -1
  28. package/build/io/.tsbuildinfo +1 -1
  29. package/build/logger/.tsbuildinfo +1 -1
  30. package/build/shared/.tsbuildinfo +1 -1
  31. package/examples/basic/client/routes/analytics.tsx +7 -5
  32. package/examples/basic/server/tsconfig.json +1 -3
  33. package/package.json +45 -44
  34. package/presets/no-uint8array-tostring.js +120 -153
  35. package/presets/oxlint.d.ts +5 -0
  36. package/presets/oxlint.js +224 -0
  37. package/server/runtime/tsconfig.json +1 -3
  38. package/tsconfig.json +2 -11
  39. package/.github/ISSUE_TEMPLATE/bug_report.md +0 -38
  40. package/.github/ISSUE_TEMPLATE/bug_report.yml +0 -90
  41. package/.github/ISSUE_TEMPLATE/config.yml +0 -8
  42. package/.github/ISSUE_TEMPLATE/feature_request.md +0 -20
  43. package/.github/PULL_REQUEST_TEMPLATE.md +0 -43
  44. package/.github/changelog-config.json +0 -45
  45. package/.github/dependabot.yml +0 -27
  46. package/.github/workflows/ci.yml +0 -222
  47. package/.vscode/settings.json +0 -10
  48. package/docs/README.md +0 -107
  49. package/docs/auth/README.md +0 -104
  50. package/docs/auth/configuration.md +0 -105
  51. package/docs/auth/emails.md +0 -92
  52. package/docs/auth/extending.md +0 -202
  53. package/docs/auth/how-it-works.md +0 -138
  54. package/docs/auth/usage.md +0 -188
  55. package/docs/backend/README.md +0 -143
  56. package/docs/backend/data.md +0 -351
  57. package/docs/backend/rest.md +0 -402
  58. package/docs/backend/rpc.md +0 -226
  59. package/docs/background/README.md +0 -114
  60. package/docs/background/daemons.md +0 -276
  61. package/docs/background/derive.md +0 -179
  62. package/docs/cli/README.md +0 -379
  63. package/docs/concepts/ai-guide.md +0 -132
  64. package/docs/concepts/config.md +0 -416
  65. package/docs/concepts/decorators.md +0 -127
  66. package/docs/concepts/security.md +0 -108
  67. package/docs/concepts/tiers.md +0 -166
  68. package/docs/concepts/types.md +0 -216
  69. package/docs/database/README.md +0 -143
  70. package/docs/database/capacity.md +0 -350
  71. package/docs/database/counters.md +0 -174
  72. package/docs/database/documents.md +0 -277
  73. package/docs/database/events.md +0 -307
  74. package/docs/database/membership.md +0 -246
  75. package/docs/database/setup.md +0 -155
  76. package/docs/database/unique.md +0 -216
  77. package/docs/database/views.md +0 -246
  78. package/docs/frontend/README.md +0 -104
  79. package/docs/frontend/components.md +0 -286
  80. package/docs/frontend/data-fetching.md +0 -260
  81. package/docs/frontend/images.md +0 -148
  82. package/docs/frontend/metadata.md +0 -344
  83. package/docs/frontend/navigation.md +0 -296
  84. package/docs/frontend/rendering.md +0 -236
  85. package/docs/frontend/routing.md +0 -346
  86. package/docs/frontend/scripts.md +0 -118
  87. package/docs/frontend/search.md +0 -191
  88. package/docs/frontend/styling.md +0 -147
  89. package/docs/frontend/toil-global.md +0 -216
  90. package/docs/getting-started/README.md +0 -81
  91. package/docs/getting-started/create-project.md +0 -131
  92. package/docs/getting-started/deploy.md +0 -101
  93. package/docs/getting-started/first-app.md +0 -215
  94. package/docs/getting-started/installation.md +0 -124
  95. package/docs/getting-started/migrating.md +0 -125
  96. package/docs/getting-started/project-structure.md +0 -163
  97. package/docs/introduction/README.md +0 -66
  98. package/docs/introduction/design-principles.md +0 -81
  99. package/docs/introduction/distributed.md +0 -91
  100. package/docs/introduction/how-it-works.md +0 -121
  101. package/docs/introduction/hyperscale.md +0 -70
  102. package/docs/introduction/modern-stack.md +0 -85
  103. package/docs/introduction/vs-other-frameworks.md +0 -64
  104. package/docs/introduction/why-toil.md +0 -99
  105. package/docs/llms.txt +0 -95
  106. package/docs/realtime/README.md +0 -102
  107. package/docs/realtime/channels.md +0 -211
  108. package/docs/realtime/streams.md +0 -369
  109. package/docs/services/README.md +0 -60
  110. package/docs/services/analytics.md +0 -279
  111. package/docs/services/caching.md +0 -175
  112. package/docs/services/cookies.md +0 -235
  113. package/docs/services/crypto.md +0 -209
  114. package/docs/services/email.md +0 -289
  115. package/docs/services/environment.md +0 -141
  116. package/docs/services/ratelimit.md +0 -174
  117. package/docs/services/time.md +0 -85
  118. package/eslint.config.js +0 -57
  119. package/examples/basic/client/routes/docs/[...slug].tsx +0 -12
  120. package/presets/eslint.js +0 -88
  121. package/src/backend/index.ts +0 -204
  122. package/src/cli/configure.ts +0 -363
  123. package/src/cli/create.ts +0 -924
  124. package/src/cli/db.ts +0 -158
  125. package/src/cli/diagnostics.ts +0 -665
  126. package/src/cli/doctor.ts +0 -982
  127. package/src/cli/features.ts +0 -170
  128. package/src/cli/index.ts +0 -326
  129. package/src/cli/notify.ts +0 -120
  130. package/src/cli/proc.ts +0 -56
  131. package/src/cli/ui.ts +0 -169
  132. package/src/cli/update.ts +0 -248
  133. package/src/cli/updates.ts +0 -92
  134. package/src/cli/validate.ts +0 -31
  135. package/src/cli/version-check.ts +0 -125
  136. package/src/client/auth.ts +0 -869
  137. package/src/client/channel/channel.ts +0 -146
  138. package/src/client/components/Form.tsx +0 -65
  139. package/src/client/components/Image.tsx +0 -160
  140. package/src/client/components/Script.tsx +0 -113
  141. package/src/client/components/Slot.tsx +0 -21
  142. package/src/client/dev/devtools.tsx +0 -1089
  143. package/src/client/dev/error-overlay.tsx +0 -225
  144. package/src/client/errors.ts +0 -11
  145. package/src/client/head/head.ts +0 -182
  146. package/src/client/head/metadata.ts +0 -110
  147. package/src/client/index.ts +0 -130
  148. package/src/client/navigation/Link.tsx +0 -108
  149. package/src/client/navigation/NavLink.tsx +0 -86
  150. package/src/client/navigation/navigation.ts +0 -235
  151. package/src/client/navigation/prefetch.ts +0 -169
  152. package/src/client/navigation/scroll.ts +0 -53
  153. package/src/client/routing/Router.tsx +0 -216
  154. package/src/client/routing/action.ts +0 -122
  155. package/src/client/routing/error-boundary.tsx +0 -43
  156. package/src/client/routing/hooks.ts +0 -140
  157. package/src/client/routing/lazy.ts +0 -77
  158. package/src/client/routing/loader.ts +0 -381
  159. package/src/client/routing/match.ts +0 -47
  160. package/src/client/routing/mount.tsx +0 -90
  161. package/src/client/routing/params-context.ts +0 -10
  162. package/src/client/routing/slot-context.ts +0 -7
  163. package/src/client/rpc.ts +0 -97
  164. package/src/client/search/search.ts +0 -189
  165. package/src/client/search/use-page-search.ts +0 -73
  166. package/src/client/ssr/markers.tsx +0 -172
  167. package/src/client/stream/client.ts +0 -237
  168. package/src/client/types.ts +0 -73
  169. package/src/compiler/auth-emails.ts +0 -312
  170. package/src/compiler/config.ts +0 -369
  171. package/src/compiler/docs.ts +0 -127
  172. package/src/compiler/email-preview.ts +0 -314
  173. package/src/compiler/emails.ts +0 -404
  174. package/src/compiler/fonts.ts +0 -89
  175. package/src/compiler/generate.ts +0 -471
  176. package/src/compiler/image-blur.ts +0 -42
  177. package/src/compiler/image-report.ts +0 -88
  178. package/src/compiler/index.ts +0 -1362
  179. package/src/compiler/pages.ts +0 -49
  180. package/src/compiler/plugin.ts +0 -391
  181. package/src/compiler/prerender.ts +0 -263
  182. package/src/compiler/routes.ts +0 -170
  183. package/src/compiler/seo.ts +0 -415
  184. package/src/compiler/sri.ts +0 -144
  185. package/src/compiler/ssg.ts +0 -177
  186. package/src/compiler/ssr-codegen.ts +0 -85
  187. package/src/compiler/template-build.ts +0 -676
  188. package/src/compiler/template.ts +0 -262
  189. package/src/compiler/toil-docs.generated.ts +0 -77
  190. package/src/compiler/vite.ts +0 -245
  191. package/src/devserver/analytics/index.ts +0 -317
  192. package/src/devserver/config/dotenv.ts +0 -102
  193. package/src/devserver/config/env.ts +0 -30
  194. package/src/devserver/config/ratelimit.ts +0 -123
  195. package/src/devserver/daemon/catalog.ts +0 -120
  196. package/src/devserver/daemon/cron.ts +0 -87
  197. package/src/devserver/daemon/host.ts +0 -134
  198. package/src/devserver/daemon/index.ts +0 -369
  199. package/src/devserver/daemon/runtime.ts +0 -48
  200. package/src/devserver/db/catalog.ts +0 -109
  201. package/src/devserver/db/database.ts +0 -1834
  202. package/src/devserver/db/derives.ts +0 -121
  203. package/src/devserver/db/index.ts +0 -20
  204. package/src/devserver/db/routeKinds.ts +0 -191
  205. package/src/devserver/db/types.ts +0 -168
  206. package/src/devserver/email/caps.ts +0 -0
  207. package/src/devserver/email/config.ts +0 -141
  208. package/src/devserver/email/console.ts +0 -348
  209. package/src/devserver/email/index.ts +0 -155
  210. package/src/devserver/email/providers.ts +0 -131
  211. package/src/devserver/email/status.ts +0 -23
  212. package/src/devserver/email/validate.ts +0 -37
  213. package/src/devserver/email/wire.ts +0 -55
  214. package/src/devserver/http/cache.ts +0 -0
  215. package/src/devserver/http/envelope.ts +0 -157
  216. package/src/devserver/http/proxy.ts +0 -172
  217. package/src/devserver/http/runtime.ts +0 -387
  218. package/src/devserver/index.ts +0 -43
  219. package/src/devserver/production-ipc.ts +0 -63
  220. package/src/devserver/production-worker.ts +0 -83
  221. package/src/devserver/production.ts +0 -718
  222. package/src/devserver/runtime/crypto.ts +0 -587
  223. package/src/devserver/runtime/host.ts +0 -427
  224. package/src/devserver/runtime/module.ts +0 -442
  225. package/src/devserver/server.ts +0 -251
  226. package/src/devserver/ssr.ts +0 -208
  227. package/src/devserver/stream/catalog.ts +0 -106
  228. package/src/devserver/stream/host.ts +0 -42
  229. package/src/devserver/stream/index.ts +0 -301
  230. package/src/devserver/stream/manager.ts +0 -167
  231. package/src/devserver/stream/router.ts +0 -101
  232. package/src/devserver/stream/wire.ts +0 -58
  233. package/src/devserver/stream/ws.ts +0 -78
  234. package/src/devserver/wasm/sections.ts +0 -59
  235. package/src/devserver/wasm/surface.ts +0 -115
  236. package/src/io/FastMap.ts +0 -151
  237. package/src/io/FastSet.ts +0 -113
  238. package/src/io/codec.ts +0 -362
  239. package/src/io/index.ts +0 -10
  240. package/src/io/lengths.ts +0 -14
  241. package/src/io/types.ts +0 -19
  242. package/src/logger/index.ts +0 -22
  243. package/src/shared/index.ts +0 -46
  244. package/test/analytics-dev.test.ts +0 -113
  245. package/test/assembly/aspect-shim.ts +0 -33
  246. package/test/assembly/cookie.spec.ts +0 -303
  247. package/test/assembly/example.spec.ts +0 -22
  248. package/test/assembly/ssr.spec.ts +0 -95
  249. package/test/assembly.test.ts +0 -59
  250. package/test/auth-emails.test.ts +0 -118
  251. package/test/built-ssr.test.ts +0 -148
  252. package/test/channel.test.ts +0 -21
  253. package/test/config-host-port.test.ts +0 -59
  254. package/test/configure.test.ts +0 -94
  255. package/test/daemon-build.test.ts +0 -278
  256. package/test/daemon-catalog.test.ts +0 -349
  257. package/test/daemon-emulation.test.ts +0 -256
  258. package/test/db.test.ts +0 -0
  259. package/test/devserver-database.test.ts +0 -1102
  260. package/test/devserver-email.test.ts +0 -306
  261. package/test/devserver-pqauth.test.ts +0 -93
  262. package/test/devserver-secrets.test.ts +0 -63
  263. package/test/devserver.test.ts +0 -259
  264. package/test/doctor.test.ts +0 -298
  265. package/test/dom/Image.test.tsx +0 -144
  266. package/test/dom/Link.test.tsx +0 -67
  267. package/test/dom/NavLink.test.tsx +0 -37
  268. package/test/dom/Script.test.tsx +0 -48
  269. package/test/dom/action.test.tsx +0 -146
  270. package/test/dom/error-overlay.test.tsx +0 -44
  271. package/test/dom/loader.test.tsx +0 -121
  272. package/test/dom/navigation.test.ts +0 -59
  273. package/test/dom/revalidate.test.tsx +0 -38
  274. package/test/dom/route-head.test.tsx +0 -56
  275. package/test/dom/router-loading.test.tsx +0 -44
  276. package/test/dom/scroll.test.ts +0 -56
  277. package/test/dom/slot.test.tsx +0 -131
  278. package/test/dom/use-metadata.test.tsx +0 -58
  279. package/test/dom/view-transitions.test.tsx +0 -53
  280. package/test/dynamic-routing.test.ts +0 -132
  281. package/test/email-console.test.ts +0 -86
  282. package/test/email-preview.test.ts +0 -73
  283. package/test/emails.test.ts +0 -58
  284. package/test/errors.test.ts +0 -21
  285. package/test/example-guestbook.test.ts +0 -123
  286. package/test/features.test.ts +0 -149
  287. package/test/fixtures/bignum-wire/spec.ts +0 -25
  288. package/test/fixtures/daemon-app.ts +0 -65
  289. package/test/fixtures/stream-echo.ts +0 -26
  290. package/test/fixtures/stream-gate.ts +0 -28
  291. package/test/fixtures/stream-trap.ts +0 -18
  292. package/test/fixtures/stream-typed.ts +0 -41
  293. package/test/fonts.test.ts +0 -28
  294. package/test/global-setup.ts +0 -17
  295. package/test/head.test.ts +0 -42
  296. package/test/image-blur.test.ts +0 -50
  297. package/test/io.test.ts +0 -117
  298. package/test/metadata.test.ts +0 -40
  299. package/test/navlink.test.ts +0 -28
  300. package/test/pages.test.ts +0 -105
  301. package/test/placeholder.test.ts +0 -9
  302. package/test/pqauth-e2e.test.ts +0 -216
  303. package/test/pqauth-email.test.ts +0 -801
  304. package/test/prerender.test.ts +0 -116
  305. package/test/prettier-plugin.test.ts +0 -46
  306. package/test/routes.test.ts +0 -76
  307. package/test/rpc-bignum-wire.test.ts +0 -164
  308. package/test/rpc-dispatch.test.ts +0 -132
  309. package/test/rpc-kinds.test.ts +0 -18
  310. package/test/rpc.test.ts +0 -66
  311. package/test/search.test.ts +0 -114
  312. package/test/seo.test.ts +0 -175
  313. package/test/slot-layouts.test.ts +0 -69
  314. package/test/sri.test.ts +0 -129
  315. package/test/ssg.test.ts +0 -36
  316. package/test/ssr-hydration.test.tsx +0 -122
  317. package/test/ssr-render.test.ts +0 -197
  318. package/test/ssr-template.test.tsx +0 -482
  319. package/test/stream-emulation.test.ts +0 -479
  320. package/test/ui.test.ts +0 -40
  321. package/test/update.test.ts +0 -58
  322. package/test/validate.test.ts +0 -42
  323. package/test/version-check.test.ts +0 -130
  324. package/tests/data-parity/generated-parity.ts +0 -99
  325. package/tests/data-parity/parity.ts +0 -80
  326. package/tests/data-parity/spec.ts +0 -46
@@ -1,1102 +0,0 @@
1
- import { mkdtempSync, rmSync } from 'node:fs';
2
- import { tmpdir } from 'node:os';
3
- import { join } from 'node:path';
4
-
5
- import { afterEach, describe, expect, it } from 'vitest';
6
-
7
- import {
8
- CollectionFamily,
9
- DbFunctionKind,
10
- __resetDbForTests,
11
- __setDbCatalogForTests,
12
- buildDatabaseImports,
13
- commitDeriveCheckpoints,
14
- configureDbPersistence,
15
- derivesForWrites,
16
- freshDbState,
17
- parseDerives,
18
- persistDb,
19
- setDbCatalog,
20
- } from '../src/devserver/db/index.js';
21
- import { parseRouteKinds, routeKindForRequest } from '../src/devserver/db/routeKinds.js';
22
- import { dbFunctionKindForRequest } from '../src/devserver/runtime/module.js';
23
- import type { MemoryRef } from '../src/devserver/runtime/host.js';
24
-
25
- const DEFAULT_CATALOG = {
26
- 'App/users': { family: CollectionFamily.Record },
27
- 'App/posts': { family: CollectionFamily.Record },
28
- 'App/docs': { family: CollectionFamily.Record },
29
- 'App/challenges': { family: CollectionFamily.Record },
30
- 'App/pages': { family: CollectionFamily.View },
31
- 'App/feed': { family: CollectionFamily.Events },
32
- 'App/likes': { family: CollectionFamily.Counter },
33
- 'App/rooms': { family: CollectionFamily.Membership },
34
- 'App/usernames': { family: CollectionFamily.Unique },
35
- 'App/seats': { family: CollectionFamily.Capacity },
36
- 'App/tickets': { family: CollectionFamily.Capacity },
37
- };
38
-
39
- function setupRaw() {
40
- const memory = new WebAssembly.Memory({ initial: 1 });
41
- const ref: MemoryRef = { memory };
42
- const db = freshDbState();
43
- const imports = buildDatabaseImports(ref, db);
44
- const buf = Buffer.from(memory.buffer);
45
- return { ref, db, imports, buf };
46
- }
47
-
48
- function setup() {
49
- __setDbCatalogForTests(DEFAULT_CATALOG);
50
- return setupRaw();
51
- }
52
-
53
- /** Write bytes at `offset`, returning the `[ptr, len]` pair the imports expect. */
54
- function put(buf: Buffer, offset: number, data: string): [number, number] {
55
- const b = Buffer.from(data);
56
- b.copy(buf, offset);
57
- return [offset, b.length];
58
- }
59
-
60
- function resolve(
61
- imports: Record<string, (...a: number[]) => number>,
62
- buf: Buffer,
63
- name: string,
64
- ): number {
65
- const [p, l] = put(buf, 0, name);
66
- expect(imports['data.resolve_collection'](p, l, 16)).toBe(0);
67
- return buf.readUInt32LE(16);
68
- }
69
-
70
- function wasmWithSection(name: string, payload: Uint8Array): Buffer {
71
- const nameBytes = Buffer.from(name);
72
- const sectionPayload = Buffer.concat([
73
- Buffer.from([nameBytes.length]),
74
- nameBytes,
75
- Buffer.from(payload),
76
- ]);
77
- return Buffer.concat([
78
- Buffer.from([0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x00, sectionPayload.length]),
79
- sectionPayload,
80
- ]);
81
- }
82
-
83
- function routeKindsSection(routes: readonly (readonly [number, number, string])[]): Buffer {
84
- const chunks: Buffer[] = [Buffer.alloc(4)];
85
- chunks[0].writeUInt16LE(1, 0);
86
- chunks[0].writeUInt16LE(routes.length, 2);
87
- for (const [method, kind, pattern] of routes) {
88
- const p = Buffer.from(pattern, 'utf8');
89
- const header = Buffer.alloc(6);
90
- header.writeUInt8(method, 0);
91
- header.writeUInt8(kind, 1);
92
- header.writeUInt32LE(p.length, 2);
93
- chunks.push(header, p);
94
- }
95
- return Buffer.concat(chunks);
96
- }
97
-
98
- function derivesSection(entries: readonly (readonly [number, string, string])[]): Buffer {
99
- const chunks: Buffer[] = [Buffer.alloc(4)];
100
- chunks[0].writeUInt16LE(1, 0); // format_version
101
- chunks[0].writeUInt16LE(entries.length, 2); // n_derives
102
- for (const [id, db, method] of entries) {
103
- const head = Buffer.alloc(2);
104
- head.writeUInt16LE(id, 0);
105
- chunks.push(head);
106
- for (const s of [db, method]) {
107
- const b = Buffer.from(s, 'utf8');
108
- const len = Buffer.alloc(4);
109
- len.writeUInt32LE(b.length, 0);
110
- chunks.push(len, b);
111
- }
112
- }
113
- return Buffer.concat(chunks);
114
- }
115
-
116
- function catalogSectionV1(fillMaxWaitMs: number, fillAllowStale: number, replication = 5): Buffer {
117
- const chunks: Buffer[] = [];
118
- const u8 = (v: number) => chunks.push(Buffer.from([v & 0xff]));
119
- const u16 = (v: number) => {
120
- const b = Buffer.alloc(2);
121
- b.writeUInt16LE(v);
122
- chunks.push(b);
123
- };
124
- const u32 = (v: number) => {
125
- const b = Buffer.alloc(4);
126
- b.writeUInt32LE(v >>> 0);
127
- chunks.push(b);
128
- };
129
- const str = (s: string) => {
130
- const b = Buffer.from(s, 'utf8');
131
- u32(b.length);
132
- chunks.push(b);
133
- };
134
-
135
- u16(1); // catalog version
136
- u16(1); // databases
137
- str('App');
138
- u16(1); // collections
139
- str('users');
140
- u8(CollectionFamily.Record);
141
- str('UserId');
142
- str('User');
143
- u32(0xaaaa);
144
- u32(0x1234);
145
- u32(0); // generation
146
- u8(replication); // replication
147
- u8(0); // placement = hashKey
148
- u32(fillMaxWaitMs);
149
- u8(fillAllowStale);
150
- u16(0); // fields
151
- u16(0); // migrations
152
- return Buffer.concat(chunks);
153
- }
154
-
155
- afterEach(() => {
156
- __resetDbForTests();
157
- });
158
-
159
- describe('toildb dev emulator (record family)', () => {
160
- it('parses route-kind metadata and only tightens matching mutating routes', () => {
161
- const wasm = wasmWithSection(
162
- 'toildb.route_kinds',
163
- routeKindsSection([
164
- [1, 0, '/api/search'],
165
- [1, 0, '/api/items/:id'],
166
- ]),
167
- );
168
- const routes = parseRouteKinds(wasm);
169
-
170
- expect(routeKindForRequest(routes, 'POST', '/api/search?q=x')).toBe(DbFunctionKind.Query);
171
- expect(routeKindForRequest(routes, 'POST', '/api/items/42')).toBe(DbFunctionKind.Query);
172
- expect(routeKindForRequest(routes, 'GET', '/api/search')).toBeNull();
173
- expect(routeKindForRequest(routes, 'POST', '/api/items')).toBeNull();
174
- });
175
-
176
- it('matches the edge method clamp when route-kind metadata is absent', () => {
177
- expect(dbFunctionKindForRequest([], 'GET', '/api/users')).toBe(DbFunctionKind.Query);
178
- expect(dbFunctionKindForRequest([], 'HEAD', '/api/users')).toBe(DbFunctionKind.Query);
179
- expect(dbFunctionKindForRequest([], 'OPTIONS', '/api/users')).toBe(DbFunctionKind.Query);
180
- expect(dbFunctionKindForRequest([], 'POST', '/api/users')).toBe(DbFunctionKind.Action);
181
- expect(dbFunctionKindForRequest([], 'PUT', '/api/users')).toBe(DbFunctionKind.Action);
182
- expect(dbFunctionKindForRequest([], 'PATCH', '/api/users')).toBe(DbFunctionKind.Action);
183
- expect(dbFunctionKindForRequest([], 'DELETE', '/api/users')).toBe(DbFunctionKind.Action);
184
- });
185
-
186
- it('uses route-kind metadata only to tighten mutating routes to query', () => {
187
- const wasm = wasmWithSection(
188
- 'toildb.route_kinds',
189
- routeKindsSection([
190
- [1, 0, '/api/search'],
191
- [0, 1, '/api/users'],
192
- ]),
193
- );
194
- const routes = parseRouteKinds(wasm);
195
-
196
- expect(dbFunctionKindForRequest(routes, 'POST', '/api/search')).toBe(DbFunctionKind.Query);
197
- expect(dbFunctionKindForRequest(routes, 'POST', '/api/write')).toBe(DbFunctionKind.Action);
198
- expect(dbFunctionKindForRequest(routes, 'GET', '/api/users')).toBe(DbFunctionKind.Query);
199
- });
200
-
201
- it('ignores malformed route-kind metadata like the edge method clamp fallback', () => {
202
- expect(
203
- parseRouteKinds(wasmWithSection('toildb.route_kinds', Buffer.from([1, 0, 1, 0, 1]))),
204
- ).toEqual([]);
205
- expect(
206
- parseRouteKinds(
207
- wasmWithSection('toildb.route_kinds', routeKindsSection([[1, 0, 'relative']])),
208
- ),
209
- ).toEqual([]);
210
- expect(
211
- parseRouteKinds(
212
- wasmWithSection('toildb.route_kinds', routeKindsSection([[1, 0, '/snowman-☃']])),
213
- ),
214
- ).toEqual([]);
215
-
216
- const invalidUtf8 = Buffer.concat([
217
- Buffer.from([1, 0, 1, 0, 1, 0, 2, 0, 0, 0]),
218
- Buffer.from([0xc3, 0x28]),
219
- ]);
220
- expect(parseRouteKinds(wasmWithSection('toildb.route_kinds', invalidUtf8))).toEqual([]);
221
- });
222
-
223
- it('rejects unknown catalog collections instead of minting arbitrary handles', () => {
224
- const { imports, buf } = setup();
225
- const [p, l] = put(buf, 0, 'App/missing');
226
- buf.writeUInt32LE(0xdeadbeef, 16);
227
- expect(imports['data.resolve_collection'](p, l, 16)).toBe(-1070);
228
- expect(buf.readUInt32LE(16)).toBe(0xdeadbeef);
229
- });
230
-
231
- it('rejects a present but malformed catalog section', () => {
232
- setDbCatalog(wasmWithSection('toildb.catalog', Buffer.from([0xff, 0x00, 0xde, 0xad])));
233
- const { imports, buf } = setupRaw();
234
- const [p, l] = put(buf, 0, 'App/users');
235
-
236
- expect(imports['data.resolve_collection'](p, l, 16)).toBe(-1070);
237
- });
238
-
239
- it('parses catalog v1 fill policy and backend replication bytes', () => {
240
- setDbCatalog(wasmWithSection('toildb.catalog', catalogSectionV1(7, 0)));
241
- const { imports, buf, db } = setupRaw();
242
- const h = resolve(imports, buf, 'App/users');
243
-
244
- expect(db.handles[h]).toMatchObject({
245
- name: 'App/users',
246
- family: CollectionFamily.Record,
247
- schemaVersion: 0x1234,
248
- replication: 5,
249
- placement: 0,
250
- fillMaxWaitMs: 7,
251
- fillAllowStale: false,
252
- });
253
- });
254
-
255
- it('rejects replication policies that require explicit policy metadata', () => {
256
- for (const replication of [3, 4]) {
257
- setDbCatalog(wasmWithSection('toildb.catalog', catalogSectionV1(7, 0, replication)));
258
- const { imports, buf } = setupRaw();
259
- const [p, l] = put(buf, 0, 'App/users');
260
-
261
- expect(imports['data.resolve_collection'](p, l, 16)).toBe(-1070);
262
- }
263
- });
264
-
265
- it('rejects malformed catalog v1 fill policy', () => {
266
- setDbCatalog(wasmWithSection('toildb.catalog', catalogSectionV1(7, 2)));
267
- const { imports, buf } = setupRaw();
268
- const [p, l] = put(buf, 0, 'App/users');
269
-
270
- expect(imports['data.resolve_collection'](p, l, 16)).toBe(-1070);
271
- });
272
-
273
- it('rejects handles used with the wrong collection family', () => {
274
- const { imports, buf } = setup();
275
- const users = resolve(imports, buf, 'App/users');
276
- const likes = resolve(imports, buf, 'App/likes');
277
- const [kPtr, kLen] = put(buf, 32, 'k');
278
- const [vPtr, vLen] = put(buf, 48, 'v');
279
-
280
- expect(imports['data.counter_add'](users, kPtr, kLen, 1, 0)).toBe(-1010);
281
- expect(imports['data.create'](likes, kPtr, kLen, vPtr, vLen, 0)).toBe(-1010);
282
- });
283
-
284
- it('query-kind dispatch denies writes and write_allowed is false', () => {
285
- const { imports, buf, db } = setup();
286
- db.functionKind = DbFunctionKind.Query;
287
- const h = resolve(imports, buf, 'App/users');
288
- const [kPtr, kLen] = put(buf, 32, 'u1');
289
- const [vPtr, vLen] = put(buf, 48, 'hello');
290
-
291
- expect(imports['data.write_allowed']()).toBe(0);
292
- expect(imports['data.get'](h, kPtr, kLen)).toBe(-2);
293
- expect(imports['data.create'](h, kPtr, kLen, vPtr, vLen, 0)).toBe(-1011);
294
- });
295
-
296
- it('query/action dispatch deny scan-class imports', () => {
297
- const { imports, buf, db } = setup();
298
- const feed = resolve(imports, buf, 'App/feed');
299
- const rooms = resolve(imports, buf, 'App/rooms');
300
- const [kPtr, kLen] = put(buf, 32, 'room1');
301
- const [vPtr, vLen] = put(buf, 48, 'ev1');
302
- const [mPtr, mLen] = put(buf, 64, 'alice');
303
-
304
- expect(imports['data.append'](feed, kPtr, kLen, vPtr, vLen, 0)).toBe(0);
305
- expect(imports['data.membership_add'](rooms, kPtr, kLen, mPtr, mLen, 0)).toBe(0);
306
-
307
- db.functionKind = DbFunctionKind.Query;
308
- expect(imports['data.latest'](feed, kPtr, kLen, 10)).toBe(-1011);
309
- expect(imports['data.membership_list'](rooms, kPtr, kLen, 10)).toBe(-1011);
310
-
311
- db.functionKind = DbFunctionKind.Action;
312
- expect(imports['data.latest'](feed, kPtr, kLen, 10)).toBe(-1011);
313
- expect(imports['data.membership_list'](rooms, kPtr, kLen, 10)).toBe(-1011);
314
-
315
- db.functionKind = DbFunctionKind.Job;
316
- expect(imports['data.latest'](feed, kPtr, kLen, 10)).toBeGreaterThan(0);
317
- expect(imports['data.membership_list'](rooms, kPtr, kLen, 10)).toBeGreaterThan(0);
318
- });
319
-
320
- it('resolve + create + get + take_result round-trips', () => {
321
- const { imports, buf } = setup();
322
- const h = resolve(imports, buf, 'App/users');
323
- const [kPtr, kLen] = put(buf, 32, 'u1');
324
- const [vPtr, vLen] = put(buf, 48, 'hello');
325
-
326
- expect(imports['data.create'](h, kPtr, kLen, vPtr, vLen, 0)).toBe(0);
327
- expect(imports['data.create'](h, kPtr, kLen, vPtr, vLen, 0)).toBe(-1003); // AlreadyExists
328
- expect(imports['data.exists'](h, kPtr, kLen)).toBe(1);
329
-
330
- expect(imports['data.get'](h, kPtr, kLen)).toBe(5);
331
- expect(imports['data.take_result'](64, 64)).toBe(5);
332
- expect(buf.toString('utf8', 64, 69)).toBe('hello');
333
- });
334
-
335
- it('patch updates and a following get sees the new value', () => {
336
- const { imports, buf } = setup();
337
- const h = resolve(imports, buf, 'App/users');
338
- const [kPtr, kLen] = put(buf, 32, 'u1');
339
- const [vPtr, vLen] = put(buf, 48, 'hello');
340
- imports['data.create'](h, kPtr, kLen, vPtr, vLen, 0);
341
-
342
- const [pPtr, pLen] = put(buf, 80, 'world!');
343
- expect(imports['data.patch'](h, kPtr, kLen, pPtr, pLen, 0)).toBe(6);
344
- expect(imports['data.get'](h, kPtr, kLen)).toBe(6);
345
- imports['data.take_result'](128, 64);
346
- expect(buf.toString('utf8', 128, 134)).toBe('world!');
347
- });
348
-
349
- it('record patch idempotency replays and rejects request mismatch', () => {
350
- const { imports, buf } = setup();
351
- const h = resolve(imports, buf, 'App/users');
352
- const [kPtr, kLen] = put(buf, 32, 'u1');
353
- const [vPtr, vLen] = put(buf, 48, 'hello');
354
- const [idemPtr] = put(buf, 64, '1234567890abcdef');
355
- imports['data.create'](h, kPtr, kLen, vPtr, vLen, 0);
356
-
357
- const [pPtr, pLen] = put(buf, 96, 'world!');
358
- expect(imports['data.patch'](h, kPtr, kLen, pPtr, pLen, idemPtr)).toBe(6);
359
- expect(imports['data.patch'](h, kPtr, kLen, pPtr, pLen, idemPtr)).toBe(6);
360
- expect(imports['data.take_result'](128, 64)).toBe(6);
361
- expect(buf.toString('utf8', 128, 134)).toBe('world!');
362
-
363
- const [otherPtr, otherLen] = put(buf, 160, 'again!');
364
- expect(imports['data.patch'](h, kPtr, kLen, otherPtr, otherLen, idemPtr)).toBe(-1004);
365
- expect(imports['data.get'](h, kPtr, kLen)).toBe(6);
366
- expect(imports['data.take_result'](192, 64)).toBe(6);
367
- expect(buf.toString('utf8', 192, 198)).toBe('world!');
368
- });
369
-
370
- it('patch on a missing key is NotFound', () => {
371
- const { imports, buf } = setup();
372
- const h = resolve(imports, buf, 'App/users');
373
- const [kPtr, kLen] = put(buf, 32, 'ghost');
374
- const [pPtr, pLen] = put(buf, 48, 'x');
375
- expect(imports['data.patch'](h, kPtr, kLen, pPtr, pLen, 0)).toBe(-2); // NotFound -> ABSENT
376
- });
377
-
378
- it('consume-once get_delete deletes exactly once', () => {
379
- const { imports, buf } = setup();
380
- const h = resolve(imports, buf, 'App/challenges');
381
- const [kPtr, kLen] = put(buf, 32, 'chal');
382
- const [vPtr, vLen] = put(buf, 48, 'nonce');
383
- imports['data.create'](h, kPtr, kLen, vPtr, vLen, 0);
384
-
385
- expect(imports['data.get_delete'](h, kPtr, kLen, 0)).toBe(5);
386
- imports['data.take_result'](64, 64);
387
- expect(buf.toString('utf8', 64, 69)).toBe('nonce');
388
- // replay defeated
389
- expect(imports['data.get_delete'](h, kPtr, kLen, 0)).toBe(-2);
390
- });
391
-
392
- it('record get_delete idempotency replays the consumed value', () => {
393
- const { imports, buf } = setup();
394
- const h = resolve(imports, buf, 'App/challenges');
395
- const [kPtr, kLen] = put(buf, 32, 'chal');
396
- const [vPtr, vLen] = put(buf, 48, 'nonce');
397
- const [idemPtr] = put(buf, 80, 'consume-idem-001');
398
- imports['data.create'](h, kPtr, kLen, vPtr, vLen, 0);
399
-
400
- expect(imports['data.get_delete'](h, kPtr, kLen, idemPtr)).toBe(5);
401
- expect(imports['data.take_result'](96, 64)).toBe(5);
402
- expect(buf.toString('utf8', 96, 101)).toBe('nonce');
403
-
404
- expect(imports['data.get_delete'](h, kPtr, kLen, idemPtr)).toBe(5);
405
- expect(imports['data.take_result'](128, 64)).toBe(5);
406
- expect(buf.toString('utf8', 128, 133)).toBe('nonce');
407
- expect(imports['data.get'](h, kPtr, kLen)).toBe(-2);
408
- });
409
-
410
- it('absent / invalid handle / buffer-too-small return the edge codes', () => {
411
- const { imports, buf } = setup();
412
- const h = resolve(imports, buf, 'App/users');
413
- const [kPtr, kLen] = put(buf, 32, 'k');
414
-
415
- expect(imports['data.get'](h, kPtr, kLen)).toBe(-2); // absent
416
- expect(imports['data.get'](999, kPtr, kLen)).toBe(-1001); // invalid handle
417
-
418
- const [vPtr, vLen] = put(buf, 48, 'bigvalue');
419
- imports['data.create'](h, kPtr, kLen, vPtr, vLen, 0);
420
- imports['data.get'](h, kPtr, kLen);
421
- expect(imports['data.take_result'](64, 2)).toBe(-1); // too small, stash kept
422
- expect(imports['data.take_result'](64, 64)).toBe(8); // retry succeeds
423
- });
424
-
425
- it('unique claim: Claimed / AlreadyOwnedByCaller / AlreadyClaimed', () => {
426
- const { imports, buf } = setup();
427
- const h = resolve(imports, buf, 'App/usernames');
428
- const [kPtr, kLen] = put(buf, 32, 'ada');
429
- const [u1Ptr, u1Len] = put(buf, 48, 'user_1');
430
- const [u2Ptr, u2Len] = put(buf, 64, 'user_2');
431
-
432
- expect(imports['data.unique_claim'](h, kPtr, kLen, u1Ptr, u1Len, 0)).toBe(0); // Claimed
433
- expect(imports['data.unique_claim'](h, kPtr, kLen, u1Ptr, u1Len, 0)).toBe(2); // AlreadyOwnedByCaller
434
- // a different owner -> AlreadyClaimed, current owner stashed
435
- expect(imports['data.unique_claim'](h, kPtr, kLen, u2Ptr, u2Len, 0)).toBe(1);
436
- expect(imports['data.take_result'](128, 64)).toBe(6);
437
- expect(buf.toString('utf8', 128, 134)).toBe('user_1');
438
-
439
- // lookup returns the owner
440
- expect(imports['data.unique_lookup'](h, kPtr, kLen)).toBe(6);
441
- imports['data.take_result'](200, 64);
442
- expect(buf.toString('utf8', 200, 206)).toBe('user_1');
443
- });
444
-
445
- it('unique claim: same owner must replay only with the same idempotency key', () => {
446
- const { imports, buf } = setup();
447
- const h = resolve(imports, buf, 'App/usernames');
448
- const [kPtr, kLen] = put(buf, 32, 'grace');
449
- const [u1Ptr, u1Len] = put(buf, 48, 'user_1');
450
- const [idem1Ptr] = put(buf, 80, 'idem-claim-0001');
451
- const [idem2Ptr] = put(buf, 112, 'idem-claim-0002');
452
-
453
- expect(imports['data.unique_claim'](h, kPtr, kLen, u1Ptr, u1Len, idem1Ptr)).toBe(0);
454
- expect(imports['data.unique_claim'](h, kPtr, kLen, u1Ptr, u1Len, idem1Ptr)).toBe(2);
455
- expect(imports['data.unique_claim'](h, kPtr, kLen, u1Ptr, u1Len, idem2Ptr)).toBe(-1004);
456
- });
457
-
458
- it('unique release: only the owner may release', () => {
459
- const { imports, buf } = setup();
460
- const h = resolve(imports, buf, 'App/usernames');
461
- const [kPtr, kLen] = put(buf, 32, 'ada');
462
- const [u1Ptr, u1Len] = put(buf, 48, 'user_1');
463
- const [u2Ptr, u2Len] = put(buf, 64, 'user_2');
464
- imports['data.unique_claim'](h, kPtr, kLen, u1Ptr, u1Len, 0);
465
-
466
- expect(imports['data.unique_release'](h, kPtr, kLen, u2Ptr, u2Len, 0)).toBe(-1004); // not owner
467
- expect(imports['data.unique_release'](h, kPtr, kLen, u1Ptr, u1Len, 0)).toBe(0); // owner releases
468
- expect(imports['data.unique_lookup'](h, kPtr, kLen)).toBe(-2); // gone
469
- });
470
-
471
- it('get_many: framed multi-get preserves order with present/absent', () => {
472
- const { imports, buf } = setup();
473
- const h = resolve(imports, buf, 'App/users');
474
- const [k1, l1] = put(buf, 32, 'u1');
475
- const [vA, lA] = put(buf, 48, 'AA');
476
- imports['data.create'](h, k1, l1, vA, lA, 0);
477
- const [k2, l2] = put(buf, 64, 'u2');
478
- const [vB, lB] = put(buf, 80, 'BB');
479
- imports['data.create'](h, k2, l2, vB, lB, 0);
480
-
481
- // keys blob at 128: count=3, then "u1","u3","u2" (u3 absent).
482
- let o = 128;
483
- o = buf.writeUInt32LE(3, o);
484
- for (const k of ['u1', 'u3', 'u2']) {
485
- o = buf.writeUInt32LE(2, o);
486
- o += buf.write(k, o, 'latin1');
487
- }
488
- const n = imports['data.get_many'](h, 128, o - 128);
489
- expect(n).toBeGreaterThan(0);
490
- expect(imports['data.take_result'](256, 256)).toBe(n);
491
-
492
- let p = 256;
493
- const count = buf.readUInt32LE(p);
494
- p += 4;
495
- expect(count).toBe(3);
496
- const got: (string | null)[] = [];
497
- for (let i = 0; i < count; i++) {
498
- const present = buf.readUInt8(p);
499
- p += 1;
500
- if (present === 0) {
501
- got.push(null);
502
- continue;
503
- }
504
- p += 4; // per-item schema_version (0 in dev)
505
- const len = buf.readUInt32LE(p);
506
- p += 4;
507
- got.push(buf.toString('utf8', p, p + len));
508
- p += len;
509
- }
510
- expect(got).toEqual(['AA', null, 'BB']);
511
-
512
- expect(imports['data.get_many'](999, 128, o - 128)).toBe(-1001); // invalid handle
513
- });
514
-
515
- it('membership: add/contains/remove + sorted framed list', () => {
516
- const { imports, buf } = setup();
517
- const h = resolve(imports, buf, 'App/rooms');
518
- const [sPtr, sLen] = put(buf, 32, 'room1');
519
-
520
- const [aPtr, aLen] = put(buf, 48, 'alice');
521
- const [bPtr, bLen] = put(buf, 64, 'bob');
522
- expect(imports['data.membership_contains'](h, sPtr, sLen, aPtr, aLen)).toBe(0); // absent
523
- expect(imports['data.membership_add'](h, sPtr, sLen, aPtr, aLen, 0)).toBe(0);
524
- expect(imports['data.membership_add'](h, sPtr, sLen, bPtr, bLen, 0)).toBe(0);
525
- expect(imports['data.membership_add'](h, sPtr, sLen, aPtr, aLen, 0)).toBe(0); // idempotent
526
- expect(imports['data.membership_contains'](h, sPtr, sLen, aPtr, aLen)).toBe(1);
527
-
528
- // list -> framed u32 count + per member (u32 len + bytes), sorted (alice, bob).
529
- const total = imports['data.membership_list'](h, sPtr, sLen, 10);
530
- expect(imports['data.take_result'](128, 128)).toBe(total);
531
- let off = 128;
532
- const count = buf.readUInt32LE(off);
533
- off += 4;
534
- expect(count).toBe(2);
535
- const out: string[] = [];
536
- for (let i = 0; i < count; i++) {
537
- off += 4; // per-item schema_version (0 in dev)
538
- const len = buf.readUInt32LE(off);
539
- off += 4;
540
- out.push(buf.toString('utf8', off, off + len));
541
- off += len;
542
- }
543
- expect(out).toEqual(['alice', 'bob']);
544
-
545
- // remove is idempotent; the member is gone.
546
- expect(imports['data.membership_remove'](h, sPtr, sLen, aPtr, aLen, 0)).toBe(0);
547
- expect(imports['data.membership_remove'](h, sPtr, sLen, aPtr, aLen, 0)).toBe(0);
548
- expect(imports['data.membership_contains'](h, sPtr, sLen, aPtr, aLen)).toBe(0);
549
- expect(imports['data.membership_contains'](h, sPtr, sLen, bPtr, bLen)).toBe(1);
550
-
551
- expect(imports['data.membership_add'](999, sPtr, sLen, aPtr, aLen, 0)).toBe(-1001); // bad handle
552
- });
553
-
554
- it('view: publish overwrites and get reads the latest (or absent)', () => {
555
- const { imports, buf } = setup();
556
- const h = resolve(imports, buf, 'App/pages');
557
- const [kPtr, kLen] = put(buf, 32, 'home');
558
-
559
- // absent before any publish
560
- expect(imports['data.view_get'](h, kPtr, kLen)).toBe(-2);
561
-
562
- const [v1Ptr, v1Len] = put(buf, 48, '<v1>');
563
- expect(imports['data.view_publish'](h, kPtr, kLen, v1Ptr, v1Len, 0)).toBe(0);
564
- expect(imports['data.view_get'](h, kPtr, kLen)).toBe(4);
565
- expect(imports['data.take_result'](64, 64)).toBe(4);
566
- expect(buf.toString('utf8', 64, 68)).toBe('<v1>');
567
-
568
- // republish overwrites (latest wins)
569
- const [v2Ptr, v2Len] = put(buf, 80, '<page2>');
570
- expect(imports['data.view_publish'](h, kPtr, kLen, v2Ptr, v2Len, 0)).toBe(0);
571
- expect(imports['data.view_get'](h, kPtr, kLen)).toBe(7);
572
- imports['data.take_result'](128, 64);
573
- expect(buf.toString('utf8', 128, 135)).toBe('<page2>');
574
-
575
- expect(imports['data.view_get'](999, kPtr, kLen)).toBe(-1001); // invalid handle
576
- });
577
-
578
- it('counter: add accumulates (saturating i64) and get reads the sum', () => {
579
- const { imports, buf } = setup();
580
- const h = resolve(imports, buf, 'App/likes');
581
- const [kPtr, kLen] = put(buf, 32, 'post1');
582
-
583
- // empty counter reads as 0 (8 LE bytes), never absent.
584
- expect(imports['data.counter_get'](h, kPtr, kLen)).toBe(8);
585
- expect(imports['data.take_result'](64, 8)).toBe(8);
586
- expect(buf.readBigInt64LE(64)).toBe(0n);
587
-
588
- expect(imports['data.counter_add'](h, kPtr, kLen, 5, 0)).toBe(0);
589
- expect(imports['data.counter_add'](h, kPtr, kLen, -2, 0)).toBe(0);
590
- expect(imports['data.counter_get'](h, kPtr, kLen)).toBe(8);
591
- imports['data.take_result'](72, 8);
592
- expect(buf.readBigInt64LE(72)).toBe(3n);
593
-
594
- const [idemPtr] = put(buf, 96, 'counter-idem-001');
595
- expect(imports['data.counter_add'](h, kPtr, kLen, 7, idemPtr)).toBe(0);
596
- expect(imports['data.counter_add'](h, kPtr, kLen, 7, idemPtr)).toBe(0);
597
- expect(imports['data.counter_add'](h, kPtr, kLen, 8, idemPtr)).toBe(-1004);
598
- expect(imports['data.counter_get'](h, kPtr, kLen)).toBe(8);
599
- imports['data.take_result'](104, 8);
600
- expect(buf.readBigInt64LE(104)).toBe(10n);
601
-
602
- // invalid handle still rejected
603
- expect(imports['data.counter_add'](999, kPtr, kLen, 1, 0)).toBe(-1001);
604
- });
605
-
606
- it('events: append then latest returns newest-first, bounded by limit', () => {
607
- const { imports, buf } = setup();
608
- const h = resolve(imports, buf, 'App/feed');
609
- const [kPtr, kLen] = put(buf, 32, 'room1');
610
-
611
- for (let i = 0; i < 4; i++) {
612
- const [eP, eL] = put(buf, 48, `ev${String(i)}`);
613
- expect(imports['data.append'](h, kPtr, kLen, eP, eL, 0)).toBe(0);
614
- }
615
-
616
- // latest(2) -> framed: u32 count=2, then [len+bytes] newest-first (ev3, ev2).
617
- const total = imports['data.latest'](h, kPtr, kLen, 2);
618
- expect(total).toBeGreaterThan(0);
619
- expect(imports['data.take_result'](128, 128)).toBe(total);
620
- let off = 128;
621
- const count = buf.readUInt32LE(off);
622
- off += 4;
623
- expect(count).toBe(2);
624
- const out: string[] = [];
625
- for (let i = 0; i < count; i++) {
626
- off += 4; // per-item schema_version (0 in dev)
627
- const len = buf.readUInt32LE(off);
628
- off += 4;
629
- out.push(buf.toString('utf8', off, off + len));
630
- off += len;
631
- }
632
- expect(out).toEqual(['ev3', 'ev2']);
633
-
634
- // an empty stream frames an empty list (count 0, 4 bytes), never absent.
635
- const [k2P, k2L] = put(buf, 256, 'empty');
636
- expect(imports['data.latest'](h, k2P, k2L, 10)).toBe(4);
637
- imports['data.take_result'](300, 8);
638
- expect(buf.readUInt32LE(300)).toBe(0);
639
- });
640
-
641
- it('events.since: drains oldest-first, stages within a run, commits on success, resumes across runs', () => {
642
- const { ref, imports, buf, db } = setup();
643
- const h = resolve(imports, buf, 'App/feed');
644
- const [kPtr, kLen] = put(buf, 32, 'room1');
645
- for (let i = 0; i < 3; i++) {
646
- const [eP, eL] = put(buf, 48, `e${String(i)}`);
647
- expect(imports['data.append'](h, kPtr, kLen, eP, eL, 0)).toBe(0);
648
- }
649
-
650
- // Decode a framed since batch (u32 count, then per-event u32 ver + u32 len + bytes), oldest-first.
651
- const drain = (
652
- imps: Record<string, (...a: number[]) => number>,
653
- total: number,
654
- at: number,
655
- ): string[] => {
656
- expect(imps['data.take_result'](at, 256)).toBe(total);
657
- let off = at;
658
- const count = buf.readUInt32LE(off);
659
- off += 4;
660
- const out: string[] = [];
661
- for (let i = 0; i < count; i++) {
662
- off += 4; // schema_version (0 in dev)
663
- const len = buf.readUInt32LE(off);
664
- off += 4;
665
- out.push(buf.toString('utf8', off, off + len));
666
- off += len;
667
- }
668
- return out;
669
- };
670
-
671
- // A derive run drains the log in bounded batches, resuming from the STAGED cursor within the run.
672
- db.functionKind = DbFunctionKind.Derive;
673
- db.deriveId = 0;
674
- expect(drain(imports, imports['data.events_since'](h, kPtr, kLen, 2), 512)).toEqual(['e0', 'e1']);
675
- expect(drain(imports, imports['data.events_since'](h, kPtr, kLen, 2), 768)).toEqual(['e2']);
676
- expect(drain(imports, imports['data.events_since'](h, kPtr, kLen, 2), 1024)).toEqual([]); // caught up
677
-
678
- // The advance is only STAGED until the fold's derive_run returns; commit persists it.
679
- expect(db.pendingCheckpoints.size).toBeGreaterThan(0);
680
- commitDeriveCheckpoints(db);
681
- expect(db.pendingCheckpoints.size).toBe(0);
682
-
683
- // A fresh derive run (new per-request state, same shared store) resumes PAST the committed cursor:
684
- // it re-reads nothing, then folds only a newly appended event.
685
- const db2 = freshDbState();
686
- db2.functionKind = DbFunctionKind.Derive;
687
- db2.deriveId = 0;
688
- const imports2 = buildDatabaseImports(ref, db2) as Record<string, (...a: number[]) => number>;
689
- const h2 = resolve(imports2, buf, 'App/feed');
690
- expect(drain(imports2, imports2['data.events_since'](h2, kPtr, kLen, 10), 512)).toEqual([]);
691
- const [e3P, e3L] = put(buf, 48, 'e3');
692
- expect(imports2['data.append'](h2, kPtr, kLen, e3P, e3L, 0)).toBe(0);
693
- expect(drain(imports2, imports2['data.events_since'](h2, kPtr, kLen, 10), 512)).toEqual(['e3']);
694
- });
695
-
696
- it('events.since: a derive run that does not commit leaves the durable cursor unmoved (trap-safety)', () => {
697
- const { ref, imports, buf, db } = setup();
698
- const h = resolve(imports, buf, 'App/feed');
699
- const [kPtr, kLen] = put(buf, 32, 'room1');
700
- for (let i = 0; i < 2; i++) {
701
- const [eP, eL] = put(buf, 48, `e${String(i)}`);
702
- expect(imports['data.append'](h, kPtr, kLen, eP, eL, 0)).toBe(0);
703
- }
704
- const count = (imps: Record<string, (...a: number[]) => number>, total: number, at: number): number => {
705
- expect(imps['data.take_result'](at, 256)).toBe(total);
706
- return buf.readUInt32LE(at);
707
- };
708
-
709
- // Run 1 drains both events but NEVER commits (simulating a fold that trapped before returning): the
710
- // staged advance lives only on this discarded per-request state.
711
- db.functionKind = DbFunctionKind.Derive;
712
- db.deriveId = 0;
713
- expect(count(imports, imports['data.events_since'](h, kPtr, kLen, 10), 512)).toBe(2);
714
-
715
- // Run 2 (fresh per-request state) still sees BOTH events: the durable cursor never moved.
716
- const db2 = freshDbState();
717
- db2.functionKind = DbFunctionKind.Derive;
718
- db2.deriveId = 0;
719
- const imports2 = buildDatabaseImports(ref, db2) as Record<string, (...a: number[]) => number>;
720
- const h2 = resolve(imports2, buf, 'App/feed');
721
- expect(count(imports2, imports2['data.events_since'](h2, kPtr, kLen, 10), 512)).toBe(2);
722
- });
723
-
724
- it('__resetDbForTests clears events.since checkpoints (no cross-test cursor leak)', () => {
725
- {
726
- const { imports, buf, db } = setup();
727
- const h = resolve(imports, buf, 'App/feed');
728
- const [kPtr, kLen] = put(buf, 32, 'room1');
729
- const [eP, eL] = put(buf, 48, 'e0');
730
- expect(imports['data.append'](h, kPtr, kLen, eP, eL, 0)).toBe(0);
731
- db.functionKind = DbFunctionKind.Derive;
732
- db.deriveId = 0;
733
- imports['data.events_since'](h, kPtr, kLen, 10);
734
- commitDeriveCheckpoints(db); // durable cursor now at 1
735
- }
736
- __resetDbForTests();
737
- {
738
- // A brand-new store reusing the same (deriveId, key): the cursor must be back at 0, so a fresh
739
- // append is read, not skipped by a stale checkpoint.
740
- const { imports, buf, db } = setup();
741
- const h = resolve(imports, buf, 'App/feed');
742
- const [kPtr, kLen] = put(buf, 32, 'room1');
743
- const [eP, eL] = put(buf, 48, 'fresh0');
744
- expect(imports['data.append'](h, kPtr, kLen, eP, eL, 0)).toBe(0);
745
- db.functionKind = DbFunctionKind.Derive;
746
- db.deriveId = 0;
747
- const total = imports['data.events_since'](h, kPtr, kLen, 10);
748
- expect(imports['data.take_result'](512, 256)).toBe(total);
749
- expect(buf.readUInt32LE(512)).toBe(1); // the fresh event, not skipped
750
- }
751
- });
752
-
753
- it('collections are isolated (no key aliasing)', () => {
754
- const { imports, buf } = setup();
755
- const users = resolve(imports, buf, 'App/users');
756
- const [n2p, n2l] = put(buf, 256, 'App/posts');
757
- expect(imports['data.resolve_collection'](n2p, n2l, 260)).toBe(0);
758
- const posts = buf.readUInt32LE(260);
759
-
760
- const [kPtr, kLen] = put(buf, 32, 'x');
761
- const [vPtr, vLen] = put(buf, 48, 'inusers');
762
- imports['data.create'](users, kPtr, kLen, vPtr, vLen, 0);
763
- // the same logical key in another collection is absent.
764
- expect(imports['data.get'](posts, kPtr, kLen)).toBe(-2);
765
- });
766
-
767
- it('append_once dedups on eventId; enqueue replaces an existing record', () => {
768
- const { imports, buf } = setup();
769
- const feed = resolve(imports, buf, 'App/feed');
770
- const [kPtr, kLen] = put(buf, 32, 'room1');
771
- const [idPtr, idLen] = put(buf, 64, 'evt-1');
772
- const [evPtr, evLen] = put(buf, 96, 'hello');
773
- // first appendOnce appends (1); the same id is a no-op (0); a new id appends (1).
774
- expect(imports['data.append_once'](feed, kPtr, kLen, idPtr, idLen, evPtr, evLen)).toBe(1);
775
- expect(imports['data.append_once'](feed, kPtr, kLen, idPtr, idLen, evPtr, evLen)).toBe(0);
776
- const [id2P, id2L] = put(buf, 128, 'evt-2');
777
- expect(imports['data.append_once'](feed, kPtr, kLen, id2P, id2L, evPtr, evLen)).toBe(1);
778
- const [appendIdemPtr] = put(buf, 144, 'append-idem-0001');
779
- expect(imports['data.append'](feed, kPtr, kLen, evPtr, evLen, appendIdemPtr)).toBe(0);
780
- expect(imports['data.append'](feed, kPtr, kLen, evPtr, evLen, appendIdemPtr)).toBe(0);
781
- // latest frames exactly 2 events (the duplicate did not double-append).
782
- const total = imports['data.latest'](feed, kPtr, kLen, 10);
783
- expect(total).toBeGreaterThan(0);
784
- imports['data.take_result'](512, total);
785
- expect(buf.readUInt32LE(512)).toBe(3);
786
-
787
- // enqueue: absent -> ABSENT (-2); after create -> replaces (0); get sees the new value.
788
- const docs = resolve(imports, buf, 'App/docs');
789
- const [dkP, dkL] = put(buf, 160, 'doc1');
790
- const [v1P, v1L] = put(buf, 192, 'AAAA');
791
- expect(imports['data.enqueue'](docs, dkP, dkL, v1P, v1L, 0)).toBe(-2);
792
- expect(imports['data.create'](docs, dkP, dkL, v1P, v1L, 0)).toBe(0);
793
- const [v2P, v2L] = put(buf, 224, 'BBBB');
794
- expect(imports['data.enqueue'](docs, dkP, dkL, v2P, v2L, 0)).toBe(0);
795
- expect(imports['data.get'](docs, dkP, dkL)).toBe(4);
796
- imports['data.take_result'](256, 4);
797
- expect(buf.toString('utf8', 256, 260)).toBe('BBBB');
798
- });
799
- });
800
-
801
- type Imports = Record<string, (...args: number[]) => number>;
802
-
803
- describe('toildb dev emulator (capacity family)', () => {
804
- // Pull the last stashed i64 available into the buffer and read it.
805
- function avail(imports: Imports, buf: Buffer, h: number, kPtr: number, kLen: number): bigint {
806
- expect(imports['data.capacity_available'](h, kPtr, kLen)).toBe(8);
807
- expect(imports['data.take_result'](512, 16)).toBe(8);
808
- return buf.readBigInt64LE(512);
809
- }
810
-
811
- it('set_total / reserve / confirm / cancel keep the ledger consistent', () => {
812
- const { imports, buf } = setup();
813
- const h = resolve(imports, buf, 'App/seats');
814
- const [kPtr, kLen] = put(buf, 32, 'showA');
815
-
816
- // an empty ledger reads 0 available, never absent.
817
- expect(avail(imports, buf, h, kPtr, kLen)).toBe(0n);
818
-
819
- // restock to 10.
820
- expect(imports['data.capacity_set_total'](h, kPtr, kLen, 10, 0)).toBe(0);
821
- expect(avail(imports, buf, h, kPtr, kLen)).toBe(10n);
822
-
823
- // reserve 3 -> a u64 id (> 0) is stashed, available drops to 7.
824
- expect(imports['data.capacity_reserve'](h, kPtr, kLen, 3, 60000, 0)).toBe(8);
825
- expect(imports['data.take_result'](512, 16)).toBe(8);
826
- const id = buf.readBigUInt64LE(512);
827
- expect(id > 0n).toBe(true);
828
- expect(avail(imports, buf, h, kPtr, kLen)).toBe(7n);
829
-
830
- const [idemPtr] = put(buf, 64, 'reserve-idem-001');
831
- expect(imports['data.capacity_reserve'](h, kPtr, kLen, 2, 60000, idemPtr)).toBe(8);
832
- expect(imports['data.take_result'](520, 16)).toBe(8);
833
- const idemId = buf.readBigUInt64LE(520);
834
- expect(imports['data.capacity_reserve'](h, kPtr, kLen, 2, 60000, idemPtr)).toBe(8);
835
- expect(imports['data.take_result'](528, 16)).toBe(8);
836
- expect(buf.readBigUInt64LE(528)).toBe(idemId);
837
- expect(imports['data.capacity_reserve'](h, kPtr, kLen, 1, 60000, idemPtr)).toBe(-1004);
838
- expect(avail(imports, buf, h, kPtr, kLen)).toBe(5n);
839
-
840
- // cancel returns the hold -> back to 10; a double-cancel is a no-op.
841
- expect(imports['data.capacity_cancel'](h, kPtr, kLen, Number(id), 0)).toBe(1);
842
- expect(avail(imports, buf, h, kPtr, kLen)).toBe(8n);
843
- expect(imports['data.capacity_cancel'](h, kPtr, kLen, Number(id), 0)).toBe(0);
844
-
845
- // reserve 4 then confirm -> a permanent consume; available holds at 6.
846
- expect(imports['data.capacity_reserve'](h, kPtr, kLen, 4, 60000, 0)).toBe(8);
847
- imports['data.take_result'](512, 16);
848
- const id2 = Number(buf.readBigUInt64LE(512));
849
- expect(imports['data.capacity_confirm'](h, kPtr, kLen, id2, 0)).toBe(1);
850
- expect(avail(imports, buf, h, kPtr, kLen)).toBe(4n);
851
- // a confirmed sale cannot be cancelled (0); re-confirm is idempotent (1).
852
- expect(imports['data.capacity_cancel'](h, kPtr, kLen, id2, 0)).toBe(0);
853
- expect(imports['data.capacity_confirm'](h, kPtr, kLen, id2, 0)).toBe(1);
854
- });
855
-
856
- it('never oversells (a reserve beyond available is refused)', () => {
857
- const { imports, buf } = setup();
858
- const h = resolve(imports, buf, 'App/tickets');
859
- const [kPtr, kLen] = put(buf, 32, 'vip');
860
- imports['data.capacity_set_total'](h, kPtr, kLen, 5, 0);
861
-
862
- // a hold for all 5 succeeds; a further hold for 1 is refused (-2 -> guest 0).
863
- expect(imports['data.capacity_reserve'](h, kPtr, kLen, 5, 60000, 0)).toBe(8);
864
- expect(imports['data.capacity_reserve'](h, kPtr, kLen, 1, 60000, 0)).toBe(-2);
865
- // a non-positive amount is a typed error (BadAmount), invalid handle rejected.
866
- expect(imports['data.capacity_reserve'](h, kPtr, kLen, 0, 60000, 0)).toBe(-1006);
867
- expect(imports['data.capacity_available'](999, kPtr, kLen)).toBe(-1001);
868
- });
869
-
870
- it('does not cache an idempotent insufficient reserve forever', () => {
871
- const { imports, buf } = setup();
872
- const h = resolve(imports, buf, 'App/tickets');
873
- const [kPtr, kLen] = put(buf, 32, 'late-restock');
874
- const [idemPtr] = put(buf, 64, 'reserve-idem-001');
875
-
876
- expect(imports['data.capacity_reserve'](h, kPtr, kLen, 1, 60000, idemPtr)).toBe(-2);
877
- expect(imports['data.capacity_set_total'](h, kPtr, kLen, 1, 0)).toBe(0);
878
- expect(imports['data.capacity_reserve'](h, kPtr, kLen, 1, 60000, idemPtr)).toBe(8);
879
- });
880
- });
881
-
882
- describe('toildb dev emulator (migration + persistence)', () => {
883
- const rsv = (imports: Imports): bigint =>
884
- (imports['data.result_schema_version'] as () => bigint)();
885
-
886
- it('stamps writes with the catalog schema_version and surfaces it on read', () => {
887
- const { imports, buf } = setup();
888
- __setDbCatalogForTests({ 'App/users': 0x1234 });
889
- const h = resolve(imports, buf, 'App/users');
890
- const [kPtr, kLen] = put(buf, 32, 'u1');
891
- const [vPtr, vLen] = put(buf, 64, 'data');
892
- imports['data.create'](h, kPtr, kLen, vPtr, vLen, 0);
893
- expect(imports['data.get'](h, kPtr, kLen)).toBe(4);
894
- expect(rsv(imports)).toBe(0x1234n); // the woven decoder dispatches on this
895
- });
896
-
897
- it('patch surfaces the current catalog schema_version (regression: was -1)', () => {
898
- const { imports, buf } = setup();
899
- __setDbCatalogForTests({ 'App/users': 0x1234 });
900
- const h = resolve(imports, buf, 'App/users');
901
- const [kPtr, kLen] = put(buf, 32, 'u1');
902
- const [vPtr, vLen] = put(buf, 64, 'orig');
903
- imports['data.create'](h, kPtr, kLen, vPtr, vLen, 0);
904
- const [pPtr, pLen] = put(buf, 96, 'patched');
905
- // patch stashes the patched record + its schema_version; the guest's woven
906
- // decoder dispatches on that version, so it MUST be the current one, not -1.
907
- imports['data.patch'](h, kPtr, kLen, pPtr, pLen, 0);
908
- expect(rsv(imports)).toBe(0x1234n);
909
- });
910
-
911
- it('an evolved @data type leaves old rows stamped with the OLD version', () => {
912
- const { imports, buf } = setup();
913
- __setDbCatalogForTests({ 'App/users': 100 }); // version A
914
- const h = resolve(imports, buf, 'App/users');
915
- const [kPtr, kLen] = put(buf, 32, 'u1');
916
- const [vPtr, vLen] = put(buf, 64, 'old');
917
- imports['data.create'](h, kPtr, kLen, vPtr, vLen, 0);
918
- // the @data type evolves + the wasm rebuilds -> the catalog version changes.
919
- __setDbCatalogForTests({ 'App/users': 200 }); // version B
920
- // a read of the existing row still reports the OLD version -> guest migrates it.
921
- imports['data.get'](h, kPtr, kLen);
922
- expect(rsv(imports)).toBe(100n);
923
- // a NEW write stamps the current version.
924
- const [k2, kl2] = put(buf, 96, 'u2');
925
- imports['data.create'](h, k2, kl2, vPtr, vLen, 0);
926
- imports['data.get'](h, k2, kl2);
927
- expect(rsv(imports)).toBe(200n);
928
- });
929
-
930
- it('persists data + versions to disk and reloads them', () => {
931
- const dir = mkdtempSync(join(tmpdir(), 'toildb-'));
932
- const file = join(dir, 'devdata.json');
933
- try {
934
- const a = setup();
935
- __setDbCatalogForTests({ 'App/users': 777 });
936
- configureDbPersistence(file);
937
- const h = resolve(a.imports, a.buf, 'App/users');
938
- const [kPtr, kLen] = put(a.buf, 32, 'u1');
939
- const [vPtr, vLen] = put(a.buf, 64, 'persisted');
940
- a.imports['data.create'](h, kPtr, kLen, vPtr, vLen, 0);
941
- persistDb();
942
-
943
- // simulate a restart: wipe memory + catalog, then reload from disk.
944
- __resetDbForTests();
945
- configureDbPersistence(file);
946
- const b = setup();
947
- const h2 = resolve(b.imports, b.buf, 'App/users');
948
- const [k2, kl2] = put(b.buf, 32, 'u1');
949
- expect(b.imports['data.get'](h2, k2, kl2)).toBe(9); // "persisted" survived restart
950
- expect(rsv(b.imports)).toBe(777n); // and so did its schema_version stamp
951
- } finally {
952
- rmSync(dir, { recursive: true, force: true });
953
- }
954
- });
955
-
956
- it('persists counter idempotency across devserver restart', () => {
957
- const dir = mkdtempSync(join(tmpdir(), 'toildb-'));
958
- const file = join(dir, 'devdata.json');
959
- try {
960
- const a = setup();
961
- configureDbPersistence(file);
962
- const h = resolve(a.imports, a.buf, 'App/likes');
963
- const [kPtr, kLen] = put(a.buf, 32, 'post-1');
964
- const [idemPtr] = put(a.buf, 64, 'counter-idem-001');
965
- expect(a.imports['data.counter_add'](h, kPtr, kLen, 7, idemPtr)).toBe(0);
966
- persistDb();
967
-
968
- __resetDbForTests();
969
- configureDbPersistence(file);
970
- const b = setup();
971
- const h2 = resolve(b.imports, b.buf, 'App/likes');
972
- const [k2, kl2] = put(b.buf, 32, 'post-1');
973
- const [idem2] = put(b.buf, 64, 'counter-idem-001');
974
- expect(b.imports['data.counter_add'](h2, k2, kl2, 7, idem2)).toBe(0);
975
- expect(b.imports['data.counter_add'](h2, k2, kl2, 8, idem2)).toBe(-1004);
976
- expect(b.imports['data.counter_get'](h2, k2, kl2)).toBe(8);
977
- expect(b.imports['data.take_result'](96, 8)).toBe(8);
978
- expect(b.buf.readBigInt64LE(96)).toBe(7n);
979
- } finally {
980
- rmSync(dir, { recursive: true, force: true });
981
- }
982
- });
983
-
984
- it('persists unique claim idempotency across devserver restart', () => {
985
- const dir = mkdtempSync(join(tmpdir(), 'toildb-'));
986
- const file = join(dir, 'devdata.json');
987
- try {
988
- const a = setup();
989
- configureDbPersistence(file);
990
- const h = resolve(a.imports, a.buf, 'App/usernames');
991
- const [kPtr, kLen] = put(a.buf, 32, 'hopper');
992
- const [u1Ptr, u1Len] = put(a.buf, 48, 'user_1');
993
- const [idemPtr] = put(a.buf, 80, 'idem-claim-0001');
994
- expect(a.imports['data.unique_claim'](h, kPtr, kLen, u1Ptr, u1Len, idemPtr)).toBe(0);
995
- persistDb();
996
-
997
- __resetDbForTests();
998
- configureDbPersistence(file);
999
- const b = setup();
1000
- const h2 = resolve(b.imports, b.buf, 'App/usernames');
1001
- const [k2, kl2] = put(b.buf, 32, 'hopper');
1002
- const [u2, u2l] = put(b.buf, 48, 'user_1');
1003
- const [sameIdem] = put(b.buf, 80, 'idem-claim-0001');
1004
- const [otherIdem] = put(b.buf, 112, 'idem-claim-0002');
1005
- expect(b.imports['data.unique_claim'](h2, k2, kl2, u2, u2l, sameIdem)).toBe(2);
1006
- expect(b.imports['data.unique_claim'](h2, k2, kl2, u2, u2l, otherIdem)).toBe(-1004);
1007
- } finally {
1008
- rmSync(dir, { recursive: true, force: true });
1009
- }
1010
- });
1011
-
1012
- it('replays idempotent capacity reservations across devserver restart', () => {
1013
- const dir = mkdtempSync(join(tmpdir(), 'toildb-'));
1014
- const file = join(dir, 'devdata.json');
1015
- try {
1016
- const a = setup();
1017
- configureDbPersistence(file);
1018
- const h = resolve(a.imports, a.buf, 'App/seats');
1019
- const [kPtr, kLen] = put(a.buf, 32, 'showB');
1020
- const [idemPtr] = put(a.buf, 64, 'reserve-idem-001');
1021
- expect(a.imports['data.capacity_set_total'](h, kPtr, kLen, 3, 0)).toBe(0);
1022
- expect(a.imports['data.capacity_reserve'](h, kPtr, kLen, 2, 60000, idemPtr)).toBe(8);
1023
- expect(a.imports['data.take_result'](96, 16)).toBe(8);
1024
- const firstId = a.buf.readBigUInt64LE(96);
1025
- persistDb();
1026
-
1027
- __resetDbForTests();
1028
- configureDbPersistence(file);
1029
- const b = setup();
1030
- const h2 = resolve(b.imports, b.buf, 'App/seats');
1031
- const [k2, kl2] = put(b.buf, 32, 'showB');
1032
- const [idem2] = put(b.buf, 64, 'reserve-idem-001');
1033
- expect(b.imports['data.capacity_reserve'](h2, k2, kl2, 2, 60000, idem2)).toBe(8);
1034
- expect(b.imports['data.take_result'](96, 16)).toBe(8);
1035
- expect(b.buf.readBigUInt64LE(96)).toBe(firstId);
1036
- expect(b.imports['data.capacity_available'](h2, k2, kl2)).toBe(8);
1037
- expect(b.imports['data.take_result'](112, 16)).toBe(8);
1038
- expect(b.buf.readBigInt64LE(112)).toBe(1n);
1039
- } finally {
1040
- rmSync(dir, { recursive: true, force: true });
1041
- }
1042
- });
1043
- });
1044
-
1045
- describe('toildb dev derives (toildb.derives parser + write routing)', () => {
1046
- const id = (d: { deriveId: number }) => d.deriveId;
1047
-
1048
- it('parses a valid toildb.derives section in declaration order', () => {
1049
- const wasm = wasmWithSection(
1050
- 'toildb.derives',
1051
- derivesSection([
1052
- [0, 'App', 'rebuild'],
1053
- [1, 'App', 'refresh'],
1054
- [2, 'Other', 'rollup'],
1055
- ]),
1056
- );
1057
- expect(parseDerives(wasm).map((d) => `${d.deriveId}:${d.dbName}.${d.methodName}`)).toEqual([
1058
- '0:App.rebuild',
1059
- '1:App.refresh',
1060
- '2:Other.rollup',
1061
- ]);
1062
- });
1063
-
1064
- it('returns [] when there is no toildb.derives section', () => {
1065
- expect(parseDerives(wasmWithSection('toildb.other', Buffer.from([1, 2, 3])))).toEqual([]);
1066
- });
1067
-
1068
- it('fails closed on a bad version, trailing bytes, or an empty db name', () => {
1069
- const badVersion = derivesSection([[0, 'App', 'r']]);
1070
- badVersion.writeUInt16LE(2, 0); // unknown format_version
1071
- expect(parseDerives(wasmWithSection('toildb.derives', badVersion))).toEqual([]);
1072
-
1073
- const trailing = Buffer.concat([derivesSection([[0, 'App', 'r']]), Buffer.from([0])]);
1074
- expect(parseDerives(wasmWithSection('toildb.derives', trailing))).toEqual([]);
1075
-
1076
- const emptyDb = derivesSection([[0, '', 'r']]);
1077
- expect(parseDerives(wasmWithSection('toildb.derives', emptyDb))).toEqual([]);
1078
- });
1079
-
1080
- it('routes a source-collection write to its database derives, coalesced', () => {
1081
- const derives = parseDerives(
1082
- wasmWithSection(
1083
- 'toildb.derives',
1084
- derivesSection([
1085
- [0, 'App', 'rebuild'],
1086
- [1, 'App', 'refresh'],
1087
- [2, 'Other', 'rollup'],
1088
- ]),
1089
- ),
1090
- );
1091
- // two App collections written in one dispatch -> App's two derives, once each
1092
- expect(derivesForWrites(derives, new Set(['App/users', 'App/posts'])).map(id)).toEqual([
1093
- 0, 1,
1094
- ]);
1095
- // a write to the other database -> only its derive
1096
- expect(derivesForWrites(derives, new Set(['Other/log'])).map(id)).toEqual([2]);
1097
- // a write to an unrelated database -> nothing runs
1098
- expect(derivesForWrites(derives, new Set(['Nope/x'])).map(id)).toEqual([]);
1099
- // no writes -> nothing runs
1100
- expect(derivesForWrites(derives, new Set<string>()).map(id)).toEqual([]);
1101
- });
1102
- });