@metaobjectsdev/cli 0.21.5 → 0.22.0-rc.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/commands/init.d.ts.map +1 -1
- package/dist/src/commands/init.js +11 -6
- package/dist/src/commands/init.js.map +1 -1
- package/dist/src/commands/verify.d.ts.map +1 -1
- package/dist/src/commands/verify.js +30 -1
- package/dist/src/commands/verify.js.map +1 -1
- package/dist/src/lib/detect-stack.d.ts +3 -1
- package/dist/src/lib/detect-stack.d.ts.map +1 -1
- package/dist/src/lib/detect-stack.js +50 -5
- package/dist/src/lib/detect-stack.js.map +1 -1
- package/dist/src/lib/load-metaobjects-config.d.ts.map +1 -1
- package/dist/src/lib/load-metaobjects-config.js +23 -1
- package/dist/src/lib/load-metaobjects-config.js.map +1 -1
- package/dist/src/lib/requirement-check.d.ts +63 -0
- package/dist/src/lib/requirement-check.d.ts.map +1 -0
- package/dist/src/lib/requirement-check.js +234 -0
- package/dist/src/lib/requirement-check.js.map +1 -0
- package/dist/src/lib/verified-by-scan.d.ts +18 -0
- package/dist/src/lib/verified-by-scan.d.ts.map +1 -0
- package/dist/src/lib/verified-by-scan.js +168 -0
- package/dist/src/lib/verified-by-scan.js.map +1 -0
- package/package.json +10 -10
- package/src/commands/init.ts +11 -6
- package/src/commands/verify.ts +28 -1
- package/src/lib/detect-stack.ts +48 -5
- package/src/lib/load-metaobjects-config.ts +23 -1
- package/src/lib/requirement-check.ts +272 -0
- package/src/lib/verified-by-scan.ts +190 -0
|
@@ -35,8 +35,10 @@ const _require = createRequire(import.meta.url);
|
|
|
35
35
|
// @metaobjectsdev/cli is this package itself, so it resolves directly from
|
|
36
36
|
// _cliDir rather than through node_modules (which would be a non-existent
|
|
37
37
|
// self-referential symlink).
|
|
38
|
+
const CODEGEN_TS_PKG = "@metaobjectsdev/codegen-ts";
|
|
39
|
+
const TS_POET_PKG = "ts-poet";
|
|
38
40
|
const CLI_PKG_PATHS: Record<string, { dist: string; src: string }> = {
|
|
39
|
-
|
|
41
|
+
[CODEGEN_TS_PKG]: {
|
|
40
42
|
dist: "node_modules/@metaobjectsdev/codegen-ts/dist/index.js",
|
|
41
43
|
src: "node_modules/@metaobjectsdev/codegen-ts/src/index.ts",
|
|
42
44
|
},
|
|
@@ -172,6 +174,26 @@ export async function loadMetaobjectsConfig(projectRoot: string): Promise<Metaob
|
|
|
172
174
|
aliasMap[specifier] = resolveCliPkg(specifier);
|
|
173
175
|
}
|
|
174
176
|
|
|
177
|
+
// ts-poet must resolve to the SAME physical copy @metaobjectsdev/codegen-ts uses.
|
|
178
|
+
// The scaffolded (ADR-0034 owned) generators compose ts-poet Code objects with the
|
|
179
|
+
// engine's render* primitives, and ts-poet recognizes nested Code/Import
|
|
180
|
+
// placeholders by `instanceof` — when the CLI tree and the project tree hold two
|
|
181
|
+
// physical ts-poet copies (globally-installed or linked CLI + the project-local
|
|
182
|
+
// devDependency `meta init` adds), a bare project-side `import ... from "ts-poet"`
|
|
183
|
+
// splits the class identity and every cross-boundary section renders standalone
|
|
184
|
+
// with its own import header (duplicate `import { eq } from "drizzle-orm"` —
|
|
185
|
+
// TS2300 on the adopter's first tsc). Newly scaffolded templates import the
|
|
186
|
+
// combinators from @metaobjectsdev/codegen-ts directly; this alias repairs
|
|
187
|
+
// EXISTING scaffolds that still import bare "ts-poet". In a flat single-tree
|
|
188
|
+
// install the alias resolves to the copy the project would load anyway (no-op).
|
|
189
|
+
// Gated by test/gen-split-tree-single-import.test.ts.
|
|
190
|
+
const codegenTsResolved = aliasMap[CODEGEN_TS_PKG];
|
|
191
|
+
if (codegenTsResolved !== undefined) {
|
|
192
|
+
try {
|
|
193
|
+
aliasMap[TS_POET_PKG] = createRequire(codegenTsResolved).resolve(TS_POET_PKG);
|
|
194
|
+
} catch { /* no ts-poet adjacent to codegen-ts — leave project resolution in place */ }
|
|
195
|
+
}
|
|
196
|
+
|
|
175
197
|
// Pre-process the config file content to rewrite @metaobjectsdev/* import
|
|
176
198
|
// specifiers to resolved absolute paths. This is the primary mechanism that
|
|
177
199
|
// ensures the CLI's own copies are used: jiti's `alias` map is kept below as
|
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
// `meta verify` — the requirement (capability) gate.
|
|
2
|
+
//
|
|
3
|
+
// Requirements are METADATA: `requirement.functional` / `requirement.architectural`
|
|
4
|
+
// are registered metamodel types, declared in `metaobjects/` beside the entities
|
|
5
|
+
// they describe. So this file parses NOTHING. It reads `requirement.*` nodes off
|
|
6
|
+
// the already-loaded model and checks the things the loader cannot.
|
|
7
|
+
//
|
|
8
|
+
// Division of labour, and the reason for it:
|
|
9
|
+
//
|
|
10
|
+
// LOADER (unconditional) the `@status` enum via `allowedValues`, required
|
|
11
|
+
// attrs, child rules, levels being integers.
|
|
12
|
+
// VERIFY (conditional) `@implementedBy` resolution, whose SEVERITY DEPENDS
|
|
13
|
+
// ON `@status`. A loader `references` descriptor
|
|
14
|
+
// always errors on an unresolved target, and an
|
|
15
|
+
// `abandoned` requirement exists precisely to name
|
|
16
|
+
// nodes that are gone — declaring it there would make
|
|
17
|
+
// the entries carrying the mechanism's only controlled
|
|
18
|
+
// evidence fail to load.
|
|
19
|
+
//
|
|
20
|
+
// Two kinds, opposite checks: `functional` fails when NOTHING implements it;
|
|
21
|
+
// `architectural` fails when something VIOLATES it (v1: an empty claim set on a
|
|
22
|
+
// live policy — claim-set arithmetic, deliberately not a predicate DSL).
|
|
23
|
+
|
|
24
|
+
import {
|
|
25
|
+
TYPE_OBJECT,
|
|
26
|
+
TYPE_REQUIREMENT,
|
|
27
|
+
OBJECT_SUBTYPE_ENTITY,
|
|
28
|
+
PACKAGE_SEPARATOR,
|
|
29
|
+
REQUIREMENT_SUBTYPE_ARCHITECTURAL,
|
|
30
|
+
REQUIREMENT_LINK_FLOOR_LEVEL,
|
|
31
|
+
REQUIREMENT_MIN_LEVEL,
|
|
32
|
+
REQUIREMENT_MAX_LEVEL,
|
|
33
|
+
REQUIREMENT_LEVEL_MEMBER,
|
|
34
|
+
REQUIREMENT_STATUSES_REQUIRING_LIVE_NODES,
|
|
35
|
+
resolveObjectRef,
|
|
36
|
+
didYouMeanHint,
|
|
37
|
+
type MetaData,
|
|
38
|
+
type MetaRequirement,
|
|
39
|
+
type RequirementStatus,
|
|
40
|
+
} from "@metaobjectsdev/metadata";
|
|
41
|
+
|
|
42
|
+
export type Severity = "error" | "warn";
|
|
43
|
+
|
|
44
|
+
export interface Diagnostic {
|
|
45
|
+
severity: Severity;
|
|
46
|
+
code: string;
|
|
47
|
+
/** The requirement node's name, when the diagnostic belongs to one. */
|
|
48
|
+
name?: string;
|
|
49
|
+
message: string;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export const ERR_REQUIREMENT_LINK_ABOVE_FLOOR = "ERR_REQUIREMENT_LINK_ABOVE_FLOOR";
|
|
53
|
+
export const ERR_REQUIREMENT_DANGLING_REF = "ERR_REQUIREMENT_DANGLING_REF";
|
|
54
|
+
export const ERR_REQUIREMENT_BAD_LEVEL = "ERR_REQUIREMENT_BAD_LEVEL";
|
|
55
|
+
export const ERR_REQUIREMENT_LEVEL_NESTING = "ERR_REQUIREMENT_LEVEL_NESTING";
|
|
56
|
+
export const ERR_REQUIREMENT_L4_NOT_OBJECT = "ERR_REQUIREMENT_L4_NOT_OBJECT";
|
|
57
|
+
export const ERR_REQUIREMENT_L5_NOT_MEMBER = "ERR_REQUIREMENT_L5_NOT_MEMBER";
|
|
58
|
+
export const ERR_REQUIREMENT_ARCH_NO_IMPLEMENTERS = "ERR_REQUIREMENT_ARCH_NO_IMPLEMENTERS";
|
|
59
|
+
export const WARN_REQUIREMENT_OBJECT_UNCLAIMED = "WARN_REQUIREMENT_OBJECT_UNCLAIMED";
|
|
60
|
+
|
|
61
|
+
/** Severity of the object-coverage gate. Promotion to `"error"` is a one-line
|
|
62
|
+
* flip here, which activates an already-written test rather than requiring new
|
|
63
|
+
* authoring under release pressure.
|
|
64
|
+
*
|
|
65
|
+
* It stays `"warn"`, and the reason is measured rather than cautious:
|
|
66
|
+
*
|
|
67
|
+
* - On a real 120-file estate carrying a SINGLE requirement, this gate reports
|
|
68
|
+
* 93 unclaimed entities — every entity in the repository. At `"error"` a
|
|
69
|
+
* project adopting requirements incrementally fails its first `verify` after
|
|
70
|
+
* authoring one entry, which teaches people to delete the entry.
|
|
71
|
+
* - The gate is satisfiable without being informative: `claimedObjects` below
|
|
72
|
+
* counts a claim from any requirement at any level and any status, so
|
|
73
|
+
* appending an FQN to an existing list clears it. Green proves an entity is
|
|
74
|
+
* NAMED, never that it is understood.
|
|
75
|
+
* - The experiment meant to settle whether forcing it yields real entries or
|
|
76
|
+
* padding stopped at its ceiling probe: at `"warn"` agents already authored
|
|
77
|
+
* proper L3/L4/L5 entries for what they added, so the arms could not differ.
|
|
78
|
+
* That is not evidence promotion is useless — it is evidence that instrument
|
|
79
|
+
* cannot see it.
|
|
80
|
+
*
|
|
81
|
+
* spec/design-docs/2026-08-11-prereg-duplication-and-levels.md, "Round E result". */
|
|
82
|
+
export const OBJECT_COVERAGE_SEVERITY: Severity = "warn";
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Split a member reference into its owning object ref and the dotted member path.
|
|
86
|
+
* `::` qualifies the ROOT-level node only, so the object ref ends at the FIRST
|
|
87
|
+
* `.` after the last `::`.
|
|
88
|
+
* `acme::sales::Order.total.display` -> `["acme::sales::Order", ["total","display"]]`
|
|
89
|
+
*/
|
|
90
|
+
export function splitMemberRef(ref: string): { owner: string; path: string[] } {
|
|
91
|
+
const pkgEnd = ref.lastIndexOf(PACKAGE_SEPARATOR);
|
|
92
|
+
const from = pkgEnd === -1 ? 0 : pkgEnd + PACKAGE_SEPARATOR.length;
|
|
93
|
+
const dot = ref.indexOf(".", from);
|
|
94
|
+
if (dot === -1) return { owner: ref, path: [] };
|
|
95
|
+
return { owner: ref.slice(0, dot), path: ref.slice(dot + 1).split(".") };
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** Walk dotted member segments by CHILD NAME from an object node. */
|
|
99
|
+
function resolveMember(obj: MetaData, path: string[]): MetaData | undefined {
|
|
100
|
+
let cur: MetaData | undefined = obj;
|
|
101
|
+
for (const seg of path) {
|
|
102
|
+
if (cur === undefined) return undefined;
|
|
103
|
+
cur = cur.children().find((c) => c.name === seg);
|
|
104
|
+
}
|
|
105
|
+
return cur;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** Every `requirement.*` node in the tree, at any nesting depth. Hierarchy IS
|
|
109
|
+
* nesting — an L1 solution contains its L2 segments — so this is a walk, not a
|
|
110
|
+
* scan of a flat list keyed by a `parent` string. */
|
|
111
|
+
export function collectRequirements(root: MetaData): MetaRequirement[] {
|
|
112
|
+
const out: MetaRequirement[] = [];
|
|
113
|
+
const walk = (n: MetaData): void => {
|
|
114
|
+
for (const c of n.children()) {
|
|
115
|
+
if (c.type === TYPE_REQUIREMENT) out.push(c as MetaRequirement);
|
|
116
|
+
walk(c);
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
walk(root);
|
|
120
|
+
return out;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Check the requirement tree against the loaded model.
|
|
125
|
+
*
|
|
126
|
+
* What a clean run proves: referential integrity — links sit at or below the
|
|
127
|
+
* link floor, nesting agrees with levels, and references resolve. What it CANNOT
|
|
128
|
+
* prove: that a status is *true*, or that a node actually implements the
|
|
129
|
+
* requirement claiming it. No test can. That truth is the adopter's job.
|
|
130
|
+
*/
|
|
131
|
+
export function checkRequirements(root: MetaData): Diagnostic[] {
|
|
132
|
+
const out: Diagnostic[] = [];
|
|
133
|
+
const reqs = collectRequirements(root);
|
|
134
|
+
if (reqs.length === 0) return out; // opt-in by declaration — no requirements, nothing to say
|
|
135
|
+
|
|
136
|
+
const claimedObjects = new Set<string>();
|
|
137
|
+
|
|
138
|
+
for (const req of reqs) {
|
|
139
|
+
const architectural = req.subType === REQUIREMENT_SUBTYPE_ARCHITECTURAL;
|
|
140
|
+
const level = req.level();
|
|
141
|
+
const refs = req.implementedBy();
|
|
142
|
+
|
|
143
|
+
if (!architectural) {
|
|
144
|
+
if (level === undefined || !Number.isInteger(level)
|
|
145
|
+
|| level < REQUIREMENT_MIN_LEVEL || level > REQUIREMENT_MAX_LEVEL) {
|
|
146
|
+
out.push({
|
|
147
|
+
severity: "error", code: ERR_REQUIREMENT_BAD_LEVEL, name: req.name,
|
|
148
|
+
message: `level must be an integer ${REQUIREMENT_MIN_LEVEL}-${REQUIREMENT_MAX_LEVEL} (got ${String(level)}). ` +
|
|
149
|
+
`L1 solution, L2 segment (app/library), L3 service, L4 object, L5 member.`,
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
// Nesting IS the hierarchy, so a child must sit strictly below its parent.
|
|
153
|
+
const parent = req.parent;
|
|
154
|
+
if (parent !== undefined && parent.type === TYPE_REQUIREMENT) {
|
|
155
|
+
const pl = (parent as MetaRequirement).level();
|
|
156
|
+
if (pl !== undefined && level !== undefined && level <= pl) {
|
|
157
|
+
out.push({
|
|
158
|
+
severity: "error", code: ERR_REQUIREMENT_LEVEL_NESTING, name: req.name,
|
|
159
|
+
message: `nested under "${parent.name}" (level ${pl}) but declares level ${level}. ` +
|
|
160
|
+
`Nesting is the hierarchy — a child sits strictly below its parent.`,
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// -- the link boundary ----------------------------------------------------
|
|
167
|
+
if (refs.length > 0 && !req.mayReferenceModel()) {
|
|
168
|
+
out.push({
|
|
169
|
+
severity: "error", code: ERR_REQUIREMENT_LINK_ABOVE_FLOOR, name: req.name,
|
|
170
|
+
message: `'implementedBy' is legal at L${REQUIREMENT_LINK_FLOOR_LEVEL} (object) and ` +
|
|
171
|
+
`L${REQUIREMENT_MAX_LEVEL} (member) only. L1-L3 are organisational and never reference ` +
|
|
172
|
+
`the model — move the links to a nested L${REQUIREMENT_LINK_FLOOR_LEVEL} child.`,
|
|
173
|
+
});
|
|
174
|
+
continue;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
for (const ref of refs) {
|
|
178
|
+
const { owner, path } = splitMemberRef(ref);
|
|
179
|
+
// referrerPkg is the requirement's own effective package, so a bare ref
|
|
180
|
+
// binds package-locally under the ADR-0042 contract — the loader's own
|
|
181
|
+
// resolver, never a parallel name scan (#228).
|
|
182
|
+
const referrerPkg = req.package ?? req.fileDefaultPackage ?? "";
|
|
183
|
+
const { node } = resolveObjectRef(root, owner, referrerPkg);
|
|
184
|
+
const isObjectRef = path.length === 0;
|
|
185
|
+
|
|
186
|
+
if (node !== undefined && (isObjectRef || resolveMember(node, path) !== undefined)) {
|
|
187
|
+
claimedObjects.add(node.resolutionKey());
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
if (!architectural && level === REQUIREMENT_LINK_FLOOR_LEVEL && !isObjectRef) {
|
|
191
|
+
out.push({
|
|
192
|
+
severity: "error", code: ERR_REQUIREMENT_L4_NOT_OBJECT, name: req.name,
|
|
193
|
+
message: `L${REQUIREMENT_LINK_FLOOR_LEVEL} references an object; '${ref}' names a member. ` +
|
|
194
|
+
`Move it to a nested L${REQUIREMENT_LEVEL_MEMBER} child, or reference the object itself.`,
|
|
195
|
+
});
|
|
196
|
+
continue;
|
|
197
|
+
}
|
|
198
|
+
if (!architectural && level === REQUIREMENT_LEVEL_MEMBER && isObjectRef) {
|
|
199
|
+
out.push({
|
|
200
|
+
severity: "error", code: ERR_REQUIREMENT_L5_NOT_MEMBER, name: req.name,
|
|
201
|
+
message: `L${REQUIREMENT_LEVEL_MEMBER} references a member (field, view or identity); ` +
|
|
202
|
+
`'${ref}' names an object. Move it to its L${REQUIREMENT_LINK_FLOOR_LEVEL} parent.`,
|
|
203
|
+
});
|
|
204
|
+
continue;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
const resolved = node !== undefined && (isObjectRef || resolveMember(node, path) !== undefined);
|
|
208
|
+
if (!resolved) {
|
|
209
|
+
// Severity is CONDITIONAL ON STATUS, and the asymmetry inverts as a pair.
|
|
210
|
+
// On abandoned/superseded the nodes are SUPPOSED to be gone — that is the
|
|
211
|
+
// entry doing its job, and the reason this check cannot live in the loader.
|
|
212
|
+
if (req.requiresLiveNodes()) {
|
|
213
|
+
out.push({
|
|
214
|
+
severity: "error", code: ERR_REQUIREMENT_DANGLING_REF, name: req.name,
|
|
215
|
+
message: `'${ref}' does not resolve in the loaded model (status '${String(req.status())}' — ` +
|
|
216
|
+
`the model moved and the requirement is stale).` + didYouMeanHint(root, owner),
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// -- architectural universality, v1: claim-set arithmetic -----------------
|
|
223
|
+
// A live policy claimed by nothing is the audited-base case: declared and
|
|
224
|
+
// applied to nothing. Deliberately NOT a violation-predicate DSL — that
|
|
225
|
+
// would be the registration this design already argued its way to, arriving
|
|
226
|
+
// a second time through the back door.
|
|
227
|
+
const status = req.status();
|
|
228
|
+
const live = status !== undefined
|
|
229
|
+
&& REQUIREMENT_STATUSES_REQUIRING_LIVE_NODES.includes(status as RequirementStatus);
|
|
230
|
+
if (architectural && live && refs.length === 0) {
|
|
231
|
+
out.push({
|
|
232
|
+
severity: "error", code: ERR_REQUIREMENT_ARCH_NO_IMPLEMENTERS, name: req.name,
|
|
233
|
+
message: `architectural requirement is '${String(status)}' but nothing implements it. ` +
|
|
234
|
+
`Its check is universality — a claim set of zero means the policy is declared and unapplied.`,
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// -- object coverage: adding an entity forces a requirement -----------------
|
|
240
|
+
// Binary per entity, never a ratio: a "% claimed" number measures what the
|
|
241
|
+
// schema can express, is biased against the hardest rules, and invites
|
|
242
|
+
// optimising the number.
|
|
243
|
+
//
|
|
244
|
+
// SCOPE, stated because it is a decision and not an oversight:
|
|
245
|
+
//
|
|
246
|
+
// ENTITIES ONLY. `object.value` and `object.projection` are exempt. A value is a
|
|
247
|
+
// shape (a DTO, a payload, a message) and a projection is DERIVED from an entity
|
|
248
|
+
// that is itself claimable — requiring both to carry their own capability claim
|
|
249
|
+
// would multiply entries without adding information.
|
|
250
|
+
//
|
|
251
|
+
// OBJECT GRAIN ONLY. Fields, views, validators and identities are never required
|
|
252
|
+
// to be claimed. Member-grain coverage is the "thousands of meaningless links"
|
|
253
|
+
// failure `spec/capability-ledger.md` argues against: plumbing members are covered
|
|
254
|
+
// by ARCHITECTURAL requirements with high fan-out (one uuid-PK rule claims every
|
|
255
|
+
// entity), not by a per-member entry. L5 exists so a claim about a specific member
|
|
256
|
+
// CAN be made when it carries real meaning — never so that every member must.
|
|
257
|
+
//
|
|
258
|
+
// So a green run means "every entity is claimed by something", not "every node is
|
|
259
|
+
// described". The stronger reading would be false.
|
|
260
|
+
for (const ent of root.children()) {
|
|
261
|
+
if (ent.type !== TYPE_OBJECT || ent.subType !== OBJECT_SUBTYPE_ENTITY) continue;
|
|
262
|
+
const key = ent.resolutionKey();
|
|
263
|
+
if (!claimedObjects.has(key)) {
|
|
264
|
+
out.push({
|
|
265
|
+
severity: OBJECT_COVERAGE_SEVERITY, code: WARN_REQUIREMENT_OBJECT_UNCLAIMED,
|
|
266
|
+
message: `no requirement claims '${key}'. Add it to an L${REQUIREMENT_LINK_FLOOR_LEVEL} requirement's 'implementedBy'.`,
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
return out;
|
|
272
|
+
}
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
// `meta verify` — the `@verifiedBy` check.
|
|
2
|
+
//
|
|
3
|
+
// A requirement's `@verifiedBy` names the tests that prove the behaviour. verify
|
|
4
|
+
// checks each name EXISTS and is NOT SKIPPED; it never runs them. Running them is
|
|
5
|
+
// the test runner's job, and a requirement gate that shelled out to one would be
|
|
6
|
+
// slow, ecosystem-specific, and wrong in CI.
|
|
7
|
+
//
|
|
8
|
+
// Until this existed, `@verifiedBy` was registered vocabulary that nothing read —
|
|
9
|
+
// the precise pattern ADR-0007 Amendment 2 and the `@role` shrink exist to punish.
|
|
10
|
+
// An attribute the loader accepts and no consumer dispatches on teaches authors
|
|
11
|
+
// that declaring is free and means nothing.
|
|
12
|
+
//
|
|
13
|
+
// PRECISION OVER RECALL, deliberately. The scan matches a name anywhere in the
|
|
14
|
+
// test corpus, as a whole word, in any language. That is the most generous
|
|
15
|
+
// possible reading, so a "missing" verdict means the name appears in NO test file
|
|
16
|
+
// at all — which is a broken claim in any ecosystem. The repo's standing rule for
|
|
17
|
+
// drift checks is to bias toward under-flagging, and a nagging gate gets disabled,
|
|
18
|
+
// which costs more than the misses.
|
|
19
|
+
//
|
|
20
|
+
// FAIL-OPEN ON INABILITY. If the project has no test files this scan can see, it
|
|
21
|
+
// says NOTHING rather than reporting every name missing. Absence of evidence is
|
|
22
|
+
// not evidence of absence, and a monorepo whose tests live outside `--cwd` must
|
|
23
|
+
// not be told its requirements are unverified.
|
|
24
|
+
|
|
25
|
+
import { readdirSync, readFileSync, statSync } from "node:fs";
|
|
26
|
+
import { join, relative, sep } from "node:path";
|
|
27
|
+
import {
|
|
28
|
+
TYPE_REQUIREMENT,
|
|
29
|
+
type MetaData,
|
|
30
|
+
type MetaRequirement,
|
|
31
|
+
} from "@metaobjectsdev/metadata";
|
|
32
|
+
|
|
33
|
+
export const ERR_REQUIREMENT_TEST_MISSING = "ERR_REQUIREMENT_TEST_MISSING";
|
|
34
|
+
export const WARN_REQUIREMENT_TEST_SKIPPED = "WARN_REQUIREMENT_TEST_SKIPPED";
|
|
35
|
+
|
|
36
|
+
export interface VerifiedByDiagnostic {
|
|
37
|
+
severity: "error" | "warn";
|
|
38
|
+
code: string;
|
|
39
|
+
name?: string;
|
|
40
|
+
message: string;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const IGNORE_SEGMENTS = new Set([
|
|
44
|
+
"node_modules", ".git", "dist", "build", "out", ".next", "coverage",
|
|
45
|
+
".metaobjects", "generated", "target", "bin", "obj", "__pycache__", ".venv", "venv",
|
|
46
|
+
]);
|
|
47
|
+
|
|
48
|
+
/** Test files across the five ecosystems this project ports to. */
|
|
49
|
+
const TEST_FILE = new RegExp(
|
|
50
|
+
[
|
|
51
|
+
"\\.(?:test|spec)\\.[cm]?[jt]sx?$", // bun / jest / vitest / mocha
|
|
52
|
+
"(?:^|[./_-])[Tt]est[^/]*\\.java$", // JUnit — TestFoo.java
|
|
53
|
+
"[A-Za-z0-9]Test(?:s)?\\.java$", // JUnit — FooTest.java / FooTests.java
|
|
54
|
+
"[A-Za-z0-9]Tests?\\.cs$", // xUnit / NUnit
|
|
55
|
+
"^test_[^/]*\\.py$", // pytest
|
|
56
|
+
"[^/]*_test\\.py$", // pytest, trailing convention
|
|
57
|
+
"[A-Za-z0-9]Test(?:s)?\\.kt$", // Kotlin
|
|
58
|
+
].join("|"),
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
/** Markers that a test exists but is disabled, across the same ecosystems. */
|
|
62
|
+
const SKIP_MARKER = new RegExp(
|
|
63
|
+
[
|
|
64
|
+
"\\b(?:it|test|describe)\\.(?:skip|todo)\\b", // jest/vitest/bun
|
|
65
|
+
"\\bx(?:it|test|describe)\\b", // mocha/jasmine
|
|
66
|
+
"@Disabled\\b", // JUnit 5
|
|
67
|
+
"@Ignore\\b", // JUnit 4 / Kotlin
|
|
68
|
+
"@pytest\\.mark\\.skip", // pytest
|
|
69
|
+
"\\[Ignore[\\](]", // MSTest / NUnit
|
|
70
|
+
"\\bSkip\\s*=", // xUnit [Fact(Skip = "...")]
|
|
71
|
+
].join("|"),
|
|
72
|
+
);
|
|
73
|
+
|
|
74
|
+
interface TestCorpus {
|
|
75
|
+
files: number;
|
|
76
|
+
/** rel path -> lines, kept so a skip marker can be located near the name. */
|
|
77
|
+
byFile: Map<string, string[]>;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function walk(dir: string, root: string, acc: TestCorpus, depth = 0): void {
|
|
81
|
+
if (depth > 12) return; // pathological trees; the scan is advisory, not exhaustive
|
|
82
|
+
let entries;
|
|
83
|
+
try {
|
|
84
|
+
entries = readdirSync(dir, { withFileTypes: true });
|
|
85
|
+
} catch {
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
for (const e of entries) {
|
|
89
|
+
if (e.isDirectory()) {
|
|
90
|
+
if (IGNORE_SEGMENTS.has(e.name) || e.name.startsWith(".")) continue;
|
|
91
|
+
walk(join(dir, e.name), root, acc, depth + 1);
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
if (!e.isFile() || !TEST_FILE.test(e.name)) continue;
|
|
95
|
+
const abs = join(dir, e.name);
|
|
96
|
+
try {
|
|
97
|
+
if (statSync(abs).size > 512 * 1024) continue;
|
|
98
|
+
acc.byFile.set(relative(root, abs).split(sep).join("/"), readFileSync(abs, "utf8").split("\n"));
|
|
99
|
+
acc.files++;
|
|
100
|
+
} catch {
|
|
101
|
+
/* unreadable file is not a finding */
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** Every `requirement.*` node in the tree, at any nesting depth. */
|
|
107
|
+
function collect(root: MetaData): MetaRequirement[] {
|
|
108
|
+
const out: MetaRequirement[] = [];
|
|
109
|
+
const rec = (n: MetaData): void => {
|
|
110
|
+
for (const c of n.children()) {
|
|
111
|
+
if (c.type === TYPE_REQUIREMENT) out.push(c as MetaRequirement);
|
|
112
|
+
rec(c);
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
rec(root);
|
|
116
|
+
return out;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* A whole-word match, so `OrderServiceTest` never satisfies a claim naming `Order`.
|
|
121
|
+
*
|
|
122
|
+
* `_` counts as a SEPARATOR, not a word character: pytest's `def test_OrderServiceTest`
|
|
123
|
+
* plainly is the test a claim naming `OrderServiceTest` means, and refusing it would
|
|
124
|
+
* emit the confident false error this scan is built to avoid. Camel-case boundaries
|
|
125
|
+
* stay strict, which is what actually prevents a short name matching a longer one.
|
|
126
|
+
*/
|
|
127
|
+
function wordRx(name: string): RegExp {
|
|
128
|
+
return new RegExp(`(?:^|[^A-Za-z0-9])${name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}(?![A-Za-z0-9])`);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Check every `@verifiedBy` name against the project's test corpus.
|
|
133
|
+
*
|
|
134
|
+
* Severity mirrors `@implementedBy`: a broken claim is an ERROR on `live`/`partial`
|
|
135
|
+
* and silent on `abandoned`/`superseded`, because a retired requirement naming a
|
|
136
|
+
* deleted test is the entry doing its job, not drift.
|
|
137
|
+
*/
|
|
138
|
+
export function checkVerifiedBy(root: MetaData, cwd: string): VerifiedByDiagnostic[] {
|
|
139
|
+
const reqs = collect(root).filter((r) => r.verifiedBy().length > 0);
|
|
140
|
+
if (reqs.length === 0) return []; // opt-in by declaration
|
|
141
|
+
|
|
142
|
+
const corpus: TestCorpus = { files: 0, byFile: new Map() };
|
|
143
|
+
walk(cwd, cwd, corpus);
|
|
144
|
+
if (corpus.files === 0) return []; // fail open: nothing to judge against
|
|
145
|
+
|
|
146
|
+
const out: VerifiedByDiagnostic[] = [];
|
|
147
|
+
for (const req of reqs) {
|
|
148
|
+
for (const test of req.verifiedBy()) {
|
|
149
|
+
const rx = wordRx(test);
|
|
150
|
+
let foundIn: string | undefined;
|
|
151
|
+
let skippedAt: string | undefined;
|
|
152
|
+
for (const [file, lines] of corpus.byFile) {
|
|
153
|
+
for (let i = 0; i < lines.length; i++) {
|
|
154
|
+
if (!rx.test(lines[i] ?? "")) continue;
|
|
155
|
+
foundIn ??= file;
|
|
156
|
+
// a decorator/annotation sits above the declaration it disables
|
|
157
|
+
const window = lines.slice(Math.max(0, i - 3), i + 1).join("\n");
|
|
158
|
+
if (SKIP_MARKER.test(window)) skippedAt ??= `${file}:${i + 1}`;
|
|
159
|
+
}
|
|
160
|
+
if (foundIn !== undefined && skippedAt !== undefined) break;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
if (foundIn === undefined) {
|
|
164
|
+
if (req.requiresLiveNodes()) {
|
|
165
|
+
out.push({
|
|
166
|
+
severity: "error",
|
|
167
|
+
code: ERR_REQUIREMENT_TEST_MISSING,
|
|
168
|
+
name: req.name,
|
|
169
|
+
message:
|
|
170
|
+
`'verifiedBy' names '${test}', which appears in none of the ` +
|
|
171
|
+
`${corpus.files} test file(s) found under this project. Either the test was ` +
|
|
172
|
+
`renamed or removed, or the claim was never true.`,
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
continue;
|
|
176
|
+
}
|
|
177
|
+
if (skippedAt !== undefined) {
|
|
178
|
+
out.push({
|
|
179
|
+
severity: "warn",
|
|
180
|
+
code: WARN_REQUIREMENT_TEST_SKIPPED,
|
|
181
|
+
name: req.name,
|
|
182
|
+
message:
|
|
183
|
+
`'verifiedBy' names '${test}', but it is disabled at ${skippedAt}. ` +
|
|
184
|
+
`A skipped test proves nothing — the requirement reads as verified and is not.`,
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
return out;
|
|
190
|
+
}
|