Coverage for src/romfarmer/cli/metadata_test_commands.py: 0%

101 statements  

« prev     ^ index     » next       coverage.py v7.15.4, created at 2026-09-01 23:08 -0700

1""" 

2CLI commands for metadata testing and debugging. 

3 

4Commands: 

5- test-dat: Test DAT manager and hash capture 

6- test-transform: Test transformation recording 

7""" 

8 

9from pathlib import Path 

10 

11import click 

12from rich.console import Console 

13 

14from ..metadata.dat_manager import DATManager 

15from ..metadata.database import MetadataDatabase 

16from ..metadata.hash_capture import SmartHashCapture 

17from ..metadata.transformation_recorder import TransformationRecorder 

18 

19console = Console() 

20 

21 

22@click.command(name="test-dat") 

23@click.option( 

24 "--dat-dirs", 

25 "-d", 

26 type=click.Path(exists=True, path_type=Path), 

27 multiple=True, 

28 help="DAT directories to load (can specify multiple)", 

29) 

30@click.option( 

31 "--test-file", 

32 "-f", 

33 type=click.Path(exists=True, path_type=Path), 

34 help="Test file to hash", 

35) 

36@click.option( 

37 "--system", 

38 "-s", 

39 help="System name for DAT lookup", 

40) 

41@click.option( 

42 "--database", 

43 type=click.Path(path_type=Path), 

44 default="metadata/database/romfarmer.db", 

45 help="Path to metadata database", 

46) 

47def test_dat( 

48 dat_dirs: tuple, 

49 test_file: Path | None, 

50 system: str | None, 

51 database: Path, 

52): 

53 """ 

54 Test DAT manager and hash capture system. 

55 

56 This command loads DAT files and tests the 3-tier hash capture: 

57 1. DAT file lookup (instant) 

58 2. Hash cache (fast) 

59 3. Calculate (slow) 

60 

61 Example: 

62 

63 # Load DATs and show statistics 

64 rom-farmer metadata test-dat -d /path/to/dats/redump -d /path/to/dats/nointro 

65 

66 # Test hash capture on a file 

67 rom-farmer metadata test-dat -d /path/to/dats/redump \\ 

68 -f /path/to/stage/saturn/3D\\ Baseball\\ \\(USA\\).chd \\ 

69 -s saturn 

70 """ 

71 try: 

72 # Load DAT manager 

73 if dat_dirs: 

74 console.print(f"[cyan]Loading DAT files from {len(dat_dirs)} directories...[/cyan]") 

75 dat_manager = DATManager(list(dat_dirs)) 

76 

77 # Show DAT statistics 

78 console.print("\n[bold green]✓ DAT Manager Loaded[/bold green]") 

79 console.print(f" Systems: {dat_manager.get_system_count()}") 

80 console.print(f" Total Entries: {dat_manager.get_entry_count():,}") 

81 console.print("\n[bold]Available Systems:[/bold]") 

82 for sys in sorted(dat_manager.get_systems())[:20]: 

83 console.print(f" - {sys}") 

84 if dat_manager.get_system_count() > 20: 

85 console.print(f" ... and {dat_manager.get_system_count() - 20} more") 

86 else: 

87 console.print( 

88 "[yellow]No DAT directories specified. Using cache/calculation only.[/yellow]" 

89 ) 

90 dat_manager = None 

91 

92 # Test hash capture if file specified 

93 if test_file: 

94 console.print("\n[bold cyan]Testing Hash Capture[/bold cyan]") 

95 console.print(f"File: {test_file}") 

96 console.print(f"Size: {test_file.stat().st_size / (1024 * 1024):.1f} MB") 

97 

98 if system: 

99 console.print(f"System: {system}") 

100 

101 # Create database and hash capture 

102 db = MetadataDatabase(database) 

103 with db.get_session() as session: 

104 capture = SmartHashCapture(dat_manager, session) 

105 

106 # Get hashes 

107 console.print("\n[cyan]Capturing hashes...[/cyan]") 

108 hashes = capture.get_source_hashes(test_file, system) 

109 

110 # Show results 

111 console.print("\n[bold green]✓ Hash Capture Complete[/bold green]") 

112 

113 if hashes.from_dat: 

114 console.print(f" [green]Source: DAT file ({hashes.dat_name})[/green]") 

115 elif hashes.from_cache: 

116 console.print(" [yellow]Source: Hash cache[/yellow]") 

117 else: 

118 console.print( 

119 f" [red]Source: Calculated ({hashes.calculation_time:.1f}s)[/red]" 

120 ) 

121 

122 console.print(f"\n MD5: {hashes.md5}") 

123 console.print(f" SHA1: {hashes.sha1}") 

124 console.print(f" SHA256: {hashes.sha256}") 

125 console.print(f" CRC32: {hashes.crc32}") 

126 console.print(f" Size: {hashes.size:,} bytes") 

127 

128 # Show performance stats 

