"""Single shared place for case-ID and tag parsing (classifier, report, stand-in detection, tag parsing). Case IDs are module-neutral: -[-...]-, e.g. P2P-INV-006, O2C-ORD-012, R2R-JE-3, P2P-E2E-001, plus TC- (e.g. TC-001, TC_01). MODULE is 2-6 chars, starts with a letter (pattern [A-Z][A-Z0-9]{1,5}). """ from __future__ import annotations import re # Not preceded/followed by an ID character, so 'XP2P-INV-001' or 'P2P-INV-001X' do not match partially. CASE_ID_PATTERN = (r"(? list[str]: """All case IDs in text, in order, de-duplicated.""" return list(dict.fromkeys(m.group(0) for m in CASE_ID_RE.finditer(str(text or "")))) def first_case_id(text: str) -> str | None: ids = find_case_ids(text) return ids[0] if ids else None def tag_case_id(tag: str) -> str | None: """Case ID from a Playwright tag ('@P2P-INV-006' or 'O2C-ORD-001'); None if the tag is not an ID.""" t = str(tag or "").strip().lstrip("@") m = CASE_ID_RE.fullmatch(t) return m.group(0) if m else None def is_standin(text: str) -> bool: return bool(STANDIN_RE.search(str(text or ""))) def module_of(case_id: str) -> str | None: """'P2P-INV-006' -> 'P2P'; TC-style ids have no module -> None.""" if not case_id or re.match(r"TC[-_]", case_id): return None return case_id.split("-", 1)[0] def modules(case_ids) -> list[str]: return sorted({m for m in (module_of(c) for c in case_ids) if m})