@mycadet/assistant 0.12.11 → 0.13.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 (176) hide show
  1. package/node_modules/@mycadet/ces-client/node_modules/@mycadet/service-contracts/src/__tests__/ingress.test.ts +2 -2
  2. package/node_modules/@mycadet/ces-client/node_modules/@mycadet/service-contracts/src/ingress.ts +2 -2
  3. package/node_modules/@mycadet/gateway-client/node_modules/@mycadet/service-contracts/src/__tests__/ingress.test.ts +2 -2
  4. package/node_modules/@mycadet/gateway-client/node_modules/@mycadet/service-contracts/src/ingress.ts +2 -2
  5. package/node_modules/@mycadet/gateway-client/src/gateway-ipc-contracts.ts +0 -20
  6. package/node_modules/@mycadet/service-contracts/src/__tests__/ingress.test.ts +2 -2
  7. package/node_modules/@mycadet/service-contracts/src/ingress.ts +2 -2
  8. package/openapi.yaml +2 -708
  9. package/package.json +2 -3
  10. package/scripts/sync-web-search-catalog.ts +1 -2
  11. package/src/__tests__/cloud-profile-guard.test.ts +2 -2
  12. package/src/__tests__/config-schema.test.ts +6 -38
  13. package/src/__tests__/conversation-queue.test.ts +45 -6
  14. package/src/__tests__/db-readiness-http-gate.test.ts +0 -6
  15. package/src/__tests__/persist-onboarding-artifacts.test.ts +0 -1
  16. package/src/__tests__/persona-resolver.test.ts +0 -51
  17. package/src/__tests__/provider-cloud-proxy-integration.test.ts +2 -110
  18. package/src/__tests__/run-due-schedules.test.ts +0 -4
  19. package/src/__tests__/schedule-store.test.ts +0 -12
  20. package/src/__tests__/stt-stream-session.test.ts +4 -4
  21. package/src/__tests__/telephony-tts-guard.test.ts +2 -50
  22. package/src/__tests__/voice-config-update.test.ts +12 -167
  23. package/src/__tests__/web-search-catalog-parity.test.ts +0 -2
  24. package/src/__tests__/workspace-migration-130-speech-mode-to-provider.test.ts +9 -7
  25. package/src/__tests__/workspace-migration-132-web-search-mode-to-provider.test.ts +6 -6
  26. package/src/calls/__tests__/voice-session-bridge.test.ts +1 -4
  27. package/src/calls/resolve-call-tts-provider.ts +6 -25
  28. package/src/calls/telephony-credential-preflight.ts +0 -10
  29. package/src/calls/telephony-tts-capability.ts +1 -17
  30. package/src/cli/commands/tts.ts +1 -2
  31. package/src/cli/index.help.ts +0 -2
  32. package/src/cli/program.ts +0 -2
  33. package/src/config/bundled-skills/settings/tools/voice-config-update.ts +4 -42
  34. package/src/config/schemas/__tests__/stt.test.ts +10 -14
  35. package/src/config/schemas/services.ts +5 -6
  36. package/src/config/schemas/stt.ts +1 -3
  37. package/src/config/schemas/tts.ts +1 -30
  38. package/src/daemon/daemon-readiness.ts +4 -29
  39. package/src/daemon/lifecycle.ts +0 -31
  40. package/src/daemon/providers-setup.ts +0 -8
  41. package/src/heartbeat/__tests__/heartbeat-service.test.ts +0 -1
  42. package/src/heartbeat/heartbeat-service.ts +1 -66
  43. package/src/ipc/__tests__/db-readiness-ipc-gate.test.ts +1 -9
  44. package/src/live-voice/__tests__/live-voice-credential-preflight.test.ts +0 -44
  45. package/src/live-voice/__tests__/live-voice-flux-turn-end.test.ts +4 -6
  46. package/src/live-voice/__tests__/live-voice-stt.test.ts +1 -1
  47. package/src/live-voice/live-voice-credential-preflight.ts +9 -33
  48. package/src/live-voice/live-voice-session.ts +10 -6
  49. package/src/live-voice/live-voice-tts.ts +1 -8
  50. package/src/plugins/defaults/memory/worker.ts +3 -3
  51. package/src/prompts/persona-resolver.ts +0 -35
  52. package/src/providers/__tests__/search-provider-catalog.test.ts +5 -8
  53. package/src/providers/__tests__/voice-error-copy.test.ts +9 -10
  54. package/src/providers/search-provider-catalog.ts +7 -39
  55. package/src/providers/speech-to-text/__tests__/provider-catalog.test.ts +3 -47
  56. package/src/providers/speech-to-text/__tests__/resolve.test.ts +6 -372
  57. package/src/providers/speech-to-text/provider-catalog.ts +2 -73
  58. package/src/providers/speech-to-text/resolve.ts +3 -120
  59. package/src/runtime/http-router-types.ts +1 -3
  60. package/src/runtime/http-server.ts +1 -1
  61. package/src/runtime/routes/__tests__/tts-routes.test.ts +2 -6
  62. package/src/runtime/routes/inbound-stages/transcribe-audio.test.ts +7 -7
  63. package/src/runtime/routes/index.ts +0 -4
  64. package/src/runtime/routes/live-voice-routes.test.ts +4 -11
  65. package/src/runtime/routes/live-voice-routes.ts +8 -11
  66. package/src/runtime/routes/secret-routes.ts +0 -6
  67. package/src/runtime/routes/tts-routes.ts +0 -38
  68. package/src/schedule/__tests__/plugin-schedule-reconciler.test.ts +1 -7
  69. package/src/schedule/__tests__/worker-mcp-readiness.test.ts +0 -4
  70. package/src/schedule/schedule-store.ts +0 -7
  71. package/src/schedule/scheduler.ts +0 -5
  72. package/src/schedule/worker.ts +1 -1
  73. package/src/stt/__tests__/daemon-batch-transcriber.test.ts +2 -10
  74. package/src/stt/daemon-batch-transcriber.ts +4 -38
  75. package/src/stt/types.ts +1 -9
  76. package/src/tools/network/__tests__/web-search.test.ts +3 -250
  77. package/src/tools/network/web-search.ts +4 -165
  78. package/src/tts/__tests__/provider-resolution.test.ts +0 -5
  79. package/src/tts/provider-catalog.ts +0 -4
  80. package/src/tts/types.ts +0 -1
  81. package/src/__tests__/cloud-speech-defaults.test.ts +0 -330
  82. package/src/__tests__/migration-cross-version-compatibility.test.ts +0 -1708
  83. package/src/__tests__/migration-export-http.test.ts +0 -557
  84. package/src/__tests__/migration-export-streaming.test.ts +0 -350
  85. package/src/__tests__/migration-export-to-gcs.test.ts +0 -706
  86. package/src/__tests__/migration-import-commit-http.test.ts +0 -1176
  87. package/src/__tests__/migration-import-from-gcs.test.ts +0 -534
  88. package/src/__tests__/migration-import-from-path.test.ts +0 -324
  89. package/src/__tests__/migration-import-from-url.test.ts +0 -588
  90. package/src/__tests__/migration-import-preflight-http.test.ts +0 -739
  91. package/src/__tests__/migration-jobs-status.test.ts +0 -139
  92. package/src/__tests__/migration-parity-persistence.test.ts +0 -1891
  93. package/src/__tests__/migration-transport.test.ts +0 -1089
  94. package/src/__tests__/migration-validate-http.test.ts +0 -634
  95. package/src/__tests__/migration-wizard.test.ts +0 -1395
  96. package/src/__tests__/rebind-secrets-screen.test.ts +0 -876
  97. package/src/__tests__/sanitize-config-for-transfer.test.ts +0 -259
  98. package/src/__tests__/transfer-progress-screen.test.ts +0 -1217
  99. package/src/__tests__/validation-results-screen.test.ts +0 -1130
  100. package/src/__tests__/vbundle-pax-and-symlink.test.ts +0 -147
  101. package/src/background-wake/background-wake-routes.test.ts +0 -906
  102. package/src/background-wake/cloud-client.test.ts +0 -328
  103. package/src/background-wake/cloud-client.ts +0 -196
  104. package/src/background-wake/next-wake.test.ts +0 -335
  105. package/src/background-wake/next-wake.ts +0 -188
  106. package/src/background-wake/publisher.ts +0 -93
  107. package/src/background-wake/wake-intent-hooks.test.ts +0 -234
  108. package/src/cli/commands/__tests__/backup.test.ts +0 -363
  109. package/src/cli/commands/backup.help.ts +0 -180
  110. package/src/cli/commands/backup.ts +0 -398
  111. package/src/cloud/cloud-speech.test.ts +0 -335
  112. package/src/cloud/cloud-speech.ts +0 -300
  113. package/src/config/cloud-speech-defaults.ts +0 -300
  114. package/src/config/sanitize-for-transfer.ts +0 -111
  115. package/src/live-voice/__tests__/live-voice-cloud-speech-fallback.test.ts +0 -250
  116. package/src/providers/speech-to-text/__tests__/cadet-cloud-realtime.test.ts +0 -457
  117. package/src/providers/speech-to-text/__tests__/cadet-cloud.test.ts +0 -271
  118. package/src/providers/speech-to-text/__tests__/cadet-speech-relay-connection.test.ts +0 -126
  119. package/src/providers/speech-to-text/cadet-cloud-flux-realtime.ts +0 -309
  120. package/src/providers/speech-to-text/cadet-cloud-realtime.ts +0 -315
  121. package/src/providers/speech-to-text/cadet-cloud.ts +0 -108
  122. package/src/providers/speech-to-text/cadet-speech-relay-connection.ts +0 -176
  123. package/src/runtime/migrations/__tests__/gcs-signed-url.test.ts +0 -254
  124. package/src/runtime/migrations/__tests__/job-registry.test.ts +0 -346
  125. package/src/runtime/migrations/__tests__/origin-mode.test.ts +0 -62
  126. package/src/runtime/migrations/__tests__/rebind-secrets-credentials.test.ts +0 -184
  127. package/src/runtime/migrations/__tests__/staged-import-path.test.ts +0 -110
  128. package/src/runtime/migrations/__tests__/v1-test-helpers.ts +0 -112
  129. package/src/runtime/migrations/__tests__/vbundle-builder-credentials.test.ts +0 -285
  130. package/src/runtime/migrations/__tests__/vbundle-builder-fd-leak.test.ts +0 -126
  131. package/src/runtime/migrations/__tests__/vbundle-builder-v1-shape.test.ts +0 -253
  132. package/src/runtime/migrations/__tests__/vbundle-import-credentials.test.ts +0 -208
  133. package/src/runtime/migrations/__tests__/vbundle-import-parity.test.ts +0 -485
  134. package/src/runtime/migrations/__tests__/vbundle-import-policy.test.ts +0 -349
  135. package/src/runtime/migrations/__tests__/vbundle-import-version-compat.test.ts +0 -196
  136. package/src/runtime/migrations/__tests__/vbundle-legacy-user-md.test.ts +0 -551
  137. package/src/runtime/migrations/__tests__/vbundle-metadata-merge-integration.test.ts +0 -428
  138. package/src/runtime/migrations/__tests__/vbundle-metadata-merge.test.ts +0 -220
  139. package/src/runtime/migrations/__tests__/vbundle-streaming-importer.test.ts +0 -1823
  140. package/src/runtime/migrations/__tests__/vbundle-streaming-validator.test.ts +0 -598
  141. package/src/runtime/migrations/__tests__/vbundle-symlink-importer.test.ts +0 -463
  142. package/src/runtime/migrations/__tests__/vbundle-symlink-streaming-importer.test.ts +0 -538
  143. package/src/runtime/migrations/__tests__/vbundle-symlink-streaming.test.ts +0 -515
  144. package/src/runtime/migrations/__tests__/vbundle-symlink-tar.test.ts +0 -453
  145. package/src/runtime/migrations/__tests__/vbundle-symlink-walker.test.ts +0 -319
  146. package/src/runtime/migrations/__tests__/vbundle-tar-stream.test.ts +0 -240
  147. package/src/runtime/migrations/__tests__/vbundle-validator-v1-schema.test.ts +0 -473
  148. package/src/runtime/migrations/gcs-signed-url.ts +0 -190
  149. package/src/runtime/migrations/job-registry.ts +0 -297
  150. package/src/runtime/migrations/migration-transport.ts +0 -612
  151. package/src/runtime/migrations/migration-wizard.ts +0 -747
  152. package/src/runtime/migrations/origin-mode.ts +0 -42
  153. package/src/runtime/migrations/rebind-secrets-screen.ts +0 -412
  154. package/src/runtime/migrations/staged-import-path.ts +0 -116
  155. package/src/runtime/migrations/transfer-progress-screen.ts +0 -323
  156. package/src/runtime/migrations/validation-results-screen.ts +0 -469
  157. package/src/runtime/migrations/vbundle-builder.ts +0 -1382
  158. package/src/runtime/migrations/vbundle-import-analyzer.ts +0 -405
  159. package/src/runtime/migrations/vbundle-import-policy.ts +0 -241
  160. package/src/runtime/migrations/vbundle-importer.ts +0 -826
  161. package/src/runtime/migrations/vbundle-metadata-merge.ts +0 -113
  162. package/src/runtime/migrations/vbundle-streaming-importer.ts +0 -2844
  163. package/src/runtime/migrations/vbundle-streaming-validator.ts +0 -426
  164. package/src/runtime/migrations/vbundle-tar-stream.ts +0 -244
  165. package/src/runtime/migrations/vbundle-validator.ts +0 -724
  166. package/src/runtime/routes/__tests__/migration-cadet-metadata-reconcile.test.ts +0 -227
  167. package/src/runtime/routes/__tests__/migration-export-secrets-redacted.test.ts +0 -54
  168. package/src/runtime/routes/__tests__/migration-import-allowed-hosts-env.test.ts +0 -70
  169. package/src/runtime/routes/__tests__/migration-import-credential-filter.test.ts +0 -257
  170. package/src/runtime/routes/background-wake-routes.ts +0 -387
  171. package/src/runtime/routes/migration-routes.ts +0 -2584
  172. package/src/tools/network/__tests__/cloud-search-proxy.test.ts +0 -282
  173. package/src/tools/network/cloud-search-proxy.ts +0 -211
  174. package/src/tts/__tests__/cadet-provider.test.ts +0 -253
  175. package/src/tts/providers/cadet-provider.ts +0 -244
  176. package/src/types/tar-stream.d.ts +0 -66
