import unittest import tempfile from pathlib import Path import numpy as np from validation import bucket_key, decode_audio, divergence, nearest_indices, neighbor_agreement, passes, summary class Validation(unittest.TestCase): def test_cosine_is_scale_invariant_but_absolute_error_is_not(self): a = np.array([1., 2.], dtype=np.float32) result = divergence(a, 2 * a) self.assertAlmostEqual(result["cosine"], 1.) self.assertEqual(result["max_abs"], 2.) self.assertFalse(passes(result, "upstream -> fp32")) def test_bad_outputs_cannot_pass(self): for bad in (np.array([np.nan, 1.]), np.zeros(2), np.ones(3)): with self.assertRaises(ValueError): divergence(np.ones(2), bad) self.assertIsNone(summary([])["max"]) def test_neighbors_exclude_self_and_break_exact_ties_in_row_order(self): values = np.array([[1., 0.], [1., 0.], [0., 1.]], dtype=np.float32) np.testing.assert_array_equal(nearest_indices(values, 2), [[1, 2], [0, 2], [0, 1]]) def test_tower_buckets_keep_shape_and_mask(self): key = bucket_key([1, 2, 1536], [True, False]) self.assertNotEqual(key, bucket_key([1, 2, 1536], [True, True])) self.assertNotEqual(key, bucket_key([1, 3, 1536], [True, False])) def test_empty_dataset_audio_is_explicit_error(self): with self.assertRaisesRegex(ValueError, "zero audio bytes"): decode_audio({"audio": {"bytes": b""}}) def test_neighbor_coverage_counts_singletons_and_small_buckets(self): scratch = Path(__file__).resolve().parents[1] / ".validation-cache" scratch.mkdir(exist_ok=True) with tempfile.TemporaryDirectory(dir=scratch) as directory: root = Path(directory) values = np.array([1., 0., .8, .2, 1., 0., 0.], dtype="