Repository navigation
Expand file tree
/
Copy pathvalidate-knowledge-source.affinity.test.ts
More file actions
230 lines (207 loc) · 9.33 KB
/
Copy pathvalidate-knowledge-source.affinity.test.ts
File metadata and controls
230 lines (207 loc) · 9.33 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
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
// [ADR-0161, the dated note of 2026-11-29 under K1] Each `source.affinity`
// entry compares its `field` with a fact about the caller, so the field must
// point at the object that fact names: `sys_user` for `caller: 'user'`,
// `sys_business_unit` for `caller: 'business_unit'`. The knowledge service's
// save door refuses any other field for a source written through the metadata
// API; this file pins the same verdict for `os validate` and the runtime
// publish gate, which judge the sources a package ships or code registers.
import { describe, expect, it } from 'vitest';
import {
KNOWLEDGE_SOURCE_AFFINITY_FIELD_NOT_CALLER_TARGET,
KNOWLEDGE_SOURCE_FIELD_UNKNOWN,
validateKnowledgeSource,
} from './validate-knowledge-source.js';
import { KNOWLEDGE_SOURCE_AFFINITY_FIELD_NOT_CALLER_TARGET as FROM_BARREL } from './index.js';
import { explainRule } from './rule-explanations.js';
import { runAuthoringRules } from './authoring-rules.js';
import { runRuntimeAuthoringRules } from './runtime-gate.js';
const objects = [
{ name: 'account', fields: { name: { type: 'text' } } },
{
name: 'policy',
fields: {
title: { type: 'text' },
body: { type: 'markdown' },
region: { type: 'text' },
owner: { type: 'user' },
reviewer: { type: 'lookup', reference: 'sys_user' },
business_unit: { type: 'lookup', reference: 'sys_business_unit' },
account: { type: 'lookup', reference: 'account' },
dangling: { type: 'lookup' },
},
},
];
type Entry = { field: string; caller: string; mode: string; places?: number };
/** A source over `policy` whose `affinity` is `entries`; `metadataFields` lists every entry field unless given. */
const policySource = (entries: Entry[] | undefined, metadataFields?: string[]) => ({
name: 'policy_kb',
label: 'Policy KB',
source: {
kind: 'object',
object: 'policy',
content: ['title', 'body'],
metadataFields: metadataFields ?? [...new Set((entries ?? []).map((e) => e.field))],
...(entries === undefined ? {} : { affinity: entries }),
},
});
const RULE = KNOWLEDGE_SOURCE_AFFINITY_FIELD_NOT_CALLER_TARGET;
/** `os validate`'s verdict: the CLI command's own registry entry over the stack. */
const atValidate = (source: Record<string, unknown>) => {
const stack = { objects, knowledgeSources: [source] };
return runAuthoringRules('validate', { parsed: stack, normalized: stack }).filter((f) =>
f.rule.startsWith('knowledge-source-'),
);
};
/** The runtime publish door's verdict on one source written against `objects`. */
const atPublish = (source: Record<string, unknown>) =>
runRuntimeAuthoringRules({ type: 'knowledge_source', item: source, context: { objects } });
const boost = (field: string, caller: string): Entry => ({ field, caller, mode: 'boost', places: 3 });
const nearest = (field: string): Entry => ({ field, caller: 'business_unit', mode: 'nearest' });
/** [what, entry, words the message must carry, words the hint must carry] */
const REFUSED: ReadonlyArray<[string, Entry, string, string]> = [
[
"caller 'user' on a lookup to another object",
boost('account', 'user'),
"with the caller's user id, but 'account' is a field that points at 'account'",
"write `caller: 'business_unit'` for a field that points at 'sys_business_unit'",
],
[
"caller 'user' on a text field",
boost('region', 'user'),
"'region' is a `text` field that points at no object",
"a `user` field, or a lookup to 'sys_user'",
],
[
"caller 'user' on a lookup to the business units",
boost('business_unit', 'user'),
"'business_unit' is a field that points at 'sys_business_unit'",
"write `caller: 'business_unit'`",
],
[
"caller 'business_unit' boost on a user field",
boost('owner', 'business_unit'),
"with the caller's business units, but 'owner' is a field that points at 'sys_user'",
"write `caller: 'user'` for a field that points at 'sys_user'",
],
[
'a nearest entry on a lookup to sys_user',
nearest('reviewer'),
"'reviewer' is a field that points at 'sys_user'",
"write a `boost` entry with `caller: 'user'` and `places` for a field that points at 'sys_user' (`nearest` orders by business unit only)",
],
[
'a nearest entry on a lookup with no readable target',
nearest('dangling'),
"'dangling' is a `lookup` field that points at no object",
"a lookup to 'sys_business_unit'",
],
];
describe('validateKnowledgeSource: source.affinity field target (os validate)', () => {
for (const [what, entry, inMessage, inHint] of REFUSED) {
it(`refuses ${what}, naming the field and the fix`, () => {
const findings = atValidate(policySource([entry]));
expect(findings.map((f) => [f.rule, f.severity, f.path, f.where])).toEqual([
[RULE, 'error', 'knowledgeSources[0].source.affinity[0].field', 'knowledge source "policy_kb"'],
]);
expect(findings[0]!.message).toContain(
`\`source.affinity[0]\` compares field '${entry.field}' of object 'policy'`,
);
expect(findings[0]!.message).toContain(inMessage);
expect(findings[0]!.message).toContain('the entry would match nothing');
expect(findings[0]!.hint).toContain('also listed in `metadataFields`');
expect(findings[0]!.hint).toContain(inHint);
});
}
it('CONTROL: each field that points at its caller fact\'s object is admitted', () => {
expect(
atValidate(
policySource([
nearest('business_unit'),
boost('business_unit', 'business_unit'),
boost('owner', 'user'),
boost('reviewer', 'user'),
]),
),
).toEqual([]);
});
it('CONTROL: a source without affinity is judged as before', () => {
expect(atValidate(policySource(undefined, ['account', 'region']))).toEqual([]);
});
it('reports each refused entry at its own index, and only those', () => {
const findings = atValidate(
policySource([nearest('business_unit'), boost('account', 'user'), boost('owner', 'user'), boost('region', 'business_unit')]),
);
expect(findings.map((f) => [f.rule, f.path])).toEqual([
[RULE, 'knowledgeSources[0].source.affinity[1].field'],
[RULE, 'knowledgeSources[0].source.affinity[3].field'],
]);
expect(findings[1]!.message).toContain('`source.affinity[3]`');
});
it('a field that metadataFields does not list is left to the parse, which names that fix', () => {
expect(atValidate(policySource([boost('account', 'user')], ['owner']))).toEqual([]);
});
it('a member the object does not have is reported once, as an unknown field', () => {
const findings = atValidate(policySource([boost('ownr', 'user')]));
expect(findings.map((f) => [f.rule, f.path])).toEqual([
[KNOWLEDGE_SOURCE_FIELD_UNKNOWN, 'knowledgeSources[0].source.metadataFields[0]'],
]);
});
it('a system column the platform adds is not judged, as at the save door', () => {
expect(atValidate(policySource([boost('owner_id', 'user'), boost('created_by', 'business_unit')]))).toEqual([]);
});
it('an entry whose caller the parse does not admit is left to the parse', () => {
expect(atValidate(policySource([{ field: 'account', caller: 'organization', mode: 'boost', places: 1 }]))).toEqual([]);
});
it('a fields array is read like a field map', () => {
const stack = {
objects: [
{
name: 'policy',
fields: [
{ name: 'title', type: 'text' },
{ name: 'body', type: 'markdown' },
{ name: 'owner', type: 'user' },
{ name: 'account', type: 'lookup', reference: 'account' },
],
},
],
knowledgeSources: [policySource([boost('account', 'user'), boost('owner', 'user')])],
};
expect(validateKnowledgeSource(stack).map((f) => [f.rule, f.path])).toEqual([
[RULE, 'knowledgeSources[0].source.affinity[0].field'],
]);
});
});
describe('validateKnowledgeSource: source.affinity field target (runtime publish door)', () => {
it('refuses the publish with the words os validate prints', () => {
const source = policySource([nearest('reviewer')]);
const verdict = atPublish(source);
expect(verdict.rulesRun).toContain('validateKnowledgeSource');
const refused = verdict.errors.filter((f) => f.rule === RULE);
expect(refused).toHaveLength(1);
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,
});
expect(refused.map(face)).toEqual(atValidate(source).filter((f) => f.rule === RULE).map(face));
});
it('CONTROL: fields that point at the caller facts\' objects publish with nothing to report', () => {
const verdict = atPublish(policySource([nearest('business_unit'), boost('owner', 'user')]));
expect(verdict.errors).toEqual([]);
expect(verdict.advisories.filter((f) => f.rule.startsWith('knowledge-source-'))).toEqual([]);
});
});
describe('the affinity target rule id is published and explained', () => {
it('the root barrel re-exports it', () => {
expect(FROM_BARREL).toBe('knowledge-source-affinity-field-not-caller-target');
});
it('os explain answers it', () => {
const entry = explainRule(RULE);
expect(entry?.rule).toBe(RULE);
expect(entry?.paragraphs.join(' ')).toContain('the entry matches no hit');
});
});