Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 285x 285x 285x 290x 290x 6x 285x 285x 285x 285x 1x 285x 285x 285x 294x 294x 294x 294x 294x 294x 295x 295x 295x 295x 295x 295x 295x 295x 295x 295x 295x 295x 295x 302x 302x 4x 285x 285x 285x 285x 239x 285x 285x 285x 287x 287x 289x 289x 287x 285x 285x 294x 294x 294x 294x 295x 295x 295x 295x 295x 295x 295x 285x 247x 11x 239x 285x 863x 49x 863x 884x 884x 21x 884x 884x 42x 842x 1195x 1195x 182x 2024x 2028x 2019x 285x 285x 290x 289x 1102x 1102x 1102x 1178x | import { canonicalId, interoperableStringError, localId } from "./id.js";
import type { MouraManifest } from "./manifest.js";
import type { MarkdownDocument } from "./markdown.js";
import type { TraceNode } from "./model.js";
import { error, type ValidationError } from "./validation.js";
export interface ParsedSources {
readonly requirements: ReadonlyMap<string, readonly string[]>;
readonly specifications: ReadonlyMap<string, MarkdownDocument>;
}
/** Pure structural validation over already parsed inputs. */
export function validateStructure(
manifest: MouraManifest,
sources: ParsedSources,
): readonly ValidationError[] {
const errors: ValidationError[] = [];
nonEmpty(
manifest.sources.requirements,
"missing-requirement-source",
"sources.requirements must contain at least one source",
errors,
);
for (const layer of manifest.verificationLayers) {
const reason = interoperableStringError(layer);
if (reason)
errors.push(
error(
"invalid-verification-layer",
`verification layer ${JSON.stringify(layer)} is invalid: ${reason}`,
),
);
}
nonEmpty(
manifest.sources.specifications,
"missing-specification-source",
"sources.specifications must contain at least one source",
errors,
);
nonEmpty(
manifest.requirements,
"missing-requirement",
"requirements must contain at least one Requirement",
errors,
);
nonEmpty(
manifest.verificationLayers,
"missing-verification-layer",
"verification.layers must contain at least one layer",
errors,
);
duplicates(
manifest.verificationLayers,
(layer) =>
error(
"duplicate-verification-layer",
`verification layer ${JSON.stringify(layer)} is declared more than once`,
),
errors,
);
const requirementIds = new Set<string>();
const canonicalIds = new Set<string>();
for (const requirement of manifest.requirements) {
validateId(requirement.localId, "Requirement", errors);
duplicate(
requirementIds,
requirement.localId,
"duplicate-requirement",
`Requirement ${JSON.stringify(requirement.localId)} is declared more than once`,
errors,
);
addCanonical([requirement], canonicalIds, errors);
nonEmpty(
requirement.scenarios,
"missing-scenario",
`${requirement.localId || "Requirement"} must contain at least one Scenario`,
errors,
);
const scenarioIds = new Set<string>();
for (const scenario of requirement.scenarios) {
validateId(scenario.localId, "Scenario", errors);
duplicate(
scenarioIds,
scenario.localId,
"duplicate-scenario",
`Scenario ${JSON.stringify(scenario.localId)} is repeated under ${requirement.localId}`,
errors,
);
addCanonical([requirement, scenario], canonicalIds, errors);
nonEmpty(
scenario.cases,
"missing-case",
`${path([requirement, scenario])} must contain at least one Case`,
errors,
);
const caseIds = new Set<string>();
for (const testCase of scenario.cases) {
const nodes = [requirement, scenario, testCase] as const;
validateId(testCase.localId, "Case", errors);
duplicate(
caseIds,
testCase.localId,
"duplicate-case",
`Case ${JSON.stringify(testCase.localId)} is repeated under ${path([requirement, scenario])}`,
errors,
);
addCanonical(nodes, canonicalIds, errors);
nonEmpty(
[...testCase.verify, ...(testCase.unimplemented ?? [])],
"missing-verify",
`${path(nodes)} must declare at least one verify or unimplemented layer`,
errors,
);
const verifies = new Set<string>();
for (const layer of [
...testCase.verify,
...(testCase.unimplemented ?? []),
]) {
duplicate(
verifies,
layer,
"duplicate-verify-layer",
`${path(nodes)} repeats verification layer ${JSON.stringify(layer)} across verify/unimplemented declarations`,
errors,
);
if (!manifest.verificationLayers.includes(layer)) {
errors.push(
error(
"unknown-verification-layer",
`${path(nodes)} references unknown verification layer ${JSON.stringify(layer)}`,
{ canonicalId: path(nodes) },
),
);
}
}
}
}
}
compareMarkdown(manifest, sources, errors);
return errors;
}
function compareMarkdown(
manifest: MouraManifest,
sources: ParsedSources,
errors: ValidationError[],
): void {
const requirementOccurrences = new Map<string, string[]>();
for (const [source, ids] of sources.requirements) {
for (const id of ids) append(requirementOccurrences, id, source);
}
const specificationOccurrences = new Map<string, string[]>();
for (const [source, document] of sources.specifications) {
for (const requirement of document.requirements) {
append(specificationOccurrences, requirement.id, source);
for (const scenario of requirement.scenarios) {
append(
specificationOccurrences,
`${requirement.id}/${scenario.id}`,
source,
);
for (const testCase of scenario.cases)
append(
specificationOccurrences,
`${requirement.id}/${scenario.id}/${testCase}`,
source,
);
}
}
}
const expected = new Set<string>();
for (const requirement of manifest.requirements) {
expected.add(requirement.localId);
missing(
requirementOccurrences,
requirement.localId,
"missing-requirement-markdown",
`${requirement.localId} is declared in moura.yaml but was not found in a requirement source`,
errors,
);
missing(
specificationOccurrences,
requirement.localId,
"missing-specification-requirement",
`${requirement.localId} is declared in moura.yaml but was not found in a specification source`,
errors,
);
for (const scenario of requirement.scenarios) {
const scenarioId = `${requirement.localId}/${scenario.localId}`;
expected.add(scenarioId);
missing(
specificationOccurrences,
scenarioId,
"missing-specification-scenario",
`${scenarioId} is declared in moura.yaml but was not found under its expected Requirement`,
errors,
);
for (const testCase of scenario.cases) {
const caseId = `${scenarioId}/${testCase.localId}`;
expected.add(caseId);
missing(
specificationOccurrences,
caseId,
"missing-specification-case",
`${caseId} is declared in moura.yaml but was not found under its expected Scenario`,
errors,
);
}
}
}
for (const [id, locations] of requirementOccurrences) {
if (!manifest.requirements.some((item) => item.localId === id))
errors.push(
error(
"unmanaged-markdown-id",
`${id} in ${locations[0]!} is not declared in moura.yaml`,
{ source: locations[0]! },
),
);
Iif (locations.length > 1)
errors.push(
error(
"duplicate-canonical-id",
`${id} occurs more than once in requirement sources`,
{ canonicalId: id },
),
);
}
for (const [id, locations] of specificationOccurrences) {
if (!expected.has(id))
errors.push(
error(
"unmanaged-markdown-id",
`${id} in ${locations[0]!} is not declared at that hierarchy in moura.yaml`,
{ canonicalId: id, source: locations[0]! },
),
);
Iif (locations.length > 1)
errors.push(
error(
"duplicate-canonical-id",
`${id} occurs more than once in specification sources`,
{ canonicalId: id },
),
);
}
}
function validateId(
value: string,
kind: string,
errors: ValidationError[],
): void {
try {
localId(value);
} catch (cause) {
errors.push(
error(
"invalid-local-id",
`${kind} ${JSON.stringify(value)} is invalid: ${cause instanceof Error ? cause.message : String(cause)}`,
),
);
}
}
function addCanonical(
nodes: readonly TraceNode[],
seen: Set<string>,
errors: ValidationError[],
): void {
let id: string;
try {
id = canonicalId(nodes);
} catch {
return;
}
duplicate(
seen,
id,
"duplicate-canonical-id",
`canonical ID ${id} is derived more than once`,
errors,
);
}
function path(nodes: readonly TraceNode[]): string {
try {
return canonicalId(nodes);
} catch {
return nodes.map((node) => node.localId).join("/");
}
}
function nonEmpty(
values: readonly unknown[],
code: string,
message: string,
errors: ValidationError[],
): void {
if (values.length === 0) errors.push(error(code, message));
}
function duplicate(
seen: Set<string>,
value: string,
code: string,
message: string,
errors: ValidationError[],
): void {
if (seen.has(value)) errors.push(error(code, message));
else seen.add(value);
}
function duplicates(
values: readonly string[],
make: (value: string) => ValidationError,
errors: ValidationError[],
): void {
const seen = new Set<string>();
for (const value of values) {
if (seen.has(value)) errors.push(make(value));
else seen.add(value);
}
}
function append(map: Map<string, string[]>, id: string, source: string): void {
const entries = map.get(id) ?? [];
entries.push(source);
map.set(id, entries);
}
function missing(
map: Map<string, string[]>,
id: string,
code: string,
message: string,
errors: ValidationError[],
): void {
if (!map.has(id)) errors.push(error(code, message, { canonicalId: id }));
}
|