agora.plan.test_scaffolder

Pure helpers for the plan_scaffold_tests framework stage.

Generates a tests/test_<x>.py skeleton whose imports match the actual project layout and whose function bodies are pytest.skip('TODO: ...') stubs — one per bullet in plan/brief.md’s ## Key deliverables section. The tester LLM then only has to replace each pytest.skip(...) with real assertions; it never has to reconstruct boilerplate or guess at import paths.

All functions are side-effect-free except discover_modules() which reads from the filesystem. Parsing / rendering are pure.

Functions

discover_modules(work_dir)

Walk <work_dir>/src/** for importable modules.

parse_deliverables(brief_text)

Extract the bullets under ## Key deliverables from brief.md.

render_scaffold(modules, deliverables, *[, ...])

Emit the test file content as a string.

replace_test_body(source, test_name, body_code)

Replace the body of def test_name() in source with body_code.

snake_from_deliverable(bullet)

Convert 'Add a long URL and get a short 6-char hash back' to 'test_add_long_url_and_get_short_hash_back'.

agora.plan.test_scaffolder.parse_deliverables(brief_text)[source]

Extract the bullets under ## Key deliverables from brief.md.

Returns a list of stripped bullet strings (without ``- `` prefix). Returns an empty list if the section isn’t found or has no bullets; callers should fall back to a single generic test stub in that case.

agora.plan.test_scaffolder.discover_modules(work_dir)[source]

Walk <work_dir>/src/** for importable modules.

Returns a mapping of dotted.module.path -> [public_top_level_names]. Uses a lightweight AST parse so we never execute any of the discovered source. Skips __init__.py, conftest.py, test files, and modules whose stem starts with _ (private).

If the project uses a flat src/<modname>.py layout, dotted paths are rooted at the parent package (e.g. src/__init__.py present → <workdir_basename>.<modname>). Otherwise modules appear as bare names.

Returns an empty dict if src/ doesn’t exist.

agora.plan.test_scaffolder.snake_from_deliverable(bullet)[source]

Convert 'Add a long URL and get a short 6-char hash back' to 'test_add_long_url_and_get_short_hash_back'.

Keeps [a-z0-9], collapses whitespace to underscores, truncates to 50 chars, ensures test_ prefix, dedupes double underscores.

agora.plan.test_scaffolder.render_scaffold(modules, deliverables, *, header='', mode='impl', api_spec_imports=None)[source]

Emit the test file content as a string.

modules is the output of discover_modules(). deliverables is the list of bullets. header is an optional one-line docstring summarizing the project; empty → default.

Each deliverable becomes a test function with a pytest.skip('TODO: ...') body so the file runs green out of the box — the tester’s LLM stage replaces each skip with real assertions.

mode controls how imports are shaped:
  • "impl" (default, Sprint 5 behavior): module-level from pkg.x import name for every discovered module. Used when the test file is authored AFTER the implementer tasks have produced the source, so the imports always resolve at collection time.

  • "contract": NO module-level imports. Header doc notes that imports must be deferred INSIDE each test function body. Used by contract tests that run before implementer tasks — module-level imports would fail collection; deferred imports let the file load and each test FAIL loudly when the implementation is missing, which is the right signal for a contract gate.

api_spec_imports (Sprint 7.5) — when provided, the scaffolder emits REAL module-level from src.X import Y lines from the shared API spec, regardless of mode. This is safe in contract mode too because the seed_workspace step pre-wrote matching src/ stubs (with NotImplementedError bodies) before the scaffolder runs — the imports always resolve at collection time, and tests fail loudly when they INVOKE a stub (NotImplementedError), not at import time.

agora.plan.test_scaffolder.replace_test_body(source, test_name, body_code)[source]

Replace the body of def test_name() in source with body_code.

Pure string transformation. Preserves the docstring (first statement if a bare string literal) and def signature; replaces everything else in the function body with body_code lines, normalized to 4-space indent (one standard indent level under the def).

body_code may have any indentation; it’s dedented first, then re-indented to 4 spaces uniformly. Multi-line code is supported — continuation lines get the same 4-space indent + any relative indent the dedented source had.

:raises - ValueError if test_name isn’t found: :raises - ValueError if body_code is empty or parses to nothing: :raises - SyntaxError if the resulting file wouldn’t parse: (caller can surface this to the LLM for retry)

Used by the fill_test_body inner tool — the framework owns the surgical edit so the 7B can just emit the body as plain Python text without wrestling with edit_file_replace’s whitespace matching.