import json,pathlib,unittest,copy
from review import review
class ParentTests(unittest.TestCase):
    @classmethod
    def setUpClass(cls):cls.c={x['case']:x for x in json.loads(pathlib.Path(__file__).with_name('filled-review.json').read_text())['cases']}
    def test_filled_classifications(self):
        for x in self.c.values():
            with self.subTest(case=x['case']):self.assertEqual(review(x['packet'])['status'],x['expected'])
    def test_nested_projection_preserves_parent(self):
        x=review(self.c['A']['packet'])['projection'];self.assertEqual([n['id'] for n in x],['C1','C3']);self.assertEqual(x[0]['comments'][0]['id'],'C2')
    def test_linear_has_three_root_children(self):
        x=review(self.c['J']['packet'])['projection'];self.assertEqual(len(x),3);self.assertTrue(all(n['comments']==[] for n in x))
    def test_dangling_reason_retained(self):
        x=review(self.c['C']['packet']);self.assertEqual((x['node'],x['parent']),('C2','C404'))
    def test_cycle_and_self_parent_stop(self):
        p=copy.deepcopy(self.c['A']['packet']);p['comments'][0]['parent']='C1';self.assertEqual(review(p)['status'],'CYCLIC_PARENT_CHAIN')
    def test_unbound_inputs_and_no_mutation(self):
        for p in [None,{},[],True]:self.assertEqual(review(p)['status'],'SOURCE_EVIDENCE_UNRESOLVED')
        p=copy.deepcopy(self.c['A']['packet']);before=copy.deepcopy(p);review(p);self.assertEqual(p,before)
    def test_maximum_and_malformed_nodes(self):
        p=copy.deepcopy(self.c['A']['packet']);p['comments']=[{'id':str(i),'parent':'POST-7','visible':True} for i in range(201)];self.assertEqual(review(p)['status'],'ENVELOPE_UNRESOLVED')
        p['comments']=[None];self.assertEqual(review(p)['status'],'NODE_ID_UNRESOLVED')
    def test_no_live_or_publication_claim(self):
        for x in self.c.values():
            y=review(x['packet']);self.assertFalse(y['liveThreadVerified']);self.assertFalse(y['fullSchemaValidated']);self.assertFalse(y['publicationApproved'])
if __name__=='__main__':unittest.main()
