import Foundation
import Testing

@testable import NucleicCore

enum Fixtures {
    static func url(_ name: String) -> URL {
        guard
            let url = Bundle.module.url(
                forResource: name, withExtension: "ndjson", subdirectory: "Fixtures/claude")
        else {
            fatalError("missing fixture \(name)")
        }
        return url
    }

    static func lines(_ name: String) throws -> [Data] {
        let data = try Data(contentsOf: url(name))
        return data.split(separator: 0x0A, omittingEmptySubsequences: true).map(Data.init)
    }

    /// Run a whole fixture through a fresh decoder, returning just the kinds.
    static func decode(_ name: String) throws -> [AgentEvent.Kind] {
        let decoder = ClaudeStreamDecoder()
        return try lines(name).flatMap { decoder.decode(line: $0).map(\.kind) }
    }
}

/// Locates sibling executable products (fake-claude) next to the test bundle.
/// The test resources bundle (Bundle.module) sits in the build products
/// directory alongside the executables for both XCTest and swift-testing runs.
var productsDirectory: URL {
    Bundle.module.bundleURL.deletingLastPathComponent()
}
