@serfab/cadre-host 1.13.0 → 1.15.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (352) hide show
  1. package/README.md +347 -423
  2. package/dist/bin/host.d.ts +6 -3
  3. package/dist/bin/host.js +332 -524
  4. package/dist/bin/host.js.map +1 -1
  5. package/dist/bin/join-output.d.ts +56 -0
  6. package/dist/bin/join-output.js +92 -0
  7. package/dist/bin/join-output.js.map +1 -0
  8. package/dist/bin/nat-output.d.ts +54 -0
  9. package/dist/bin/nat-output.js +138 -0
  10. package/dist/bin/nat-output.js.map +1 -0
  11. package/dist/bin/ui-access.d.ts +22 -0
  12. package/dist/bin/ui-access.js +44 -0
  13. package/dist/bin/ui-access.js.map +1 -0
  14. package/dist/hosted/claim-details.d.ts +66 -0
  15. package/dist/hosted/claim-details.js +60 -0
  16. package/dist/hosted/claim-details.js.map +1 -0
  17. package/dist/hosted/hosted-node-service.d.ts +325 -0
  18. package/dist/hosted/hosted-node-service.js +678 -0
  19. package/dist/hosted/hosted-node-service.js.map +1 -0
  20. package/dist/hosted/hosted-node-store.d.ts +40 -0
  21. package/dist/hosted/hosted-node-store.js +126 -0
  22. package/dist/hosted/hosted-node-store.js.map +1 -0
  23. package/dist/{donation/donation-supervisor.d.ts → hosted/hosted-node-supervisor.d.ts} +70 -53
  24. package/dist/hosted/hosted-node-supervisor.js +396 -0
  25. package/dist/hosted/hosted-node-supervisor.js.map +1 -0
  26. package/dist/hosted/hosted-node-watcher.d.ts +95 -0
  27. package/dist/hosted/hosted-node-watcher.js +260 -0
  28. package/dist/hosted/hosted-node-watcher.js.map +1 -0
  29. package/dist/hosted/index.d.ts +16 -0
  30. package/dist/hosted/index.js +13 -0
  31. package/dist/hosted/index.js.map +1 -0
  32. package/dist/hosted/node-status.d.ts +51 -0
  33. package/dist/hosted/node-status.js +77 -0
  34. package/dist/hosted/node-status.js.map +1 -0
  35. package/dist/hosted/types.d.ts +141 -0
  36. package/dist/hosted/types.js +22 -0
  37. package/dist/hosted/types.js.map +1 -0
  38. package/dist/index.d.ts +12 -24
  39. package/dist/index.js +9 -19
  40. package/dist/index.js.map +1 -1
  41. package/dist/installer/config.d.ts +27 -34
  42. package/dist/installer/config.js +46 -15
  43. package/dist/installer/config.js.map +1 -1
  44. package/dist/installer/index.d.ts +9 -19
  45. package/dist/installer/index.js +21 -85
  46. package/dist/installer/index.js.map +1 -1
  47. package/dist/installer/paths.d.ts +0 -2
  48. package/dist/installer/paths.js +0 -4
  49. package/dist/installer/paths.js.map +1 -1
  50. package/dist/installer/wizard.d.ts +12 -9
  51. package/dist/installer/wizard.js +15 -14
  52. package/dist/installer/wizard.js.map +1 -1
  53. package/dist/nat/address-resolver.d.ts +18 -17
  54. package/dist/nat/address-resolver.js +79 -25
  55. package/dist/nat/address-resolver.js.map +1 -1
  56. package/dist/nat/address-watch.d.ts +37 -0
  57. package/dist/nat/address-watch.js +84 -0
  58. package/dist/nat/address-watch.js.map +1 -0
  59. package/dist/nat/ddns/duckdns.d.ts +2 -1
  60. package/dist/nat/ddns/duckdns.js +5 -1
  61. package/dist/nat/ddns/duckdns.js.map +1 -1
  62. package/dist/nat/ddns/index.d.ts +6 -0
  63. package/dist/nat/ddns/index.js.map +1 -1
  64. package/dist/nat/index.d.ts +13 -9
  65. package/dist/nat/index.js +7 -5
  66. package/dist/nat/index.js.map +1 -1
  67. package/dist/nat/nat-service.d.ts +220 -77
  68. package/dist/nat/nat-service.js +653 -239
  69. package/dist/nat/nat-service.js.map +1 -1
  70. package/dist/nat/nat-store.d.ts +12 -5
  71. package/dist/nat/nat-store.js +124 -29
  72. package/dist/nat/nat-store.js.map +1 -1
  73. package/dist/nat/network-mode.d.ts +19 -0
  74. package/dist/nat/network-mode.js +32 -0
  75. package/dist/nat/network-mode.js.map +1 -0
  76. package/dist/nat/port-mapper.d.ts +83 -79
  77. package/dist/nat/port-mapper.js +148 -189
  78. package/dist/nat/port-mapper.js.map +1 -1
  79. package/dist/nat/reachability.d.ts +32 -15
  80. package/dist/nat/reachability.js +110 -22
  81. package/dist/nat/reachability.js.map +1 -1
  82. package/dist/nat/types.d.ts +98 -27
  83. package/dist/nat/types.js +8 -5
  84. package/dist/nat/types.js.map +1 -1
  85. package/dist/orchestrator/host-process-orchestrator.d.ts +32 -74
  86. package/dist/orchestrator/host-process-orchestrator.js +84 -233
  87. package/dist/orchestrator/host-process-orchestrator.js.map +1 -1
  88. package/dist/{installer/identity.d.ts → orchestrator/identity-file.d.ts} +5 -5
  89. package/dist/{installer/identity.js → orchestrator/identity-file.js} +6 -6
  90. package/dist/orchestrator/identity-file.js.map +1 -0
  91. package/dist/orchestrator/index.d.ts +2 -3
  92. package/dist/orchestrator/index.js +1 -1
  93. package/dist/orchestrator/index.js.map +1 -1
  94. package/dist/orchestrator/node-identity.d.ts +6 -6
  95. package/dist/orchestrator/node-identity.js +7 -7
  96. package/dist/orchestrator/node-identity.js.map +1 -1
  97. package/dist/orchestrator/port-allocator.d.ts +5 -6
  98. package/dist/orchestrator/port-allocator.js +9 -11
  99. package/dist/orchestrator/port-allocator.js.map +1 -1
  100. package/dist/orchestrator/state-store.d.ts +3 -5
  101. package/dist/orchestrator/state-store.js +2 -10
  102. package/dist/orchestrator/state-store.js.map +1 -1
  103. package/dist/orchestrator/types.d.ts +33 -22
  104. package/dist/orchestrator/types.js.map +1 -1
  105. package/dist/package-version.d.ts +1 -0
  106. package/dist/package-version.js +4 -0
  107. package/dist/package-version.js.map +1 -0
  108. package/dist/server/error-handler.d.ts +5 -25
  109. package/dist/server/error-handler.js +11 -74
  110. package/dist/server/error-handler.js.map +1 -1
  111. package/dist/server/events/types.d.ts +7 -11
  112. package/dist/server/events/types.js +2 -1
  113. package/dist/server/events/types.js.map +1 -1
  114. package/dist/server/index.d.ts +11 -37
  115. package/dist/server/index.js +30 -48
  116. package/dist/server/index.js.map +1 -1
  117. package/dist/server/routes/hosted-nodes.d.ts +28 -0
  118. package/dist/server/routes/hosted-nodes.js +68 -0
  119. package/dist/server/routes/hosted-nodes.js.map +1 -0
  120. package/dist/server/routes/nat.d.ts +4 -1
  121. package/dist/server/routes/nat.js +13 -2
  122. package/dist/server/routes/nat.js.map +1 -1
  123. package/dist/server/routes/nodes.d.ts +6 -20
  124. package/dist/server/routes/nodes.js +7 -86
  125. package/dist/server/routes/nodes.js.map +1 -1
  126. package/dist/server/routes/settings.d.ts +3 -7
  127. package/dist/server/routes/settings.js +18 -8
  128. package/dist/server/routes/settings.js.map +1 -1
  129. package/dist/server/routes/status.d.ts +2 -25
  130. package/dist/server/routes/status.js +3 -43
  131. package/dist/server/routes/status.js.map +1 -1
  132. package/dist/server/server.js +8 -8
  133. package/dist/server/settings-store.d.ts +2 -2
  134. package/dist/server/settings-store.js +2 -2
  135. package/dist/server/static.js +1 -2
  136. package/dist/server/static.js.map +1 -1
  137. package/dist/ui/assets/index-BHd8CWZA.css +1 -0
  138. package/dist/ui/assets/index-Ccr2GjPT.js +23 -0
  139. package/dist/ui/assets/index-Ccr2GjPT.js.map +1 -0
  140. package/dist/ui/index.html +2 -2
  141. package/dist/update/index.d.ts +2 -2
  142. package/dist/update/index.js +2 -2
  143. package/dist/update/store.d.ts +1 -1
  144. package/dist/update/store.js +1 -1
  145. package/dist/update/types.d.ts +1 -1
  146. package/dist/update/types.js +1 -1
  147. package/package.json +5 -7
  148. package/src/__tests__/cli-nat.smoke.test.ts +219 -212
  149. package/src/__tests__/cli.smoke.test.ts +2 -2
  150. package/src/__tests__/global-setup.ts +2 -3
  151. package/src/__tests__/orchestrator-claim-secret.test.ts +111 -0
  152. package/src/__tests__/orchestrator-env-scrub.test.ts +160 -163
  153. package/src/__tests__/orchestrator-handle-contract.ts +134 -134
  154. package/src/__tests__/orchestrator-node-identity.test.ts +177 -203
  155. package/src/__tests__/orchestrator-ports.test.ts +124 -143
  156. package/src/__tests__/orchestrator-push.test.ts +138 -146
  157. package/src/__tests__/orchestrator.test.ts +1017 -1063
  158. package/src/__tests__/ui-access.test.ts +30 -0
  159. package/src/bin/host.ts +397 -572
  160. package/src/bin/join-output.ts +120 -0
  161. package/src/bin/nat-output.ts +188 -0
  162. package/src/bin/ui-access.ts +50 -0
  163. package/src/{donation → hosted}/__tests__/fake-orchestrator.test.ts +172 -173
  164. package/src/{donation → hosted}/__tests__/fake-orchestrator.ts +43 -23
  165. package/src/hosted/__tests__/hosted-node-service.test.ts +708 -0
  166. package/src/hosted/__tests__/hosted-node-supervisor.test.ts +475 -0
  167. package/src/hosted/__tests__/test-invitation.ts +33 -0
  168. package/src/hosted/claim-details.ts +103 -0
  169. package/src/hosted/hosted-node-service.ts +814 -0
  170. package/src/hosted/hosted-node-store.ts +144 -0
  171. package/src/hosted/hosted-node-supervisor.ts +439 -0
  172. package/src/hosted/hosted-node-watcher.ts +288 -0
  173. package/src/hosted/index.ts +53 -0
  174. package/src/hosted/node-status.ts +97 -0
  175. package/src/hosted/types.ts +160 -0
  176. package/src/index.ts +72 -91
  177. package/src/installer/__tests__/config.test.ts +15 -40
  178. package/src/installer/__tests__/installer.smoke.test.ts +184 -263
  179. package/src/installer/__tests__/wizard.test.ts +25 -13
  180. package/src/installer/config.ts +201 -175
  181. package/src/installer/index.ts +252 -327
  182. package/src/installer/paths.ts +104 -109
  183. package/src/installer/wizard.ts +24 -26
  184. package/src/nat/__tests__/address-resolver.test.ts +107 -82
  185. package/src/nat/__tests__/nat-service.test.ts +608 -515
  186. package/src/nat/__tests__/nat-store.test.ts +49 -12
  187. package/src/nat/__tests__/network-mode.test.ts +38 -0
  188. package/src/nat/__tests__/port-mapper.test.ts +30 -191
  189. package/src/nat/__tests__/reachability.test.ts +96 -13
  190. package/src/nat/address-resolver.ts +94 -52
  191. package/src/nat/address-watch.ts +92 -0
  192. package/src/nat/ddns/duckdns.ts +5 -1
  193. package/src/nat/ddns/index.ts +6 -0
  194. package/src/nat/index.ts +33 -13
  195. package/src/nat/nat-service.ts +942 -501
  196. package/src/nat/nat-store.ts +126 -31
  197. package/src/nat/network-mode.ts +36 -0
  198. package/src/nat/port-mapper.ts +241 -329
  199. package/src/nat/reachability.ts +155 -34
  200. package/src/nat/types.ts +110 -37
  201. package/src/{installer/__tests__/identity.test.ts → orchestrator/__tests__/identity-file.test.ts} +34 -34
  202. package/src/orchestrator/__tests__/node-identity.test.ts +62 -62
  203. package/src/orchestrator/host-process-orchestrator.ts +1089 -1260
  204. package/src/{installer/identity.ts → orchestrator/identity-file.ts} +56 -56
  205. package/src/orchestrator/index.ts +9 -10
  206. package/src/orchestrator/node-identity.ts +41 -41
  207. package/src/orchestrator/port-allocator.ts +71 -73
  208. package/src/orchestrator/state-store.ts +76 -90
  209. package/src/orchestrator/types.ts +34 -23
  210. package/src/package-version.ts +4 -0
  211. package/src/server/__tests__/bus.test.ts +38 -38
  212. package/src/server/__tests__/error-handler.test.ts +81 -118
  213. package/src/server/__tests__/fakes.ts +15 -18
  214. package/src/server/__tests__/hosted-nodes-route.test.ts +161 -0
  215. package/src/server/__tests__/nodes-route.test.ts +114 -283
  216. package/src/server/__tests__/publishers.test.ts +193 -180
  217. package/src/server/__tests__/server.smoke.test.ts +134 -223
  218. package/src/server/__tests__/settings-route.test.ts +210 -195
  219. package/src/server/__tests__/sse-route.test.ts +119 -119
  220. package/src/server/__tests__/status-route.test.ts +82 -121
  221. package/src/server/error-handler.ts +112 -178
  222. package/src/server/events/types.ts +20 -24
  223. package/src/server/index.ts +223 -267
  224. package/src/server/routes/hosted-nodes.ts +83 -0
  225. package/src/server/routes/nat.ts +75 -63
  226. package/src/server/routes/nodes.ts +96 -188
  227. package/src/server/routes/settings.ts +20 -15
  228. package/src/server/routes/status.ts +5 -67
  229. package/src/server/server.ts +8 -8
  230. package/src/server/settings-store.ts +2 -2
  231. package/src/server/static.ts +1 -2
  232. package/src/update/index.ts +2 -2
  233. package/src/update/store.ts +1 -1
  234. package/src/update/types.ts +1 -1
  235. package/dist/auth/duration.d.ts +0 -1
  236. package/dist/auth/duration.js +0 -32
  237. package/dist/auth/duration.js.map +0 -1
  238. package/dist/auth/index.d.ts +0 -11
  239. package/dist/auth/index.js +0 -9
  240. package/dist/auth/index.js.map +0 -1
  241. package/dist/auth/self-label.d.ts +0 -36
  242. package/dist/auth/self-label.js +0 -57
  243. package/dist/auth/self-label.js.map +0 -1
  244. package/dist/auth/trust-circle-store.d.ts +0 -26
  245. package/dist/auth/trust-circle-store.js +0 -129
  246. package/dist/auth/trust-circle-store.js.map +0 -1
  247. package/dist/auth/trust-circle.d.ts +0 -156
  248. package/dist/auth/trust-circle.js +0 -307
  249. package/dist/auth/trust-circle.js.map +0 -1
  250. package/dist/auth/types.d.ts +0 -84
  251. package/dist/auth/types.js +0 -24
  252. package/dist/auth/types.js.map +0 -1
  253. package/dist/donation/donation-service.d.ts +0 -389
  254. package/dist/donation/donation-service.js +0 -828
  255. package/dist/donation/donation-service.js.map +0 -1
  256. package/dist/donation/donation-store.d.ts +0 -51
  257. package/dist/donation/donation-store.js +0 -152
  258. package/dist/donation/donation-store.js.map +0 -1
  259. package/dist/donation/donation-supervisor.js +0 -364
  260. package/dist/donation/donation-supervisor.js.map +0 -1
  261. package/dist/donation/grant-service.d.ts +0 -80
  262. package/dist/donation/grant-service.js +0 -210
  263. package/dist/donation/grant-service.js.map +0 -1
  264. package/dist/donation/grant-store.d.ts +0 -38
  265. package/dist/donation/grant-store.js +0 -135
  266. package/dist/donation/grant-store.js.map +0 -1
  267. package/dist/donation/index.d.ts +0 -20
  268. package/dist/donation/index.js +0 -17
  269. package/dist/donation/index.js.map +0 -1
  270. package/dist/donation/types.d.ts +0 -224
  271. package/dist/donation/types.js +0 -40
  272. package/dist/donation/types.js.map +0 -1
  273. package/dist/installer/identity.js.map +0 -1
  274. package/dist/owner/index.d.ts +0 -11
  275. package/dist/owner/index.js +0 -11
  276. package/dist/owner/index.js.map +0 -1
  277. package/dist/owner/owner-node-client.d.ts +0 -110
  278. package/dist/owner/owner-node-client.js +0 -184
  279. package/dist/owner/owner-node-client.js.map +0 -1
  280. package/dist/server/routes/bootstrap-node-validation.d.ts +0 -115
  281. package/dist/server/routes/bootstrap-node-validation.js +0 -197
  282. package/dist/server/routes/bootstrap-node-validation.js.map +0 -1
  283. package/dist/server/routes/grants-admin.d.ts +0 -29
  284. package/dist/server/routes/grants-admin.js +0 -58
  285. package/dist/server/routes/grants-admin.js.map +0 -1
  286. package/dist/server/routes/grants.d.ts +0 -28
  287. package/dist/server/routes/grants.js +0 -165
  288. package/dist/server/routes/grants.js.map +0 -1
  289. package/dist/server/routes/provision-request-validation.d.ts +0 -45
  290. package/dist/server/routes/provision-request-validation.js +0 -97
  291. package/dist/server/routes/provision-request-validation.js.map +0 -1
  292. package/dist/server/routes/strands.d.ts +0 -20
  293. package/dist/server/routes/strands.js +0 -38
  294. package/dist/server/routes/strands.js.map +0 -1
  295. package/dist/server/routes/trust-circle.d.ts +0 -17
  296. package/dist/server/routes/trust-circle.js +0 -35
  297. package/dist/server/routes/trust-circle.js.map +0 -1
  298. package/dist/strands/index.d.ts +0 -7
  299. package/dist/strands/index.js +0 -6
  300. package/dist/strands/index.js.map +0 -1
  301. package/dist/strands/strand-service.d.ts +0 -50
  302. package/dist/strands/strand-service.js +0 -103
  303. package/dist/strands/strand-service.js.map +0 -1
  304. package/dist/strands/types.d.ts +0 -93
  305. package/dist/strands/types.js +0 -23
  306. package/dist/strands/types.js.map +0 -1
  307. package/dist/ui/assets/index-C6nELNfC.js +0 -23
  308. package/dist/ui/assets/index-C6nELNfC.js.map +0 -1
  309. package/dist/ui/assets/index-TtIYSeZO.css +0 -1
  310. package/src/__tests__/cli-invite.smoke.test.ts +0 -192
  311. package/src/__tests__/host-owner.smoke.test.ts +0 -123
  312. package/src/__tests__/orchestrator-owner.test.ts +0 -234
  313. package/src/__tests__/orchestrator-pin-keys.test.ts +0 -152
  314. package/src/auth/__tests__/duration.test.ts +0 -28
  315. package/src/auth/__tests__/self-label.test.ts +0 -85
  316. package/src/auth/__tests__/trust-circle-integration.test.ts +0 -152
  317. package/src/auth/__tests__/trust-circle-store.test.ts +0 -115
  318. package/src/auth/__tests__/trust-circle.test.ts +0 -445
  319. package/src/auth/duration.ts +0 -33
  320. package/src/auth/index.ts +0 -21
  321. package/src/auth/self-label.ts +0 -74
  322. package/src/auth/trust-circle-store.ts +0 -155
  323. package/src/auth/trust-circle.ts +0 -395
  324. package/src/auth/types.ts +0 -99
  325. package/src/donation/__tests__/donation-service.test.ts +0 -1193
  326. package/src/donation/__tests__/donation-supervisor.test.ts +0 -548
  327. package/src/donation/__tests__/grant-service.test.ts +0 -213
  328. package/src/donation/__tests__/grant-store.test.ts +0 -113
  329. package/src/donation/donation-service.ts +0 -1033
  330. package/src/donation/donation-store.ts +0 -176
  331. package/src/donation/donation-supervisor.ts +0 -411
  332. package/src/donation/grant-service.ts +0 -252
  333. package/src/donation/grant-store.ts +0 -152
  334. package/src/donation/index.ts +0 -65
  335. package/src/donation/types.ts +0 -260
  336. package/src/owner/__tests__/owner-node-client.test.ts +0 -194
  337. package/src/owner/index.ts +0 -12
  338. package/src/owner/owner-node-client.ts +0 -259
  339. package/src/server/__tests__/bootstrap-node-validation.test.ts +0 -138
  340. package/src/server/__tests__/grants-admin-route.test.ts +0 -171
  341. package/src/server/__tests__/grants-route.test.ts +0 -394
  342. package/src/server/__tests__/strands-route.test.ts +0 -234
  343. package/src/server/routes/bootstrap-node-validation.ts +0 -206
  344. package/src/server/routes/grants-admin.ts +0 -74
  345. package/src/server/routes/grants.ts +0 -193
  346. package/src/server/routes/provision-request-validation.ts +0 -105
  347. package/src/server/routes/strands.ts +0 -54
  348. package/src/server/routes/trust-circle.ts +0 -49
  349. package/src/strands/__tests__/strand-service.test.ts +0 -165
  350. package/src/strands/index.ts +0 -14
  351. package/src/strands/strand-service.ts +0 -135
  352. package/src/strands/types.ts +0 -102
