Skip to content

test(cli): tests double only process edges and mirror the source tree #1307

Closed

Description

@dan-petty

Problem

devops-cli's tests break when code is refactored without a change in behaviour, and a reader cannot find the tests for a package. Tests patch devops-cli's own functions and import its private names. Each file builds its own doubles, and product code has grown branches that detect mocks. This parent makes tests double only what devops-cli reaches beyond its own process (its edges), through one seam per edge and one shared fake per edge, and lays tests/ out as a mirror of src/devops_cli.

The vocabulary used here and in every child (Edge, Seam, Fake, Contract suite, Hermetic floor, Live test, CLI test, Invariant test, Ratchet, Builder, Snapshot) is defined in docs/TESTING.md. ADR 0005 (docs/adr/0005-tests-double-only-process-edges.md) records the decision. Both are drafted and land with #1308.

Owner decisions (2026-10-07). The owner ranked coupling to internals as the first pain and findability second, then settled the design question by question, and reaffirmed Q32 (filing in phases) and Q34 (amending existing issues) later the same day. Every decision is listed under Key questions; the children state them as decided.

Evidence tags used in this issue and its children.

Tests are coupled to internals

  • 3,655 patch sites in the 447 test files: 2,770 aimed at devops-cli internals, 168 patch.object calls on local objects, 674 at edges and 43 other; 151 files have none. 210 test files import 937 private names from devops_cli [measured: census].
  • Most patched: run_subprocess 262 sites in 61 files, run_gh 199 in 20, load_settings 135 in 38, _run_mcp_cmd 68 in 19, _run_cmd 68 in 10 [measured: census].
  • tests/test_pr_cmd.py has 216 patch sites for 101 tests, and tests/test_review_verification.py imports 75 private names.
  • Where the coupling reaches [measured: recount]. 2,502 patch sites name a devops_cli.… path as a string; all but a few dozen (such as devops_cli.<module>.subprocess.run or .time.sleep) aim at internals. The other internal patches pass an imported devops-cli module or object.
Package the test reaches into String patch targets Private names imported
commands/ (top-level modules) 804 185
github/ 367 87
commands/k8s/ 230 87
security/ 141 28
k8s/ 128 15
core/ 120 9
config/ 94 11
ai/review/ 92 292
ai/rag/ 74 21
ai/mcp/ 73 8
ai/ elsewhere (top level and other subpackages) 125 119
roadmap/ 11 2
Other packages (git/, argo/, sandbox/, telemetry/, …) 243 73
All packages 2,502 937

The suite is flat

  • 447 tests/test_*.py files hold 175,620 lines and 7,006 test functions in one directory. The only subdirectories are tests/fixtures/ (24 data files) and tests/golden/ (3 JSON files). 32 files are over 1,000 lines and 9 over 2,000; tests/test_ui_reactive.py has 3,463 lines and 198 tests.
  • Files are named after work, not behaviour: test_batch_2_review_defects.py, ten test_consolidation_*.py and three *_hardening.py, and AGENTS.md:161 names two consolidation files as canonical. Catch-alls include test_ci_helpers_and_edge_cases [code: tests/test_ci.py:383] and test_classify_license_all_branches [code: tests/test_aibom.py:149]. 263 test names end in _success, _failure, _basic or _command.
  • Tooling assumes the flat layout:
    • devops test globs test_*.py non-recursively [code: commands/test_cmd.py:72] and maps a source stem to three flat file names [code: commands/test_cmd.py:87-91];
    • 47 test files find the repo root by __file__ depth;
    • 3 files import from tests.conftest, and tests/test_roadmap_dry_runs.py:47 imports from another test file.

