import 'dart:io'; import 'package:flutter_test/flutter_test.dart'; import 'package:ponshu_room_lite/services/analysis_cache_service.dart'; void main() { group('AnalysisCacheService - computeImageHash', () { late String tempFilePath; late String tempFilePath2; setUp(() async { // テスト用の一時ファイルを作成 final tempDir = Directory.systemTemp.createTempSync('ponshu_test_'); final tempFile = File('${tempDir.path}/test_image.jpg'); await tempFile.writeAsBytes([0xFF, 0xD8, 0xFF, 0xE0, 0x00, 0x10]); // JPEG header bytes tempFilePath = tempFile.path; final tempFile2 = File('${tempDir.path}/test_image_2.jpg'); await tempFile2.writeAsBytes([0xFF, 0xD8, 0xFF, 0xE0, 0x00, 0x20]); // Different bytes tempFilePath2 = tempFile2.path; }); tearDown(() { // 一時ファイルをクリーンアップ try { File(tempFilePath).deleteSync(); File(tempFilePath2).deleteSync(); File(tempFilePath).parent.deleteSync(); } catch (_) {} }); test('should return consistent hash for the same file', () async { final hash1 = await AnalysisCacheService.computeImageHash(tempFilePath); final hash2 = await AnalysisCacheService.computeImageHash(tempFilePath); expect(hash1, equals(hash2)); expect(hash1.length, 64); // SHA-256 produces 64 hex characters }); test('should return different hashes for different files', () async { final hash1 = await AnalysisCacheService.computeImageHash(tempFilePath); final hash2 = await AnalysisCacheService.computeImageHash(tempFilePath2); expect(hash1, isNot(equals(hash2))); }); test('should return the file path when file does not exist', () async { const nonExistentPath = '/non/existent/file.jpg'; final result = await AnalysisCacheService.computeImageHash(nonExistentPath); // エラー時はファイルパスがそのまま返される(フォールバック動作) expect(result, nonExistentPath); }); test('hash should be a valid hex string', () async { final hash = await AnalysisCacheService.computeImageHash(tempFilePath); // SHA-256ハッシュは0-9a-fの64文字 expect(hash, matches(RegExp(r'^[0-9a-f]{64}$'))); }); }); }