@@ -1,394 +0,0 @@
1
- /**
2
- * Grantee-facing `/grants` route tests — the bearer-gated provisioning surface.
3
- *
4
- * Uses a fake orchestrator (no real children) so the focus is the HTTP contract:
5
- * bearer auth, request validation, per-grant ownership isolation, the quota gate,
6
- * and seedToken redaction. The live-node round-trips (peer/seed) are covered by
7
- * the cross-package integration scenario.
8
- */
9
-
10
- import type { FastifyInstance } from 'fastify';
11
- import { afterEach, beforeEach, describe, expect, it } from 'vitest';
12
- import { mkdtempSync, rmSync } from 'node:fs';
13
- import { tmpdir } from 'node:os';
14
- import { join } from 'node:path';
15
-
16
- import type {
17
- Orchestrator,
18
- OrchestratorCreateRequest,
19
- OrchestratorCreateResult,
20
- OrchestratorStats,
21
- } from '@serfab/cadre-provider';
22
-
23
- import { registerErrorHandler } from '../error-handler.js';
24
- import { registerGrantsRoutes } from '../routes/grants.js';
25
- import { buildFastify } from '../server.js';
26
- import { DonationService } from '../../donation/donation-service.js';
27
- import { DonationStore } from '../../donation/donation-store.js';
28
- import { GrantService } from '../../donation/grant-service.js';
29
- import { GrantStore } from '../../donation/grant-store.js';
30
-
31
- class FakeOrchestrator implements Orchestrator {
32
- stopped: string[] = [];
33
- removed: string[] = [];
34
- private counter = 0;
35
- async createContainer(_request: OrchestratorCreateRequest): Promise<OrchestratorCreateResult> {
36
- const n = ++this.counter;
37
- return {
38
- dockerId: `dock_${n}`,
39
- healthEndpoint: `http://127.0.0.1:${9000 + n}/health`,
40
- metricsEndpoint: `http://127.0.0.1:${9000 + n}/metrics`,
41
- seedEndpoint: `http://127.0.0.1:${9000 + n}/seed`,
42
- seedToken: `seed-token-${n}`,
43
- p2pPort: 4000 + n,
44
- };
45
- }
46
- async stopContainer(id: string): Promise<void> { this.stopped.push(id); }
47
- async removeContainer(id: string): Promise<void> { this.removed.push(id); }
48
- async getStats(): Promise<OrchestratorStats> {
49
- return { cpuPercent: 0, memoryBytes: 0, networkRxBytes: 0, networkTxBytes: 0 };
50
- }
51
- async isRunning(): Promise<boolean> { return true; }
52
- async getLogs(): Promise<string> { return ''; }
53
- }
54
-
55
- let tmpRoot: string;
56
- let app: FastifyInstance;
57
- let grants: GrantService;
58
- let store: DonationStore;
59
- let token: string;
60
- const originalFetch = globalThis.fetch;
61
-
62
- /**
63
- * A well-formed 32-byte base64url owner key. `DonationService.provision`
64
- * shape-checks the pins, so a placeholder string is a 400 rather than a fixture.
65
- */
66
- const OWNER_KEY = Buffer.alloc(32, 7).toString('base64url');
67
-
68
- const body = {
69
- partyId: 'party-P',
70
- bootstrapNodes: ['/ip4/127.0.0.1/tcp/4001/p2p/12D3KooWA9hbnKrRnPRSPTRkzXqTHzGE8YpJ3JHZmQ5tGwLRTMmp'],
71
- ownerKeys: [OWNER_KEY],
72
- };
73
-
74
- const bearer = (t: string) => ({ authorization: `Bearer ${t}` });
75
-
76
- beforeEach(() => {
77
- tmpRoot = mkdtempSync(join(tmpdir(), 'cadre-host-grants-svc-route-'));
78
- grants = new GrantService({ store: new GrantStore(join(tmpRoot, 'grants')) });
79
- token = grants.issue({ label: 'Alice', maxNodes: 2 }).token;
80
- store = new DonationStore(join(tmpRoot, 'donations'));
81
- const donations = new DonationService({ orchestrator: new FakeOrchestrator(), grants, store });
82
- // The production constructor, so these tests run with the same body parsing.
83
- app = buildFastify();
84
- registerErrorHandler(app);
85
- registerGrantsRoutes(app, { donations, grants });
86
- });
87
-
88
- afterEach(async () => {
89
- globalThis.fetch = originalFetch;
90
- await app.close();
91
- try { rmSync(tmpRoot, { recursive: true, force: true }); } catch { /* ignore */ }
92
- });
93
-
94
- describe('POST /grants', () => {
95
- it('provisions a donated node → 201, awaiting_seed, seedToken redacted', async () => {
96
- const res = await app.inject({ method: 'POST', url: '/grants', headers: bearer(token), payload: body });
97
- expect(res.statusCode).toBe(201);
98
- const donation = (res.json() as { data: { donation: Record<string, unknown> } }).data.donation;
99
- expect(donation.status).toBe('awaiting_seed');
100
- expect(donation.partyId).toBe('party-P');
101
- expect(donation.seedToken).toBeUndefined();
102
- expect(donation.seedEndpoint).toBeUndefined();
103
- });
104
-
105
- it('rejects a missing bearer → 401', async () => {
106
- const res = await app.inject({ method: 'POST', url: '/grants', payload: body });
107
- expect(res.statusCode).toBe(401);
108
- expect((res.json() as { error: { code: string } }).error.code).toBe('unauthorized');
109
- });
110
-
111
- it('rejects an unknown bearer → 401', async () => {
112
- const res = await app.inject({ method: 'POST', url: '/grants', headers: bearer('nope'), payload: body });
113
- expect(res.statusCode).toBe(401);
114
- });
115
-
116
- it('rejects a missing partyId → 400 invalid_request', async () => {
117
- const res = await app.inject({
118
- method: 'POST', url: '/grants', headers: bearer(token),
119
- payload: { bootstrapNodes: body.bootstrapNodes, ownerKeys: body.ownerKeys },
120
- });
121
- expect(res.statusCode).toBe(400);
122
- expect((res.json() as { error: { code: string } }).error.code).toBe('invalid_request');
123
- });
124
-
125
- it('rejects empty ownerKeys → 400 invalid_request', async () => {
126
- const res = await app.inject({
127
- method: 'POST', url: '/grants', headers: bearer(token),
128
- payload: { ...body, ownerKeys: [] },
129
- });
130
- expect(res.statusCode).toBe(400);
131
- });
132
-
133
- // A typo'd pin used to be answered 201 and only fail when the spawned child
134
- // tried to decode it at boot — by which point the caller had no way to act on it.
135
- it('rejects a malformed ownerKeys entry → 400 invalid_request, naming the bad key', async () => {
136
- const res = await app.inject({
137
- method: 'POST', url: '/grants', headers: bearer(token),
138
- payload: { ...body, ownerKeys: [OWNER_KEY, 'this-is-not-a-key'] },
139
- });
140
- expect(res.statusCode).toBe(400);
141
- const error = (res.json() as { error: { code: string; message: string } }).error;
142
- expect(error.code).toBe('invalid_request');
143
- expect(error.message).toContain('this-is-not-a-key');
144
- });
145
-
146
- // Element types are the route's job: the service's shape check assumes strings,
147
- // so a number here would come back as an internal `.trim is not a function`.
148
- it('rejects a non-string ownerKeys entry → 400 naming the field, not a decoder error', async () => {
149
- const res = await app.inject({
150
- method: 'POST', url: '/grants', headers: bearer(token),
151
- payload: { ...body, ownerKeys: [42] },
152
- });
153
- expect(res.statusCode).toBe(400);
154
- const error = (res.json() as { error: { code: string; message: string } }).error;
155
- expect(error.code).toBe('invalid_request');
156
- expect(error.message).toBe('ownerKeys must be an array of strings');
157
- });
158
-
159
- // The address rule the whole `bootstrap-node-validation.ts` module exists for,
160
- // at the route boundary. Its accept/reject table is pinned in
161
- // `bootstrap-node-validation.test.ts`; what matters here is that a bad address
162
- // is a 400 naming the entry, and that no child was spawned on the way.
163
- it('rejects an unusable bootstrapNodes entry → 400 invalid_request, naming the bad address', async () => {
164
- const res = await app.inject({
165
- method: 'POST', url: '/grants', headers: bearer(token),
166
- payload: { ...body, bootstrapNodes: [...body.bootstrapNodes, 'not-an-address'] },
167
- });
168
- expect(res.statusCode).toBe(400);
169
- const error = (res.json() as { error: { code: string; message: string } }).error;
170
- expect(error.code).toBe('invalid_request');
171
- expect(error.message).toContain('not-an-address');
172
- });
173
-
174
- it('provisions nothing for any of the unusable-address shapes', async () => {
175
- const bad: unknown[] = [
176
- [42],
177
- ['not-an-address'],
178
- // Parses, but names no peer: the child would start with zero bootstrap
179
- // peers and never reach the requester's cadre.
180
- ['/ip4/127.0.0.1/tcp/4001'],
181
- // Names a peer id that does not decode: the child dies constructing libp2p.
182
- ['/ip4/127.0.0.1/tcp/4001/p2p/12D3KooReq'],
183
- // Names a decodable peer but no place to dial it: bootstrap keeps it, libp2p
184
- // finds no transport for it, and the node is alone just the same.
185
- ['/p2p/12D3KooWA9hbnKrRnPRSPTRkzXqTHzGE8YpJ3JHZmQ5tGwLRTMmp'],
186
- [''],
187
- ];
188
- for (const bootstrapNodes of bad) {
189
- const res = await app.inject({
190
- method: 'POST', url: '/grants', headers: bearer(token),
191
- payload: { ...body, bootstrapNodes },
192
- });
193
- expect(res.statusCode, `expected 400 for ${JSON.stringify(bootstrapNodes)}`).toBe(400);
194
- }
195
-
196
- const list = await app.inject({ method: 'GET', url: '/grants', headers: bearer(token) });
197
- expect((list.json() as { data: { donations: unknown[] } }).data.donations).toHaveLength(0);
198
- });
199
-
200
- // A phone has no address to give, so it sends none and dials the lent node itself:
201
- // leaving the field out, or sending an empty list, provisions a node with no
202
- // bootstrap peers rather than being rejected.
203
- it('provisions with bootstrapNodes absent or empty → 201, recording an empty list', async () => {
204
- const { bootstrapNodes: _bootstrapNodes, ...withoutBootstrap } = body;
205
- for (const payload of [withoutBootstrap, { ...body, bootstrapNodes: [] }]) {
206
- const res = await app.inject({ method: 'POST', url: '/grants', headers: bearer(token), payload });
207
- expect(res.statusCode, `expected 201 for ${JSON.stringify(payload)}`).toBe(201);
208
- const donation = (res.json() as { data: { donation: { id: string; bootstrapNodes?: string[] } } }).data.donation;
209
- expect(donation.bootstrapNodes).toEqual([]);
210
- expect(store.get(donation.id)?.bootstrapNodes).toEqual([]);
211
- }
212
- });
213
-
214
- it('rejects a non-string partyId → 400, rather than stringifying it into the child config', async () => {
215
- for (const partyId of [42, { id: 'party-P' }, ' ']) {
216
- const res = await app.inject({
217
- method: 'POST', url: '/grants', headers: bearer(token),
218
- payload: { ...body, partyId },
219
- });
220
- expect(res.statusCode, `expected 400 for partyId ${JSON.stringify(partyId)}`).toBe(400);
221
- expect((res.json() as { error: { message: string } }).error.message).toBe('partyId is required');
222
- }
223
- });
224
-
225
- it('rejects an unknown profile → 400', async () => {
226
- const res = await app.inject({
227
- method: 'POST', url: '/grants', headers: bearer(token),
228
- payload: { ...body, profile: 'archive' },
229
- });
230
- expect(res.statusCode).toBe(400);
231
- expect((res.json() as { error: { message: string } }).error.message)
232
- .toBe('profile must be "storage" or "transaction"');
233
- });
234
-
235
- it('enforces the grant quota → 429 quota_exceeded once maxNodes is reached', async () => {
236
- const g = grants.issue({ label: 'Bob', maxNodes: 1 }).token;
237
- const first = await app.inject({ method: 'POST', url: '/grants', headers: bearer(g), payload: body });
238
- expect(first.statusCode).toBe(201);
239
- const second = await app.inject({ method: 'POST', url: '/grants', headers: bearer(g), payload: body });
240
- expect(second.statusCode).toBe(429);
241
- expect((second.json() as { error: { code: string } }).error.code).toBe('quota_exceeded');
242
- });
243
- });
244
-
245
- describe('GET /grants auth', () => {
246
- it('rejects an unknown bearer → 401 unauthorized (code matches the status)', async () => {
247
- const res = await app.inject({ method: 'GET', url: '/grants', headers: bearer('nope') });
248
- expect(res.statusCode).toBe(401);
249
- expect((res.json() as { error: { code: string } }).error.code).toBe('unauthorized');
250
- });
251
- });
252
-
253
- describe('GET /grants (ownership isolation)', () => {
254
- it('lists only the donations the presented grant authorized', async () => {
255
- const other = grants.issue({ label: 'Carol' }).token;
256
- await app.inject({ method: 'POST', url: '/grants', headers: bearer(token), payload: body });
257
- await app.inject({ method: 'POST', url: '/grants', headers: bearer(other), payload: body });
258
-
259
- const mine = await app.inject({ method: 'GET', url: '/grants', headers: bearer(token) });
260
- const donations = (mine.json() as { data: { donations: Array<{ grantToken: string }> } }).data.donations;
261
- expect(donations).toHaveLength(1);
262
- expect(donations[0].grantToken).toBe(token);
263
- });
264
-
265
- it('a foreign grant cannot read another grant’s donation → 404', async () => {
266
- const other = grants.issue({ label: 'Carol' }).token;
267
- const created = await app.inject({ method: 'POST', url: '/grants', headers: bearer(token), payload: body });
268
- const id = (created.json() as { data: { donation: { id: string } } }).data.donation.id;
269
-
270
- const mine = await app.inject({ method: 'GET', url: `/grants/${id}`, headers: bearer(token) });
271
- expect(mine.statusCode).toBe(200);
272
-
273
- const foreign = await app.inject({ method: 'GET', url: `/grants/${id}`, headers: bearer(other) });
274
- expect(foreign.statusCode).toBe(404);
275
- });
276
- });
277
-
278
- describe('PUT /grants/:id/seed validation', () => {
279
- it('rejects a missing seed body → 400 invalid_request', async () => {
280
- const created = await app.inject({ method: 'POST', url: '/grants', headers: bearer(token), payload: body });
281
- const id = (created.json() as { data: { donation: { id: string } } }).data.donation.id;
282
- const res = await app.inject({ method: 'PUT', url: `/grants/${id}/seed`, headers: bearer(token), payload: {} });
283
- expect(res.statusCode).toBe(400);
284
- expect((res.json() as { error: { code: string } }).error.code).toBe('invalid_request');
285
- });
286
-
287
- it('answers 409 invalid_state when the loan ends while the seed is in flight', async () => {
288
- const created = await app.inject({ method: 'POST', url: '/grants', headers: bearer(token), payload: body });
289
- const id = (created.json() as { data: { donation: { id: string } } }).data.donation.id;
290
-
291
- // The borrower's DELETE lands inside the seed window: the node accepts the
292
- // seed, but by the time it answers the loan is over. The ending wins, so the
293
- // route must NOT report a 200 that implies a live, freshly seeded node.
294
- globalThis.fetch = (async () => {
295
- await app.inject({ method: 'DELETE', url: `/grants/${id}`, headers: bearer(token) });
296
- return { ok: true, json: async () => ({ success: true, peersAdded: 2 }) } as unknown as Response;
297
- }) as typeof globalThis.fetch;
298
-
299
- const res = await app.inject({
300
- method: 'PUT', url: `/grants/${id}/seed`, headers: bearer(token), payload: { seed: 'encoded-seed' },
301
- });
302
-
303
- expect(res.statusCode).toBe(409);
304
- expect((res.json() as { error: { code: string } }).error.code).toBe('invalid_state');
305
- const after = await app.inject({ method: 'GET', url: `/grants/${id}`, headers: bearer(token) });
306
- expect((after.json() as { data: { donation: { status: string } } }).data.donation.status)
307
- .toBe('terminated');
308
- });
309
-
310
- it('answers 404 not_found when the record vanishes while the seed is in flight', async () => {
311
- const created = await app.inject({ method: 'POST', url: '/grants', headers: bearer(token), payload: body });
312
- const id = (created.json() as { data: { donation: { id: string } } }).data.donation.id;
313
-
314
- // The row is gone by the time the node answers — the other half of the
315
- // abandon mapping, which has no status to report.
316
- globalThis.fetch = (async () => {
317
- store.remove(id);
318
- return { ok: true, json: async () => ({ success: true, peersAdded: 2 }) } as unknown as Response;
319
- }) as typeof globalThis.fetch;
320
-
321
- const res = await app.inject({
322
- method: 'PUT', url: `/grants/${id}/seed`, headers: bearer(token), payload: { seed: 'encoded-seed' },
323
- });
324
-
325
- expect(res.statusCode).toBe(404);
326
- expect((res.json() as { error: { code: string } }).error.code).toBe('not_found');
327
- });
328
- });
329
-
330
- describe('DELETE /grants/:id', () => {
331
- it('terminates a donation and drops it from the live tally', async () => {
332
- const created = await app.inject({ method: 'POST', url: '/grants', headers: bearer(token), payload: body });
333
- const id = (created.json() as { data: { donation: { id: string } } }).data.donation.id;
334
-
335
- const del = await app.inject({ method: 'DELETE', url: `/grants/${id}`, headers: bearer(token) });
336
- expect(del.statusCode).toBe(200);
337
-
338
- const one = await app.inject({ method: 'GET', url: `/grants/${id}`, headers: bearer(token) });
339
- expect((one.json() as { data: { donation: { status: string } } }).data.donation.status).toBe('terminated');
340
- });
341
-
342
- // The phone app sent `content-type: application/json` on its body-less DELETE, and
343
- // Fastify's default parser answered 400 FST_ERR_CTP_EMPTY_JSON_BODY — so the loan,
344
- // and the host's node, kept running.
345
- it('ends the loan when the DELETE declares JSON but sends no body', async () => {
346
- const created = await app.inject({ method: 'POST', url: '/grants', headers: bearer(token), payload: body });
347
- const id = (created.json() as { data: { donation: { id: string } } }).data.donation.id;
348
-
349
- const del = await app.inject({
350
- method: 'DELETE', url: `/grants/${id}`,
351
- headers: { ...bearer(token), 'content-type': 'application/json' },
352
- });
353
- expect(del.statusCode).toBe(200);
354
- expect(store.get(id)?.status).toBe('terminated');
355
- });
356
- });
357
-
358
- describe('JSON body parsing (buildFastify)', () => {
359
- const json = (t: string) => ({ ...bearer(t), 'content-type': 'application/json' });
360
-
361
- it('treats a declared-JSON empty body as no body, so the route answers its own 400', async () => {
362
- const res = await app.inject({ method: 'POST', url: '/grants', headers: json(token) });
363
- expect(res.statusCode).toBe(400);
364
- const error = (res.json() as { error: { code: string; message: string } }).error;
365
- expect(error.code).toBe('invalid_request');
366
- expect(error.message).toBe('partyId is required');
367
- });
368
-
369
- it('still rejects a non-empty malformed body → 400 FST_ERR_CTP_INVALID_JSON_BODY', async () => {
370
- const res = await app.inject({ method: 'POST', url: '/grants', headers: json(token), payload: '{"partyId":' });
371
- expect(res.statusCode).toBe(400);
372
- expect((res.json() as { error: { code: string } }).error.code).toBe('FST_ERR_CTP_INVALID_JSON_BODY');
373
- });
374
-
375
- it('still refuses a prototype-poisoning body, as Fastify\'s own parser does', async () => {
376
- const res = await app.inject({
377
- method: 'POST', url: '/grants', headers: json(token),
378
- payload: '{"partyId":"party-P","__proto__":{"polluted":true}}',
379
- });
380
- expect(res.statusCode).toBe(400);
381
- expect((res.json() as { error: { code: string } }).error.code).toBe('FST_ERR_CTP_INVALID_JSON_BODY');
382
- const list = await app.inject({ method: 'GET', url: '/grants', headers: bearer(token) });
383
- expect((list.json() as { data: { donations: unknown[] } }).data.donations).toHaveLength(0);
384
- });
385
-
386
- it('parses a well-formed body with a charset parameter on the content type', async () => {
387
- const res = await app.inject({
388
- method: 'POST', url: '/grants',
389
- headers: { ...bearer(token), 'content-type': 'application/json; charset=utf-8' },
390
- payload: JSON.stringify(body),
391
- });
392
- expect(res.statusCode).toBe(201);
393
- });
394
- });
@@ -1,234 +0,0 @@
1
- /**
2
- * `/api/strands` route tests — a real Fastify built by `createLocalUiServer`
3
- * (so the error handler, the mount gate, and the static not-found fallback are
4
- * all the production ones) over a fake StrandService.
5
- *
6
- * What the service does is covered by `strands/__tests__/strand-service.test.ts`;
7
- * this file is about the HTTP contract: envelope shape, status mapping, the
8
- * `confirm` query param, and exactly-once bus publication.
9
- */
10
-
11
- import { mkdtempSync, rmSync } from 'node:fs';
12
- import { tmpdir } from 'node:os';
13
- import { join } from 'node:path';
14
-
15
- import { describe, it, expect, beforeEach, afterEach } from 'vitest';
16
-
17
- import { createLocalUiServer } from '../index.js';
18
- import type { LocalUiEvent } from '../events/types.js';
19
- import { fakeFounder } from './fakes.js';
20
- import type { HostProcessOrchestrator } from '../../orchestrator/index.js';
21
- import type { StrandService } from '../../strands/index.js';
22
- import { StrandError } from '../../strands/index.js';
23
- import type { StrandListSnapshot, StrandRemovalResult } from '../../strands/index.js';
24
-
25
- function fakeOrchestrator(): HostProcessOrchestrator {
26
- return {
27
- listNodes: () => [],
28
- getNode: () => undefined,
29
- resolveDockerId: () => undefined,
30
- onStateChange: () => () => undefined,
31
- } as unknown as HostProcessOrchestrator;
32
- }
33
-
34
- const SNAPSHOT: StrandListSnapshot = {
35
- strands: [
36
- { id: 'open-one', type: 'o', running: true, status: 'active' },
37
- { id: 'closed-one', type: 'c', running: false, status: null },
38
- ],
39
- controlConnections: 3,
40
- };
41
-
42
- interface FakeStrands {
43
- service: StrandService;
44
- /** Every (id, confirm) the route forwarded. */
45
- calls: Array<{ strandId: string; confirm: boolean }>;
46
- /** Set to make `list` and `remove` throw. */
47
- failWith?: unknown;
48
- /** Set to make `remove` report a strand that was never published. */
49
- absent?: boolean;
50
- }
51
-
52
- function fakeStrandService(): FakeStrands {
53
- const state: FakeStrands = {
54
- calls: [],
55
- service: undefined as unknown as StrandService,
56
- };
57
- state.service = {
58
- async list(): Promise<StrandListSnapshot> {
59
- if (state.failWith) throw state.failWith;
60
- return SNAPSHOT;
61
- },
62
- async remove(strandId: string, opts: { confirm: boolean }): Promise<StrandRemovalResult> {
63
- state.calls.push({ strandId, confirm: opts.confirm });
64
- if (state.failWith) throw state.failWith;
65
- return state.absent
66
- ? { strandId, published: false, type: null, removed: false, alone: false }
67
- : { strandId, published: true, type: 'o', removed: true, alone: false };
68
- },
69
- } as unknown as StrandService;
70
- return state;
71
- }
72
-
73
- let dataDir: string;
74
- let server: ReturnType<typeof createLocalUiServer>;
75
- let strands: FakeStrands;
76
- let events: LocalUiEvent[];
77
-
78
- function buildServer(opts: { withStrands: boolean }): void {
79
- server = createLocalUiServer({
80
- uiPort: 8765,
81
- dataDir,
82
- orchestrator: fakeOrchestrator(),
83
- ...(opts.withStrands ? { founder: fakeFounder({ strands: strands.service }) } : {}),
84
- forcePort: 0,
85
- });
86
- events = [];
87
- server.events.subscribe((e) => events.push(e));
88
- }
89
-
90
- beforeEach(() => {
91
- dataDir = mkdtempSync(join(tmpdir(), 'cadre-host-strands-route-'));
92
- strands = fakeStrandService();
93
- buildServer({ withStrands: true });
94
- });
95
-
96
- afterEach(async () => {
97
- await server.app.close();
98
- rmSync(dataDir, { recursive: true, force: true });
99
- });
100
-
101
- describe('GET /api/strands', () => {
102
- it('returns the snapshot in the /api/* envelope', async () => {
103
- const res = await server.app.inject({ method: 'GET', url: '/api/strands' });
104
- expect(res.statusCode).toBe(200);
105
- const body = res.json() as { ok: boolean; data: StrandListSnapshot };
106
- expect(body.ok).toBe(true);
107
- expect(body.data.strands).toHaveLength(2);
108
- expect(body.data.strands[0]?.id).toBe('open-one');
109
- expect(body.data.controlConnections).toBe(3);
110
- });
111
-
112
- it('maps a service failure through the same error handler as the DELETE arm', async () => {
113
- strands.failWith = new StrandError('node_unavailable', 'Owner node unavailable: refused');
114
- const res = await server.app.inject({ method: 'GET', url: '/api/strands' });
115
- expect(res.statusCode).toBe(503);
116
- expect((res.json() as { error: { code: string } }).error.code).toBe('node_unavailable');
117
- });
118
- });
119
-
120
- describe('DELETE /api/strands/:id', () => {
121
- it('removes a published strand → 200 and exactly one strands-changed event', async () => {
122
- const res = await server.app.inject({ method: 'DELETE', url: '/api/strands/open-one' });
123
- expect(res.statusCode).toBe(200);
124
- const body = res.json() as { ok: boolean; data: StrandRemovalResult };
125
- expect(body.ok).toBe(true);
126
- expect(body.data).toMatchObject({ strandId: 'open-one', published: true, removed: true });
127
- expect(events).toEqual([{ type: 'strands-changed', kind: 'removed' }]);
128
- });
129
-
130
- it('an absent strand → 200, published: false, and NO event', async () => {
131
- strands.absent = true;
132
- const res = await server.app.inject({ method: 'DELETE', url: '/api/strands/never-published' });
133
- expect(res.statusCode).toBe(200);
134
- const body = res.json() as { data: StrandRemovalResult };
135
- expect(body.data.published).toBe(false);
136
- expect(body.data.removed).toBe(false);
137
- expect(events).toEqual([]);
138
- });
139
-
140
- it('forwards confirm=1 as true and its absence as false', async () => {
141
- await server.app.inject({ method: 'DELETE', url: '/api/strands/a' });
142
- await server.app.inject({ method: 'DELETE', url: '/api/strands/b?confirm=1' });
143
- await server.app.inject({ method: 'DELETE', url: '/api/strands/c?confirm=true' });
144
- expect(strands.calls).toEqual([
145
- { strandId: 'a', confirm: false },
146
- { strandId: 'b', confirm: true },
147
- { strandId: 'c', confirm: true },
148
- ]);
149
- });
150
-
151
- it('treats a guessy confirm value as NOT confirmed', async () => {
152
- // `yes`/`on`/empty are refused deliberately — a typo must not destroy a
153
- // closed strand's membership key.
154
- await server.app.inject({ method: 'DELETE', url: '/api/strands/a?confirm=yes' });
155
- await server.app.inject({ method: 'DELETE', url: '/api/strands/b?confirm=' });
156
- expect(strands.calls).toEqual([
157
- { strandId: 'a', confirm: false },
158
- { strandId: 'b', confirm: false },
159
- ]);
160
- });
161
-
162
- it('ignores query params other than confirm rather than proxying them', async () => {
163
- await server.app.inject({ method: 'DELETE', url: '/api/strands/a?force=1&confirm=1' });
164
- expect(strands.calls).toEqual([{ strandId: 'a', confirm: true }]);
165
- });
166
-
167
- // The route is one path segment, so an id holding a `/` has to arrive
168
- // percent-encoded. Fastify decodes `params`, so the service sees the literal id
169
- // and forwards it — nothing in this chain needs to refuse such an id.
170
- it('carries a percent-encoded "/" through to the service as a literal', async () => {
171
- const res = await server.app.inject({ method: 'DELETE', url: '/api/strands/ns%2Fstrand' });
172
- expect(res.statusCode).toBe(200);
173
- expect(strands.calls).toEqual([{ strandId: 'ns/strand', confirm: false }]);
174
- });
175
-
176
- it('404s an UNencoded "/" at the router, before any handler runs', async () => {
177
- const res = await server.app.inject({ method: 'DELETE', url: '/api/strands/ns/strand' });
178
- expect(res.statusCode).toBe(404);
179
- expect(strands.calls).toEqual([]);
180
- });
181
- });
182
-
183
- describe('DELETE /api/strands/:id — error mapping', () => {
184
- it('confirmation_required → 428 with the node message intact', async () => {
185
- const message = 'Refusing to remove closed strand closed-one without confirmation: …';
186
- strands.failWith = new StrandError('confirmation_required', message);
187
- const res = await server.app.inject({ method: 'DELETE', url: '/api/strands/closed-one' });
188
- expect(res.statusCode).toBe(428);
189
- const body = res.json() as { ok: boolean; error: { code: string; message: string } };
190
- expect(body.ok).toBe(false);
191
- expect(body.error.code).toBe('confirmation_required');
192
- expect(body.error.message).toBe(message);
193
- expect(events).toEqual([]);
194
- });
195
-
196
- it('node_unavailable → 503', async () => {
197
- strands.failWith = new StrandError('node_unavailable', 'Owner node unavailable: refused');
198
- const res = await server.app.inject({ method: 'DELETE', url: '/api/strands/open-one' });
199
- expect(res.statusCode).toBe(503);
200
- expect((res.json() as { error: { code: string } }).error.code).toBe('node_unavailable');
201
- });
202
-
203
- it('invalid_id → 400', async () => {
204
- strands.failWith = new StrandError('invalid_id', 'A strand id is required');
205
- const res = await server.app.inject({ method: 'DELETE', url: '/api/strands/%20' });
206
- expect(res.statusCode).toBe(400);
207
- expect((res.json() as { error: { code: string } }).error.code).toBe('invalid_id');
208
- });
209
-
210
- it('internal → 500', async () => {
211
- strands.failWith = new StrandError('internal', 'node said something new');
212
- const res = await server.app.inject({ method: 'DELETE', url: '/api/strands/open-one' });
213
- expect(res.statusCode).toBe(500);
214
- expect((res.json() as { error: { code: string } }).error.code).toBe('internal');
215
- });
216
- });
217
-
218
- describe('donor-only mode (no founder services)', () => {
219
- beforeEach(async () => {
220
- await server.app.close();
221
- buildServer({ withStrands: false });
222
- });
223
-
224
- it('404s GET /api/strands through the static not-found handler', async () => {
225
- const res = await server.app.inject({ method: 'GET', url: '/api/strands' });
226
- expect(res.statusCode).toBe(404);
227
- expect((res.json() as { error: { code: string } }).error.code).toBe('not_found');
228
- });
229
-
230
- it('404s DELETE /api/strands/:id', async () => {
231
- const res = await server.app.inject({ method: 'DELETE', url: '/api/strands/open-one' });
232
- expect(res.statusCode).toBe(404);
233
- });
234
- });