import XCTest @testable import MarkBaseFS class FrameIndexTableTests: XCTestCase { private var frameIndexTable: FrameIndexTable? override func setUpWithError() throws { // Create a temporary database for testing let tempDir = FileManager.default.temporaryDirectory let testDbPath = tempDir.appendingPathComponent("test_markbasefs_unit.sqlite").path frameIndexTable = FrameIndexTable(dbPath: testDbPath) print("Test database created at: \(testDbPath)") } override func tearDownWithError() throws { // Clean up test database frameIndexTable = nil // Remove test database file let tempDir = FileManager.default.temporaryDirectory let testDbPath = tempDir.appendingPathComponent("test_markbasefs_unit.sqlite").path if FileManager.default.fileExists(atPath: testDbPath) { try FileManager.default.removeItem(atPath: testDbPath) print("Test database removed") } } // Test database creation func testDatabaseCreation() throws { XCTAssertNotNil(frameIndexTable, "FrameIndexTable should be initialized") print("Database creation test passed") } // Test single frame insert func testInsertSingleFrame() throws { guard let table = frameIndexTable else { XCTFail("FrameIndexTable not initialized") return } let success = table.insertFrame( frameId: "unit_test_frame_001", videoId: "unit_test_video_001", frameIndex: 1, frameFile: "/unit_test/video001/frame001.dpx", frameOffset: 0, frameSize: 1024000, frameChecksum: "unit_test_checksum_001" ) XCTAssert(success, "Single frame insert should succeed") print("Single frame insert test passed") } // Test get frame func testGetFrame() throws { guard let table = frameIndexTable else { XCTFail("FrameIndexTable not initialized") return } // Insert a frame first let insertSuccess = table.insertFrame( frameId: "unit_test_frame_get", videoId: "unit_test_video_get", frameIndex: 1, frameFile: "/unit_test/video_get/frame1.dpx", frameOffset: 0, frameSize: 1024000, frameChecksum: "unit_test_checksum_get" ) XCTAssert(insertSuccess, "Frame insert should succeed") // Get the frame let frameInfo = table.getFrame(frameId: "unit_test_frame_get") XCTAssertNotNil(frameInfo, "Frame info should be retrieved") if let info = frameInfo { XCTAssertEqual(info["frame_id"] as? String, "unit_test_frame_get") XCTAssertEqual(info["video_id"] as? String, "unit_test_video_get") XCTAssertEqual(info["frame_index"] as? Int, 1) print("Get frame test passed") } } // Test update frame func testUpdateFrame() throws { guard let table = frameIndexTable else { XCTFail("FrameIndexTable not initialized") return } // Insert a frame first let insertSuccess = table.insertFrame( frameId: "unit_test_frame_update", videoId: "unit_test_video_update", frameIndex: 1, frameFile: "/unit_test/video_update/frame1.dpx", frameOffset: 0, frameSize: 1024000, frameChecksum: "unit_test_checksum_update" ) XCTAssert(insertSuccess, "Frame insert should succeed") // Update the frame let updateSuccess = table.updateFrame(frameId: "unit_test_frame_update", updates: [ "frame_size": 2048000, "frame_checksum": "updated_checksum" ]) XCTAssert(updateSuccess, "Frame update should succeed") // Verify update let frameInfo = table.getFrame(frameId: "unit_test_frame_update") if let info = frameInfo { XCTAssertEqual(info["frame_size"] as? Int, 2048000) XCTAssertEqual(info["frame_checksum"] as? String, "updated_checksum") print("Update frame test passed") } } // Test delete frame func testDeleteFrame() throws { guard let table = frameIndexTable else { XCTFail("FrameIndexTable not initialized") return } // Insert a frame first let insertSuccess = table.insertFrame( frameId: "unit_test_frame_delete", videoId: "unit_test_video_delete", frameIndex: 1, frameFile: "/unit_test/video_delete/frame1.dpx", frameOffset: 0, frameSize: 1024000, frameChecksum: "unit_test_checksum_delete" ) XCTAssert(insertSuccess, "Frame insert should succeed") // Delete the frame let deleteSuccess = table.deleteFrame(frameId: "unit_test_frame_delete") XCTAssert(deleteSuccess, "Frame delete should succeed") // Verify delete let frameInfo = table.getFrame(frameId: "unit_test_frame_delete") XCTAssertNil(frameInfo, "Deleted frame should not be found") print("Delete frame test passed") } // Test lock and unlock frame func testLockUnlockFrame() throws { guard let table = frameIndexTable else { XCTFail("FrameIndexTable not initialized") return } // Insert a frame first let insertSuccess = table.insertFrame( frameId: "unit_test_frame_lock", videoId: "unit_test_video_lock", frameIndex: 1, frameFile: "/unit_test/video_lock/frame1.dpx", frameOffset: 0, frameSize: 1024000, frameChecksum: "unit_test_checksum_lock" ) XCTAssert(insertSuccess, "Frame insert should succeed") // Lock the frame let lockSuccess = table.lockFrame(frameId: "unit_test_frame_lock") XCTAssert(lockSuccess, "Frame lock should succeed") // Verify lock state let lockedFrame = table.getFrame(frameId: "unit_test_frame_lock") if let info = lockedFrame { XCTAssertEqual(info["frame_lock_state"] as? Int, 1) print("Lock frame test passed") } // Unlock the frame let unlockSuccess = table.unlockFrame(frameId: "unit_test_frame_lock") XCTAssert(unlockSuccess, "Frame unlock should succeed") // Verify unlock state let unlockedFrame = table.getFrame(frameId: "unit_test_frame_lock") if let info = unlockedFrame { XCTAssertEqual(info["frame_lock_state"] as? Int, 0) print("Unlock frame test passed") } } }