import json from pathlib import Path import pytest from strangebench.strict.audit_independent import ( COCO80, acquire_shard_lock, assemble_manifest, clip_box, job_is_live, load_json_overrides, mask_fill, order_scenes, prompts_for_class, read_partial, shard_scenes) def test_shard_lock(tmp_path: Path, monkeypatch): lock = tmp_path / 'sam3_shard0.lock' assert acquire_shard_lock(lock, '111') assert json.loads(lock.read_text())['job_id'] == '111' assert acquire_shard_lock(lock, '111') import strangebench.strict.audit_independent as m class R: def __init__(self, out): self.stdout = out monkeypatch.setattr(m.subprocess, 'run', lambda *a, **k: R('52696954_0 gpu sb_v2 user R 0:10\n')) assert not acquire_shard_lock(lock, '222') monkeypatch.setattr(m.subprocess, 'run', lambda *a, **k: R('')) assert acquire_shard_lock(lock, '222') assert json.loads(lock.read_text())['job_id'] == '222' assert job_is_live('5', '7_0 x\n') and not job_is_live('5', ' \n') and not job_is_live('5', None) def test_prompts_for_class_override_and_union(): primary = {'mouse': 'a computer mouse', 'bed': 'a picture of a {category}'} secondary = {'mouse': 'a computer mouse on a desk'} assert prompts_for_class('dog', 'a {label}', primary) == ['a dog'] assert prompts_for_class('mouse', 'a {label}', primary) == ['a computer mouse'] assert prompts_for_class('bed', 'a {label}', primary) == ['a picture of a bed'] assert prompts_for_class('mouse', 'a {label}', primary, secondary) == ['a computer mouse', 'a computer mouse on a desk'] assert prompts_for_class('dog', 'a {label}', primary, secondary) == ['a dog', 'dog'] assert prompts_for_class('dog', '{label}', {}, {}) == ['dog'] def test_shard_and_order(): specs = [{'scene_id': f's{i:02d}'} for i in range(10)] parts = [shard_scenes(specs, 3, k) for k in range(3)] assert sorted(s['scene_id'] for p in parts for s in p) == [s['scene_id'] for s in specs] assert [s['scene_id'] for s in parts[1]] == ['s01', 's04', 's07'] with pytest.raises(ValueError): shard_scenes(specs, 3, 3) ids = {'s07', 's01'} assert [s['scene_id'] for s in order_scenes(parts[1], ids, 'first')] == ['s01', 's07', 's04'] assert [s['scene_id'] for s in order_scenes(parts[1], ids, 'only')] == ['s01', 's07'] assert order_scenes(parts[1], None, 'first') == parts[1] def test_clip_box_and_mask_fill(): assert clip_box([-5, 10, 2000, 500.26], 1024, 1024) == [0.0, 10.0, 1024.0, 500.3] assert mask_fill(50, [0, 0, 10, 10]) == 0.5 assert mask_fill(50, [0, 0, 0, 10]) == 0.0 def test_partial_resume_and_manifest(tmp_path: Path): p = tmp_path / 'sam3_shard0.partial.jsonl' rec_a = {'scene_id': 'b', 'sub_tier': 't', 'intended': ['dog'], 'width': 4, 'height': 4, 'detections': []} rec_b = {'scene_id': 'a', 'sub_tier': 't', 'intended': [], 'width': 4, 'height': 4, 'detections': [{'class': 'cat', 'score': 0.5, 'bbox': [0, 0, 1, 1]}]} p.write_text(json.dumps(rec_a) + '\n' + json.dumps(rec_b) + '\n{"scene_id": "trunc') done = read_partial(p) assert set(done) == {'a', 'b'} m = assemble_manifest(done.values(), 'sam3', {'score_thr': 0.05}) assert [s['scene_id'] for s in m['scenes']] == ['a', 'b'] and m['n_scenes'] == 2 assert m['audit_config']['score_thr'] == 0.05 and m['auditor'] == 'sam3' def test_rexomni_to_dets(): from strangebench.strict.audit_independent import rexomni_to_dets ex = {'computer mouse': [{'type': 'box', 'coords': [10, 10, 50, 50]}, {'type': 'point', 'coords': [1, 1]}], 'dog': [{'type': 'box', 'coords': [-3, 0, 2000, 30]}] * 3, 'cat': []} dets = rexomni_to_dets(ex, {'computer mouse': 'mouse'}, 100, 100, max_per_class=2) assert [(d['class'], d['score']) for d in dets] == [('mouse', 1.0), ('dog', 1.0), ('dog', 1.0)] assert dets[1]['bbox'] == [0.0, 0.0, 100.0, 30.0] and dets[0]['prompt'] == 'computer mouse' assert rexomni_to_dets({}, {}, 10, 10, 5) == [] and rexomni_to_dets(None, {}, 10, 10, 5) == [] def test_overrides_drop_comments(tmp_path: Path): f = tmp_path / 'o.json' f.write_text(json.dumps({'_comment': 'x', 'skis': 'a pair of skis'})) assert load_json_overrides(f) == {'skis': 'a pair of skis'} assert load_json_overrides(tmp_path / 'missing.json') == {} assert len(COCO80) == 80