|
| 1 | +"""Tests for gen code functionality.""" |
| 2 | + |
| 3 | +from cs2_inspect import generate, parse_gen_code, to_gen_code |
| 4 | +from cs2_inspect import ItemPreviewData, Sticker |
| 5 | + |
| 6 | +INSPECT_BASE = "steam://rungame/730/76561202255233023/+csgo_econ_action_preview%20" |
| 7 | + |
| 8 | + |
| 9 | +class TestToGenCode: |
| 10 | + def test_basic_item(self): |
| 11 | + item = ItemPreviewData(defindex=7, paintindex=474, paintseed=306, |
| 12 | + paintwear=0.22540508210659027) |
| 13 | + code = to_gen_code(item) |
| 14 | + assert code == "!gen 7 474 306 0.22540508" |
| 15 | + |
| 16 | + def test_custom_prefix(self): |
| 17 | + item = ItemPreviewData(defindex=7, paintindex=474, paintseed=306, |
| 18 | + paintwear=0.22540508210659027) |
| 19 | + code = to_gen_code(item, prefix="!g") |
| 20 | + assert code == "!g 7 474 306 0.22540508" |
| 21 | + |
| 22 | + def test_with_sticker_in_slot_2(self): |
| 23 | + item = ItemPreviewData( |
| 24 | + defindex=7, paintindex=941, paintseed=2, |
| 25 | + paintwear=0.22540508210659027, |
| 26 | + stickers=[Sticker(slot=2, sticker_id=7203, wear=0.0)], |
| 27 | + ) |
| 28 | + code = to_gen_code(item, prefix="!g") |
| 29 | + assert code == "!g 7 941 2 0.22540508 0 0 0 0 7203 0 0 0 0 0" |
| 30 | + |
| 31 | + def test_with_sticker_and_keychain(self): |
| 32 | + item = ItemPreviewData( |
| 33 | + defindex=7, paintindex=941, paintseed=2, |
| 34 | + paintwear=0.22540508210659027, |
| 35 | + stickers=[Sticker(slot=2, sticker_id=7203, wear=0.0)], |
| 36 | + keychains=[Sticker(slot=0, sticker_id=36, wear=0.0)], |
| 37 | + ) |
| 38 | + code = to_gen_code(item, prefix="!g") |
| 39 | + assert code == "!g 7 941 2 0.22540508 0 0 0 0 7203 0 0 0 0 0 36 0" |
| 40 | + |
| 41 | + def test_zero_wear_float_format(self): |
| 42 | + item = ItemPreviewData(defindex=7, paintindex=474, paintseed=306, |
| 43 | + paintwear=0.0) |
| 44 | + code = to_gen_code(item) |
| 45 | + assert code == "!gen 7 474 306 0" |
| 46 | + |
| 47 | + |
| 48 | +class TestParseGenCode: |
| 49 | + def test_basic_parse(self): |
| 50 | + item = parse_gen_code("!gen 7 474 306 0.22540508") |
| 51 | + assert item.defindex == 7 |
| 52 | + assert item.paintindex == 474 |
| 53 | + assert item.paintseed == 306 |
| 54 | + assert abs(item.paintwear - 0.22540508) < 1e-6 |
| 55 | + |
| 56 | + def test_parse_without_prefix(self): |
| 57 | + item = parse_gen_code("7 474 306 0.22540508") |
| 58 | + assert item.defindex == 7 |
| 59 | + |
| 60 | + def test_parse_with_sticker(self): |
| 61 | + item = parse_gen_code("!gen 7 941 2 0.22540508 0 0 0 0 7203 0 0 0 0 0") |
| 62 | + assert len(item.stickers) == 1 |
| 63 | + assert item.stickers[0].slot == 2 |
| 64 | + assert item.stickers[0].sticker_id == 7203 |
| 65 | + |
| 66 | + def test_parse_with_sticker_and_keychain(self): |
| 67 | + item = parse_gen_code("!g 7 941 2 0.22540508 0 0 0 0 7203 0 0 0 0 0 36 0") |
| 68 | + assert len(item.stickers) == 1 |
| 69 | + assert item.stickers[0].sticker_id == 7203 |
| 70 | + assert len(item.keychains) == 1 |
| 71 | + assert item.keychains[0].sticker_id == 36 |
| 72 | + |
| 73 | + |
| 74 | +class TestGenCodeFromLink: |
| 75 | + def test_from_hex(self): |
| 76 | + from cs2_inspect import generate, gen_code_from_link |
| 77 | + url = generate(7, 474, 306, 0.22540508) |
| 78 | + hex_payload = url.split("csgo_econ_action_preview%20")[1] |
| 79 | + code = gen_code_from_link(hex_payload) |
| 80 | + assert code.startswith("!gen 7 474 306") |
| 81 | + |
| 82 | + def test_from_full_url(self): |
| 83 | + from cs2_inspect import generate, gen_code_from_link |
| 84 | + url = generate(7, 474, 306, 0.22540508) |
| 85 | + code = gen_code_from_link(url) |
| 86 | + assert code.startswith("!gen 7 474 306") |
| 87 | + |
| 88 | + |
| 89 | +class TestGenerate: |
| 90 | + def test_returns_inspect_url(self): |
| 91 | + url = generate(7, 474, 306, 0.22540508) |
| 92 | + assert url.startswith(INSPECT_BASE) |
| 93 | + |
| 94 | + def test_roundtrip(self): |
| 95 | + from cs2_inspect import deserialize |
| 96 | + url = generate(7, 474, 306, 0.22540508) |
| 97 | + # Extract hex payload and deserialize |
| 98 | + hex_payload = url.split(INSPECT_BASE)[1] |
| 99 | + item = deserialize(hex_payload) |
| 100 | + assert item.defindex == 7 |
| 101 | + assert item.paintindex == 474 |
| 102 | + assert item.paintseed == 306 |
| 103 | + assert item.paintwear is not None |
| 104 | + assert abs(item.paintwear - 0.22540508) < 1e-5 |
| 105 | + |
| 106 | + def test_with_stickers(self): |
| 107 | + from cs2_inspect import deserialize |
| 108 | + stickers = [Sticker(slot=0, sticker_id=123, wear=0.1)] |
| 109 | + url = generate(7, 474, 306, 0.22540508, stickers=stickers) |
| 110 | + hex_payload = url.split(INSPECT_BASE)[1] |
| 111 | + item = deserialize(hex_payload) |
| 112 | + assert len(item.stickers) == 1 |
| 113 | + assert item.stickers[0].sticker_id == 123 |
0 commit comments