File size: 1,522 Bytes
3e04895
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
"""Unit tests for the state codec (no torch / GPU required)."""

import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parents[1]))

from qwenjev.state_codec import (  # noqa: E402
    canonical_state, encode_observation, frame_hash, grid_diff, grid_to_image,
    grid_to_text,
)

GRID = [[0, 0, 3], [0, 5, 3], [1, 0, 0]]
GRID2 = [[0, 0, 3], [2, 5, 3], [1, 0, 0]]  # one cell changed (0,0)->? (1,0)


def test_grid_to_text_roundtrip_shape():
    text = grid_to_text(GRID)
    lines = text.split("\n")
    assert len(lines) == 3
    assert lines[0] == "003"


def test_diff_marks_changed_cells():
    diff = grid_diff(GRID, GRID2)
    changed_row = diff.split("\n")[1]
    assert changed_row[0] == "*"
    # unchanged cells keep their hex value
    assert diff.split("\n")[0] == "003"


def test_canonical_state_is_deterministic():
    a = canonical_state("003\n053", {"L": 2, "H": 3})
    b = canonical_state("003\n053", {"L": 2, "H": 3})
    assert a == b
    assert a.startswith("qwenjev-state-v1")


def test_frame_hash_stable():
    assert frame_hash(GRID) == frame_hash([list(r) for r in GRID])
    assert frame_hash(GRID) != frame_hash(GRID2)


def test_encode_observation_changed_flag():
    obs = encode_observation([GRID2], [GRID], levels_completed=1, win_levels=3)
    assert obs.changed is True
    assert obs.levels_completed == 1
    assert len(obs.frame_hashes) == 1


def test_grid_to_image_dimensions():
    img = grid_to_image(GRID, cell_px=2)
    assert img.size == (6, 6)