129 console.print() 

130 capture.print_stats() 

131 

132 elif dat_dirs: 

133 console.print( 

134 "\n[yellow]Tip: Use --test-file to test hash capture on a ROM file[/yellow]" 

135 ) 

136 

137 except FileNotFoundError as e: 

138 console.print(f"[red]✗ File not found:[/red] {e}") 

139 raise click.Abort() from e 

140 except Exception as e: 

141 console.print(f"[red]✗ Error:[/red] {e}") 

142 import traceback 

143 

144 traceback.print_exc() 

145 raise click.Abort() from e 

146 

147 

148@click.command(name="test-transform") 

149@click.option( 

150 "--dat-dirs", 

151 "-d", 

152 type=click.Path(exists=True, path_type=Path), 

153 multiple=True, 

154 help="DAT directories to load", 

155) 

156@click.option( 

157 "--source-file", 

158 "-s", 

159 type=click.Path(exists=True, path_type=Path), 

160 required=True, 

161 help="Source file (before transformation)", 

162) 

163@click.option( 

164 "--final-file", 

165 "-f", 

166 type=click.Path(exists=True, path_type=Path), 

167 required=True, 

168 help="Final file (after transformation)", 

169) 

170@click.option( 

171 "--system", 

172 type=str, 

173 required=True, 

174 help="System name (e.g., 'saturn', 'xbox360')", 

175) 

176@click.option( 

177 "--tool", 

178 type=str, 

179 default="manual", 

180 help="Transformation tool name", 

181) 

182@click.option( 

183 "--version", 

184 type=str, 

185 default="unknown", 

186 help="Tool version", 

187) 

188@click.option( 

189 "--database", 

190 type=click.Path(path_type=Path), 

191 default="metadata/database/romfarmer.db", 

192 help="Path to metadata database", 

193) 

194def test_transform( 

195 dat_dirs: tuple, 

196 source_file: Path, 

197 final_file: Path, 

198 system: str, 

199 tool: str, 

200 version: str, 

201 database: Path, 

202): 

203 """ 

204 Test transformation recording. 

205 

206 This command simulates recording a ROM transformation. Use this to test 

207 the transformation tracking system before integrating it into your 

208 ROM processing workflow. 

209 

210 Example: 

211 

212 # Record a Saturn ISO → CHD transformation 

213 rom-farmer metadata test-transform \\ 

214 -d /path/to/dats/redump \\ 

215 -s /path/to/source/saturn/game.iso \\ 

216 -f /path/to/stage/saturn/game.chd \\ 

217 --system saturn \\ 

218 --tool chdman \\ 

219 --version 0.251 

220 

221 This will: 

222 1. Look up source ISO hash in Redump DAT (instant!) 

223 2. Calculate CHD hash (or use cache) 

224 3. Store transformation: ISO hash → CHD hash 

225 4. Show statistics 

226 """ 

227 try: 

228 # Load DAT manager if specified 

229 dat_manager = None 

230 if dat_dirs: 

231 console.print("[cyan]Loading DAT files...[/cyan]") 

232 dat_manager = DATManager(list(dat_dirs)) 

233 console.print(f"[green]✓ Loaded {dat_manager.get_entry_count():,} entries[/green]") 

234 

235 # Create database and recorder 

236 db = MetadataDatabase(database) 

237 

238 with db.get_session() as session: 

239 recorder = TransformationRecorder(session, dat_manager) 

240 

241 # Simulate a transformation 

242 console.print("\n[bold cyan]Simulating Transformation Recording...[/bold cyan]") 

243 

244 with recorder.record_transformation( 

245 source_file=source_file, 

246 system=system, 

247 tool=tool, 

248 version=version, 

249 params={"test": True, "simulated": True}, 

250 ) as transform: 

251 # In real workflow, processing would happen here 

252 # For testing, we just set the final file that already exists 

253 transform.set_final_file(final_file) 

254 

255 # Show statistics 

256 console.print() 

257 recorder.print_stats() 

258 

259 # Test reverse lookup 

260 console.print("\n[bold cyan]Testing Reverse Lookup...[/bold cyan]") 

261 found = recorder.find_source_hash(final_file) 

262 

263 if found: 

264 console.print("[green]✓ Successfully found source hash for final file![/green]") 

265 console.print(f" Source: {found.source_file_name}") 

266 console.print(f" Final: {found.final_file_name}") 

267 console.print( 

268 f" Tool: {found.transformation_tool} v{found.transformation_version}" 

269 ) 

270 console.print(f" Duration: {found.transformation_duration_seconds:.1f}s") 

271 

272 except FileNotFoundError as e: 

273 console.print(f"[red]✗ File not found:[/red] {e}") 

274 raise click.Abort() from e 

275 except Exception as e: 

276 console.print(f"[red]✗ Error:[/red] {e}") 

277 import traceback 

278 

279 traceback.print_exc() 

280 raise click.Abort() from e