diff --git a/tests/test_api_gpu.py b/tests/test_api_gpu.py index db51353..e36c490 100644 --- a/tests/test_api_gpu.py +++ b/tests/test_api_gpu.py @@ -362,19 +362,33 @@ def test_the_agent_heartbeats_whether_or_not_it_holds_a_lease(): Read from the source rather than by running the loop: it is a `while True` with a sleep, so exercising it means threads and timing, and the property - is simply that the call is not behind a `if ids:`. + is simply that the call is not behind a guard on `ids`. + + Parsed rather than grepped, and that is not fussiness — the first cut + searched the raw function text and failed on its own explanation. The + docstring of the fix quotes the construct the fix removed, because that is + what a docstring explaining a fix DOES. A source-text assertion cannot + tell the code from the prose about the code; the AST can, so the body is + unparsed with its docstring dropped. """ + import ast from pathlib import Path src = ( Path(__file__).resolve().parents[1] / "agent" / "fc_agent" / "worker.py" ).read_text() - loop = src[src.index("def _heartbeat_loop"):] - loop = loop[:loop.index("\n def ")] + fn = next( + n for n in ast.walk(ast.parse(src)) + if isinstance(n, ast.FunctionDef) and n.name == "_heartbeat_loop" + ) + code = "\n".join( + ast.unparse(n) for n in fn.body + if not (isinstance(n, ast.Expr) and isinstance(n.value, ast.Constant)) + ) - assert "self.client.heartbeat(ids)" in loop - assert "if ids:" not in loop, ( + assert "self.client.heartbeat(ids)" in code + assert "if ids" not in code, ( "the heartbeat is gated on holding leases again; an idle agent then " "reads as stopped after 300s while sleep mode backs its lease poll " - "off to 900s" + f"off to 900s\n\n{code}" )