Repository navigation
Expand file tree
/
Copy pathvalidate-knowledge-source.test.ts
More file actions
366 lines (328 loc) · 21.3 KB
/
Copy pathvalidate-knowledge-source.test.ts
File metadata and controls
366 lines (328 loc) · 21.3 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
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
import { describe, expect, it } from 'vitest';
import {
KNOWLEDGE_SOURCE_FIELD_NEVER_INDEXED,
KNOWLEDGE_SOURCE_FIELD_UNKNOWN,
KNOWLEDGE_SOURCE_OBJECT_NOT_SEARCHABLE,
KNOWLEDGE_SOURCE_OBJECT_UNKNOWN,
KNOWLEDGE_SOURCE_SYSTEM_OBJECT_NOT_AI_READABLE,
validateKnowledgeSource,
} from './validate-knowledge-source.js';
import { explainRule } from './rule-explanations.js';
import { runAuthoringRules } from './authoring-rules.js';
import { runRuntimeAuthoringRules, runtimeAuthoringRulesFor, stackKeyForType } from './runtime-gate.js';
const objects = [
{
name: 'kb_article',
fields: {
title: { type: 'text' },
body: { type: 'markdown' },
status: { type: 'select' },
api_token: { type: 'secret' },
triage_note: { type: 'text', internal: true },
},
},
{ name: 'private_note', enable: { apiEnabled: false }, fields: { body: { type: 'text' } } },
{ name: 'get_only', enable: { apiMethods: ['get'] }, fields: { body: { type: 'text' } } },
{ name: 'unsearchable', enable: { apiMethods: ['get', 'list'], searchable: false }, fields: { body: { type: 'text' } } },
{ name: 'sys_audit_note', fields: { body: { type: 'text' } } },
{ name: 'sys_help_topic', enable: { aiReadable: true }, fields: { body: { type: 'text' } } },
];
/** A source over `kb_article` that names only what the object defines; `source` overrides the source keys. */
const kbSource = (source: Record<string, unknown> = {}, name = 'support_kb') => ({
name,
label: 'Support KB',
source: { kind: 'object', object: 'kb_article', content: ['title', 'body'], metadataFields: ['status'], ...source },
});
const KS_RULE = /^knowledge-source-/;
/** `os validate`'s verdict on one source: the CLI command's own registry entry over the stack. */
const atValidate = (source: Record<string, unknown>, extra: Record<string, unknown> = {}) => {
const stack = { objects, knowledgeSources: [source], ...extra };
return runAuthoringRules('validate', { parsed: stack, normalized: stack }).filter((f) => KS_RULE.test(f.rule));
};
/** The runtime publish door's verdict on one source written against `objects` as the live context. */
const atPublish = (source: Record<string, unknown>) =>
runRuntimeAuthoringRules({ type: 'knowledge_source', item: source, context: { objects } });
const face = (f: { rule: string; severity: string; where: string; message: string; hint?: string }) =>
({ rule: f.rule, severity: f.severity, where: f.where, message: f.message, hint: f.hint });
/**
* The card's acceptance families: each prints exactly one finding, with the
* rule id, the path, and the object or field named in its message.
*/
const FAMILIES: ReadonlyArray<[string, Record<string, unknown>, string, 'error' | 'warning', string, string]> = [
['a missing object', kbSource({ object: 'kb_artcle' }), KNOWLEDGE_SOURCE_OBJECT_UNKNOWN, 'error', 'knowledgeSources[0].source.object', "'kb_artcle'"],
['a missing content field', kbSource({ content: ['title', 'bdy'] }), KNOWLEDGE_SOURCE_FIELD_UNKNOWN, 'error', 'knowledgeSources[0].source.content[1]', "'bdy'"],
['a missing metadata field', kbSource({ metadataFields: ['stage'] }), KNOWLEDGE_SOURCE_FIELD_UNKNOWN, 'error', 'knowledgeSources[0].source.metadataFields[0]', "'stage'"],
['an object with apiEnabled: false', kbSource({ object: 'private_note', content: ['body'], metadataFields: [] }), KNOWLEDGE_SOURCE_OBJECT_NOT_SEARCHABLE, 'error', 'knowledgeSources[0].source.object', "'private_note'"],
['an object whose apiMethods do not grant search', kbSource({ object: 'get_only', content: ['body'], metadataFields: [] }), KNOWLEDGE_SOURCE_OBJECT_NOT_SEARCHABLE, 'error', 'knowledgeSources[0].source.object', "'get_only'"],
['an object that lists but declares searchable: false', kbSource({ object: 'unsearchable', content: ['body'], metadataFields: [] }), KNOWLEDGE_SOURCE_OBJECT_NOT_SEARCHABLE, 'error', 'knowledgeSources[0].source.object', "'unsearchable'"],
['a sys_ object without enable.aiReadable', kbSource({ object: 'sys_audit_note', content: ['body'], metadataFields: [] }), KNOWLEDGE_SOURCE_SYSTEM_OBJECT_NOT_AI_READABLE, 'error', 'knowledgeSources[0].source.object', "'sys_audit_note'"],
['a credential-class content field', kbSource({ content: ['title', 'api_token'] }), KNOWLEDGE_SOURCE_FIELD_NEVER_INDEXED, 'warning', 'knowledgeSources[0].source.content[1]', "'api_token'"],
['an internal metadata field', kbSource({ metadataFields: ['triage_note'] }), KNOWLEDGE_SOURCE_FIELD_NEVER_INDEXED, 'warning', 'knowledgeSources[0].source.metadataFields[0]', "'triage_note'"],
];
describe('validateKnowledgeSource in os validate (ADR-0161 K4, row 4)', () => {
for (const [what, source, rule, severity, path, named] of FAMILIES) {
it(`${what} prints one ${severity} ${rule}, naming ${named}`, () => {
const findings = atValidate(source);
expect(findings.map((f) => [f.rule, f.severity, f.path])).toEqual([[rule, severity, path]]);
expect(findings[0]!.message).toContain(named);
expect(findings[0]!.where).toBe('knowledge source "support_kb"');
});
}
it('a clean source prints nothing', () => {
expect(atValidate(kbSource())).toEqual([]);
});
it('an apiMethods list that grants list grants search, as the exposure decision derives it', () => {
const withList = [...objects, { name: 'listed', enable: { apiMethods: ['get', 'list'] }, fields: { body: { type: 'text' } } }];
const stack = { objects: withList, knowledgeSources: [kbSource({ object: 'listed', content: ['body'], metadataFields: [] })] };
expect(validateKnowledgeSource(stack)).toEqual([]);
});
it('a sys_ object that declares enable.aiReadable: true passes the system-object gate', () => {
expect(atValidate(kbSource({ object: 'sys_help_topic', content: ['body'], metadataFields: [] }))).toEqual([]);
});
it('a system column the platform injects is a field the source may name', () => {
expect(atValidate(kbSource({ metadataFields: ['owner_id', 'created_at'] }))).toEqual([]);
});
it('`id`, which the driver provisions without a declaration, resolves like a declared field', () => {
expect(atValidate(kbSource({ metadataFields: ['id', 'status'] }))).toEqual([]);
expect(atValidate(kbSource({ content: ['id', 'title'] }))).toEqual([]);
expect(atPublish(kbSource({ metadataFields: ['id'] })).errors).toEqual([]);
});
it('`enable.searchable: false` with no `apiMethods` list does not refuse search', () => {
const open = { name: 'open_note', enable: { searchable: false }, fields: { body: { type: 'text' } } };
const source = kbSource({ object: 'open_note', content: ['body'], metadataFields: [] });
expect(validateKnowledgeSource({ objects: [open], knowledgeSources: [source] })).toEqual([]);
});
it('a field an object extension of the stack adds resolves', () => {
const extension = { extend: 'kb_article', fields: { product: { type: 'text' } } };
expect(atValidate(kbSource({ metadataFields: ['product'] }), { objectExtensions: [extension] })).toEqual([]);
expect(atValidate(kbSource({ metadataFields: ['product'] })).map((f) => f.rule)).toEqual([KNOWLEDGE_SOURCE_FIELD_UNKNOWN]);
});
it('an object a package of the same artifact ships resolves, and is judged', () => {
const packages = [{ manifest: { id: 'com.acme.kb', objects: [{ name: 'faq_entry', fields: { answer: { type: 'text' } } }] } }];
expect(atValidate(kbSource({ object: 'faq_entry', content: ['answer'], metadataFields: [] }), { packages })).toEqual([]);
expect(atValidate(kbSource({ object: 'faq_entry', content: ['answr'], metadataFields: [] }), { packages }).map((f) => f.rule))
.toEqual([KNOWLEDGE_SOURCE_FIELD_UNKNOWN]);
});
it('a platform object a known package registers is not judged when the stack does not carry it', () => {
expect(atValidate(kbSource({ object: 'sys_user', content: ['name'], metadataFields: [] }))).toEqual([]);
});
it('a platform-prefixed name no known package registers is a warning, not an error', () => {
const findings = atValidate(kbSource({ object: 'sys_kb_article', content: ['body'], metadataFields: [] }));
expect(findings.map((f) => [f.rule, f.severity])).toEqual([[KNOWLEDGE_SOURCE_OBJECT_UNKNOWN, 'warning']]);
});
it('on the CLI, a missing object is a warning while the manifest declares a dependency the artifact does not carry', () => {
const manifest = { id: 'app.kb', dependencies: { 'com.acme.faq': '^1.0.0' } };
const source = kbSource({ object: 'faq_entry', content: ['answer'], metadataFields: [] });
const findings = atValidate(source, { manifest });
expect(findings.map((f) => [f.rule, f.severity, f.path])).toEqual([
[KNOWLEDGE_SOURCE_OBJECT_UNKNOWN, 'warning', 'knowledgeSources[0].source.object'],
]);
expect(findings[0]!.message).toContain('"com.acme.faq"');
expect(findings[0]!.hint).toContain('Ignore this if a declared dependency defines the object');
});
it('on the CLI, a missing object stays an error when every declared dependency is carried, or none is declared', () => {
const carried = [{ manifest: { id: 'com.acme.faq', objects: [{ name: 'faq_topic', fields: { title: { type: 'text' } } }] } }];
const manifest = { id: 'app.kb', dependencies: { 'com.acme.faq': '^1.0.0' } };
const source = kbSource({ object: 'faq_entry', content: ['answer'], metadataFields: [] });
expect(atValidate(source, { manifest, packages: carried }).map((f) => [f.rule, f.severity]))
.toEqual([[KNOWLEDGE_SOURCE_OBJECT_UNKNOWN, 'error']]);
expect(atValidate(source, { manifest: { id: 'app.kb' } }).map((f) => [f.rule, f.severity]))
.toEqual([[KNOWLEDGE_SOURCE_OBJECT_UNKNOWN, 'error']]);
});
it('a missing object is reported once, and its fields are not judged', () => {
const findings = atValidate(kbSource({ object: 'nothing_here', content: ['a', 'b'], metadataFields: ['c'] }));
expect(findings.map((f) => f.rule)).toEqual([KNOWLEDGE_SOURCE_OBJECT_UNKNOWN]);
});
it('the error refuses os validate, not only os lint', () => {
for (const command of ['validate', 'build', 'lint'] as const) {
const stack = { objects, knowledgeSources: [kbSource({ object: 'kb_artcle' })] };
const errors = runAuthoringRules(command, { parsed: stack, normalized: stack })
.filter((f) => f.rule === KNOWLEDGE_SOURCE_OBJECT_UNKNOWN && f.severity === 'error');
expect(errors, command).toHaveLength(1);
}
});
});
/**
* ADR-0161 K4: "a packaged item meets the gates that need no principal in
* `os validate` and at publish". The runtime publish door answers the rule
* ids and messages `os validate` prints for the same source; its errors
* refuse the publish, unlike the `ai_context` row's advisories.
*/
describe('validateKnowledgeSource on the runtime publish door', () => {
it('the door dispatches a knowledge_source write to the rule, into the knowledgeSources collection', () => {
expect(runtimeAuthoringRulesFor('knowledge_source').map((r) => r.name)).toContain('validateKnowledgeSource');
expect(stackKeyForType('knowledge_source')).toBe('knowledgeSources');
});
for (const [what, source, rule, severity] of FAMILIES) {
it(`${what} is reported at publish as ${rule}, in the words os validate prints${severity === 'error' ? ', and refuses the publish' : ''}`, () => {
const verdict = atPublish(source);
expect(verdict.rulesRun).toContain('validateKnowledgeSource');
const refused = verdict.errors.filter((f) => KS_RULE.test(f.rule));
const advised = verdict.advisories.filter((f) => KS_RULE.test(f.rule));
expect((severity === 'error' ? refused : advised).map((f) => f.rule)).toEqual([rule]);
expect((severity === 'error' ? advised : refused)).toEqual([]);
// A write that names no package is worded for its author, who has no
// stack and no manifest; every other verdict prints the CLI's words.
const sameWords = rule !== KNOWLEDGE_SOURCE_OBJECT_UNKNOWN;
const at = (f: ReturnType<typeof face>) => (sameWords ? f : { rule: f.rule, severity: f.severity, where: f.where });
expect([...refused, ...advised].map(face).map(at)).toEqual(atValidate(source).map(face).map(at));
});
}
it('a clean source is published with nothing to report', () => {
const verdict = atPublish(kbSource());
expect(verdict.rulesRun).toContain('validateKnowledgeSource');
expect(verdict.errors).toEqual([]);
expect(verdict.advisories).toEqual([]);
});
it('under a stated package scope, an object outside the closure is refused with the closure remedy', () => {
const scoped = objects.map((o) => ({ ...o, _packageId: o.name === 'kb_article' ? 'com.other' : 'app.kb' }));
const verdict = runRuntimeAuthoringRules({
type: 'knowledge_source',
item: kbSource(),
context: { objects: scoped },
packageScope: { packageId: 'app.kb', dependencies: [] },
});
expect(verdict.errors.map((f) => [f.rule, f.path])).toEqual([[KNOWLEDGE_SOURCE_OBJECT_UNKNOWN, expect.stringContaining('source.object')]]);
const [finding] = verdict.errors;
expect(finding!.message).toContain('outside the closure of package "app.kb"');
expect(finding!.hint).toBe(
'Declare a dependency on the package that defines "kb_article" in the `dependencies` of "app.kb"\'s manifest, '
+ 'or name an object inside that closure',
);
// Non-vacuous: declaring the dependency brings the object into the closure, and the source is clean.
expect(runRuntimeAuthoringRules({
type: 'knowledge_source',
item: kbSource(),
context: { objects: scoped },
packageScope: { packageId: 'app.kb', dependencies: ['com.other'] },
}).errors).toEqual([]);
});
it('with no scope the object verdict keeps the words os validate prints', () => {
const [finding] = validateKnowledgeSource({ objects, knowledgeSources: [kbSource({ object: 'kb_artcle' })] });
expect(finding!.message).toContain("`source.object` names 'kb_artcle', which this stack does not define");
expect(finding!.hint).toBe(
'Name an object the stack defines (check the spelling) or a platform object by its full name; for an object '
+ 'another package defines, declare that package in the manifest\'s `dependencies` and install it',
);
});
it('against the whole collection the host serves, a missing object is an error even beside an unseen declared dependency', () => {
const stack = {
objects,
manifest: { id: 'app.kb', dependencies: { 'com.acme.faq': '^1.0.0' } },
knowledgeSources: [kbSource({ object: 'faq_entry', content: ['answer'], metadataFields: [] })],
};
expect(validateKnowledgeSource(stack, { wholeCollection: true }).map((f) => [f.rule, f.severity]))
.toEqual([[KNOWLEDGE_SOURCE_OBJECT_UNKNOWN, 'error']]);
// Non-vacuous: the same stack on the CLI is the hedged warning.
expect(validateKnowledgeSource(stack).map((f) => [f.rule, f.severity]))
.toEqual([[KNOWLEDGE_SOURCE_OBJECT_UNKNOWN, 'warning']]);
});
it('the door passes the whole-collection reading when no package scope is stated', () => {
const item = kbSource({ object: 'faq_entry', content: ['answer'], metadataFields: [] });
// An empty served collection is no universe at the door: no object verdict is drawn from it.
expect(runRuntimeAuthoringRules({ type: 'knowledge_source', item, context: { objects: [] } }).errors).toEqual([]);
// Non-vacuous: a served collection that lacks the object refuses it.
expect(runRuntimeAuthoringRules({ type: 'knowledge_source', item, context: { objects } }).errors
.map((f) => [f.rule, f.severity])).toEqual([[KNOWLEDGE_SOURCE_OBJECT_UNKNOWN, 'error']]);
});
it('against the whole collection, an absent platform name, prefixed or known, is refused as an unprefixed one is', () => {
for (const object of ['sys_kb_article', 'sys_user']) {
const item = kbSource({ object, content: ['body'], metadataFields: [] });
const verdict = runRuntimeAuthoringRules({ type: 'knowledge_source', item, context: { objects } });
expect(verdict.errors.map((f) => [f.rule, f.severity, f.path]), object)
.toEqual([[KNOWLEDGE_SOURCE_OBJECT_UNKNOWN, 'error', 'knowledgeSources[0].source.object']]);
expect(verdict.errors[0]!.message, object).toContain(`'${object}', which is not an object of this organization`);
expect(verdict.advisories.filter((f) => KS_RULE.test(f.rule)), object).toEqual([]);
}
// The CLI arms stay: a prefixed name no known package registers warns, a known platform name is not judged.
expect(atValidate(kbSource({ object: 'sys_kb_article', content: ['body'], metadataFields: [] })).map((f) => f.severity))
.toEqual(['warning']);
expect(atValidate(kbSource({ object: 'sys_user', content: ['name'], metadataFields: [] }))).toEqual([]);
// CONTROL: a served platform object is no unknown object at the door.
const served = [...objects, { name: 'sys_kb_article', enable: { aiReadable: true }, fields: { body: { type: 'text' } } }];
expect(runRuntimeAuthoringRules({
type: 'knowledge_source',
item: kbSource({ object: 'sys_kb_article', content: ['body'], metadataFields: [] }),
context: { objects: served },
}).errors).toEqual([]);
});
it('a write that names no package is refused in words that name neither a stack nor a manifest', () => {
const verdict = runRuntimeAuthoringRules({ type: 'knowledge_source', item: kbSource({ object: 'kb_artcle' }), context: { objects } });
expect(verdict.errors.map((f) => [f.rule, f.severity])).toEqual([[KNOWLEDGE_SOURCE_OBJECT_UNKNOWN, 'error']]);
const [finding] = verdict.errors;
for (const word of [/\bstack\b/i, /manifest/i, /dependencies/]) {
expect(finding!.message).not.toMatch(word);
expect(finding!.hint).not.toMatch(word);
}
expect(finding!.message).toContain('organization');
expect(finding!.hint).toMatch(/install the package/);
// CONTROL: the CLI keeps its stack-and-manifest remedy, which its author can follow.
expect(atValidate(kbSource({ object: 'kb_artcle' }))[0]!.hint).toMatch(/manifest's `dependencies`/);
});
/**
* Limb 1 of the closure (`narrowObjectsToPackageClosure`) keeps every object
* `isSystemObject` accepts, and by name alone that is every `sys_` object:
* under a stated closure an absent `sys_` name is absent from the host.
*/
describe('under a stated closure, an absent sys_ name', () => {
const scope = { packageId: 'app.kb', dependencies: [] as string[] };
const served = [
...objects.map((o) => ({ ...o, _packageId: 'app.kb' })),
{ name: 'sys_help_topic', _packageId: 'com.platform.help', enable: { aiReadable: true }, fields: { body: { type: 'text' } } },
];
const atScoped = (object: string, context = served) => runRuntimeAuthoringRules({
type: 'knowledge_source',
item: kbSource({ object, content: ['body'], metadataFields: [] }),
context: { objects: context },
packageScope: scope,
});
it('is refused, known or not, with no dependency remedy', () => {
for (const object of ['sys_user', 'sys_kb_article']) {
const verdict = atScoped(object);
expect(verdict.errors.map((f) => [f.rule, f.severity, f.path]), object)
.toEqual([[KNOWLEDGE_SOURCE_OBJECT_UNKNOWN, 'error', 'knowledgeSources[0].source.object']]);
expect(verdict.errors[0]!.message, object).toContain(`'${object}', a system object the host does not serve`);
expect(verdict.errors[0]!.hint, object).not.toMatch(/dependenc/);
expect(verdict.advisories.filter((f) => KS_RULE.test(f.rule)), object).toEqual([]);
}
});
it('CONTROL: a sys_ object the host serves, from a package outside the closure, passes', () => {
expect(atScoped('sys_help_topic').errors).toEqual([]);
});
it('cloud_ and ai_ names are not on that arm: limb 1 keeps them only on their definition\'s isSystem', () => {
const foreign = (extra: Record<string, unknown>) => [
...served,
...['cloud_usage_note', 'ai_memory_note'].map((name) => ({ name, _packageId: 'com.cloud', ...extra, fields: { body: { type: 'text' } } })),
];
for (const object of ['cloud_usage_note', 'ai_memory_note']) {
// Not isSystem: the closure drops it, so the verdict is the closure's, with the dependency remedy.
const dropped = atScoped(object, foreign({}));
expect(dropped.errors.map((f) => f.rule), object).toEqual([KNOWLEDGE_SOURCE_OBJECT_UNKNOWN]);
expect(dropped.errors[0]!.message, object).toContain('outside the closure of package "app.kb"');
expect(dropped.errors[0]!.hint, object).toMatch(/^Declare a dependency on the package that defines/);
// isSystem: true: limb 1 keeps it, and the source is clean.
expect(atScoped(object, foreign({ isSystem: true })).errors, object).toEqual([]);
}
});
});
it('an empty whole collection at the door is no universe, and judges no object', () => {
const stack = { objects: [], knowledgeSources: [kbSource({ object: 'faq_entry' })] };
expect(validateKnowledgeSource(stack, { wholeCollection: true })).toEqual([]);
});
});
describe('os explain for the knowledge source rules', () => {
for (const rule of [
KNOWLEDGE_SOURCE_OBJECT_UNKNOWN,
KNOWLEDGE_SOURCE_FIELD_UNKNOWN,
KNOWLEDGE_SOURCE_OBJECT_NOT_SEARCHABLE,
KNOWLEDGE_SOURCE_SYSTEM_OBJECT_NOT_AI_READABLE,
KNOWLEDGE_SOURCE_FIELD_NEVER_INDEXED,
]) {
it(`answers ${rule}`, () => {
const entry = explainRule(rule);
expect(entry?.rule).toBe(rule);
expect(entry?.paragraphs.length).toBeGreaterThan(0);
});
}
});