import * as fs from 'fs'; import * as path from 'path'; import YAML from 'yaml'; /** * Reads Fusion Test Designer cases for ANY module. Layouts supported (both at once): * ../tests/cases/.yaml (flat; P2P lives here) * ../tests/cases//.yaml (per module; e.g. o2c/, r2r/) * Case IDs are --NNN (e.g. P2P-INV-001, O2C-AR-001, R2R-GL-001). The module comes from the YAML `module` * field, falling back to the ID prefix. Priority comes ONLY from each YAML's `priority` field, so re-scoring a case in * YAML changes whether its spec runs (see playwright.config.ts) without code edits. */ export const CASES_DIR = path.resolve(__dirname, '../../tests/cases'); /** Any module prefix: 2-6 upper-case letters/digits, an area, a number. */ export const CASE_ID_RE = /^([A-Z][A-Z0-9]{1,5})-([A-Z0-9]+)-(\d+)$/; export type CaseYaml = { id: string; title: string; priority: string; module?: string; test_values?: Record; stand_in?: string; [k: string]: any }; export type CaseInfo = CaseYaml & { file: string; moduleId: string }; function yamlFiles(dir: string): string[] { if (!fs.existsSync(dir)) return []; return fs.readdirSync(dir, { withFileTypes: true }).sort((a, b) => a.name.localeCompare(b.name)).flatMap((e) => { const p = path.join(dir, e.name); if (e.isDirectory()) return e.name.startsWith('.') || e.name.startsWith('_') ? [] : yamlFiles(p); return /\.ya?ml$/.test(e.name) ? [p] : []; }); } export function moduleOf(c: { id: string; module?: string }): string { const m = String(c.module || '').trim().toUpperCase(); return m || (CASE_ID_RE.exec(c.id) || [])[1] || 'UNKNOWN'; } let cache: Map | null = null; export function allCases() { if (cache) return cache; cache = new Map(); for (const f of yamlFiles(CASES_DIR)) { const c = YAML.parse(fs.readFileSync(f, 'utf8')); if (!c || typeof c.id !== 'string' || !CASE_ID_RE.test(c.id)) continue; if (cache.has(c.id)) throw new Error(`Duplicate case id ${c.id}: ${cache.get(c.id)!.file} and ${path.relative(CASES_DIR, f)}`); cache.set(c.id, { ...c, file: path.relative(CASES_DIR, f), moduleId: moduleOf(c) }); } return cache; } export function caseYaml(id: string): CaseInfo { const c = allCases().get(id); if (!c) throw new Error(`Case ${id} not found under ${CASES_DIR}`); return c; } const list = (v: string | undefined, dflt: string) => (v ?? dflt).split(',').map((s) => s.trim().toUpperCase()).filter(Boolean); /** Tiers to execute. Default P1,P2 (P3/P4 deferred by team decision 9 Oct 2026). Override: RUN_TIERS=P1 */ export function runTiers(): string[] { return list(process.env.RUN_TIERS, 'P1,P2'); } /** Modules to execute (e.g. RUN_MODULES=O2C or P2P,O2C). Empty/unset = all modules. */ export function runModules(): string[] { return list(process.env.RUN_MODULES, ''); } /** * Lifecycle class (tests/README.md; same precedence as qa/audit.py case_class): retired > held > tolerant. * retired (status: retired) -> never selected ("do not run"). * held (execution_hold: ...) -> out of the go/no-go suite (like qa/audit.py). Scripted held cases run only in the separate * held suite (RUN_SUITE=held), MOCK only, and only for modules in HELD_RUN_MODULES (default HCM; * CX is scripted but never run, per steering 9 Oct 2026). * tolerant (suite: tolerant or go_no_go: false) -> runs only in the separate tolerant suite (RUN_SUITE=tolerant), never in go/no-go results. */ export type Lifecycle = 'retired' | 'held' | 'tolerant' | null; export function lifecycle(c: CaseYaml): Lifecycle { if (String(c.status ?? '').toLowerCase() === 'retired') return 'retired'; if (String(c.execution_hold ?? '').trim()) return 'held'; if (String(c.suite ?? '').toLowerCase() === 'tolerant' || ['false', 'no', '0'].includes(String(c.go_no_go ?? '').toLowerCase())) return 'tolerant'; return null; } export type Suite = 'gate' | 'tolerant' | 'held'; /** Suite to execute: 'gate' (default; go/no-go results), 'tolerant' (AI/probabilistic, reported separately) or 'held' (execution_hold; MOCK only). */ export function runSuite(): Suite { const s = String(process.env.RUN_SUITE || 'gate').toLowerCase(); return s === 'tolerant' || s === 'held' ? s : 'gate'; } /** Modules whose held cases may run against the MOCK in the held suite (never against a pod). */ export function heldRunModules(): string[] { return list(process.env.HELD_RUN_MODULES, 'HCM'); } /** * Chief of Staff ruling 9 Oct 2026 (overrides QA's HCM pause): HCM and CX may run on the MOCK with synthetic data, outside the gate, * but NEVER against a pod until test-pod masking is confirmed (HCM-Q02 / CX-Q02). Guard: these modules (and every execution_hold case) * are deselected unless the target base URL is the local mock (loopback host). Not overridable by env. */ export const POD_BLOCKED_MODULES = ['HCM', 'CX']; export function targetBaseUrl(): string { return process.env.BASE_URL || `http://127.0.0.1:${Number(process.env.MOCK_PORT || 3100)}`; } export function isMockTarget(url = targetBaseUrl()): boolean { try { const h = new URL(url).hostname.replace(/^\[|\]$/g, '').toLowerCase(); return h === '127.0.0.1' || h === 'localhost' || h === '::1'; } catch { return false; } } export function selected(c: CaseInfo, tiers = runTiers(), modules = runModules(), suite: Suite = runSuite()): boolean { const lc = lifecycle(c); if (lc === 'retired') return false; if ((POD_BLOCKED_MODULES.includes(c.moduleId) || lc === 'held') && !isMockTarget()) return false; // never against a pod if ((lc === 'tolerant') !== (suite === 'tolerant')) return false; if ((lc === 'held') !== (suite === 'held')) return false; if (lc === 'held' && !heldRunModules().includes(c.moduleId)) return false; return tiers.includes(String(c.priority).toUpperCase()) && (!modules.length || modules.includes(c.moduleId)); } /** Tags derived from YAML: @, @, @stand-in, @tolerant, @execution-hold. The @ tag is written literally in each spec. */ export function caseTags(id: string): string[] { const c = caseYaml(id); const lc = lifecycle(c); return [`@${c.moduleId}`, `@${c.priority}`, ...(c.stand_in ? ['@stand-in'] : []), ...(lc === 'tolerant' ? ['@tolerant'] : []), ...(lc === 'held' ? ['@execution-hold'] : [])]; } export function tv(id: string): Record { return caseYaml(id).test_values || {}; } export function caseTitle(id: string): string { return caseYaml(id).title; }