Repository navigation
Expand file tree
/
Copy pathbook.test.ts
More file actions
529 lines (487 loc) · 24.4 KB
/
Copy pathbook.test.ts
File metadata and controls
529 lines (487 loc) · 24.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
import { describe, it, expect, expectTypeOf } from 'vitest';
import type { z } from 'zod';
import {
BookSchema,
resolveBookTree,
deriveImplicitPackageBook,
isPublicAudience,
audienceAllows,
resolveBookClaimedDocs,
resolveDocAudiences,
docAudienceAllows,
ResolvedEntrySchema,
ResolvedGroupSchema,
ResolvedBookSchema,
type Book,
type ResolverDoc,
type ResolvedEntry,
type ResolvedGroup,
type ResolvedBook,
} from './book.zod';
import { DocSchema } from './doc.zod';
const docs = (names: string[]): ResolverDoc[] => names.map((name) => ({ name }));
describe('BookSchema (ADR-0046 §6)', () => {
it('accepts a minimal spine', () => {
expect(() =>
BookSchema.parse({ name: 'crm_guide', groups: [{ key: 'start', label: 'Start', include: 'crm_*' }] }),
).not.toThrow();
});
it('rejects a non snake_case name', () => {
expect(() => BookSchema.parse({ name: 'CrmGuide', groups: [] })).toThrow();
});
it('rejects a non snake_case group key', () => {
expect(() => BookSchema.parse({ name: 'crm_guide', groups: [{ key: 'Start', label: 'x' }] })).toThrow();
});
it('accepts audience variants', () => {
for (const audience of ['org', 'public', { permissionSet: 'crm_admin' }] as const) {
expect(() => BookSchema.parse({ name: 'b', audience, groups: [] })).not.toThrow();
}
});
it('rejects the removed { profile } audience shape (ADR-0090 D2)', () => {
expect(() => BookSchema.parse({ name: 'b', audience: { profile: 'admin' }, groups: [] })).toThrow();
});
});
describe('resolveBookTree — derived membership (the AI-safety core)', () => {
it('derives membership by glob include, leaving docs untouched', () => {
const book: Book = {
name: 'crm',
groups: [
{ key: 'guides', label: 'Guides', include: 'crm_guide_*' },
{ key: 'ref', label: 'Reference', include: 'crm_ref_*' },
],
};
const tree = resolveBookTree(book, docs(['crm_guide_lead', 'crm_guide_deal', 'crm_ref_api']));
expect(tree.groups.map((g) => g.key)).toEqual(['guides', 'ref']);
expect(tree.groups[0].entries.map((e) => e.doc)).toEqual(['crm_guide_deal', 'crm_guide_lead']); // alpha
expect(tree.groups[1].entries.map((e) => e.doc)).toEqual(['crm_ref_api']);
});
it('derives membership by TAG include — the variant DocSchema.tags finally makes reachable', () => {
// #4509: the tag branch of `matchesInclude` shipped long before anything
// could feed it. `DocSchema` is strict and had no `tags` key, so authoring
// tags was a parse error, every doc arrived with `tags === undefined`, and
// this variant could not match a single doc in any stack. Declaring
// `DocSchema.tags` was the whole fix — the matcher, the REST transport and
// `ResolverDoc.tags` were all already in place.
const book: Book = {
name: 'crm',
groups: [{ key: 'tut', label: 'Tutorials', include: { tag: 'tutorial' } }],
};
const tree = resolveBookTree(book, [
{ name: 'crm_guide_lead', tags: ['tutorial'] },
{ name: 'setup_sso', tags: ['tutorial', 'admin'] },
{ name: 'crm_ref_api', tags: ['reference'] },
{ name: 'crm_ref_bare' }, // no tags at all
]);
// Tags cut ACROSS the naming convention — that is the point of the variant:
// a glob could not have collected these two.
expect(tree.groups[0].entries.map((e) => e.doc)).toEqual(['crm_guide_lead', 'setup_sso']);
});
it('accepts `tags` on an authored doc (the strict schema used to reject it)', () => {
expect(() => DocSchema.parse({
name: 'crm_guide_lead',
content: '# Leads',
tags: ['tutorial', 'crm'],
})).not.toThrow();
});
it('a NEW doc matching a rule appears with zero edits to the book (create-and-forget)', () => {
const book: Book = { name: 'crm', groups: [{ key: 'guides', label: 'Guides', include: 'crm_guide_*' }] };
const before = resolveBookTree(book, docs(['crm_guide_lead']));
expect(before.groups[0].entries).toHaveLength(1);
// AI adds crm_guide_deal — same unchanged book spine:
const after = resolveBookTree(book, docs(['crm_guide_lead', 'crm_guide_deal']));
expect(after.groups[0].entries.map((e) => e.doc)).toEqual(['crm_guide_deal', 'crm_guide_lead']);
});
it('sorts within a group by doc.order then label', () => {
const book: Book = { name: 'crm', groups: [{ key: 'g', label: 'G', include: '*' }] };
const tree = resolveBookTree(book, [
{ name: 'b_doc', order: 2 },
{ name: 'a_doc', order: 1 },
{ name: 'z_doc' }, // order 0 → first
]);
expect(tree.groups[0].entries.map((e) => e.doc)).toEqual(['z_doc', 'a_doc', 'b_doc']);
});
it('honours explicit doc.group placement', () => {
const book: Book = {
name: 'crm',
groups: [
{ key: 'guides', label: 'Guides', include: 'never_*' },
{ key: 'admin', label: 'Admin' },
],
};
const tree = resolveBookTree(book, [{ name: 'setup', group: 'admin' }]);
expect(tree.groups.find((g) => g.key === 'admin')!.entries.map((e) => e.doc)).toEqual(['setup']);
});
it('routes unmatched docs to a synthetic Uncategorized group, never dropping them', () => {
const book: Book = { name: 'crm', groups: [{ key: 'guides', label: 'Guides', include: 'crm_guide_*' }] };
const tree = resolveBookTree(book, docs(['crm_guide_lead', 'crm_stray', 'crm_other']));
const unc = tree.groups.at(-1)!;
expect(unc.key).toBe('uncategorized');
expect(unc.entries.map((e) => e.doc)).toEqual(['crm_other', 'crm_stray']);
});
it('first group (by order) wins when two rules match the same doc — no duplicates', () => {
const book: Book = {
name: 'crm',
groups: [
{ key: 'second', label: 'Second', order: 2, include: 'crm_*' },
{ key: 'first', label: 'First', order: 1, include: 'crm_*' },
],
};
const tree = resolveBookTree(book, docs(['crm_x']));
expect(tree.groups.map((g) => g.key)).toEqual(['first', 'second']);
expect(tree.groups[0].entries.map((e) => e.doc)).toEqual(['crm_x']);
expect(tree.groups[1].entries).toHaveLength(0);
});
it('explicit pages override: verbatim order, separator, missing doc, and ... rest', () => {
const book: Book = {
name: 'crm',
groups: [
{
key: 'tut',
label: 'Tutorial',
include: 'crm_tut_*',
pages: ['crm_tut_intro', '---', 'crm_tut_missing', '...'],
},
],
};
const tree = resolveBookTree(book, docs(['crm_tut_intro', 'crm_tut_b', 'crm_tut_a']));
const e = tree.groups[0].entries;
expect(e[0]).toMatchObject({ doc: 'crm_tut_intro' });
expect(e[1]).toMatchObject({ separator: true });
expect(e[2]).toMatchObject({ doc: 'crm_tut_missing' }); // missing → renderer shows not-found
// '...' sweeps the rest of this group's matches, not the pinned intro:
expect(e.slice(3).map((x) => x.doc)).toEqual(['crm_tut_a', 'crm_tut_b']);
});
it('object node carries badge/icon and label override; href node is a link', () => {
const book: Book = {
name: 'crm',
groups: [{ key: 'g', label: 'G', pages: [{ doc: 'crm_api', badge: 'beta' }, { href: 'https://x', label: 'CHANGELOG' }] }],
};
const tree = resolveBookTree(book, [{ name: 'crm_api', label: 'API' }]);
expect(tree.groups[0].entries[0]).toMatchObject({ doc: 'crm_api', badge: 'beta', label: 'API' });
expect(tree.groups[0].entries[1]).toMatchObject({ href: 'https://x', label: 'CHANGELOG' });
});
it('include scoped by package ignores docs from other packages', () => {
const book: Book = { name: 'crm', groups: [{ key: 'g', label: 'G', include: '*', package: 'crm' }] };
const tree = resolveBookTree(book, [
{ name: 'a', packageId: 'crm' },
{ name: 'b', packageId: 'other' },
]);
expect(tree.groups[0].entries.map((e) => e.doc)).toEqual(['a']);
// 'b' is another package's doc: not this book's orphan either (#20980,
// ADR-0046 §6.4), so no Uncategorized group is appended for it.
expect(tree.groups.map((g) => g.key)).toEqual(['g']);
});
});
// [#20980] ADR-0046 §6.4: "any doc left unplaced rolls up under a synthetic
// *Uncategorized* group" — the unplaced docs are the PACKAGE's. The orphan
// pass used to sweep every doc handed in, so `GET /meta/book/:name/tree`
// (which resolves over an env-wide corpus) filed every other package's
// ungrouped docs under this book's Uncategorized group.
describe('resolveBookTree — the Uncategorized group holds only the book\'s own packages\' docs (§6.4)', () => {
const book: Book = { name: 'crm_guide', groups: [{ key: 'start', label: 'Start', include: 'crm_intro' }] };
const keysAndDocs = (tree: ResolvedBook) =>
Object.fromEntries(tree.groups.map((g) => [g.key, g.entries.map((e) => e.doc)]));
it('another package\'s ungrouped doc is NOT in the book\'s Uncategorized group; the book\'s own is', () => {
const tree = resolveBookTree(book, [
{ name: 'crm_intro', packageId: 'crm' },
{ name: 'crm_stray', packageId: 'crm' },
{ name: 'ops_keys', packageId: 'ops' },
], 'crm');
expect(keysAndDocs(tree)).toEqual({ start: ['crm_intro'], uncategorized: ['crm_stray'] });
});
it('control: a cross-package doc whose `group` names the book\'s group still joins it (placement is unscoped)', () => {
const tree = resolveBookTree(book, [
{ name: 'crm_intro', packageId: 'crm' },
{ name: 'ops_placed', packageId: 'ops', group: 'start' },
{ name: 'ops_keys', packageId: 'ops' },
], 'crm');
expect(keysAndDocs(tree)).toEqual({ start: ['crm_intro', 'ops_placed'] });
});
it('a group\'s `package` is one of the book\'s packages: that package\'s unmatched doc is an orphan here', () => {
const crossBook: Book = {
name: 'crm_guide',
groups: [{ key: 'ops', label: 'Operations', include: 'ops_runbook_*', package: 'ops' }],
};
const tree = resolveBookTree(crossBook, [
{ name: 'crm_stray', packageId: 'crm' },
{ name: 'ops_runbook_keys', packageId: 'ops' },
{ name: 'ops_other', packageId: 'ops' },
{ name: 'hr_policy', packageId: 'hr' },
], 'crm');
expect(keysAndDocs(tree)).toEqual({ ops: ['ops_runbook_keys'], uncategorized: ['crm_stray', 'ops_other'] });
});
it('the implicit package book (no `bookPackage`, the tree route\'s fallback) catches no other package\'s doc', () => {
const tree = resolveBookTree(deriveImplicitPackageBook('crm', 'CRM'), [
{ name: 'crm_intro', packageId: 'crm' },
{ name: 'ops_keys', packageId: 'ops' },
]);
expect(keysAndDocs(tree)).toEqual({ all: ['crm_intro'] });
});
it('no package declared anywhere: every unclaimed doc is an orphan, as before', () => {
const tree = resolveBookTree(book, [
{ name: 'crm_intro', packageId: 'crm' },
{ name: 'crm_stray', packageId: 'crm' },
{ name: 'ops_keys', packageId: 'ops' },
]);
expect(keysAndDocs(tree)).toEqual({ start: ['crm_intro'], uncategorized: ['crm_stray', 'ops_keys'] });
});
it('a doc with no stamped `packageId` is in every scope — the same reading `include` scoping uses', () => {
const tree = resolveBookTree(book, [{ name: 'crm_intro', packageId: 'crm' }, { name: 'loose_note' }], 'crm');
expect(keysAndDocs(tree)).toEqual({ start: ['crm_intro'], uncategorized: ['loose_note'] });
});
it('the claim set is unchanged: neither the own nor the foreign orphan is claimed', () => {
const corpus: ResolverDoc[] = [
{ name: 'crm_intro', packageId: 'crm' },
{ name: 'crm_stray', packageId: 'crm' },
{ name: 'ops_keys', packageId: 'ops' },
];
expect([...resolveBookClaimedDocs(book, corpus, 'crm')]).toEqual(['crm_intro']);
});
});
// [#27091] ADR-0046 §6.4 rolls up docs "left unplaced"; a doc a SIBLING book
// places is placed. Two books over one package (split by audience, §6.7) used
// to list each other's pages under *Uncategorized*, because "claimed" was
// judged against the book's own groups only.
describe('resolveBookTree — a doc a sibling book claims is not this book\'s orphan (§6.4)', () => {
const corpus: ResolverDoc[] = [
{ name: 'crm_overview', packageId: 'crm' },
{ name: 'crm_sales_leads', packageId: 'crm' },
{ name: 'crm_admin_access', packageId: 'crm' },
{ name: 'crm_admin_automation', packageId: 'crm' },
];
const withStray: ResolverDoc[] = [...corpus, { name: 'crm_stray', packageId: 'crm' }];
const userManual: Book & { packageId?: string } = {
name: 'crm_manual',
packageId: 'crm',
groups: [
{ key: 'start', label: 'Start', include: 'crm_overview' },
{ key: 'sales', label: 'Sales', include: 'crm_sales_*' },
],
};
const adminManual: Book & { packageId?: string } = {
name: 'crm_admin_manual',
packageId: 'crm',
audience: { permissionSet: 'system_admin' },
groups: [{ key: 'admin', label: 'Admin', include: 'crm_admin_*' }],
};
const books = [userManual, adminManual];
const keysAndDocs = (tree: ResolvedBook) =>
Object.fromEntries(tree.groups.map((g) => [g.key, g.entries.map((e) => e.doc)]));
const docsOf = (tree: ResolvedBook) => tree.groups.flatMap((g) => g.entries.map((e) => e.doc));
it('two books with disjoint includes over one package: neither tree has an Uncategorized group', () => {
expect(keysAndDocs(resolveBookTree(userManual, corpus, 'crm', books))).toEqual({
start: ['crm_overview'],
sales: ['crm_sales_leads'],
});
expect(keysAndDocs(resolveBookTree(adminManual, corpus, 'crm', books))).toEqual({
admin: ['crm_admin_access', 'crm_admin_automation'],
});
});
it('a doc NO book claims still rolls up under Uncategorized, in every book of its package', () => {
expect(keysAndDocs(resolveBookTree(userManual, withStray, 'crm', books)).uncategorized).toEqual(['crm_stray']);
expect(keysAndDocs(resolveBookTree(adminManual, withStray, 'crm', books)).uncategorized).toEqual(['crm_stray']);
});
it('an audience-gated sibling\'s docs never reach the other book\'s tree, and nothing appears anywhere new', () => {
const userDocs = docsOf(resolveBookTree(userManual, withStray, 'crm', books));
expect(userDocs).not.toContain('crm_admin_access');
expect(userDocs).not.toContain('crm_admin_automation');
// The catch-all only shrinks: every entry with siblings was an entry without them.
for (const book of books) {
const before = docsOf(resolveBookTree(book, withStray, 'crm'));
expect(before).toEqual(expect.arrayContaining(docsOf(resolveBookTree(book, withStray, 'crm', books))));
}
});
it('the §6.7 audience union is unmoved: the claim sets never read the catch-all', () => {
expect(Object.fromEntries(resolveDocAudiences(books, withStray))).toEqual({
crm_overview: ['org'],
crm_sales_leads: ['org'],
crm_admin_access: [{ permissionSet: 'system_admin' }],
crm_admin_automation: [{ permissionSet: 'system_admin' }],
crm_stray: ['org'],
});
for (const book of books) {
const authored = resolveBookTree(book, withStray, 'crm', books).groups.filter((g) => g.key !== 'uncategorized');
expect([...resolveBookClaimedDocs(book, withStray, 'crm')]).toEqual(authored.flatMap((g) => g.entries.map((e) => e.doc)));
}
});
it('a single-book package is byte-identical to the tree without `books`', () => {
const alone = resolveBookTree(userManual, withStray, 'crm');
expect(JSON.stringify(resolveBookTree(userManual, withStray, 'crm', [userManual]))).toBe(JSON.stringify(alone));
expect(keysAndDocs(alone).uncategorized).toEqual(['crm_admin_access', 'crm_admin_automation', 'crm_stray']);
});
});
describe('deriveImplicitPackageBook + audience', () => {
it('synthesizes a one-group book including every doc of the package', () => {
const book = deriveImplicitPackageBook('app_todo', 'Todo');
const tree = resolveBookTree(book, docs(['todo_index', 'todo_guide']));
expect(tree.groups).toHaveLength(1);
expect(tree.groups[0].entries.map((e) => e.doc)).toEqual(['todo_guide', 'todo_index']);
});
it('isPublicAudience only true for public', () => {
expect(isPublicAudience('public')).toBe(true);
expect(isPublicAudience('org')).toBe(false);
expect(isPublicAudience({ permissionSet: 'crm_admin' })).toBe(false);
expect(isPublicAudience(undefined)).toBe(false);
});
});
describe('audienceAllows (ADR-0046 §6.7, ADR-0090 vocabulary)', () => {
const anon = { authenticated: false };
const member = { authenticated: true, permissionSets: ['member_default'] };
const admin = { authenticated: true, permissionSets: ['member_default', 'crm_admin'] };
const unresolved = { authenticated: true }; // security resolution unavailable
it('public allows everyone, including anonymous', () => {
for (const caller of [anon, member, admin, unresolved]) {
expect(audienceAllows('public', caller)).toBe(true);
}
});
it('org (and unset) allows any authenticated principal, never anonymous', () => {
for (const audience of ['org', undefined] as const) {
expect(audienceAllows(audience, anon)).toBe(false);
expect(audienceAllows(audience, member)).toBe(true);
expect(audienceAllows(audience, unresolved)).toBe(true);
}
});
it('permission-set gate requires holding the named set', () => {
const gated = { permissionSet: 'crm_admin' };
expect(audienceAllows(gated, admin)).toBe(true);
expect(audienceAllows(gated, member)).toBe(false);
expect(audienceAllows(gated, anon)).toBe(false);
});
it('permission-set gate FAILS CLOSED when holdings are unknown (ADR-0049)', () => {
expect(audienceAllows({ permissionSet: 'crm_admin' }, unresolved)).toBe(false);
});
});
describe('resolveDocAudiences — union over claiming books (§6.7)', () => {
const corpus: ResolverDoc[] = [
{ name: 'crm_guide_intro', packageId: 'crm' },
{ name: 'crm_guide_flows', packageId: 'crm' },
{ name: 'crm_admin_setup', packageId: 'crm' },
{ name: 'crm_unclaimed', packageId: 'crm' },
];
const publicBook: Book & { packageId?: string } = {
name: 'crm_guide',
audience: 'public',
groups: [{ key: 'g', label: 'Guide', include: 'crm_guide_*' }],
packageId: 'crm',
};
const adminBook: Book & { packageId?: string } = {
name: 'crm_admin_guide',
audience: { permissionSet: 'crm_admin' },
groups: [{ key: 'a', label: 'Admin', include: 'crm_admin_*' }],
packageId: 'crm',
};
it('claimed docs union the audiences of every claiming book', () => {
const audiences = resolveDocAudiences([publicBook, adminBook], corpus);
expect(audiences.get('crm_guide_intro')).toEqual(['public']);
expect(audiences.get('crm_admin_setup')).toEqual([{ permissionSet: 'crm_admin' }]);
});
it('a doc claimed by no book defaults to org — orphans do NOT ride along with public books', () => {
const audiences = resolveDocAudiences([publicBook, adminBook], corpus);
expect(audiences.get('crm_unclaimed')).toEqual(['org']);
// The tree view still renders the orphan (nothing is dropped)…
const tree = resolveBookTree(publicBook, corpus, 'crm');
expect(tree.groups.at(-1)!.entries.some((e) => e.doc === 'crm_unclaimed')).toBe(true);
// …but the CLAIM set excludes it, so it never inherits `public`.
expect(resolveBookClaimedDocs(publicBook, corpus, 'crm').has('crm_unclaimed')).toBe(false);
});
it('docAudienceAllows applies the union: any allowing book grants access', () => {
const both: Book & { packageId?: string } = {
...adminBook,
name: 'crm_all',
audience: 'org',
groups: [{ key: 'all', label: 'All', include: 'crm_admin_*' }],
};
const audiences = resolveDocAudiences([adminBook, both], corpus);
const member = { authenticated: true, permissionSets: ['member_default'] };
// Reachable via the org book even without crm_admin.
expect(docAudienceAllows(audiences.get('crm_admin_setup'), member)).toBe(true);
// Gated-only doc stays gated.
const gatedOnly = resolveDocAudiences([adminBook], corpus);
expect(docAudienceAllows(gatedOnly.get('crm_admin_setup'), member)).toBe(false);
expect(docAudienceAllows(gatedOnly.get('crm_admin_setup'), { authenticated: false })).toBe(false);
});
it('an unknown doc (absent from the corpus map) gets the org default', () => {
expect(docAudienceAllows(undefined, { authenticated: true })).toBe(true);
expect(docAudienceAllows(undefined, { authenticated: false })).toBe(false);
});
});
// ── Inline translation maps retired in 17.0.0 (#4667, ADR-0049) ─────────────
describe('retired book translation maps', () => {
it('rejects a book-level `translations` map and names the live neighbour', () => {
// The prescription has to mention `doc.translations`: that key is live on
// every doc render path and is what the author actually wanted. Without it
// the rejection reads as "localization is unsupported", which is false.
const parse = () => BookSchema.parse({
name: 'crm_guide', label: 'CRM Guide',
translations: { 'zh-CN': { label: 'CRM 指南' } },
groups: [{ key: 'basics', label: 'Basics' }],
});
expect(parse).toThrow(/translations.*removed.*17\.0\.0/s);
expect(parse).toThrow(/doc\.translations/s);
});
it('rejects the `i18n` alias with the same prescription, not a rename', () => {
// `i18n` used to alias `translations`. An alias surviving its target would
// answer "did you mean `translations`?" — a rename onto a key that is gone.
expect(() => BookSchema.parse({
name: 'crm_guide', label: 'CRM Guide',
i18n: { 'zh-CN': { label: 'CRM 指南' } },
groups: [{ key: 'basics', label: 'Basics' }],
})).toThrow(/translations.*removed/s);
});
it('rejects a GROUP-level `translations` map — the tombstone, not a silent strip', () => {
// BookGroupSchema is a plain z.object with no .strict(), so this key is
// TOMBSTONED rather than deleted. Pinning the throw is what proves the
// tombstone is still there: a plain delete here would make zod strip the
// key silently and this test would fail with "expected to throw".
expect(() => BookSchema.parse({
name: 'crm_guide', label: 'CRM Guide',
groups: [{ key: 'basics', label: 'Basics', translations: { 'zh-CN': { label: '基础' } } }],
})).toThrow(/translations.*removed.*17\.0\.0/s);
});
it('still accepts a book with no translation map', () => {
expect(() => BookSchema.parse({
name: 'crm_guide', label: 'CRM Guide',
groups: [{ key: 'basics', label: 'Basics', include: 'crm_*' }],
})).not.toThrow();
});
});
// ==========================================
// book-tree response contract (#12038)
// ==========================================
describe('ResolvedBookSchema is the book-tree response contract', () => {
// The conformance suite for the `GET /meta/book/:name/tree` ledger rows
// (#3877's no-row-without-conformance rule). Stronger than the handwritten
// captures its meta.* siblings use: `resolveBookTree()` is pure and lives in
// this file's module, so the suite drives the REAL producer and parses what
// it actually returns.
const spine: Book = {
name: 'crm_guide',
label: 'CRM Guide',
groups: [
{ key: 'basics', label: 'Basics', include: 'crm_*' },
{ key: 'links', label: 'Links', pages: ['---', { href: 'https://example.com', label: 'Site', badge: 'new' }] },
],
};
const resolverDocs: ResolverDoc[] = [
{ name: 'crm_intro', label: 'Intro', description: 'Start here', order: 1 },
{ name: 'crm_setup', label: 'Setup', order: 2 },
];
it('parses the real resolver output and PRESERVES it', () => {
const tree = resolveBookTree(BookSchema.parse(spine) as Book, resolverDocs);
const result = ResolvedBookSchema.safeParse(tree);
expect(result.success).toBe(true);
if (result.success) expect(result.data).toEqual(JSON.parse(JSON.stringify(tree)));
});
it('the honest-empty tree parses — a book whose rules match nothing is a declared, legal body', () => {
const empty = { name: 'crm_guide', groups: [] };
expect(ResolvedBookSchema.safeParse(empty).success).toBe(true);
});
it('each schema stays type-identical to the interface the resolver is typed by', () => {
// The interfaces remain the compile-time source `resolveBookTree` is typed
// by; these pins are what keeps the Zod transcription from drifting.
expectTypeOf<z.infer<typeof ResolvedEntrySchema>>().toEqualTypeOf<ResolvedEntry>();
expectTypeOf<z.infer<typeof ResolvedGroupSchema>>().toEqualTypeOf<ResolvedGroup>();
expectTypeOf<z.infer<typeof ResolvedBookSchema>>().toEqualTypeOf<ResolvedBook>();
});
});