diff --git a/docs/architecture.md b/docs/architecture.md index 862fbe3..761b759 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -74,6 +74,8 @@ flowchart LR | Nodes → Output | Validated node set; output commands interpret as needed | | Config → Output | `fieldMap` (reverse) applied by template-sync for file field names | +Validation runs as one pipeline over the whole space. `validateSpace` returns its full result, and `validateFile` returns the part that belongs to one file, so a file's results match its part of a full run. + --- ## Field Remapping diff --git a/src/commands/validate-file.ts b/src/commands/validate-file.ts index b1d2c6a..f14c482 100644 --- a/src/commands/validate-file.ts +++ b/src/commands/validate-file.ts @@ -1,8 +1,9 @@ import { loadConfig } from '../config'; import { judgeForRun } from '../judge/factory'; import { parseRuleFilter } from '../schema/rule-filter'; -import type { FileValidationIssue, FileValidationResult } from '../types'; -import { resolveFileSpace, validateFile } from '../validate'; +import { createSpaceContext } from '../space-context'; +import type { FileValidationIssue, FileValidationResult, ValidateFileOutput } from '../types'; +import { fileNotInSpace, resolveFileSpace, validateResolvedFile } from '../validate'; export { validateFile } from '../validate'; @@ -16,12 +17,18 @@ export async function validateFileCommand( options: { json?: boolean; includeGated?: boolean; semantic?: boolean; judge?: string; ruleFilter?: string[] } = {}, ): Promise { const config = loadConfig(); - const space = resolveFileSpace(filePath, config)?.space; - const result = await validateFile(filePath, config, { - includeGated: options.includeGated, - judge: space && judgeForRun(config, space, options), - ruleFilter: options.ruleFilter && parseRuleFilter(options.ruleFilter), - }); + const resolved = resolveFileSpace(filePath, config); + let result: ValidateFileOutput; + if (resolved) { + const judge = judgeForRun(config, resolved.space, options); + result = await validateResolvedFile(filePath, resolved.label, createSpaceContext(resolved.space.name, config), { + includeGated: options.includeGated, + judge, + ruleFilter: options.ruleFilter && parseRuleFilter(options.ruleFilter), + }); + } else { + result = fileNotInSpace(filePath); + } if (!result.inSpace) { if (options.json) { diff --git a/src/validate.ts b/src/validate.ts index 546c149..451b6d2 100644 --- a/src/validate.ts +++ b/src/validate.ts @@ -188,7 +188,7 @@ type SpaceRuleEvaluation = { /** * Evaluate a space's rules. With a main hierarchy, evaluation cascades: schema, hierarchy * and gating-rule failures (the `seeds`) suppress rule evaluation for the failing node's subtree. - * Shared by `validateSpace` and `validateFile` so both report identical rule results for a node. + * Called by `validateNodes`, the core behind `validateSpace` and `validateFile`, so both report identical rule results for a node. * Semantic rules run only when a `judge` is supplied. */ async function evaluateSpaceRules( @@ -220,104 +220,98 @@ async function evaluateSpaceRules( }); } +/** A node that failed type or schema validation. */ +interface NodeValidationFailure { + node: SpaceNode; + errors: ErrorObject[]; + /** Set when the node's `type` is not one the schema allows (checked before full schema validation). */ + invalidType?: { validTypes: string[] }; +} + +/** The outcome of validating a set of nodes, before either view shapes it. */ +interface SpaceValidationResult { + validCount: number; + nodeFailures: NodeValidationFailure[]; + refErrors: ValidationResult['refErrors']; + duplicateErrors: ValidationResult['duplicateErrors']; + ruleViolations: RuleViolation[]; + hierarchyViolations: GraphViolation[]; + orphans: SpaceNode[]; + unresolvedContentLinks: ValidationResult['unresolvedContentLinks']; + coverageGates: CoverageGate[]; +} + /** - * Run full validation on a space and return a structured result. - * Does not output to console or call process.exit. + * The one validation pipeline: type and schema checks, duplicate titles, reference and hierarchy + * checks, cascade-gated rules, and unresolved content links. Returns every entry; `validateSpace` + * and `validateFile` are views over it, so a file's results match its entry in a full run. + * `judgeTarget` limits judging to one file (and the ancestors that can gate it). */ -export async function validateSpace( +async function validateNodes( context: SpaceContext, - options: ValidateSpaceOptions = {}, -): Promise { + nodes: SpaceNode[], + unresolvedRefs: ReadSpaceResult['unresolvedRefs'], + options: { + includeGated?: boolean; + ruleFilter?: RuleFilter; + judge?: SemanticJudgeProvider; + judgeTarget?: string; + } = {}, +): Promise { const { schema, schemaRefRegistry, schemaValidator } = context; const metadata = schema.metadata; - const readResult = options.readResult ?? (await readSpace(context)); - const { parseIssues } = readResult; - let nodes = readResult.nodes; - - // Depth limiting: evaluate only hierarchy nodes within N breadth-first levels from the - // top. Non-hierarchy nodes are unaffected — depth scoping only applies to the hierarchy DAG. - const skippedByDepth: Array<{ file: string; title: string }> = []; - if (options.depth !== undefined && metadata.hierarchy) { - const fullGraph = buildSpaceGraph(nodes, metadata.hierarchy); - const depths = computeHierarchyDepths(fullGraph); - const included: SpaceNode[] = []; - for (const node of nodes) { - const nodeDepth = depths.get(node.title); - if (nodeDepth !== undefined && nodeDepth > options.depth) { - skippedByDepth.push({ file: node.label, title: node.title }); - } else { - included.push(node); - } - } - nodes = included; - } - // Pre-extract valid types for early type validation const validTypes = Array.isArray(schema.oneOf) ? extractEntityInfo(schema, schemaRefRegistry).map((e) => e.type) : []; - const result: ValidationResult = { + const result: SpaceValidationResult = { validCount: 0, - nodeErrorCount: 0, - nodeErrors: [], + nodeFailures: [], refErrors: [], duplicateErrors: [], ruleViolations: [], hierarchyViolations: [], orphans: [], - parseIssues, unresolvedContentLinks: [], coverageGates: [], - skippedByDepth, - depthLimit: options.depth, }; for (const node of nodes) { // Early type validation - check before full AJV validation to prevent cascading errors const nodeType = (node.schemaData as Record).type as string | undefined; if (nodeType !== undefined && validTypes.length > 0 && !validTypes.includes(nodeType)) { - result.nodeErrorCount++; - result.nodeErrors.push({ - file: node.source?.file ?? node.label, - ...(node.source?.line !== undefined ? { line: node.source.line } : {}), + const sortedTypes = [...validTypes].sort(); + result.nodeFailures.push({ + node, + invalidType: { validTypes }, errors: [ { instancePath: '/type', keyword: 'enum', - message: `Invalid type "${nodeType}". Valid types are: ${validTypes.sort().join(', ')}`, - params: { allowedValues: validTypes }, + message: `Invalid type "${nodeType}". Valid types are: ${sortedTypes.join(', ')}`, + params: { allowedValues: sortedTypes }, schemaPath: '#/oneOf', data: nodeType, } as ErrorObject, ], - nodeData: node.schemaData as Record, }); continue; } - const valid = schemaValidator(node.schemaData); - - if (valid) { + if (schemaValidator(node.schemaData)) { result.validCount++; } else { - result.nodeErrorCount++; - result.nodeErrors.push({ - file: node.source?.file ?? node.label, - ...(node.source?.line !== undefined ? { line: node.source.line } : {}), - errors: schemaValidator.errors || [], - nodeData: node.schemaData as Record, - }); + result.nodeFailures.push({ node, errors: schemaValidator.errors || [] }); } } // Detect duplicate node keys (titles) const titleToFiles = new Map(); for (const node of nodes) { - const title = node.title; - if (!titleToFiles.has(title)) { - titleToFiles.set(title, []); + if (!titleToFiles.has(node.title)) { + titleToFiles.set(node.title, []); } - titleToFiles.get(title)!.push(node.label); + titleToFiles.get(node.title)!.push(node.label); } for (const [title, files] of titleToFiles) { if (files.length > 1) { @@ -326,7 +320,7 @@ export async function validateSpace( } // Validate all hierarchy constraints (field references and structure) - const hierarchyValidation = validateGraph(nodes, metadata, readResult.unresolvedRefs); + const hierarchyValidation = validateGraph(nodes, metadata, unresolvedRefs); result.refErrors.push(...hierarchyValidation.refErrors); result.hierarchyViolations = [...hierarchyValidation.violations]; @@ -338,16 +332,13 @@ export async function validateSpace( // Load and execute rules validation if schema defines rules (with cascade gating, see docs/rules.md). if (metadata.rules) { const seeds: GatingSeed[] = [ - ...result.nodeErrors.map( - (e): GatingSeed => ({ file: e.file, title: String(e.nodeData.title ?? e.file), reason: 'schema' }), - ), + ...result.nodeFailures.map(({ node }): GatingSeed => { + const file = node.source?.file ?? node.label; + return { file, title: String(node.schemaData.title ?? file), reason: 'schema' }; + }), ...result.hierarchyViolations.map((v): GatingSeed => ({ file: v.file, title: v.nodeTitle, reason: 'hierarchy' })), ]; - const { violations, gates } = await evaluateSpaceRules(nodes, metadata, seeds, { - includeGated: options.includeGated, - ruleFilter: options.ruleFilter, - judge: options.judge, - }); + const { violations, gates } = await evaluateSpaceRules(nodes, metadata, seeds, options); result.ruleViolations = violations; result.coverageGates = gates; } @@ -369,6 +360,65 @@ export async function validateSpace( return result; } +/** + * Run full validation on a space and return a structured result. + * Does not output to console or call process.exit. + */ +export async function validateSpace( + context: SpaceContext, + options: ValidateSpaceOptions = {}, +): Promise { + const metadata = context.schema.metadata; + + const readResult = options.readResult ?? (await readSpace(context)); + let nodes = readResult.nodes; + + // Depth limiting: evaluate only hierarchy nodes within N breadth-first levels from the + // top. Non-hierarchy nodes are unaffected — depth scoping only applies to the hierarchy DAG. + const skippedByDepth: Array<{ file: string; title: string }> = []; + if (options.depth !== undefined && metadata.hierarchy) { + const fullGraph = buildSpaceGraph(nodes, metadata.hierarchy); + const depths = computeHierarchyDepths(fullGraph); + const included: SpaceNode[] = []; + for (const node of nodes) { + const nodeDepth = depths.get(node.title); + if (nodeDepth !== undefined && nodeDepth > options.depth) { + skippedByDepth.push({ file: node.label, title: node.title }); + } else { + included.push(node); + } + } + nodes = included; + } + + const result = await validateNodes(context, nodes, readResult.unresolvedRefs, { + includeGated: options.includeGated, + ruleFilter: options.ruleFilter, + judge: options.judge, + }); + + return { + validCount: result.validCount, + nodeErrorCount: result.nodeFailures.length, + nodeErrors: result.nodeFailures.map(({ node, errors }) => ({ + file: node.source?.file ?? node.label, + ...(node.source?.line !== undefined ? { line: node.source.line } : {}), + errors, + nodeData: node.schemaData as Record, + })), + refErrors: result.refErrors, + duplicateErrors: result.duplicateErrors, + ruleViolations: result.ruleViolations, + hierarchyViolations: result.hierarchyViolations, + orphans: result.orphans, + parseIssues: readResult.parseIssues, + unresolvedContentLinks: result.unresolvedContentLinks, + coverageGates: result.coverageGates, + skippedByDepth, + depthLimit: options.depth, + }; +} + /** Find which space a file belongs to by checking directory containment. */ export function resolveFileSpace(filePath: string, config: Config): { space: SpaceConfig; label: string } | null { const absFile = isAbsolute(filePath) ? filePath : resolve(process.cwd(), filePath); @@ -395,177 +445,153 @@ export interface ValidateFileOptions { } /** - * Validate a single file within its space. - * - * Reads the full space (required for graph correctness) but filters all reported - * errors to only those attributable to the target file. - * - * Returns a result object — does not output to console or call process.exit. - * If the file is not in any configured space, returns a result with `inSpace: false`. + * Select one file's entries from a space's validation result: issues keyed and formatted, errors split from warnings. + * Pure — a file's entries are exactly its slice of the result. */ -export async function validateFile( - filePath: string, - config: Config, - options: ValidateFileOptions = {}, -): Promise { - const resolved = resolveFileSpace(filePath, config); - - if (!resolved) { - return { - file: filePath, - inSpace: false, - message: 'File does not belong to any configured space.', - } satisfies FileNotInSpaceResult; - } - - const { space, label } = resolved; - const context = createSpaceContext(space.name, config, { configDir: options.configDir }); - const { schema, schemaRefRegistry, schemaValidator } = context; - const metadata = schema.metadata; - - const readResult = await readSpace(context); - const { nodes } = readResult; - - // Pre-extract valid types for early type validation - const validTypes = Array.isArray(schema.oneOf) ? extractEntityInfo(schema, schemaRefRegistry).map((e) => e.type) : []; - +function issuesForFile( + result: SpaceValidationResult, + label: string, + context: SpaceContext, + includeGated: boolean | undefined, +): { errors: Record; warnings: Record } { + const { schema, schemaRefRegistry } = context; const errors: Record = {}; const warnings: Record = {}; - // Schema validation errors for this node - for (const node of nodes) { + for (const { node, errors: ajvErrors, invalidType } of result.nodeFailures) { if (node.label !== label) continue; - - // Early type validation - check before full AJV validation to prevent cascading errors - const nodeType = (node.schemaData as Record).type as string | undefined; - if (nodeType !== undefined && validTypes.length > 0 && !validTypes.includes(nodeType)) { - errors[`schema:type:${validTypes.join(',')}`] = { + const position = { + ...(node.source?.line !== undefined ? { line: node.source.line } : {}), + ...(node.source?.column !== undefined ? { column: node.source.column } : {}), + }; + if (invalidType) { + errors[`schema:type:${invalidType.validTypes.join(',')}`] = { kind: 'schema', - message: `Invalid type "${nodeType}". Valid types are: ${validTypes.sort().join(', ')}`, - ...(node.source?.line !== undefined ? { line: node.source.line } : {}), - ...(node.source?.column !== undefined ? { column: node.source.column } : {}), + message: ajvErrors[0]!.message!, + ...position, }; continue; } - - const valid = schemaValidator(node.schemaData); - if (!valid) { - const formatted = formatErrors( - schemaValidator.errors ?? [], - schema, - schemaRefRegistry, - node.schemaData as Record, - ); - for (const { message, dedupeKey } of formatted) { - errors[`schema:${dedupeKey}`] = { - kind: 'schema', - message, - ...(node.source?.line !== undefined ? { line: node.source.line } : {}), - ...(node.source?.column !== undefined ? { column: node.source.column } : {}), - }; - } + const formatted = formatErrors(ajvErrors, schema, schemaRefRegistry, node.schemaData as Record); + for (const { message, dedupeKey } of formatted) { + errors[`schema:${dedupeKey}`] = { kind: 'schema', message, ...position }; } } // Duplicate key errors — include if this file is one of the duplicates - const titleToFiles = new Map(); - for (const node of nodes) { - if (!titleToFiles.has(node.title)) titleToFiles.set(node.title, []); - titleToFiles.get(node.title)!.push(node.label); - } - for (const [title, files] of titleToFiles) { - if (files.length > 1 && files.includes(label)) { - const others = files.filter((f) => f !== label); - errors[`duplicate:${title}`] = { - kind: 'duplicate', - message: `Duplicate title "${title}" also exists in: ${others.join(', ')}`, - }; - } + for (const { title, files } of result.duplicateErrors) { + if (!files.includes(label)) continue; + const others = files.filter((f) => f !== label); + errors[`duplicate:${title}`] = { + kind: 'duplicate', + message: `Duplicate title "${title}" also exists in: ${others.join(', ')}`, + }; } // Broken links and hierarchy violations from this file - const hierarchyValidation = validateGraph(nodes, metadata, readResult.unresolvedRefs); - for (const { file, parent, error } of hierarchyValidation.refErrors) { + for (const { file, parent, error } of result.refErrors) { if (file === label) { errors[`broken-link:${parent}`] = { kind: 'broken-link', message: `${parent} → ${error}` }; } } - for (const v of hierarchyValidation.violations) { + for (const v of result.hierarchyViolations) { if (v.file === label) { errors[`hierarchy:${v.description}`] = { kind: 'hierarchy', message: v.description }; } } - // Rule violations for this node — only error severity counts as an error. - // Rules are evaluated for the whole space with the same cascade gating as `validateSpace`, then - // restricted to this file, so a file's rule results match its entry in a full `validate` run. - if (metadata.rules) { - const seeds: GatingSeed[] = [ - ...nodes - .filter((node) => { - const nodeType = (node.schemaData as Record).type as string | undefined; - const invalidType = nodeType !== undefined && validTypes.length > 0 && !validTypes.includes(nodeType); - return invalidType || !schemaValidator(node.schemaData); - }) - .map((node): GatingSeed => { - const file = node.source?.file ?? node.label; - return { file, title: String(node.schemaData.title ?? file), reason: 'schema' }; - }), - ...hierarchyValidation.violations.map( - (v): GatingSeed => ({ file: v.file, title: v.nodeTitle, reason: 'hierarchy' }), - ), - ]; - const { violations, gates } = await evaluateSpaceRules(nodes, metadata, seeds, { - includeGated: options.includeGated, - ruleFilter: options.ruleFilter, - judge: options.judge, - judgeTarget: label, - }); - for (const v of violations) { - if (v.file === label) { - const target = v.severity === 'error' ? errors : warnings; - target[`rule:${v.ruleId}`] = { - kind: 'rule', - message: ruleViolationMessage(v), - severity: v.severity, - }; - } - } - // A gated file with no violations would otherwise look clean — say that its rules were not checked. - for (const gate of gates) { - if (!gate.gatedFiles.includes(label)) continue; - const cause = gate.reason === 'rule' ? `blocking rule "${gate.ruleId}"` : `${gate.reason} failure`; - warnings[`gated:${gate.file}`] = { - kind: 'gated', - message: `Rule checks ${options.includeGated ? 'evaluated anyway' : 'not evaluated'}: gated by ${cause} on ${gate.file}`, - severity: 'info', + // Rule violations — only error severity counts as an error. + for (const v of result.ruleViolations) { + if (v.file === label) { + const target = v.severity === 'error' ? errors : warnings; + target[`rule:${v.ruleId}`] = { + kind: 'rule', + message: ruleViolationMessage(v), + severity: v.severity, }; } } + // A gated file with no violations would otherwise look clean — say that its rules were not checked. + for (const gate of result.coverageGates) { + if (!gate.gatedFiles.includes(label)) continue; + const cause = gate.reason === 'rule' ? `blocking rule "${gate.ruleId}"` : `${gate.reason} failure`; + warnings[`gated:${gate.file}`] = { + kind: 'gated', + message: `Rule checks ${includeGated ? 'evaluated anyway' : 'not evaluated'}: gated by ${cause} on ${gate.file}`, + severity: 'info', + }; + } - // Unresolved content links for this node (warnings — do not affect errorCount) - const targetNode = nodes.find((n) => n.label === label); - if (targetNode) { - const seen = new Set(); - for (const link of targetNode.resolvedLinks) { - if (link.location === 'internal' && !seen.has(link.target)) { - seen.add(link.target); - warnings[`content-link:${link.target}`] = { - kind: 'content-link', - message: `Unresolved content link: ${link.target}`, - }; - } + // Unresolved content links (warnings — do not affect errorCount) + for (const link of result.unresolvedContentLinks) { + if (link.file === label) { + warnings[`content-link:${link.target}`] = { + kind: 'content-link', + message: `Unresolved content link: ${link.target}`, + }; } } + return { errors, warnings }; +} + +/** The result for a file that belongs to no configured space. */ +export function fileNotInSpace(filePath: string): FileNotInSpaceResult { + return { + file: filePath, + inSpace: false, + message: 'File does not belong to any configured space.', + }; +} + +/** + * Validate a file in an already-resolved space. Checks the whole space (required for graph + * correctness), judging only the file, then selects the file's issues from the result. + */ +export async function validateResolvedFile( + filePath: string, + label: string, + context: SpaceContext, + options: Omit = {}, +): Promise { + const readResult = await readSpace(context); + const result = await validateNodes(context, readResult.nodes, readResult.unresolvedRefs, { + includeGated: options.includeGated, + ruleFilter: options.ruleFilter, + judge: options.judge, + judgeTarget: label, + }); + const { errors, warnings } = issuesForFile(result, label, context, options.includeGated); + return { file: isAbsolute(filePath) ? filePath : resolve(process.cwd(), filePath), label, - space: space.name, + space: context.space.name, errors, errorCount: Object.keys(errors).length, warnings, warningCount: Object.keys(warnings).length, inSpace: true, - } satisfies FileValidationResult; + }; +} + +/** + * Validate a single file within its space. + * + * Reads the full space (required for graph correctness) but filters all reported + * errors to only those attributable to the target file. + * + * Returns a result object — does not output to console or call process.exit. + * If the file is not in any configured space, returns a result with `inSpace: false`. + */ +export async function validateFile( + filePath: string, + config: Config, + options: ValidateFileOptions = {}, +): Promise { + const resolved = resolveFileSpace(filePath, config); + if (!resolved) return fileNotInSpace(filePath); + + const context = createSpaceContext(resolved.space.name, config, { configDir: options.configDir }); + return validateResolvedFile(filePath, resolved.label, context, options); } diff --git a/tests/validate/validate-views.test.ts b/tests/validate/validate-views.test.ts new file mode 100644 index 0000000..94fbe02 --- /dev/null +++ b/tests/validate/validate-views.test.ts @@ -0,0 +1,153 @@ +import { afterAll, describe, expect, it } from 'bun:test'; +import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import { join } from 'node:path'; +import type { Config } from '../../src/config'; +import { FakeJudgeProvider } from '../../src/judge/provider/fake'; +import { bundledSchemasDir } from '../../src/schema/schema'; +import { createSpaceContext } from '../../src/space-context'; +import type { FileValidationResult } from '../../src/types'; +import { formatErrors, validateFile, validateSpace } from '../../src/validate'; + +const META = + 'https://raw.githubusercontent.com/mindsocket/structured-context/main/schemas/generated/latest/_structured_context_schema_meta.json'; +const STRICT_SCHEMA_PATH = join(bundledSchemasDir, 'strict_ost.json'); + +/** An invalid `type` is keyed differently in the two views (the view lists the types in schema order). */ +const normaliseKey = (key: string) => + key.startsWith('schema:type:') || key.startsWith('schema:/type:enum:') ? 'schema:invalid-type' : key; + +describe('validateFile is a view over the validateSpace core', () => { + const config: Config = { + spaces: [ + { name: 'invalid', path: join(import.meta.dir, '../fixtures/strict_ost/invalid'), schema: STRICT_SCHEMA_PATH }, + ], + }; + const context = createSpaceContext('invalid', config, { configDir: import.meta.dir }); + + it('reports for each file exactly the issues validateSpace reports for it', async () => { + const space = await validateSpace(context); + const files = new Set([ + ...space.nodeErrors.map((e) => e.file), + ...space.refErrors.map((e) => e.file), + ...space.duplicateErrors.flatMap((e) => e.files), + ...space.ruleViolations.map((v) => v.file), + ...space.hierarchyViolations.map((v) => v.file), + ...space.unresolvedContentLinks.map((l) => l.file), + ...space.coverageGates.flatMap((g) => g.gatedFiles), + ]); + files.delete(''); + expect(files.size).toBeGreaterThan(1); + + for (const file of files) { + const expected = [ + ...space.nodeErrors + .filter((e) => e.file === file) + .flatMap((e) => + formatErrors(e.errors, context.schema, context.schemaRefRegistry, e.nodeData).map( + (f) => `schema:${f.dedupeKey}`, + ), + ), + ...space.duplicateErrors.filter((d) => d.files.includes(file)).map((d) => `duplicate:${d.title}`), + ...space.refErrors.filter((r) => r.file === file).map((r) => `broken-link:${r.parent}`), + ...space.hierarchyViolations.filter((v) => v.file === file).map((v) => `hierarchy:${v.description}`), + ...space.ruleViolations.filter((v) => v.file === file).map((v) => `rule:${v.ruleId}`), + ...space.coverageGates.filter((g) => g.gatedFiles.includes(file)).map((g) => `gated:${g.file}`), + ...space.unresolvedContentLinks.filter((l) => l.file === file).map((l) => `content-link:${l.target}`), + ].map(normaliseKey); + + const result = (await validateFile(join(context.space.path, file), config, { + configDir: import.meta.dir, + })) as FileValidationResult; + const actual = Object.keys({ ...result.errors, ...result.warnings }).map(normaliseKey); + expect(actual.sort()).toEqual([...new Set(expected)].sort()); + } + }); +}); + +describe('judging a node that fails an expression rule', () => { + const dirs: string[] = []; + afterAll(() => { + for (const dir of dirs) rmSync(dir, { recursive: true, force: true }); + }); + + /** One file `A.md` of type `item` with a failing expression rule and a semantic rule. */ + function setup(hierarchy: boolean) { + const dir = mkdtempSync(join(tmpdir(), 'sctx-views-')); + dirs.push(dir); + const schemaPath = join(dir, 'schema.json'); + const rules = [ + { + id: 'always-fails', + type: 'item', + check: 'false', + category: 'validation', + severity: 'error', + description: 'always fails', + }, + { + id: 'sound', + type: 'item', + mode: 'semantic', + category: 'validation', + severity: 'warning', + description: 'sound', + }, + ]; + writeFileSync( + schemaPath, + JSON.stringify({ + $schema: META, + $id: 'sctx://views.test', + $metadata: { ...(hierarchy && { hierarchy: { levels: ['item'] } }), rules }, + oneOf: [ + { + type: 'object', + properties: { type: { const: 'item' }, title: { type: 'string' } }, + required: ['type', 'title'], + }, + ], + }), + ); + const space = join(dir, 'space'); + mkdirSync(space); + writeFileSync(join(space, 'A.md'), '---\ntype: item\n---\n'); + const config: Config = { spaces: [{ name: 'views', path: space, schema: schemaPath }] }; + return { config, dir, file: join(space, 'A.md') }; + } + + const judgedRules = (judge: FakeJudgeProvider) => judge.callHistory.flatMap((c) => c.rules.map((r) => r.id)); + + it('skips the node semantic rules in a space with no hierarchy', async () => { + const { config, dir, file } = setup(false); + const judge = new FakeJudgeProvider({ pass: true }); + const result = (await validateFile(file, config, { judge, configDir: dir })) as FileValidationResult; + expect(result.errors['rule:always-fails']).toBeDefined(); + expect(judgedRules(judge)).toEqual([]); + }); + + it('still judges the node semantic rules in a space with a hierarchy', async () => { + const { config, dir, file } = setup(true); + const judge = new FakeJudgeProvider({ pass: true }); + const result = (await validateFile(file, config, { judge, configDir: dir })) as FileValidationResult; + expect(result.errors['rule:always-fails']).toBeDefined(); + expect(judgedRules(judge)).toEqual(['sound']); + }); +}); + +describe('invalid node type', () => { + const config: Config = { + spaces: [ + { name: 'invalid', path: join(import.meta.dir, '../fixtures/strict_ost/invalid'), schema: STRICT_SCHEMA_PATH }, + ], + }; + const context = createSpaceContext('invalid', config, { configDir: import.meta.dir }); + + it('lists the allowed types alphabetically in the message and in allowedValues', async () => { + const { nodeErrors } = await validateSpace(context); + const [error] = nodeErrors.find((e) => e.nodeData.type === 'vision')?.errors ?? []; + const sorted = ['assumption_test', 'opportunity', 'outcome', 'solution']; + expect(error?.params.allowedValues).toEqual(sorted); + expect(error?.message).toEndWith(`Valid types are: ${sorted.join(', ')}`); + }); +});