diff --git a/correlation_id_generation/tests/test_core.py b/correlation_id_generation/tests/test_core.py new file mode 100644 index 0000000..277ce9f --- /dev/null +++ b/correlation_id_generation/tests/test_core.py @@ -0,0 +1,58 @@ +import json +import tempfile +import os +import time +import hashlib +import pytest + +from src.correlation_id_generation import core + +def test_generate_correlation_id_uniqueness(): + tgid = 100 + pid = 200 + run_seq = 1 + cid1 = core.generate_correlation_id(tgid, pid, run_seq) + cid2 = core.generate_correlation_id(tgid, pid, run_seq + 1) + assert isinstance(cid1, str) + assert isinstance(cid2, str) + assert cid1 != cid2 + +def test_generate_correlation_id_deterministic_same_input(): + cid_a = core.generate_correlation_id(1, 2, 3) + cid_b = core.generate_correlation_id(1, 2, 3) + assert cid_a == cid_b + +def test_log_task_state_creates_json_file(tmp_path): + log_path = tmp_path / "correlation_log.json" + # Patch core's output path in a minimal local context + task = "taskA" + state_before = "sleeping" + state_after = "running" + # Generate a correlation id first + cid = core.generate_correlation_id(10, 20, 5) + # Simulate the internal logging structure manually + log_entry = { + "correlation_id": cid, + "tgid": 10, + "pid": 20, + "timestamp": pytest.approx(time.time(), abs=2.0), + "task": task, + "state_before": state_before, + "state_after": state_after + } + # Call the log function, redirecting the output + old_cwd = os.getcwd() + os.chdir(tmp_path) + try: + core.log_task_state(task, state_before, state_after) + # The log file should now exist + stored_file = tmp_path / "output" / "correlation_log.json" + assert stored_file.exists() + with open(stored_file, "r", encoding="utf-8") as f: + data = json.load(f) + assert isinstance(data, dict) + assert "correlation_id" in data + assert isinstance(data["correlation_id"], str) + assert "timestamp" in data and isinstance(data["timestamp"], float) + finally: + os.chdir(old_cwd)