import numpy as np
"""Regression tests for Suite29 preflight and stand-alone reporting."""
from pathlib import Path
import tempfile
import contextlib
import io
import unittest
import pandas as pd
from check_suite29_environment import preflight
from suite29_report import render_report


class ReportingAndPreflightTests(unittest.TestCase):
    def test_count_binary_failures_int8_does_not_overflow(self):
        from train_suite29 import count_binary_failures
        labels = np.concatenate([
            np.ones(189, dtype=np.int8), np.zeros(5222 - 189, dtype=np.int8)
        ])
        self.assertEqual(count_binary_failures(labels), 189)
        self.assertEqual(count_binary_failures(np.ones(768, dtype=np.int8)), 768)
        with self.assertRaises(ValueError):
            count_binary_failures(np.array([0, -1, 1], dtype=np.int8))

    def test_preflight_finds_both_cache_kinds(self):
        with tempfile.TemporaryDirectory() as d, contextlib.redirect_stdout(io.StringIO()):
            root=Path(d)
            canonical=root/'canonical'; structured=root/'structured'
            canonical.mkdir(); structured.mkdir()
            (canonical/'faculty_a_fold_1.joblib').write_bytes(b'test')
            (structured/'faculty_a_fold_1.joblib').write_bytes(b'test')
            out=root/'out'
            a=preflight(['--root',d,'--cache-dir',str(canonical),
                '--structured-dir',str(structured),'--output-dir',str(out),
                '--folds','1','--faculties','all'])
            self.assertEqual(a['faculties'],1)
            self.assertTrue(out.is_dir())
            (structured/'faculty_a_fold_1.joblib').unlink()
            with self.assertRaises(FileNotFoundError):
                preflight(['--root',d,'--cache-dir',str(canonical),
                    '--structured-dir',str(structured),'--folds','1'])

    def test_final_report_paired_delta_and_coverage(self):
        with tempfile.TemporaryDirectory() as d, contextlib.redirect_stdout(io.StringIO()):
            path=Path(d)
            table=[]
            for f in ('faculty_a','faculty_b'):
                for name,ap in (('suite29_self_only',.25),('suite29_set',.31),('committee',.20)):
                    table.append(dict(fold=1,seed=29,faculty=f,model=name,
                        rows=100,failures=10,ap=ap,auc=.7,
                        fbeta_at_20=.2,brier=.1,ece10=.05))
            pd.DataFrame(table).to_csv(path/'all_metrics.csv',index=False)
            pd.DataFrame([dict(fold=1,faculty='faculty_a',partial_groups=1,
                rows_in_partial_groups=2,
                fraction_retained_rows_in_partial_groups=.05)]).to_csv(
                path/'all_history_coverage_audit.csv',index=False)
            info=render_report(path)
            self.assertEqual(info['matched_cells'],2)
            paired=pd.read_csv(path/'summary_paired_all.csv')
            self.assertTrue((abs(paired.delta_ap-.06)<1e-8).all())
            coverage=pd.read_csv(path/'summary_reference_coverage.csv')
            self.assertTrue(coverage.complete_coverage.all())
            self.assertTrue((path/'summary_history_partial_groups.csv').exists())


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