From 561e4cee6bc8768e75b27ef63f1b1661cb155d77 Mon Sep 17 00:00:00 2001 From: Nikolay Petrov Date: Sat, 5 Dec 2026 22:21:14 -0800 Subject: [PATCH] test(cli): ratchet that every leaf is wrapped by the --json envelope A leaf is wrapped when its parser sets emits_cli_output. The new test fails on an unwrapped leaf that is not listed in tests/data/cli_emits_unwrapped.txt, and on a listed leaf that is now wrapped or gone. The list holds the 76 leaves unwrapped on main; no behaviour change. Signed-off-by: Nikolay Petrov --- dokimi/tests/data/cli_emits_unwrapped.txt | 86 +++++++++++++++++++++++ dokimi/tests/test_cli_emits_ratchet.py | 71 +++++++++++++++++++ 2 files changed, 157 insertions(+) create mode 100644 dokimi/tests/data/cli_emits_unwrapped.txt create mode 100644 dokimi/tests/test_cli_emits_ratchet.py diff --git a/dokimi/tests/data/cli_emits_unwrapped.txt b/dokimi/tests/data/cli_emits_unwrapped.txt new file mode 100644 index 000000000..16a9f2896 --- /dev/null +++ b/dokimi/tests/data/cli_emits_unwrapped.txt @@ -0,0 +1,86 @@ +# Copyright (C) 2026 Intel Corporation +# SPDX-License-Identifier: Apache-2.0 + +# Ratchet for the --json envelope wrap (tests/test_cli_emits_ratchet.py, issue #94 F2). +# +# One leaf per row, as its command words: `eval personas discover`. A leaf listed here has no +# `emits_cli_output` default on its parser yet (cli_output.v1 is not guaranteed for its --json). +# The list only shrinks: a row for a leaf that is now wrapped, or no longer a leaf, fails the test, +# which names the row to delete. A leaf that is not listed must be wrapped, so a new command +# opts in with `set_defaults(emits_cli_output=True, cli_kind=...)` or adds no row to pass. +answers generate +arms matrix-run +arms plugin-delta +arms run +baseline compare +baseline list +baseline save +benchmark batch +benchmark preflight +benchmark run +catalog intents +catalog list +catalog novelty +catalog provenance +catalog repair-receipt +catalog routing +catalog routing-compare +catalog show +catalog trigger-probe +dashboard generate +data export +data import docling +data schema list +data schema show +docs check +docs compare +eval headroom +eval import +eval judge agreement +eval judge validate +eval judge wording +eval leak +eval native-import +eval personas validate +eval questions analyze +eval questions generate +eval questions refine +eval questions review +eval redteam +eval score checklist +eval score grounding +eval score pairwise +eval score panel +eval score pointwise +eval score ragas +eval score reference +eval tasks generate +eval tasks list +eval triggers generate +evaluate +explore run +repo hits sheet +repo reach +repo run +repo security +repo skill-reach +report eval +report generate +report model-compare +report opportunities +report summary +runs export +skill check +skill novelty analyze +skill novelty atoms +skill novelty ground +skill novelty items +skill novelty probe +skill novelty retest +skill novelty view +skill sensitivity +skill shorten +subjects evidence +subjects run +tasks harnesses +tasks run diff --git a/dokimi/tests/test_cli_emits_ratchet.py b/dokimi/tests/test_cli_emits_ratchet.py new file mode 100644 index 000000000..66ae09e6b --- /dev/null +++ b/dokimi/tests/test_cli_emits_ratchet.py @@ -0,0 +1,71 @@ +# Copyright (C) 2026 Intel Corporation +# SPDX-License-Identifier: Apache-2.0 + +"""Every CLI leaf is wrapped by the --json envelope, or listed as not yet (issue #94 F2). + +A leaf is wrapped when its parser sets ``emits_cli_output`` (``set_defaults``); the envelope of a +--json run of it is then cli_output.v1 on every exit. ``tests/data/cli_emits_unwrapped.txt`` lists +the leaves that are not wrapped today. The list only shrinks: a new unlisted unwrapped leaf fails, +and a listed leaf that is now wrapped (or gone) fails until its row is deleted. +""" + +from __future__ import annotations + +import argparse +from pathlib import Path + +from dokimi import cli + +ALLOWLIST = Path(__file__).parent / "data" / "cli_emits_unwrapped.txt" + + +def _leaves(parser: argparse.ArgumentParser, prefix: tuple[str, ...] = ()): + """Every leaf parser under ``parser`` as (path, parser); an alias is the same parser twice.""" + sub = next((a for a in parser._actions if isinstance(a, argparse._SubParsersAction)), None) + if sub is None: + yield prefix, parser + return + seen: set[int] = set() + for name, child in sub.choices.items(): + if id(child) in seen: + continue + seen.add(id(child)) + yield from _leaves(child, (*prefix, name)) + + +def _wrapped_and_not() -> tuple[set[str], set[str]]: + wrapped, unwrapped = set(), set() + for path, parser in _leaves(cli.build_parser()): + (wrapped if parser.get_default("emits_cli_output") else unwrapped).add(" ".join(path)) + return wrapped, unwrapped + + +def _listed() -> list[str]: + rows = (line.split("#", 1)[0].strip() for line in ALLOWLIST.read_text(encoding="utf-8").splitlines()) + return [row for row in rows if row] + + +def test_every_leaf_is_wrapped_or_listed_as_not_yet(): + _, unwrapped = _wrapped_and_not() + new = sorted(unwrapped - set(_listed())) + assert not new, ( + "these leaves have no emits_cli_output default and are not in tests/data/cli_emits_unwrapped.txt:\n " + + "\n ".join(new) + + "\nWrap each with set_defaults(emits_cli_output=True, cli_kind=...) and no row is needed." + ) + + +def test_a_listed_leaf_is_still_unwrapped(): + wrapped, unwrapped = _wrapped_and_not() + listed = _listed() + now_wrapped = sorted(row for row in listed if row in wrapped) + gone = sorted(row for row in listed if row not in wrapped and row not in unwrapped) + assert not (now_wrapped or gone), ( + "delete these rows from tests/data/cli_emits_unwrapped.txt (wrapped now, or no such leaf):\n " + + "\n ".join([*now_wrapped, *gone]) + ) + + +def test_the_list_has_no_duplicate_rows(): + listed = _listed() + assert sorted({row for row in listed if listed.count(row) > 1}) == []