File size: 1,890 Bytes
af1bee9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Single shared place for case-ID and tag parsing (classifier, report, stand-in detection, tag parsing).



Case IDs are module-neutral: <MODULE>-<AREA>[-<PART>...]-<NUMBER>, e.g. P2P-INV-006, O2C-ORD-012, R2R-JE-3,

P2P-E2E-001, plus TC-<anything> (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"(?<![A-Za-z0-9_-])(?:[A-Z][A-Z0-9]{1,5}(?:-[A-Z0-9]+)+-\d+|TC[-_][A-Za-z0-9]+(?:[-_][A-Za-z0-9]+)*)"
                   r"(?![A-Za-z0-9_])")
CASE_ID_RE = re.compile(CASE_ID_PATTERN)
# Stand-in (substitute) markers: @stand-in, @standin, @substitute, or "stand-in" in a title.
STANDIN_RE = re.compile(r"@?(?:stand[-_ ]?in|substitute)\b", re.I)


def find_case_ids(text: str) -> 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})