@@ -1,634 +0,0 @@
1
- /**
2
- * HTTP-layer integration tests for POST /v1/migrations/validate.
3
- *
4
- * Tests cover:
5
- * - Success: valid .vbundle archive returns is_valid: true with manifest
6
- * - Validation failures: invalid gzip, missing entries, bad manifest, checksum mismatches
7
- * - Request errors: empty body, invalid multipart
8
- * - Auth: route policy enforcement (settings.read scope required)
9
- * - Integration: existing routes are unaffected by the new endpoint
10
- */
11
- import { createHash } from "node:crypto";
12
- import { gzipSync } from "node:zlib";
13
- import { describe, expect, test } from "bun:test";
14
-
15
- /** Convert a Uint8Array to an ArrayBuffer for BodyInit compatibility. */
16
- function toArrayBuffer(data: Uint8Array): ArrayBuffer {
17
- // Slice to get an owned ArrayBuffer (not a view into a SharedArrayBuffer)
18
- return data.buffer.slice(
19
- data.byteOffset,
20
- data.byteOffset + data.byteLength,
21
- ) as ArrayBuffer;
22
- }
23
-
24
- process.env.DISABLE_HTTP_AUTH = "true";
25
-
26
- import { validateVBundle } from "../runtime/migrations/vbundle-validator.js";
27
- import { BadRequestError } from "../runtime/routes/errors.js";
28
- import { handleMigrationValidate } from "../runtime/routes/migration-routes.js";
29
- import type { RouteHandlerArgs } from "../runtime/routes/types.js";
30
-
31
- // ---------------------------------------------------------------------------
32
- // Tar archive builder helpers
33
- // ---------------------------------------------------------------------------
34
-
35
- const BLOCK_SIZE = 512;
36
-
37
- function padToBlock(data: Uint8Array): Uint8Array {
38
- const remainder = data.length % BLOCK_SIZE;
39
- if (remainder === 0) {
40
- return data;
41
- }
42
- const padded = new Uint8Array(data.length + (BLOCK_SIZE - remainder));
43
- padded.set(data);
44
- return padded;
45
- }
46
-
47
- function writeOctal(
48
- buf: Uint8Array,
49
- offset: number,
50
- length: number,
51
- value: number,
52
- ): void {
53
- const str = value.toString(8).padStart(length - 1, "0");
54
- for (let i = 0; i < str.length; i++) {
55
- buf[offset + i] = str.charCodeAt(i);
56
- }
57
- buf[offset + length - 1] = 0;
58
- }
59
-
60
- function computeHeaderChecksum(header: Uint8Array): number {
61
- // Per tar spec, checksum uses spaces (0x20) in the checksum field
62
- let sum = 0;
63
- for (let i = 0; i < 512; i++) {
64
- if (i >= 148 && i < 156) {
65
- sum += 0x20; // space
66
- } else {
67
- sum += header[i];
68
- }
69
- }
70
- return sum;
71
- }
72
-
73
- function createTarEntry(name: string, data: Uint8Array): Uint8Array {
74
- const header = new Uint8Array(BLOCK_SIZE);
75
- const encoder = new TextEncoder();
76
-
77
- // File name (0-99)
78
- const nameBytes = encoder.encode(name);
79
- header.set(nameBytes.subarray(0, 100), 0);
80
-
81
- // File mode (100-107): 0644
82
- writeOctal(header, 100, 8, 0o644);
83
-
84
- // Owner ID (108-115)
85
- writeOctal(header, 108, 8, 0);
86
-
87
- // Group ID (116-123)
88
- writeOctal(header, 116, 8, 0);
89
-
90
- // File size (124-135)
91
- writeOctal(header, 124, 12, data.length);
92
-
93
- // Modification time (136-147)
94
- writeOctal(header, 136, 12, Math.floor(Date.now() / 1000));
95
-
96
- // Type flag (156): regular file
97
- header[156] = "0".charCodeAt(0);
98
-
99
- // USTAR magic (257-262)
100
- const magic = encoder.encode("ustar\0");
101
- header.set(magic, 257);
102
-
103
- // USTAR version (263-264)
104
- header[263] = "0".charCodeAt(0);
105
- header[264] = "0".charCodeAt(0);
106
-
107
- // Compute and write checksum (148-155)
108
- const checksum = computeHeaderChecksum(header);
109
- writeOctal(header, 148, 7, checksum);
110
- header[155] = 0x20; // trailing space
111
-
112
- // Combine header + data (padded to block boundary) + end-of-archive
113
- const paddedData = padToBlock(data);
114
- const result = new Uint8Array(header.length + paddedData.length);
115
- result.set(header, 0);
116
- result.set(paddedData, header.length);
117
- return result;
118
- }
119
-
120
- function createTarArchive(
121
- entries: Array<{ name: string; data: Uint8Array }>,
122
- ): Uint8Array {
123
- const parts: Uint8Array[] = [];
124
- for (const entry of entries) {
125
- parts.push(createTarEntry(entry.name, entry.data));
126
- }
127
- // End-of-archive: two zero blocks
128
- parts.push(new Uint8Array(BLOCK_SIZE * 2));
129
-
130
- const totalLength = parts.reduce((sum, p) => sum + p.length, 0);
131
- const result = new Uint8Array(totalLength);
132
- let offset = 0;
133
- for (const part of parts) {
134
- result.set(part, offset);
135
- offset += part.length;
136
- }
137
- return result;
138
- }
139
-
140
- function sha256Hex(data: Uint8Array | string): string {
141
- return createHash("sha256").update(data).digest("hex");
142
- }
143
-
144
- function canonicalizeJson(obj: unknown): string {
145
- return JSON.stringify(obj, (_key, value) => {
146
- if (value && typeof value === "object" && !Array.isArray(value)) {
147
- const sorted: Record<string, unknown> = {};
148
- for (const k of Object.keys(value as Record<string, unknown>).sort()) {
149
- sorted[k] = (value as Record<string, unknown>)[k];
150
- }
151
- return sorted;
152
- }
153
- return value;
154
- });
155
- }
156
-
157
- interface VBundleFile {
158
- path: string;
159
- data: Uint8Array;
160
- }
161
-
162
- /** Build a v1-compliant manifest skeleton; tests override specific fields. */
163
- function v1Skeleton() {
164
- return {
165
- schema_version: 1,
166
- bundle_id: "00000000-0000-4000-8000-000000000000",
167
- created_at: new Date().toISOString(),
168
- assistant: { id: "self", name: "Test", runtime_version: "0.0.0-test" },
169
- origin: { mode: "self-hosted-local" as const },
170
- compatibility: {
171
- min_runtime_version: "0.0.0-test",
172
- max_runtime_version: null,
173
- },
174
- secrets_redacted: false,
175
- export_options: {
176
- include_logs: false,
177
- include_browser_state: false,
178
- include_memory_vectors: false,
179
- },
180
- };
181
- }
182
-
183
- function createValidVBundle(files?: VBundleFile[]): Uint8Array {
184
- const dbData = new Uint8Array([0x53, 0x51, 0x4c, 0x69, 0x74, 0x65]); // "SQLite"
185
- const bundleFiles = files ?? [{ path: "data/db/assistant.db", data: dbData }];
186
-
187
- const contents = bundleFiles.map((f) => ({
188
- path: f.path,
189
- sha256: sha256Hex(f.data),
190
- size_bytes: f.data.length,
191
- }));
192
-
193
- const manifestWithEmptyChecksum = {
194
- schema_version: 1,
195
- bundle_id: "00000000-0000-4000-8000-000000000000",
196
- created_at: new Date().toISOString(),
197
- assistant: { id: "self", name: "Test", runtime_version: "0.0.0-test" },
198
- origin: { mode: "self-hosted-local" },
199
- compatibility: {
200
- min_runtime_version: "0.0.0-test",
201
- max_runtime_version: null,
202
- },
203
- contents,
204
- checksum: "",
205
- secrets_redacted: false,
206
- export_options: {
207
- include_logs: false,
208
- include_browser_state: false,
209
- include_memory_vectors: false,
210
- },
211
- };
212
-
213
- const checksum = sha256Hex(canonicalizeJson(manifestWithEmptyChecksum));
214
- const manifest = { ...manifestWithEmptyChecksum, checksum };
215
- const manifestData = new TextEncoder().encode(JSON.stringify(manifest));
216
-
217
- const tarEntries = [
218
- { name: "manifest.json", data: manifestData },
219
- ...bundleFiles.map((f) => ({ name: f.path, data: f.data })),
220
- ];
221
-
222
- const tar = createTarArchive(tarEntries);
223
- return gzipSync(tar);
224
- }
225
-
226
- // ---------------------------------------------------------------------------
227
- // Validator unit tests
228
- // ---------------------------------------------------------------------------
229
-
230
- describe("validateVBundle", () => {
231
- test("valid bundle returns is_valid: true with manifest", () => {
232
- const vbundle = createValidVBundle();
233
- const result = validateVBundle(vbundle);
234
-
235
- expect(result.is_valid).toBe(true);
236
- expect(result.errors).toHaveLength(0);
237
- expect(result.manifest).toBeDefined();
238
- expect(result.manifest?.schema_version).toBe(1);
239
- expect(result.manifest?.contents).toHaveLength(1);
240
- expect(result.manifest?.contents[0].path).toBe("data/db/assistant.db");
241
- });
242
-
243
- test("valid bundle with multiple files", () => {
244
- const configData = new TextEncoder().encode('{"key": "value"}');
245
- const dbData = new Uint8Array([0x53, 0x51, 0x4c, 0x69, 0x74, 0x65]);
246
- const files: VBundleFile[] = [
247
- { path: "data/db/assistant.db", data: dbData },
248
- { path: "config/settings.json", data: configData },
249
- ];
250
- const vbundle = createValidVBundle(files);
251
- const result = validateVBundle(vbundle);
252
-
253
- expect(result.is_valid).toBe(true);
254
- expect(result.errors).toHaveLength(0);
255
- expect(result.manifest?.contents).toHaveLength(2);
256
- });
257
-
258
- test("invalid gzip returns INVALID_GZIP error", () => {
259
- const result = validateVBundle(new Uint8Array([0x00, 0x01, 0x02]));
260
-
261
- expect(result.is_valid).toBe(false);
262
- expect(result.errors).toHaveLength(1);
263
- expect(result.errors[0].code).toBe("INVALID_GZIP");
264
- });
265
-
266
- test("missing manifest.json returns MISSING_ENTRY error", () => {
267
- const dbData = new Uint8Array([0x53, 0x51, 0x4c, 0x69, 0x74, 0x65]);
268
- const tar = createTarArchive([
269
- { name: "data/db/assistant.db", data: dbData },
270
- ]);
271
- const vbundle = gzipSync(tar);
272
- const result = validateVBundle(vbundle);
273
-
274
- expect(result.is_valid).toBe(false);
275
- const manifestError = result.errors.find(
276
- (e) => e.code === "MISSING_ENTRY" && e.path === "manifest.json",
277
- );
278
- expect(manifestError).toBeDefined();
279
- });
280
-
281
- test("bundle with only manifest (no data files) — valid schema rejects empty contents via refine", () => {
282
- // The v1 schema's `.refine()` requires `data/db/assistant.db` in
283
- // contents, so a manifest declaring `contents: []` is rejected by
284
- // the validator. This documents the new behavior: bundles must
285
- // declare the assistant DB even when no other data files are
286
- // present.
287
- const skeleton = {
288
- ...v1Skeleton(),
289
- contents: [] as Array<{
290
- path: string;
291
- sha256: string;
292
- size_bytes: number;
293
- }>,
294
- checksum: "",
295
- };
296
- skeleton.checksum = sha256Hex(canonicalizeJson(skeleton));
297
- const manifestData = new TextEncoder().encode(JSON.stringify(skeleton));
298
- const tar = createTarArchive([
299
- { name: "manifest.json", data: manifestData },
300
- ]);
301
- const vbundle = gzipSync(tar);
302
- const result = validateVBundle(vbundle);
303
-
304
- expect(result.is_valid).toBe(false);
305
- });
306
-
307
- test("invalid manifest JSON returns INVALID_MANIFEST_JSON error", () => {
308
- const badJson = new TextEncoder().encode("not valid json{{{");
309
- const dbData = new Uint8Array([0x53, 0x51, 0x4c, 0x69, 0x74, 0x65]);
310
- const tar = createTarArchive([
311
- { name: "manifest.json", data: badJson },
312
- { name: "data/db/assistant.db", data: dbData },
313
- ]);
314
- const vbundle = gzipSync(tar);
315
- const result = validateVBundle(vbundle);
316
-
317
- expect(result.is_valid).toBe(false);
318
- expect(result.errors.some((e) => e.code === "INVALID_MANIFEST_JSON")).toBe(
319
- true,
320
- );
321
- });
322
-
323
- test("manifest with missing required fields returns MANIFEST_SCHEMA_ERROR", () => {
324
- // Missing schema_version, created_at, files, manifest_sha256
325
- const manifestData = new TextEncoder().encode(
326
- JSON.stringify({ extra: "field" }),
327
- );
328
- const dbData = new Uint8Array([0x53, 0x51, 0x4c, 0x69, 0x74, 0x65]);
329
- const tar = createTarArchive([
330
- { name: "manifest.json", data: manifestData },
331
- { name: "data/db/assistant.db", data: dbData },
332
- ]);
333
- const vbundle = gzipSync(tar);
334
- const result = validateVBundle(vbundle);
335
-
336
- expect(result.is_valid).toBe(false);
337
- expect(result.errors.some((e) => e.code === "MANIFEST_SCHEMA_ERROR")).toBe(
338
- true,
339
- );
340
- });
341
-
342
- test("manifest checksum mismatch returns MANIFEST_CHECKSUM_MISMATCH", () => {
343
- const dbData = new Uint8Array([0x53, 0x51, 0x4c, 0x69, 0x74, 0x65]);
344
- const manifest = {
345
- ...v1Skeleton(),
346
- contents: [
347
- {
348
- path: "data/db/assistant.db",
349
- sha256: sha256Hex(dbData),
350
- size_bytes: dbData.length,
351
- },
352
- ],
353
- // Deliberately wrong checksum (correct shape, wrong value).
354
- checksum:
355
- "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
356
- };
357
- const manifestData = new TextEncoder().encode(JSON.stringify(manifest));
358
- const tar = createTarArchive([
359
- { name: "manifest.json", data: manifestData },
360
- { name: "data/db/assistant.db", data: dbData },
361
- ]);
362
- const vbundle = gzipSync(tar);
363
- const result = validateVBundle(vbundle);
364
-
365
- expect(result.is_valid).toBe(false);
366
- expect(
367
- result.errors.some((e) => e.code === "MANIFEST_CHECKSUM_MISMATCH"),
368
- ).toBe(true);
369
- });
370
-
371
- test("file checksum mismatch returns FILE_CHECKSUM_MISMATCH", () => {
372
- const dbData = new Uint8Array([0x53, 0x51, 0x4c, 0x69, 0x74, 0x65]);
373
- const wrongChecksum =
374
- "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb";
375
-
376
- const manifest = {
377
- ...v1Skeleton(),
378
- contents: [
379
- {
380
- path: "data/db/assistant.db",
381
- sha256: wrongChecksum,
382
- size_bytes: dbData.length,
383
- },
384
- ],
385
- checksum: "",
386
- };
387
- manifest.checksum = sha256Hex(canonicalizeJson(manifest));
388
- const manifestData = new TextEncoder().encode(JSON.stringify(manifest));
389
-
390
- const tar = createTarArchive([
391
- { name: "manifest.json", data: manifestData },
392
- { name: "data/db/assistant.db", data: dbData },
393
- ]);
394
- const vbundle = gzipSync(tar);
395
- const result = validateVBundle(vbundle);
396
-
397
- expect(result.is_valid).toBe(false);
398
- expect(result.errors.some((e) => e.code === "FILE_CHECKSUM_MISMATCH")).toBe(
399
- true,
400
- );
401
- });
402
-
403
- test("file size mismatch returns FILE_SIZE_MISMATCH", () => {
404
- const dbData = new Uint8Array([0x53, 0x51, 0x4c, 0x69, 0x74, 0x65]);
405
-
406
- const manifest = {
407
- ...v1Skeleton(),
408
- contents: [
409
- {
410
- path: "data/db/assistant.db",
411
- sha256: sha256Hex(dbData),
412
- size_bytes: 999,
413
- },
414
- ],
415
- checksum: "",
416
- };
417
- manifest.checksum = sha256Hex(canonicalizeJson(manifest));
418
- const manifestData = new TextEncoder().encode(JSON.stringify(manifest));
419
-
420
- const tar = createTarArchive([
421
- { name: "manifest.json", data: manifestData },
422
- { name: "data/db/assistant.db", data: dbData },
423
- ]);
424
- const vbundle = gzipSync(tar);
425
- const result = validateVBundle(vbundle);
426
-
427
- expect(result.is_valid).toBe(false);
428
- expect(result.errors.some((e) => e.code === "FILE_SIZE_MISMATCH")).toBe(
429
- true,
430
- );
431
- });
432
-
433
- test("missing declared file returns MISSING_DECLARED_FILE", () => {
434
- const dbData = new Uint8Array([0x53, 0x51, 0x4c, 0x69, 0x74, 0x65]);
435
- const missingFileHash = sha256Hex(new TextEncoder().encode("missing"));
436
-
437
- const manifest = {
438
- ...v1Skeleton(),
439
- contents: [
440
- {
441
- path: "data/db/assistant.db",
442
- sha256: sha256Hex(dbData),
443
- size_bytes: dbData.length,
444
- },
445
- {
446
- path: "config/missing.json",
447
- sha256: missingFileHash,
448
- size_bytes: 7,
449
- },
450
- ],
451
- checksum: "",
452
- };
453
- manifest.checksum = sha256Hex(canonicalizeJson(manifest));
454
- const manifestData = new TextEncoder().encode(JSON.stringify(manifest));
455
-
456
- const tar = createTarArchive([
457
- { name: "manifest.json", data: manifestData },
458
- { name: "data/db/assistant.db", data: dbData },
459
- ]);
460
- const vbundle = gzipSync(tar);
461
- const result = validateVBundle(vbundle);
462
-
463
- expect(result.is_valid).toBe(false);
464
- expect(
465
- result.errors.some(
466
- (e) =>
467
- e.code === "MISSING_DECLARED_FILE" &&
468
- e.path === "config/missing.json",
469
- ),
470
- ).toBe(true);
471
- });
472
- });
473
-
474
- // ---------------------------------------------------------------------------
475
- // HTTP route handler tests
476
- // ---------------------------------------------------------------------------
477
-
478
- /** Build RouteHandlerArgs from multipart FormData by serializing through a temp Request. */
479
- async function multipartArgs(formData: FormData): Promise<RouteHandlerArgs> {
480
- const tmpReq = new Request("http://localhost", {
481
- method: "POST",
482
- body: formData,
483
- });
484
- // Read content-type BEFORE consuming the body — Bun clears headers after arrayBuffer().
485
- const contentType = tmpReq.headers.get("content-type") ?? "";
486
- const rawBody = new Uint8Array(await tmpReq.arrayBuffer());
487
- return { rawBody, headers: { "content-type": contentType } };
488
- }
489
-
490
- describe("handleMigrationValidate", () => {
491
- test("POST with valid vbundle returns is_valid: true", async () => {
492
- const vbundle = createValidVBundle();
493
- const body = (await handleMigrationValidate({
494
- rawBody: vbundle,
495
- headers: { "content-type": "application/octet-stream" },
496
- })) as Record<string, unknown>;
497
-
498
- expect(body.is_valid).toBe(true);
499
- expect(body.errors).toEqual([]);
500
- expect(body.manifest).toBeDefined();
501
- });
502
-
503
- test("POST with invalid gzip returns is_valid: false", async () => {
504
- const body = (await handleMigrationValidate({
505
- rawBody: new Uint8Array([0xde, 0xad, 0xbe, 0xef]),
506
- headers: { "content-type": "application/octet-stream" },
507
- })) as { is_valid: boolean; errors: Array<{ code: string }> };
508
-
509
- expect(body.is_valid).toBe(false);
510
- expect(body.errors.length).toBeGreaterThan(0);
511
- expect(body.errors[0].code).toBe("INVALID_GZIP");
512
- });
513
-
514
- test("POST with empty body throws BadRequestError", async () => {
515
- await expect(
516
- handleMigrationValidate({
517
- rawBody: new Uint8Array(0),
518
- headers: { "content-type": "application/octet-stream" },
519
- }),
520
- ).rejects.toBeInstanceOf(BadRequestError);
521
- });
522
-
523
- test("POST with multipart form data containing valid vbundle", async () => {
524
- const vbundle = createValidVBundle();
525
- const formData = new FormData();
526
- formData.append("file", new Blob([toArrayBuffer(vbundle)]), "test.vbundle");
527
-
528
- const body = (await handleMigrationValidate(
529
- await multipartArgs(formData),
530
- )) as Record<string, unknown>;
531
-
532
- expect(body.is_valid).toBe(true);
533
- });
534
-
535
- test("POST multipart without file field throws BadRequestError", async () => {
536
- const formData = new FormData();
537
- formData.append("notfile", "some text");
538
-
539
- await expect(
540
- handleMigrationValidate(await multipartArgs(formData)),
541
- ).rejects.toBeInstanceOf(BadRequestError);
542
- });
543
-
544
- test("POST with missing manifest returns validation errors", async () => {
545
- const dbData = new Uint8Array([0x53, 0x51, 0x4c, 0x69, 0x74, 0x65]);
546
- const tar = createTarArchive([
547
- { name: "data/db/assistant.db", data: dbData },
548
- ]);
549
- const vbundle = gzipSync(tar);
550
-
551
- const body = (await handleMigrationValidate({
552
- rawBody: vbundle,
553
- headers: { "content-type": "application/octet-stream" },
554
- })) as {
555
- is_valid: boolean;
556
- errors: Array<{ code: string; path?: string }>;
557
- };
558
-
559
- expect(body.is_valid).toBe(false);
560
- expect(
561
- body.errors.some(
562
- (e) => e.code === "MISSING_ENTRY" && e.path === "manifest.json",
563
- ),
564
- ).toBe(true);
565
- });
566
-
567
- test("POST with checksum mismatch returns validation errors", async () => {
568
- const dbData = new Uint8Array([0x53, 0x51, 0x4c, 0x69, 0x74, 0x65]);
569
- const manifest = {
570
- ...v1Skeleton(),
571
- contents: [
572
- {
573
- path: "data/db/assistant.db",
574
- sha256: sha256Hex(dbData),
575
- size_bytes: dbData.length,
576
- },
577
- ],
578
- // Deliberately wrong checksum (correct shape, wrong value).
579
- checksum:
580
- "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
581
- };
582
- const manifestData = new TextEncoder().encode(JSON.stringify(manifest));
583
- const tar = createTarArchive([
584
- { name: "manifest.json", data: manifestData },
585
- { name: "data/db/assistant.db", data: dbData },
586
- ]);
587
- const vbundle = gzipSync(tar);
588
-
589
- const body = (await handleMigrationValidate({
590
- rawBody: vbundle,
591
- headers: { "content-type": "application/octet-stream" },
592
- })) as { is_valid: boolean; errors: Array<{ code: string }> };
593
-
594
- expect(body.is_valid).toBe(false);
595
- expect(
596
- body.errors.some((e) => e.code === "MANIFEST_CHECKSUM_MISMATCH"),
597
- ).toBe(true);
598
- });
599
- });
600
-
601
- // ---------------------------------------------------------------------------
602
- // Auth policy registration test
603
- // ---------------------------------------------------------------------------
604
-
605
- describe("route policy registration", () => {
606
- test("migrations/validate policy requires settings.read scope", async () => {
607
- // Import route-policy to verify the registration exists
608
- const { ROUTES } = await import("../runtime/routes/index.js");
609
- const policy =
610
- ROUTES.find((r) => r.endpoint === "migrations/validate")?.policy ?? null;
611
-
612
- expect(policy).not.toBeNull();
613
- expect(policy?.requiredScopes).toContain("settings.read");
614
- expect(policy?.allowedPrincipalTypes).toContain("actor");
615
- expect(policy?.allowedPrincipalTypes).toContain("svc_gateway");
616
- expect(policy?.allowedPrincipalTypes).toContain("local");
617
- });
618
- });
619
-
620
- // ---------------------------------------------------------------------------
621
- // Integration: existing routes unaffected
622
- // ---------------------------------------------------------------------------
623
-
624
- describe("integration: existing routes unaffected", () => {
625
- test("GET /v1/health still works (not intercepted by migration routes)", async () => {
626
- const { handleDetailedHealth } =
627
- await import("../runtime/routes/identity-routes.js");
628
- const res = await handleDetailedHealth();
629
- const body = (await res.json()) as Record<string, unknown>;
630
-
631
- expect(res.status).toBe(200);
632
- expect(body.status).toBe("healthy");
633
- });
634
- });