import json import pytest from mouh.jev import ( AuditLog, JevBus, JevRouter, JevSchemaError, experts_for, prior_table, ) from mouh.jev.types import ( JevChoice, JevComponent, JevMessage, JevMessageType as T, JevScore, JevState, JevToolCall, TaskPayload, new_id, CodePayload, ) def task_msg(sid=None, receiver=JevComponent.ROUTER, mtype=T.ROUTE_REQUEST): return JevMessage(session_id=sid or new_id(), type=mtype, sender=JevComponent.USER, receiver=receiver, payload=TaskPayload(instruction="x")) def test_score_normalisation_and_validation(): s = JevScore.from_weights(("a", "b", "c"), (1.0, 2.0, 1.0)) assert abs(sum(s.values) - 1.0) < 1e-12 and s.top_label == "b" and s.certainty == 0.5 with pytest.raises(ValueError): JevScore(kind="probabilistic", labels=("a", "b"), values=(0.7, 0.7)) with pytest.raises(ValueError): JevScore(kind="formal", labels=("a", "b"), values=(0.5, 0.5)) def test_choice_bounds(): s = JevScore.one_hot(("a", "b"), 1) assert JevChoice(alternatives=("a", "b"), index=1, score=s).chosen == "b" with pytest.raises(ValueError): JevChoice(alternatives=("a", "b"), index=2, score=s) with pytest.raises(ValueError): JevChoice(alternatives=("a", "c"), index=0, score=s) def test_payload_must_match_type(): with pytest.raises(ValueError): JevMessage(session_id=new_id(), type=T.CODE_PROPOSAL, sender=JevComponent.USER, receiver=JevComponent.KERNEL, payload=TaskPayload(instruction="x")) def test_bus_rejects_nonconforming_and_audits(): bus = JevBus() with pytest.raises(JevSchemaError): bus.publish({"type": "task_submission", "bogus": 1}) with pytest.raises(JevSchemaError): bus.publish("{not json") kinds = [r["event"]["kind"] for r in bus.audit.records()] assert kinds.count("rejected") == 2 assert bus.audit.verify() == (True, None) def test_bus_rejects_extra_fields_in_valid_message(): bus = JevBus() d = json.loads(task_msg().to_json()) d["payload"]["surprise"] = True with pytest.raises(JevSchemaError): bus.publish(d) def test_bus_routes_and_replies(): bus = JevBus() router = JevRouter(top_k=4) bus.subscribe(T.ROUTE_REQUEST, JevComponent.ROUTER, router.handle) out = bus.publish(task_msg()) assert [m.type for m in out] == [T.ROUTE_REQUEST, T.ROUTE_DECISION] assert out[1].payload.experts == (3, 4, 2, 6) # TASK_SUBMISSION -> experts n°3,4,2,6 assert out[1].parent_id == out[0].message_id def test_router_is_deterministic(): r1, r2 = JevRouter(), JevRouter() for t in T: assert r1.decide(t) == r2.decide(t) assert len(r1.decide(t).experts) == 4 assert experts_for(T.CORRECTION_REQUEST)[0] == 4 # expert n°5 (refactoring) en tête tab = prior_table(1.0) assert len(tab) == len(T) and all(len(r) == 8 for r in tab) def test_handler_exception_isolated_and_diagnosed(): bus = JevBus() seen = [] def boom(m): raise RuntimeError("panne") bus.subscribe(T.ROUTE_REQUEST, JevComponent.ROUTER, boom) bus.subscribe(T.ERROR_DIAGNOSTIC, JevComponent.CORRECTOR, lambda m: seen.append(m) or None) out = bus.publish(task_msg()) assert out[-1].type is T.ERROR_DIAGNOSTIC and seen assert out[-1].payload.diagnostics[0].error_type.value == "handler_failure" def test_handler_timeout_does_not_collapse_bus(): import time bus = JevBus(handler_timeout_s=0.2) bus.subscribe(T.ROUTE_REQUEST, JevComponent.ROUTER, lambda m: time.sleep(5)) out = bus.publish(task_msg()) assert out[-1].payload.diagnostics[0].error_type.value == "timeout" def test_subscribe_after_freeze_forbidden(): from mouh.jev import JevRoutingError bus = JevBus() bus.freeze() with pytest.raises(JevRoutingError): bus.subscribe(T.ROUTE_REQUEST, JevComponent.ROUTER, lambda m: None) def test_audit_chain_tamper_detection(tmp_path): log = AuditLog(tmp_path / "a.jsonl") for i in range(5): log.append({"i": i}) assert log.verify() == (True, None) lines = (tmp_path / "a.jsonl").read_text().splitlines() rec = json.loads(lines[2]); rec["event"]["i"] = 99; lines[2] = json.dumps(rec) (tmp_path / "a.jsonl").write_text("\n".join(lines) + "\n") ok, bad = AuditLog(tmp_path / "a.jsonl").verify() assert not ok and bad == 2 def test_audit_resumes_chain(tmp_path): p = tmp_path / "a.jsonl" AuditLog(p).append({"a": 1}) log2 = AuditLog(p); log2.append({"b": 2}) assert AuditLog(p).verify() == (True, None) def test_state_immutable_and_advances(): st = JevState.new() m = JevMessage(session_id=st.session_id, type=T.CODE_PROPOSAL, sender=JevComponent.KERNEL, receiver=JevComponent.VERIFIER, payload=CodePayload(files={"a.py": "x=1"})) st2 = st.advance(m) assert st.step == 0 and st2.step == 1 and st2.attempts == 1 and st2.history == (m.message_id,) with pytest.raises(Exception): st2.step = 5 # type: ignore[misc] def test_toolcall_validation(): with pytest.raises(ValueError): JevToolCall(skill="bad name!") with pytest.raises(ValueError): JevToolCall(skill="X", timeout_s=0)