import pytest from app.web import create_app from .conftest import write_task from .test_runner import wait_for, wait_for_output @pytest.fixture def output_run(settings): app = create_app(settings) store = app.extensions["run_store"] run, _ = store.enqueue(target_kind="task", target_path="example/task.py", queue_group="example") store.claim_next("example") path = settings.state_root / "test.log" path.write_bytes(b"first\n") with store._connect() as connection: connection.execute("UPDATE runs SET log_path = ? WHERE id = ?", (str(path), run.id)) return app.test_client(), run.id, path, store def test_log_polling_returns_only_appended_bytes_and_status(output_run): client, run_id, path, store = output_run first = client.get(f"/runs/{run_id}/output?offset=0").get_json() assert first == { "output": "first\n", "offset": 6, "reset": False, "has_more": False, "status": "running", "display_status": "running", "progress": "", "recovery_of_run_id": None, } with path.open("ab") as stream: stream.write("next: \u20ac\n".encode()) store.finish(run_id, status="succeeded") next_chunk = client.get(f"/runs/{run_id}/output?offset=6").get_json() assert next_chunk["output"] == "next: \u20ac\n" assert next_chunk["offset"] == path.stat().st_size assert next_chunk["status"] == "succeeded" assert client.get(f"/runs/{run_id}/output").get_json() == {"output": "first\nnext: \u20ac\n"} def test_incomplete_utf8_is_deferred_until_the_next_poll(output_run): client, run_id, path, _ = output_run path.write_bytes(b"ok\xe2\x82") first = client.get(f"/runs/{run_id}/output?offset=0").get_json() assert first["output"] == "ok" assert first["offset"] == 2 with path.open("ab") as stream: stream.write(b"\xac") second = client.get(f"/runs/{run_id}/output?offset=2").get_json() assert second["output"] == "\u20ac" assert second["offset"] == 5 def test_truncated_log_resets_the_browser_offset(output_run): client, run_id, path, _ = output_run path.write_bytes(b"new") result = client.get(f"/runs/{run_id}/output?offset=6").get_json() assert result["reset"] is True assert result["output"] == "new" assert result["offset"] == 3 def test_large_log_is_drained_in_bounded_chunks(output_run): client, run_id, path, store = output_run path.write_bytes(b"x" * (1024 * 1024 - 1) + "\u20ac".encode()) store.finish(run_id, status="succeeded") first = client.get(f"/runs/{run_id}/output?offset=0").get_json() assert first["has_more"] is True assert first["offset"] == 1024 * 1024 - 1 second = client.get(f"/runs/{run_id}/output?offset={first['offset']}").get_json() assert second["output"] == "\u20ac" assert second["has_more"] is False @pytest.mark.parametrize("offset", ["-1", "abc", "1.5", "9" * 100, str(2**63)]) def test_invalid_log_offset_is_rejected(output_run, offset): client, run_id, _, _ = output_run assert client.get(f"/runs/{run_id}/output?offset={offset}").status_code == 400 def test_running_task_output_is_visible_before_completion(settings): write_task(settings.task_root, "example", "long.py", 'import time\nprint("working", flush=True)\ntime.sleep(30)\n') app = create_app(settings) manager = app.extensions["run_manager"] run, _ = manager.run_task(app.extensions["catalog"].task("example/long.py")) try: wait_for_output(settings.state_root / "runs" / f"{run.id}.log", "working") response = app.test_client().get(f"/runs/{run.id}/output?offset=0").get_json() assert response["status"] == "running" assert response["output"] == "working\n" finally: manager.request_cancel(run.id) wait_for(manager, run.id, {"cancelled"})