test(cli): add subprocess contract tests for train/encode/decode#33
Merged
Conversation
bpetite workflows
PR #33: tracked workflows are still running. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes Task 4-2 by adding 12 subprocess-level contract tests that pin the
bpetiteCLI to its stdout/stderr boundary, its exact JSON summary shape, and every documented failure mode.Why
Task 4-1 shipped the CLI behind a single deterministic contract (FR-33/FR-34 channel discipline, exact
trainsummary keys,decoderaw-text semantics, typed exits), but nothing enforced it at the subprocess boundary. In-process tests collapse the stdout/stderr split, so a regression like a strayprint()on stdout or a missing non-zero exit on an error path would slip through. These tests drive the real installedbpetiteentry point throughsubprocess.run, so every assertion witnesses the same surface a user or CI smoke test would see.Changes
tests/test_cli.py— 12 new tests covering:trainhappy path (JSON summary shape pinned to exact required key set;corpus_bytestied to the fixture's on-disk size; JSON absent from stderr on all five key substrings plus the fulljson.dumpsstring, so a double-write regression is caught)trainprogress lifecycle ("Training started: planned="and"Training complete: merges="— substrings that only the_train_with_progress._on_eventconsole.printlines produce, not the panel titles, so deleting either branch fails the test)trainevery-100-merges branch (newprogress_corpus_pathsession fixture generates a deterministic ~15 KB synthetic corpus via fixed-seedrandom.Random(0xBADC0FFEE); with--vocab-size 480the run plans 224 merges and empirically fires 2 merge events, asserted via"Training merges:"on stderr and absent from stdout)trainfailure modes: nonexistent input, invalid UTF-8 input (reusinginvalid_utf8.bin), save without--forceto existing path, save with--forceoverwrites cleanlyencodehappy path (compact JSON array viajson.dumps(ids, separators=(",", ":"))equality, no spaces, stderr empty under non-interactive subprocess)decodehappy path (raw text bytes, stderr empty; roundtrip of"Hello, world!"through encode → decode)decodefailure modes: unknown token ID ([999999]), invalid UTF-8 byte sequence ([128], lone continuation byte), missing model filetests/conftest.py— addedtiny_corpus_pathsession fixture (CLI subprocess tests need the actual file path, not the decoded string already exposed bytiny_corpus)Validation
uv run pytest— 192 passed (12 new, no regressions)uv run ruff check .— cleanuv run ruff format --check .— cleanuv run mypy --strict— cleanManual checks:
bpetiteentry point viaPath(sys.executable).parent / "bpetite"rather thanuv run bpetite, so each test avoids nested uv resolution inside pytest and runs fast on both macOS and Linux CI targets.Risks / Follow-ups
# noqa: S311; the RNG is deterministic fixture generation, not a security surface.