Doubles are duplicated, and the shared ones patch internals

  • Processes: 248 hand-built CompletedProcess( in 57 files. _mock_proc is defined in tests/test_docker.py:21, tests/test_k8s.py:22, tests/test_uv.py:18 and tests/test_gh_project_workflows.py:28.
  • GitHub: _FakeGithub four times in tests/test_github_client.py, _FakeGitHub in tests/test_review_path_routing.py:111 and tests/test_review_scope.py:28, _RecordedGithub [code: tests/test_github_session.py:33] and RecordedGh [code: tests/test_roadmap_github_store.py:93].
  • Elsewhere: five DummyClient model fakes in tests/test_review_path.py; FakeValkey in tests/test_run_store.py:49 and tests/test_run_comparison.py:39; a _finding(...) helper in 14 files; 1,417 MagicMock( calls in 196 files.
  • The shared fakes install by patching private names. route_client and route_llm_clients replace LLMClient's private _shared_client and _create_http_client [code: tests/llm_stream_fakes.py:98-127], and 12 files patch or call the tracer's private _send_payload [code: telemetry/tracer.py:1232] or _send_payload_sync [code: telemetry/tracer.py:1216].

Edges have no single seam

Product code accommodates tests

  • Mock detection:
    • security/base.py imports NonCallableMock [code: security/base.py:17], and its _run_subprocess calls a mock that _is_mocked finds on the scanner's module in sys.modules instead of running the tool [code: security/base.py:327-417];
    • a missing path counts as a scannable manifest when kube-linter is mocked [code: ai/review/pipeline.py:710-715];
    • the MCP server checks hasattr(run_subprocess, "assert_called") [code: ai/mcp/server.py:88];
    • commands/ci.py reads PYTEST_CURRENT_TEST [code: commands/ci.py:253, :257], and the process runner passes that variable through to child processes [code: core/process.py:93];
    • the async runner reroutes through the sync one when subprocess.run is patched [code: core/process.py:545];
    • the host sandbox answers a mocked Popen through _communicate_mock [code: sandbox/host.py:88-98, :247].
  • Shaped for patching:
    • _K8sModule.__setattr__ forwards patches into four submodules [code: commands/k8s/init.py:223-253];
    • five command modules (release, ci, uv, workspace, branches) look dependencies up through _LAZY_OBJECT_MAPPING and _get;
    • seven scanner modules re-import run_subprocess and is_dry_run under # noqa: F401 only to give tests a patch target;
    • the settings cache puts CONFIG_PATH in its key because "tests rebind it" [code: config/settings.py:928].
  • Test-only code ships in the wheel: roadmap/memory_store.py (1,093 lines) has no importer in src.

The root conftest is both the floor and a domain toolbox

  • tests/conftest.py is 917 lines with 17 autouse fixtures.
    • It turns telemetry export on for every test [code: tests/conftest.py:465, :471], so spans queue exports that the network guard refuses [measured: census].
    • tmp_ssh_dir and tmp_repos_dir have no users, and verify_test_paths_isolated cannot fail.
  • 22 process-wide caches and singletons get no public reset from any fixture (the conftest pins one, the core.process GitHub token, through private names), and 8 of them have no reset function [measured: census].
  • State escapes the test's directories:
  • 131 test files build their own module-level runner = CliRunner(...).
  • The 21 @pytest.mark.bwrap live tests never run: if True: [code: tests/conftest.py:873] skips them even where bwrap is installed (fix(ci): the live bubblewrap tests run wherever bwrap is installed and skip only where it is missing #1337).

The roadmap package shows the alternative works

461 test functions in 15 tests/test_roadmap*.py files carry 68 patch sites. They run on InMemoryRoadmapStore, whose constructor takes clock= [code: roadmap/memory_store.py:176], and on an injected runner= typed by the GhRunner Protocol [code: roadmap/github_store.py:136]. A contract suite pins the store's promises against the in-memory adapter (tests/test_roadmap_store_contract.py, SteppedClock at :44), and the GitHub adapter keeps the same promises over recorded gh output (tests/test_roadmap_github_store.py). #983 replaces this GhRunner injection with the GitHub session: the store keeps no GhRunner, and its tests build it on a session over MockTransport for REST and a scripted runner for the board commands. #1335 builds its stores on #983's session fake.

Speed is out of scope

9,278 tests pass in about 130 s wall against 1,015.9 s of summed test time, so about half the wall time is overhead; every worker repeats collection, and tests/test_ui_reactive.py alone sums 146.3 s [measured: timing]. #1338 and #1339 take these, outside this parent (Q12).

Considered options

Chosen: tests double only process edges, each through one seam in src with one shared fake and its contract suite, in a tests/ tree that mirrors src/devops_cli.

  • Patch at the library boundary only (subprocess.run, httpx2 transports, SDK classes), with no source changes: rejected. Several SDKs speak HTTP through other stacks (plain httpx, urllib3, requests) that a class swap cannot reach, and the shared HTTP pool hands out cached clients that bypass it. Patching the stdlib object stays as a fallback for stdlib edges.
  • Pass every client down the call graph as a parameter: rejected. It rewrites signatures across hundreds of modules for no gain over one seam per edge.
  • Allow internal patches with a stated reason: rejected. Exceptions accumulate until the rule means nothing.
  • freezegun, or a home-grown Clock: rejected. freezegun is unmaintained, and its frozen monotonic clock hangs asyncio and tenacity. A home-grown Clock would duplicate what the libraries already accept: time-machine fakes wall-clock reads, and waiting goes through the parameters tenacity, ratelimit and pydantic-ai already offer.
  • One test file per source module: rejected. Tests target the package's public interface, one file per behaviour (Q10).
  • Move tests package by package, together with their design fixes: rejected. One git mv PR at a quiet point on main keeps each file's history and makes every open PR rebase once; design fixes follow per package [inference].
  • Keep in-memory test implementations in src: rejected. They move to tests/support/fakes/, and fakes never ship in the wheel (Q25).
  • Keep the testing vocabulary in CONTEXT.md or AGENTS.md: rejected. CONTEXT.md stays a domain glossary, and AGENTS.md links to docs/TESTING.md (Q5).
  • Keep the tuple-assert mandate: rejected. 15 linear asserts score McCabe 1 under ruff C901, so the mandate's complexity rationale does not hold (Q7).
  • File the package migrations lazily, once their seams land: rejected. A person can order only the work that is filed (Q33).

Consequences

Acceptance criteria

Phases are an order, not milestones: a person plans each child on the board.

Phase A: conventions

Phase B: floor and prep

Phase C: move

Phase D: seams and fakes

Each child except #1320 adds one seam in src (a Protocol plus the one place that supplies the real implementation), the edge's fake under tests/support/fakes/ and the fake's contract suite. #1320 adds no Clock type: time-machine and the libraries' own parameters serve instead (Q30).

Phase E: package migrations

Each E child does the following for its directories:

Each E child names its test files. Every test file outside tests/invariants/ is named in exactly one E child, so a shared directory such as tests/commands/ or tests/ai/ has no file that no child owns. Where #775 or #1001 already routes a package's bypassing callers, the E child confirms that none remain.

Phase F: finish

Dependencies

These are hard orderings, and each one is also a native blocked_by link (Q31).

Child Waits for, besides #1313
#1321 #775, #983, #891, #1318, #1319, #1320
#1322 #775, #1314, #1315, #1318, #1320
#1323 #775, #1315, #1318, #1320
#1324 #775, #983, #891, #1320
#1325 #775, #1314, #1316, #1318, #1320
#1326 #775, #983, #891, #1001, #772, #1314, #1317, #1318, #1319
#1327 #775, #772, #1314, #1315, #1316, #1317, #1318, #1319, #1320
#1328 #775, #983, #891, #1001, #772, #1317, #1320
#1329 #775, #1314, #1315, #1317, #1318, #1319, #1320
#1330 #775, #1001, #772, #1314, #1317, #1318, #1320
#1331 #775, #983, #891, #1001, #1314, #1317, #1318, #1319, #1320
#1332 #775, #983, #891, #772, #1314, #1317, #1318, #1319, #1320
#1333 #775, #1311, #1314, #1318, #1319
#1334 #775, #1314, #1315, #1316, #1317, #1318, #1319, #1320
#1335 #775, #983, #1001, #1314, #1315, #1316, #1317, #1318, #1319, #1320

Key questions

Strategic alignment

Alternatives check of 2026-10-29: not-applicable.
Tool: none found.

Proposed design

Problem statement

Tests in devops-cli break when code is refactored without a change in behaviour. A reader cannot find the tests for a package. The parent issue #1307 moves the suite to a design where tests double only process edges through shared fakes and tests/ mirrors src/devops_cli. Twenty-eight of the 34 child issues are merged. Six children stay open: #1314, #1329, #1330, #1333, #1335 and #1336. The parent has no code of its own. It is done when #1336 lands and the exit state holds.

Acceptance criteria

Requirement: No internal patches remain

The test suite SHALL contain zero patches aimed at devops-cli internals and zero imports of private names from devops_cli.

Scenario: Ratchet at zero

Requirement: No test accommodations in product code

The source tree SHALL contain zero uses of unittest.mock, zero reads of PYTEST_CURRENT_TEST and zero mock-attribute checks.

Scenario: Source ban at zero

  • WHEN the open E children have deleted the accommodations in their directories
  • THEN the ban test in tests/invariants/test_source_tree.py finds 0 occurrences in src/devops_cli

Requirement: Mirror layout

Every test_*.py file SHALL sit in a directory that mirrors src/devops_cli or in a subject-named file in tests/invariants/.

Scenario: Layout ban

Requirement: Every fake has a contract suite

Every fake module under tests/support/fakes/ SHALL have a contract suite that runs against the fake and its real implementation.

Scenario: Fake without a contract suite

Requirement: Coverage gate kept

The change SHALL keep the coverage floor at the value of fail_under in pyproject.toml.

Scenario: Floor after the final child

Existing Tool Considered

The alternatives check of 2026-10-29 found no open, maintained tool that covers this need (not-applicable). The project already uses syrupy 6.1.1 and time-machine 3.5.1, both in the dev group of pyproject.toml. The seams, fakes, contract suites and the mirror layout are project code. The parent adds no code itself. The cost is the work of the six open child issues, each delivered as its own pull request.

Key questions

Fit

Needs splitting into 6 items:

  1. refactor(cli): Unify HTTP and DNS seams with shared fakes #1314: one HTTP and DNS seam
  2. test(ai): Wire rag once in ai/rag, test with edge fakes #1329: ai/rag/ and ai/library/ migration
  3. test(mcp): mcp, agent and tool tests run on edge fakes #1330: ai/mcp/, agents, harness, tools migration
  4. test(cli): Use edge fakes and real git; drop test rebind hooks #1333: foundation packages migration
  5. test(infra): infrastructure tests run on edge fakes #1335: infra packages migration
  6. test(cli): Convert ratchets to bans and merge invariant tests #1336: ratchets become bans

Suspected block

None.

Dependencies

#1314, #1329, #1330, #1333, #1335, #1336

Activity

  1. added
    type/test
    scope/cli
    priority/p2-medium
    on Oct 7, 2026
  2. dan-petty-bot commented on Oct 8, 2026

    @dan-petty-bot
    Collaborator

    Intake placed this item to the backlog: at intake only a critical fix goes into a release; a release start pulls Ready items in, P0 features first.

    • Priority: P2-Medium. It addresses the top two developer pain points (internal coupling and findability) but does not block end users or fix a defect, so it is important but not urgent.
    • Value: High. It removes 2,770 internal patch sites and reorganises 447 flat test files into a mirror tree, directly improving maintainability and onboarding for every contributor.
    • Effort: High. It spans 447 test files, 175,620 lines, 3,655 patch sites, and is explicitly broken into multiple phases (E, F, etc.) across many child issues.
  3. dan-petty commented on Oct 26, 2026

    @dan-petty
    OwnerAuthor

    Intake placed this item to the backlog: intake puts every new item in the backlog. A person plans the milestones on the board.

    • Priority: P1-High. Improving tests helps with maintainability and correctness of the codebase.
    • Value: High. This change improves test quality, findability, and reduces coupling to internals.
    • Effort: High. Refactoring tests to use shared fakes and mirroring the source tree requires significant effort.
    • Possible duplicate: test(cli): migrate 49 tests to public interfaces #2167, which may ask for the same change. The model names tests double only process edges and mirror the source tree as the change that both ask for. Both items ask for tests to double only process edges and mirror the source tree, with no internal patches or private imports. A person closes one of the two if that is so.
  4. dan-petty commented on Oct 26, 2026

    @dan-petty
    OwnerAuthor

    Refine cannot set this item to Ready at 48c78ca until a person answers the questions below. Reply in a comment or edit the body, and the next refine round looks at the item again.

  5. added
    priority/p1-high
    status/new
    effort/high
    value/high
    and removed
    priority/p2-medium
    on Oct 28, 2026
  6. devops-cli-service commented on Oct 29, 2026

    @devops-cli-service
    Contributor

    Alternatives check: not-applicable

    Tool: none found

    Checked on 2026-10-29. A person decides what this means for the item. The check changes no label and closes no issue.

  7. dan-petty commented on Oct 30, 2026

    @dan-petty
    OwnerAuthor

    Issue #1307 cannot be built as written in one pull request. It is a feature parent issue that has 34 child issues across six phases. Several child issues are already merged, and 19 child issues remain open. Delivery occurs through the individual child issues.

  8. devops-cli-service commented on Oct 31, 2026

    @devops-cli-service
    Contributor

    The proposal at 8234a83 needs splitting into 6 items. A person splits the item into the items below, or replies in a comment or edits the body to keep one item, and the next refine round looks at the item again.

    1. Phase A: conventions (docs(cli): docs/TESTING.md holds the test conventions, ADR 0005 records that tests double only process edges, and AGENTS.md and the contributor docs point to them #1308, test(cli): retire the tuple-assert mandate and detect opaque tuple asserts and presence-only tests #851, test(cli): ratchets hold internal patches, private imports and test accommodations in src at today's counts and name each site that would raise one #1309)
    2. Phase B: floor and prep (test(cli): the hermetic floor fails a test that reaches an edge without a fake and resets all process-wide state after every test #1310, fix(ci): tests touch no state outside their temporary directories #1311, test(cli): tests find the repository root and fixtures from any depth, import no other test module and build test data with shared builders, and devops test maps a change to its mirror directory #1312)
    3. Phase C: move (test(cli): every test file sits in the directory mirroring the package it tests or in tests/invariants/, and the root conftest holds only the hermetic floor #1313)
    4. Phase D: seams and fakes (refactor(cli): one process runner owns execution, process groups and dry-run #775, refactor(github): the github session owns transport, ledger, error classes and link pagination #983, refactor(ai): one model seam: llmclient over three wire adapters behind one factory, no second provider stack #1001, feat(review): review tools run through one injectable executor in a network-less sandbox with CPU, memory and time limits #772, refactor(cli): Unify HTTP and DNS seams with shared fakes #1314, refactor(k8s): KubernetesService is the Kubernetes API seam, with a shared fake cluster and its contract suite #1315, refactor(infra): DockerEngineService is the Docker engine seam, with a shared in-memory engine fake and its contract suite #1316, refactor(ai): Valkey, Qdrant and the embedder each have one seam, with shared fakes and contract suites #1317, refactor(security): one keyring seam owns the keyring and the Secret Service lock check, with a shared keyring fake and its contract suite #1318, refactor(telemetry): OTLP export goes through one exporter seam, and tests read exported spans and metrics from a shared fake #1319, refactor(cli): tests control time: time-machine fakes wall-clock reads, and retry and rate-limit waits return at once and are recorded #1320)
    5. Phase E: package migrations (test(github): github/ is tested through its public interface over the gh process and session fakes, with no internal patches or private imports #1321 through test(infra): infrastructure tests run on edge fakes #1335)
    6. Phase F: finish (test(cli): Convert ratchets to bans and merge invariant tests #1336)
  9. dan-petty commented on Nov 9, 2026

    @dan-petty
    OwnerAuthor

    Issue #1307 cannot be built as written in one pull request. It is a feature parent issue that has 34 child issues across six phases (#1308 through #1336). Twenty-eight child issues are merged and closed. Six child issues (#1314, #1329, #1330, #1333, #1335, #1336) stay open. Rule 1 requires one issue and one pull request. Delivery occurs through the individual child issues.

  10. devops-cli-service commented on Nov 9, 2026

    @devops-cli-service
    Contributor

    The proposal at d37762b needs splitting into 6 items. A person splits the item into the items below, or replies in a comment or edits the body to keep one item, and the next refine round looks at the item again.

    1. refactor(cli): Unify HTTP and DNS seams with shared fakes #1314: one HTTP and DNS seam
    2. test(ai): Wire rag once in ai/rag, test with edge fakes #1329: ai/rag/ and ai/library/ migration
    3. test(mcp): mcp, agent and tool tests run on edge fakes #1330: ai/mcp/, agents, harness, tools migration
    4. test(cli): Use edge fakes and real git; drop test rebind hooks #1333: foundation packages migration
    5. test(infra): infrastructure tests run on edge fakes #1335: infra packages migration
    6. test(cli): Convert ratchets to bans and merge invariant tests #1336: ratchets become bans
  11. dan-petty commented on Nov 10, 2026

    @dan-petty
    OwnerAuthor

    All child issues of #1307 are delivered. The final child issue #1336 landed in pull request #4294.

    The invariant test suite in tests/invariants/ enforces the exit criteria with hard bans:

    • Zero patches target devops_cli internals.
    • Zero private names are imported from devops_cli in tests/.
    • Zero test accommodations exist in src/devops_cli/.
    • All test files mirror src/devops_cli/ or reside in tests/invariants/.
    • Every fake module under tests/support/fakes/ has a contract suite.
    • The coverage floor is maintained.

    This parent issue has no code of its own and is now complete.

  12. removed
    status/new
    on Nov 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    effort/highpriority/p1-highscope/clitype/testvalue/high

    Type

    No type

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions