-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_tools.py
More file actions
317 lines (248 loc) · 11.2 KB
/
Copy pathtest_tools.py
File metadata and controls
317 lines (248 loc) · 11.2 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# OpenROM — Universal ROM Compression Suite
# M5 Dev | GPL v3 + Commons Clause
"""
Unit tests for OpenROM Compressor, M3U Generator, CUE Generator, BIN Merger, Header Remover, and CLI integration.
"""
import os
import tempfile
import pytest
import zipfile
import py7zr
from core.compressor import Compressor, CompressionJob, EXCLUDED_EXTENSIONS
from core.converter import CHD_CD_COMPRESSION, CHD_DVD_COMPRESSION
from core.m3u_generator import generate_m3u, clean_disc_name
from core.cue_generator import generate_cue, detect_bin_mode
from core.bin_merger import merge_bins, parse_cue
from core.header_remover import detect_header, remove_header
from core.cli import build_parser, main
def test_compressor_zip_and_extract():
with tempfile.TemporaryDirectory() as td:
sample_file = os.path.join(td, "game.smc")
with open(sample_file, "w") as f:
f.write("Super Mario World ROM Data")
out_dir = os.path.join(td, "out")
os.makedirs(out_dir, exist_ok=True)
job = CompressionJob(filepath=sample_file, output_dir=out_dir, format="zip")
compressor = Compressor()
ok = compressor.compress(job)
assert ok is True
assert job.status == "Done"
assert job.progress == 100.0
zip_path = os.path.join(out_dir, "game.zip")
assert os.path.exists(zip_path)
assert zipfile.is_zipfile(zip_path)
# Test Extraction
ext_dir = os.path.join(td, "ext")
ext_job = CompressionJob(filepath=zip_path, output_dir=ext_dir, format="extract")
ok_ext = compressor.extract(ext_job)
assert ok_ext is True
assert os.path.exists(os.path.join(ext_dir, "game.smc"))
def test_compressor_7z_and_skipped():
with tempfile.TemporaryDirectory() as td:
sample_file = os.path.join(td, "game.sfc")
with open(sample_file, "w") as f:
f.write("SNES ROM Data")
out_dir = os.path.join(td, "out")
job = CompressionJob(filepath=sample_file, output_dir=out_dir, format="7z", level="fast")
compressor = Compressor()
ok = compressor.compress(job)
assert ok is True
assert job.status == "Done"
z7_path = os.path.join(out_dir, "game.7z")
assert os.path.exists(z7_path)
# Test Skipped extensions
chd_file = os.path.join(td, "game.chd")
with open(chd_file, "w") as f:
f.write("CHD Data")
job_chd = CompressionJob(filepath=chd_file, output_dir=out_dir, format="7z")
ok_chd = compressor.compress(job_chd)
assert ok_chd is True
assert job_chd.error == "Skipped (already compressed)"
def test_m3u_generator_manual_and_auto_sort():
with tempfile.TemporaryDirectory() as td:
d1 = os.path.join(td, "Resident Evil 2 (Disc 1).chd")
d2 = os.path.join(td, "Resident Evil 2 (Disc 2).chd")
for d in (d1, d2):
with open(d, "w") as f:
f.write("disc")
# Test manual order preservation
m3u_manual = generate_m3u([d2, d1], td, relative=True, auto_sort=False)
with open(m3u_manual, "r") as f:
content_manual = f.read().splitlines()
assert content_manual[0] == "Resident Evil 2 (Disc 2).chd"
assert content_manual[1] == "Resident Evil 2 (Disc 1).chd"
# Test auto sort
m3u_auto = generate_m3u([d2, d1], td, relative=True, auto_sort=True)
with open(m3u_auto, "r") as f:
content_auto = f.read().splitlines()
assert content_auto[0] == "Resident Evil 2 (Disc 1).chd"
assert content_auto[1] == "Resident Evil 2 (Disc 2).chd"
def test_clean_disc_name():
assert clean_disc_name("Final Fantasy VII (Disc 1).chd") == "Final Fantasy VII"
assert clean_disc_name("Metal Gear Solid (Disk 2)") == "Metal Gear Solid"
assert clean_disc_name("Game CD 1.iso") == "Game"
def test_cue_generator():
with tempfile.TemporaryDirectory() as td:
bin_path = os.path.join(td, "game.bin")
# 2352 bytes per sector with SYNC header and mode byte 0x02 (MODE2/2352)
sync_hdr = b'\x00' + b'\xff' * 10 + b'\x00' + b'\x00\x00\x00\x02'
sector_data = sync_hdr + b'\x00' * (2352 - len(sync_hdr))
with open(bin_path, "wb") as f:
f.write(sector_data * 10)
cue_path = generate_cue(bin_path)
assert os.path.exists(cue_path)
with open(cue_path, "r") as f:
lines = f.read().splitlines()
assert 'FILE "game.bin" BINARY' in lines[0]
assert 'TRACK 01 MODE2/2352' in lines[1]
# Test suffix when cue exists
cue_path_2 = generate_cue(bin_path)
assert cue_path_2.endswith("game_generated.cue")
def test_bin_merger():
with tempfile.TemporaryDirectory() as td:
bin1 = os.path.join(td, "game (Track 1).bin")
bin2 = os.path.join(td, "game (Track 2).bin")
with open(bin1, "wb") as f:
f.write(b"A" * 2352 * 5)
with open(bin2, "wb") as f:
f.write(b"B" * 2352 * 5)
cue_path = os.path.join(td, "game.cue")
with open(cue_path, "w") as f:
f.write('FILE "game (Track 1).bin" BINARY\n')
f.write(' TRACK 01 MODE2/2352\n')
f.write(' INDEX 01 00:00:00\n')
f.write('FILE "game (Track 2).bin" BINARY\n')
f.write(' TRACK 02 AUDIO\n')
f.write(' INDEX 01 00:00:00\n')
merged_bin, merged_cue = merge_bins(cue_path)
assert os.path.exists(merged_bin)
assert os.path.exists(merged_cue)
assert os.path.getsize(merged_bin) == 2352 * 10
tracks = parse_cue(merged_cue)
assert len(tracks) == 2
def test_header_remover():
with tempfile.TemporaryDirectory() as td:
# NES header test
nes_file = os.path.join(td, "mario.nes")
with open(nes_file, "wb") as f:
f.write(b"NES\x1a" + b"\x00" * 12 + b"ROMDATA")
info_nes = detect_header(nes_file)
assert info_nes["has_header"] is True
assert info_nes["header_size"] == 16
clean_nes = remove_header(nes_file, backup=True)
assert os.path.exists(nes_file + ".bak")
with open(clean_nes, "rb") as f:
assert f.read() == b"ROMDATA"
# SNES header test (512 bytes header + 1024 bytes ROM)
snes_file = os.path.join(td, "snes.smc")
with open(snes_file, "wb") as f:
f.write(b"H" * 512 + b"S" * 1024)
info_snes = detect_header(snes_file)
assert info_snes["has_header"] is True
assert info_snes["header_size"] == 512
clean_snes = remove_header(snes_file, backup=False)
assert os.path.getsize(clean_snes) == 1024
def test_iso_to_chd_command_selection():
from core.converter import Converter, ConversionJob
class TestConverter(Converter):
def _run(self, cmd, job):
self.last_cmd = cmd
return True
c = TestConverter()
job = ConversionJob('ps1_game.iso', '/tmp', 'CHD')
c._to_chd(job, 'ps1_game.iso', 'ISO', {'platform': 'PS1'})
assert c.last_cmd[1] == 'createcd'
job = ConversionJob('dc_game.iso', '/tmp', 'CHD')
c._to_chd(job, 'dc_game.iso', 'ISO', {'platform': 'Dreamcast'})
assert c.last_cmd[1] == 'createcd'
job = ConversionJob('ps2_game.iso', '/tmp', 'CHD')
c._to_chd(job, 'ps2_game.iso', 'ISO', {'platform': 'PS2'})
assert c.last_cmd[1] == 'createdvd'
def test_force_platform_overrides_unknown():
"""ConversionJob with force_platform should override detected platform."""
from core.converter import ConversionJob
job = ConversionJob(
filepath="modded_game.iso",
output_dir="/tmp",
target_format="CHD",
force_platform="PS2"
)
# Simulate detect_file returning UNKNOWN
job._file_info = {"format": "ISO", "platform": "UNKNOWN"}
# get_file_info should override with force_platform
job._file_info = None # reset cache
# Can't call detect_file on fake path, so test the field directly
assert job.force_platform == "PS2"
def test_force_platform_ps1_uses_createcd():
"""force_platform=PS1 should result in createcd command for ISO→CHD."""
from core.converter import ConversionJob
job = ConversionJob(
filepath="modded_ps1.iso",
output_dir="/tmp",
target_format="CHD",
force_platform="PS1"
)
job._file_info = {"format": "ISO", "platform": "PS1",
"paired_cue": None, "chd_type": "cd"}
info = job.get_file_info()
assert info["platform"] == "PS1"
def test_force_platform_none_preserves_detection():
"""ConversionJob without force_platform should not override detection."""
from core.converter import ConversionJob
job = ConversionJob(
filepath="normal_game.iso",
output_dir="/tmp",
target_format="CHD",
)
assert job.force_platform is None
def test_ps2_normal_compression_is_not_cdlz():
"""PS2 uses createdvd — cdlz is a CD-only codec and must never appear."""
codec = CHD_DVD_COMPRESSION.get("Normal")
assert codec == "zlib"
assert "cdlz" not in codec
assert "cdzs" not in codec
def test_ps2_high_compression_is_valid():
"""PS2 High compression must use DVD-compatible codecs."""
codec = CHD_DVD_COMPRESSION.get("High")
assert "cdlz" not in codec
assert "cdzs" not in codec
def test_ps2_max_compression_is_valid():
"""PS2 Max compression must use DVD-compatible codecs."""
codec = CHD_DVD_COMPRESSION.get("Max")
assert "cdlz" not in codec
def test_ps1_normal_compression_is_cdlz():
"""PS1 uses createcd — cdlz is correct here."""
codec = CHD_CD_COMPRESSION.get("Normal")
assert codec == "cdlz"
def test_dreamcast_normal_compression_is_cdlz():
"""Dreamcast uses createcd — cdlz is correct."""
codec = CHD_CD_COMPRESSION.get("Normal")
assert codec == "cdlz"
def test_gamecube_normal_compression_is_not_cdlz():
"""GameCube uses createdvd — must not use CD codecs."""
codec = CHD_DVD_COMPRESSION.get("Normal")
assert "cdlz" not in codec
def test_xbox_normal_compression_is_not_cdlz():
"""Xbox uses createdvd — must not use CD codecs."""
codec = CHD_DVD_COMPRESSION.get("Normal")
assert "cdlz" not in codec
def test_get_default_bundled_path_linux():
from unittest.mock import patch
from core.config import get_default_bundled_path
# Test Linux x86_64
with patch('platform.system', return_value='Linux'), patch('platform.machine', return_value='x86_64'):
with patch('os.path.isfile', return_value=True):
path = get_default_bundled_path('chdman')
assert 'assets/linux/x86_64/chdman' in path
# Test Linux aarch64
with patch('platform.system', return_value='Linux'), patch('platform.machine', return_value='aarch64'):
with patch('os.path.isfile', return_value=True):
path = get_default_bundled_path('ecm')
assert 'assets/linux/arm64/ecm' in path
# Test Linux fallback to flat directory when arch folder missing
def fake_isfile(p):
return p.endswith('assets/linux/chdman')
with patch('platform.system', return_value='Linux'), patch('platform.machine', return_value='aarch64'):
with patch('os.path.isfile', side_effect=fake_isfile):
path = get_default_bundled_path('chdman')
assert path.endswith('assets/linux/chdman')