Zum Inhalt

apple/PhysarumApp/Tests/CaptureDeviceCrashTests.swift

import XCTest
@testable import Physarum
import PhysarumEngine

/// End-to-end regression for the capture-from-live-state crash (2026-06-13): a live snapshot
/// → CaptureService.capture restores it on a background worker. Pre-fix this crashed in RELEASE
/// (EXC_BAD_ACCESS in SimEngine.restore's throws return under -O). Verified on macOS, iOS Sim
/// and iPhone JK. NOTE: reproduces only in RELEASE + ENABLE_TESTABILITY.
@MainActor
final class CaptureDeviceCrashTests: XCTestCase {
    func testRealSnapshotStillCapture() async throws {
        let preset = PresetCatalog.fallbackPreset
        guard let coord = RenderCoordinator(initialField: FieldSize(width: 512, height: 910),
                                            initialPreset: preset, live: preset.params) else {
            throw XCTSkip("no metal device")
        }
        guard let snap = coord.takeSnapshot() else { XCTFail("takeSnapshot returned nil"); return }
        let service = CaptureService()
        service.capture(preset: snap.preset, presetName: "devtest",
                        viewWidth: 512, viewHeight: 910, cover: false, format: .portrait,
                        config: CaptureConfig(kind: .still, longEdge: 2048, photoFormat: .png),
                        start: .fromLiveState, snapshot: snap)
        for _ in 0..<300 {
            try await Task.sleep(nanoseconds: 50_000_000)
            switch service.state {
            case .done: return
            case .failed(let m): XCTFail("capture failed: \(m)"); return
            default: continue
            }
        }
        XCTFail("timed out")
    }
}