import unittest

from compile_collective_cognition import (
    compile_graph,
    load_contributions,
)


class CollectiveCognitionCompilerTests(unittest.TestCase):
    def test_repository_contributions_compile_without_authority(self):
        result = compile_graph(load_contributions())
        self.assertGreaterEqual(result["contribution_count"], 1)
        self.assertGreater(result["node_count"], 1)
        self.assertFalse(result["execution_authority"])
        self.assertTrue(result["correction_edges"])

    def test_unknown_parent_fails_closed(self):
        contribution = load_contributions()[0]
        broken = {
            **contribution,
            "nodes": [
                *contribution["nodes"],
                {
                    "id": "BROKEN",
                    "kind": "decision",
                    "statement": "broken",
                    "parents": ["MISSING"],
                    "evidence": [],
                    "state": "pending",
                },
            ],
        }
        with self.assertRaises(ValueError):
            compile_graph([broken])

    def test_cycles_fail_closed(self):
        contribution = load_contributions()[0]
        first = contribution["nodes"][0]["id"]
        last = contribution["nodes"][-1]["id"]
        cyclic_nodes = [
            {**node, "parents": [last]} if node["id"] == first else node
            for node in contribution["nodes"]
        ]
        with self.assertRaises(ValueError):
            compile_graph([{**contribution, "nodes": cyclic_nodes}])


if __name__ == "__main__":
    unittest.main()
