core/scripts/dump-enums.py
Shay e89463a975
feat(workbench-ui): design system v1 scaffold (ADR-0162 Branch 1) (#295)
* feat(workbench-ui): design system v1 scaffold

* fix(workbench): close R1 (GroundingSource enum coverage) + R4 (digest test)

R1 — Promote GroundingSource to a typed Literal in core/epistemic_state.py
so it has the same single-source-of-truth shape as ReviewState.  The
existing epistemic_state_for_grounding_source() function already
enumerates the six labels (pack, teaching, vault, partial, oov, none);
this codifies them.

scripts/dump-enums.py now snapshots GroundingSource via the existing
literal_values helper.  workbench-ui's enumCoverage.test.ts gains a
fourth assertion that the badge mapping matches the Python source
1:1.  Adding a grounding-source value on the Python side without
updating the badge fails the build-time test loud — same discipline
as the other three enums.

R4 — Add an explicit DigestBadge test to StableJsonViewer.test.tsx:
asserts the badge text matches the SHA-256 prefix of the source bytes,
and clicking the badge copies the FULL digest (not the truncated
prefix).  Recomputes the expected digest via crypto.subtle to avoid
hard-coding a hex string that could drift.

R2 (component-level reduced-motion enforcement), R3 (EmptyState
copy-CLI affordance), and R5 (`uv run core` packaging paper cut) are
deferred — R2/R3 become meaningful with W-027/W-029, R5 is a
packaging-layer concern outside this PR's scope.

Validation:
- pnpm test: 19 passed (was 17, +1 enum coverage, +1 digest test)
- pnpm build: clean
- pnpm test:enum-coverage: 4 passed
- core test --suite smoke -q: 67 passed
2026-05-26 11:33:27 -07:00

60 lines
2.2 KiB
Python

"""Dump ratified engine enum values for Workbench UI coverage tests.
Read-only helper: parses source with Python AST and writes JSON to stdout.
"""
from __future__ import annotations
import ast
import json
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
def enum_values(path: Path, class_name: str) -> list[str]:
module = ast.parse(path.read_text(encoding="utf-8"))
for node in module.body:
if isinstance(node, ast.ClassDef) and node.name == class_name:
values: list[str] = []
for statement in node.body:
if (
isinstance(statement, ast.Assign)
and isinstance(statement.value, ast.Constant)
and isinstance(statement.value.value, str)
):
values.append(statement.value.value)
return values
raise SystemExit(f"enum class not found: {class_name}")
def literal_values(path: Path, name: str) -> list[str]:
module = ast.parse(path.read_text(encoding="utf-8"))
for node in module.body:
if isinstance(node, ast.Assign):
if not any(isinstance(target, ast.Name) and target.id == name for target in node.targets):
continue
value = node.value
if (
isinstance(value, ast.Subscript)
and isinstance(value.value, ast.Name)
and value.value.id == "Literal"
):
items = value.slice.elts if isinstance(value.slice, ast.Tuple) else [value.slice]
return [
item.value
for item in items
if isinstance(item, ast.Constant) and isinstance(item.value, str)
]
raise SystemExit(f"literal alias not found: {name}")
snapshot = {
"EpistemicState": enum_values(ROOT / "core" / "epistemic_state.py", "EpistemicState"),
"GroundingSource": literal_values(ROOT / "core" / "epistemic_state.py", "GroundingSource"),
"NormativeClearance": enum_values(ROOT / "core" / "epistemic_state.py", "NormativeClearance"),
"ReviewState": literal_values(ROOT / "teaching" / "proposals.py", "ReviewState"),
}
print(json.dumps(snapshot, indent=2, sort_keys=True))