Download tests/test_recording_processing.py from CharlieDreemur/Recreate-AAA: direct link, hf CLI and curl.
- Browser
- Download file 5.59 kB
-
https://huggingface.co/CharlieDreemur/Recreate-AAA/resolve/main/tests/test_recording_processing.py
- Command line
-
hf download hf://CharlieDreemur/Recreate-AAA/tests/test_recording_processing.py
-
curl -L -o test_recording_processing.py https://huggingface.co/CharlieDreemur/Recreate-AAA/resolve/main/tests/test_recording_processing.py
5.59 kB
| import json | |
| import random | |
| import statistics | |
| import pytest | |
| from bench_builder import recording_clip, reference_frames | |
| from bench_builder.util.text import iter_text_lines | |
| def _original_distance(left, right): | |
| return (sum((a - b) ** 2 for a, b in zip(left, right, strict=True)) / len(left)) ** 0.5 | |
| def _original_motion(thumbnails): | |
| if len(thumbnails) < 2: | |
| return (0.0,) * len(thumbnails) | |
| return tuple(max(_original_distance(thumbnail, thumbnails[max(0, index - 1)]), | |
| _original_distance(thumbnail, thumbnails[min(len(thumbnails) - 1, index + 1)])) | |
| for index, thumbnail in enumerate(thumbnails)) | |
| def test_frame_selection_preserves_exact_motion_mean_and_ties(pixels): | |
| rng = random.Random(7) | |
| for thumbnails in ( | |
| [bytes(pixels)], | |
| [bytes(pixels)] * 4, | |
| [bytes(pixels), bytes([255]) * pixels], | |
| [rng.randbytes(pixels) for _ in range(11)], | |
| ): | |
| expected_motion = _original_motion(thumbnails) | |
| assert reference_frames.frame_motion(thumbnails) == expected_motion | |
| moving = [index for index, value in enumerate(expected_motion) if value > statistics.median(expected_motion)] | |
| moving = moving or list(range(len(thumbnails))) | |
| mean = bytes(sum(thumbnails[index][pixel] for index in moving) // len(moving) | |
| for pixel in range(pixels)) | |
| expected_choice = min(moving, key=lambda index: (_original_distance(thumbnails[index], mean), index)) | |
| assert reference_frames.select_design_still_index(thumbnails) == expected_choice | |
| assert reference_frames.select_design_still_index(thumbnails, motion=expected_motion) == expected_choice | |
| def test_thumbnail_distance_keeps_invalid_length_behavior(left, right): | |
| with pytest.raises((ValueError, ZeroDivisionError)) as original: | |
| _original_distance(left, right) | |
| with pytest.raises(type(original.value)) as optimized: | |
| reference_frames._thumbnail_distance(left, right) | |
| assert str(optimized.value) == str(original.value) | |
| def test_incremental_lines_match_read_text_splitlines(tmp_path, chunk_chars, ending): | |
| path = tmp_path / "input.jsonl" | |
| for content in ("", ending, "a" + ending, ending + "🙂日志" + ending, | |
| "long" * 101 + ending + ending + "tail", | |
| "a\r\nb\rc\n\v\f\x1c\x1d\x1e\x85\u2028\u2029z"): | |
| path.write_bytes(content.encode("utf-8")) | |
| assert list(iter_text_lines(path, chunk_chars=chunk_chars)) == path.read_text(encoding="utf-8").splitlines() | |
| def test_clipped_input_preserves_bytes_sequences_boundaries_and_unicode(tmp_path): | |
| source = tmp_path / "input.jsonl" | |
| destination = tmp_path / "clipped.jsonl" | |
| events = [ | |
| {"event": "key_press", "time_ns": 999_999_999, "seq": 19}, | |
| {"event": "key_press", "key": "日志🙂", "time_ns": 1_000_000_000, "seq": 41}, | |
| {"event": "key_release", "time_ns": 2_000_000_000.4, "extra": {"x": 1}}, | |
| {"time_ns": True}, {"time_ns": "1200000000"}, {}, | |
| {"event": "key_press", "seq": -1, "time_ns": 1_500_000_000}, | |
| {"time_ns": 2_000_000_001}, | |
| ] | |
| source.write_bytes(("\r\n\u2028".join(json.dumps(event, ensure_ascii=False) for event in events) + "\n").encode()) | |
| expected = [dict(events[1], time_ns=0, seq=0), dict(events[2], time_ns=1_000_000_000, seq=1), | |
| dict(events[6], time_ns=500_000_000, seq=2)] | |
| recording_clip._clip_input_log(source, destination, start_seconds=1, end_seconds=2) | |
| assert destination.read_bytes() == "".join(json.dumps(event, ensure_ascii=False, separators=(",", ":")) + "\n" | |
| for event in expected).encode() | |
| assert not list(tmp_path.glob(".*.tmp")) | |
| def test_invalid_clip_input_preserves_destination_and_cleans_staging(tmp_path, invalid, error): | |
| source = tmp_path / "input.jsonl" | |
| destination = tmp_path / "output.jsonl" | |
| destination.write_bytes(b"retained destination") | |
| source.write_bytes(b'{"time_ns":1000000000}\n' * 10000 + invalid) | |
| with pytest.raises(error): | |
| recording_clip._clip_input_log(source, destination, start_seconds=1, end_seconds=2) | |
| assert destination.read_bytes() == b"retained destination" | |
| assert not list(tmp_path.glob(".*.tmp")) | |
| def test_reference_input_reader_keeps_unicode_lines_line_numbers_and_stable_order(tmp_path): | |
| source = tmp_path / "input.jsonl" | |
| items = [ | |
| {"time_ns": 2, "seq": 3, "id": "third"}, | |
| {"time_ns": 1, "seq": 2, "id": "first"}, | |
| {"time_ns": 1, "seq": 2, "id": "second"}, | |
| [], {"time_ns": None}, {}, | |
| ] | |
| source.write_text("\u2028".join(json.dumps(item) for item in items), encoding="utf-8") | |
| assert [event["id"] for event in reference_frames.load_input_events(source)] == ["first", "second", "third"] | |
| with source.open("a", encoding="utf-8") as output: | |
| output.write("\x85{") | |
| with pytest.raises(reference_frames.ReferenceFrameError, match="line 7"): | |
| reference_frames.load_input_events(source) | |