feat: антипаттерны с жизненным циклом и seed из граблей библиотеки (BHL-015)
- spec/schemas/antipattern.schema.json: level и lifecycle разделены - seed: 3 антипаттерна + 3 proposed-требования + домен/возможность - валидатор: цели relations антипаттернов и ссылки требований - реальный spec/ проходит bhl validate; пакет 18/18, корень зелёный
This commit is contained in:
+103
-26
@@ -23,6 +23,13 @@ interface Doc {
|
||||
value: Record<string, unknown>;
|
||||
}
|
||||
|
||||
interface Indexes {
|
||||
domains: Set<string>;
|
||||
capabilities: Map<string, string>;
|
||||
requirements: Set<string>;
|
||||
antipatterns: Set<string>;
|
||||
}
|
||||
|
||||
interface Changelog {
|
||||
entries: Doc[];
|
||||
}
|
||||
@@ -32,6 +39,7 @@ const ENTITY_DIRS: readonly { dir: string; schema: string }[] = [
|
||||
{ dir: 'capabilities', schema: 'capability' },
|
||||
{ dir: 'requirements', schema: 'requirement' },
|
||||
{ dir: 'parameters', schema: 'parameter' },
|
||||
{ dir: 'antipatterns', schema: 'antipattern' },
|
||||
];
|
||||
|
||||
const SCHEMA_BASE = 'https://baseline-harness.dev/spec/schemas/';
|
||||
@@ -284,44 +292,77 @@ class Validator {
|
||||
}
|
||||
}
|
||||
|
||||
private buildIndexes(docs: Doc[]): {
|
||||
domains: Set<string>;
|
||||
capabilities: Map<string, string>;
|
||||
requirements: Set<string>;
|
||||
} {
|
||||
const domains = new Set<string>();
|
||||
const capabilities = new Map<string, string>();
|
||||
const requirements = new Set<string>();
|
||||
private buildIndexes(docs: Doc[]): Indexes {
|
||||
const indexes: Indexes = {
|
||||
domains: new Set<string>(),
|
||||
capabilities: new Map<string, string>(),
|
||||
requirements: new Set<string>(),
|
||||
antipatterns: new Set<string>(),
|
||||
};
|
||||
for (const doc of docs) {
|
||||
const id = doc.value['id'];
|
||||
if (typeof id !== 'string') {
|
||||
continue;
|
||||
this.indexDoc(doc, indexes);
|
||||
}
|
||||
return indexes;
|
||||
}
|
||||
|
||||
private indexDoc(doc: Doc, indexes: Indexes): void {
|
||||
const id = doc.value['id'];
|
||||
if (typeof id !== 'string') {
|
||||
return;
|
||||
}
|
||||
switch (doc.kind) {
|
||||
case 'domain': {
|
||||
indexes.domains.add(id);
|
||||
break;
|
||||
}
|
||||
if (doc.kind === 'domain') {
|
||||
domains.add(id);
|
||||
case 'requirement': {
|
||||
indexes.requirements.add(id);
|
||||
break;
|
||||
}
|
||||
if (doc.kind === 'requirement') {
|
||||
requirements.add(id);
|
||||
case 'antipattern': {
|
||||
indexes.antipatterns.add(id);
|
||||
break;
|
||||
}
|
||||
if (doc.kind === 'capability' && typeof doc.value['domain'] === 'string') {
|
||||
capabilities.set(id, doc.value['domain']);
|
||||
case 'capability': {
|
||||
const domain = doc.value['domain'];
|
||||
if (typeof domain === 'string') {
|
||||
indexes.capabilities.set(id, domain);
|
||||
}
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return { domains, capabilities, requirements };
|
||||
}
|
||||
|
||||
private checkReferences(docs: Doc[], changelog: Changelog | undefined): void {
|
||||
const { domains, capabilities, requirements } = this.buildIndexes(docs);
|
||||
const indexes = this.buildIndexes(docs);
|
||||
for (const doc of docs) {
|
||||
if (doc.kind === 'capability') {
|
||||
this.checkCapability(doc, domains);
|
||||
}
|
||||
if (doc.kind === 'requirement') {
|
||||
this.checkRequirement(doc, domains, capabilities, requirements);
|
||||
}
|
||||
this.checkDocReferences(doc, indexes);
|
||||
}
|
||||
if (changelog !== undefined) {
|
||||
this.checkChangelogReferences(changelog, requirements);
|
||||
this.checkChangelogReferences(changelog, indexes.requirements);
|
||||
}
|
||||
}
|
||||
|
||||
private checkDocReferences(doc: Doc, indexes: Indexes): void {
|
||||
if (doc.kind === 'capability') {
|
||||
this.checkCapability(doc, indexes.domains);
|
||||
return;
|
||||
}
|
||||
if (doc.kind === 'requirement') {
|
||||
this.checkRequirement(
|
||||
doc,
|
||||
indexes.domains,
|
||||
indexes.capabilities,
|
||||
indexes.requirements,
|
||||
indexes.antipatterns,
|
||||
);
|
||||
return;
|
||||
}
|
||||
if (doc.kind === 'antipattern') {
|
||||
this.checkAntipattern(doc, indexes.requirements);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -356,9 +397,45 @@ class Validator {
|
||||
domains: Set<string>,
|
||||
capabilities: Map<string, string>,
|
||||
requirements: Set<string>,
|
||||
antipatterns: Set<string>,
|
||||
): void {
|
||||
this.checkRequirementTargets(doc, domains, capabilities);
|
||||
this.checkRequirementRelations(doc, requirements);
|
||||
this.checkRequirementAntipatterns(doc, antipatterns);
|
||||
}
|
||||
|
||||
private checkRequirementAntipatterns(doc: Doc, antipatterns: Set<string>): void {
|
||||
const items = doc.value['antipatterns'];
|
||||
if (!Array.isArray(items)) {
|
||||
return;
|
||||
}
|
||||
const known: readonly unknown[] = items;
|
||||
for (const item of known) {
|
||||
if (typeof item === 'string' && !antipatterns.has(item)) {
|
||||
this.push(doc.file, 'unknown-antipattern', `antipattern ${item} is not defined`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private checkAntipattern(doc: Doc, requirements: Set<string>): void {
|
||||
const relations = doc.value['relations'];
|
||||
if (!Array.isArray(relations)) {
|
||||
return;
|
||||
}
|
||||
const items: readonly unknown[] = relations;
|
||||
for (const relation of items) {
|
||||
if (!isRecord(relation)) {
|
||||
continue;
|
||||
}
|
||||
const target = relation['target'];
|
||||
if (typeof target === 'string' && !requirements.has(target)) {
|
||||
this.push(
|
||||
doc.file,
|
||||
'unknown-requirement',
|
||||
`antipattern contradicts undefined requirement ${target}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private checkRequirementTargets(
|
||||
|
||||
@@ -65,6 +65,16 @@ const SCHEMAS = {
|
||||
description: { type: 'string' },
|
||||
},
|
||||
},
|
||||
'antipattern.schema.json': {
|
||||
$id: 'https://baseline-harness.dev/spec/schemas/antipattern.schema.json',
|
||||
type: 'object',
|
||||
required: ['id', 'level', 'relations'],
|
||||
properties: {
|
||||
id: { type: 'string', pattern: '^ant-[a-z0-9-]+$' },
|
||||
level: { enum: ['SHOULD NOT', 'MUST NOT'] },
|
||||
relations: { type: 'array', items: { type: 'object' } },
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const BASE_FILES = {
|
||||
@@ -94,6 +104,7 @@ function fixture(t, overrides = {}) {
|
||||
writeFileSync(target, contents, 'utf8');
|
||||
}
|
||||
mkdirSync(path.join(root, 'parameters'), { recursive: true });
|
||||
mkdirSync(path.join(root, 'antipatterns'), { recursive: true });
|
||||
t.after(() => rmSync(root, { recursive: true, force: true }));
|
||||
return root;
|
||||
}
|
||||
@@ -192,3 +203,23 @@ test('обязательная структура корня: schemas и дир
|
||||
assert.equal(result.code, 1);
|
||||
assert.ok(codes(result).includes('missing-directory'));
|
||||
});
|
||||
|
||||
test('антипаттерны: двусторонние ссылки проходят, висячие цели падают', (t) => {
|
||||
const ok = fixture(t, {
|
||||
'requirements/req-gamma.yaml':
|
||||
'id: req-gamma\ndomain: dom-build\ncapability: cap-verify\nstatement: Проект MUST NOT молчать о провале.\nlevel: MUST NOT\nantipatterns:\n - ant-silent-fail\n',
|
||||
'antipatterns/ant-silent-fail.yaml':
|
||||
'id: ant-silent-fail\nlevel: MUST NOT\nrelations:\n - target: req-gamma\n kind: contradicts\n',
|
||||
});
|
||||
assert.equal(runValidate(ok).code, 0);
|
||||
const dangling = fixture(t, {
|
||||
'antipatterns/ant-ghost.yaml':
|
||||
'id: ant-ghost\nlevel: MUST NOT\nrelations:\n - target: req-ghost\n kind: contradicts\n',
|
||||
});
|
||||
assert.ok(codes(runValidate(dangling)).includes('unknown-requirement'));
|
||||
const orphanLink = fixture(t, {
|
||||
'requirements/req-delta.yaml':
|
||||
'id: req-delta\ndomain: dom-build\ncapability: cap-verify\nstatement: Проект SHOULD кэшировать артефакты.\nlevel: SHOULD\nantipatterns:\n - ant-never-defined\n',
|
||||
});
|
||||
assert.ok(codes(runValidate(orphanLink)).includes('unknown-antipattern'));
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user