"""倒计时前缀文字位图测试(证据 analysis/web/timedjs2.js:180-236)。""" import pytest from ppclock import textbitmap as TB class TestCharUnits: def test_chinese_counts_one(self): assert TB.char_units("距离") == 2.0 def test_english_counts_half(self): assert TB.char_units("ab") == 1.0 assert TB.char_units("ab中") == 2.0 assert TB.char_units("a中") == 1.5 class TestBitmap: def test_clock_bitmap_size(self): """16px 格:n 个单位 → n*32 字节""" bmp = TB.render_text_bitmap("中考", grid=16) assert len(bmp) == 2 * 32 def test_calendar_bitmap_size(self): """24px 格:n 个单位 → n*72 字节""" bmp = TB.render_text_bitmap("中考", grid=24) assert len(bmp) == 2 * 72 def test_bitmap_not_blank(self): bmp = TB.render_text_bitmap("试", grid=16) assert any(bmp) def test_grid_must_be_16_or_24(self): with pytest.raises(ValueError): TB.render_text_bitmap("x", grid=12) class TestFrames: def test_clock_single_pack(self): """≤4 单位:单包 0{n}+位图""" frames = TB.prefix_frames("目标", "clock") assert len(frames) == 1 assert frames[0][0:1] == bytes([0x02]) # '0'+2 assert len(frames[0]) == 1 + 2 * 32 def test_clock_two_packs(self): """>4 单位:首包 04+128B,次包 0{n}+余量""" frames = TB.prefix_frames("距离中考还有", "clock") # 6 单位 assert len(frames) == 2 assert frames[0][0] == 0x04 assert len(frames[0]) == 1 + 128 assert frames[1][0] == 0x06 assert len(frames[1]) == 1 + 2 * 32 def test_clock_over_8_units_rejected(self): with pytest.raises(ValueError): TB.prefix_frames("一二三四五六七八九", "clock") def test_calendar_per_char_pack(self): """日历模式:每字一包 990{j}+72B""" frames = TB.prefix_frames("中考", "calendar") assert len(frames) == 2 assert frames[0][:2] == bytes([0x99, 0x00]) assert len(frames[0]) == 2 + 72 assert frames[1][:2] == bytes([0x99, 0x01]) def test_calendar_over_4_units_rejected(self): with pytest.raises(ValueError): TB.prefix_frames("一二三四五", "calendar") def test_fractional_units_rejected(self): """0.5 单位(英文混排)JS 会产生非法 hex——CLI 拒绝""" with pytest.raises(ValueError): TB.prefix_frames("a中", "clock")