Commit d82b192
test(models): unit tests + simplify registry + writer mock seam
Part of #628 (Phase 1 step 7). 47 new unit tests + 1 Scope.test addition.
backendRegistry.test.js (8 tests): direct-mapping API, default
logicalName, multiple names, kind isolation, not-found error,
re-mapping replaces, clearRegistry empties, error message does not
enumerate registrations.
TestBackend.test.js (11 tests): capabilities + name, embed shape +
determinism + token math, generate string + Message[] + {messages}
inputs + usage, generateStream chunks + finishReason.
Models.test.js (17 tests): unwraps to Float32Array[], analytics
record on success/failure, no-ALS fallback (tenant/app undefined),
ALS-bound accounting, extractTenantId user.tenant→user.tenantId
fallback, AbortSignal precedence (opts > ctx), ctx.signal forwarded
when opts.signal absent, ModelCapabilityError, ModelPendingNotSupportedError,
sanitized error_code (backend_error/aborted), no leak of raw
upstream message into the row, generate adapter+conversation_id,
generateStream success record at stream end, generateStream
mid-stream throw → success=false.
analyticsTable.test.js (11 tests): pure-buffer behavior via a mock
table. write buffers without writing, multiple writes accumulate,
write after stop is a no-op, flush writes batch + clears buffer,
flush no-op on empty buffer, flush works after stop (drain),
records carry the expected schema fields, put-throw doesn't drop
remaining records, maxBufferSize triggers out-of-cadence flush,
cleanup removes old rows, cleanup is a no-op when nothing is old.
Real-LMDB analytics coverage is intentionally not unit-tested —
mirrors `analytics/write.ts`'s posture (unit tests only the pure
helpers; LMDB writes are end-to-end-only).
Scope.test.js: extends the existing 'should create a default entry
handler' test to assert `scope.models` is a `Models` instance and
exposes the three callable methods. The wiring point itself, in
the right place.
Registry refactor (resources/models/backendRegistry.ts): the prior
commit's `envGet('models.<kind>.<logicalName>.backend')` doesn't
work — Harper's `getConfigValue` only reads keys in
`CONFIG_PARAM_MAP`, so dynamic config paths return undefined.
Adding entries per logical name is impossible (logical names are
user-defined). Refactored to direct `setEmbedding(name, backend)` /
`setGenerative(name, backend)`. Phase 2 will add a YAML→registry
bootstrapper alongside the first real backend, calling these
setters at boot. Error class collapsed to one
`ModelBackendNotFoundError`; message identifies kind + logical name
without enumerating other registrations. Models.ts is unaffected —
it only uses `resolveEmbedding` / `resolveGenerative`, whose
signatures didn't change.
Writer testability (resources/models/analyticsTable.ts): added
`getTable` to `ModelCallAnalyticsWriterOpts` so tests inject a
mock table. Default still resolves to the real
`getModelCallsTable()`. Also: `flush()` now awaits each
`primaryStore.put()` return value (when promise-shaped) before
resolving, so callers can read back inserted rows
deterministically.
Cycle workaround in Models.test.js: requires
`#src/resources/databases` before `#src/resources/transaction` to
mirror the load order in other unit tests; mocha's ESM-first
loader otherwise hits a transaction.ts ↔ DatabaseTransaction/blob
cycle when transaction is the first edge into the graph.
Coverage on `resources/models/`: 94% statements, 92.6% branches.
Models.ts and TestBackend.ts at 100%. analyticsTable.ts at 82%
with the lazy-getter for the real LMDB table the deliberate gap.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>1 parent 7fe0f23 commit d82b192
7 files changed
Lines changed: 716 additions & 48 deletions
File tree
- resources/models
- unitTests
- components
- resources/models
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
81 | 81 | | |
82 | 82 | | |
83 | 83 | | |
| 84 | + | |
| 85 | + | |
84 | 86 | | |
85 | 87 | | |
86 | 88 | | |
| |||
100 | 102 | | |
101 | 103 | | |
102 | 104 | | |
| 105 | + | |
103 | 106 | | |
104 | 107 | | |
105 | 108 | | |
106 | 109 | | |
107 | 110 | | |
108 | 111 | | |
109 | 112 | | |
| 113 | + | |
110 | 114 | | |
111 | 115 | | |
112 | 116 | | |
| |||
130 | 134 | | |
131 | 135 | | |
132 | 136 | | |
133 | | - | |
| 137 | + | |
| 138 | + | |
134 | 139 | | |
135 | 140 | | |
136 | | - | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
137 | 145 | | |
138 | 146 | | |
139 | 147 | | |
140 | 148 | | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
141 | 152 | | |
142 | 153 | | |
143 | 154 | | |
144 | 155 | | |
145 | | - | |
| 156 | + | |
146 | 157 | | |
147 | 158 | | |
148 | 159 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | 1 | | |
3 | 2 | | |
4 | 3 | | |
5 | 4 | | |
6 | 5 | | |
7 | 6 | | |
8 | | - | |
9 | | - | |
10 | | - | |
11 | | - | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
12 | 11 | | |
13 | 12 | | |
14 | | - | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
15 | 16 | | |
16 | 17 | | |
17 | 18 | | |
18 | 19 | | |
19 | | - | |
| 20 | + | |
| 21 | + | |
20 | 22 | | |
21 | | - | |
22 | | - | |
23 | | - | |
24 | | - | |
25 | | - | |
26 | | - | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
27 | 31 | | |
28 | 32 | | |
29 | 33 | | |
30 | | - | |
31 | | - | |
32 | | - | |
| 34 | + | |
| 35 | + | |
33 | 36 | | |
34 | 37 | | |
35 | | - | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
36 | 41 | | |
37 | 42 | | |
38 | 43 | | |
39 | | - | |
40 | | - | |
41 | | - | |
| 44 | + | |
| 45 | + | |
42 | 46 | | |
43 | 47 | | |
44 | | - | |
45 | | - | |
46 | | - | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
55 | | - | |
56 | | - | |
57 | | - | |
58 | | - | |
| 48 | + | |
| 49 | + | |
59 | 50 | | |
60 | 51 | | |
61 | 52 | | |
62 | | - | |
63 | | - | |
64 | | - | |
65 | | - | |
66 | | - | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
67 | 57 | | |
68 | 58 | | |
69 | | - | |
70 | | - | |
71 | | - | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
72 | 62 | | |
73 | | - | |
74 | | - | |
| 63 | + | |
| 64 | + | |
75 | 65 | | |
76 | 66 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
| 2 | + | |
2 | 3 | | |
3 | 4 | | |
4 | 5 | | |
| |||
59 | 60 | | |
60 | 61 | | |
61 | 62 | | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
62 | 71 | | |
63 | 72 | | |
64 | 73 | | |
| |||
0 commit comments