import XCTest
@testable import SaplingEditor

final class EditorPerformanceProfilingTests: XCTestCase {
    func testDocumentProfileRecordsMarkdownStructure() {
        let source = """
        # Title

        - Item
        1. Step
        > Quote
        Text with **bold**, *italic*, `code`, [ref], and ![image](image.png).
        ```
        code
        ```
        """

        let profile = EditorBenchmarkProfiler.documentProfile(fileName: "Sample.md", source: source)

        XCTAssertEqual(profile.fileName, "Sample.md")
        XCTAssertEqual(profile.headingCount, 1)
        XCTAssertEqual(profile.unorderedListItemCount, 1)
        XCTAssertEqual(profile.orderedListItemCount, 1)
        XCTAssertEqual(profile.blockquoteCount, 1)
        XCTAssertEqual(profile.fencedCodeFenceCount, 2)
        XCTAssertEqual(profile.inlineCodeLineCount, 1)
        XCTAssertEqual(profile.boldLineCount, 1)
        XCTAssertEqual(profile.italicLineCount, 1)
        XCTAssertEqual(profile.inlineLinkCount, 0)
        XCTAssertEqual(profile.referenceLinkLikeCount, 1)
        XCTAssertEqual(profile.imageCount, 1)
    }

    func testProfilerProducesBenchmarkMeasurements() throws {
        let directory = FileManager.default.temporaryDirectory
            .appendingPathComponent(UUID().uuidString, isDirectory: true)
        try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true)
        let url = directory.appendingPathComponent("Benchmark.md")
        try "# Heading\n\nText with **bold** and `code`.\n".write(to: url, atomically: true, encoding: .utf8)

        let result = try EditorBenchmarkProfiler.profileDocument(at: url)

        XCTAssertEqual(result.document.lineCount, 4)
        XCTAssertTrue(result.renderedModeTrace.renderedModeWasScheduled)
        XCTAssertTrue(result.measurements.contains { $0.name == "file_read" })
        XCTAssertTrue(result.measurements.contains { $0.name == "document_parse_editor_state_init" })
        XCTAssertTrue(result.measurements.contains { $0.name == "render_plan_generation_all_lines" })
        XCTAssertTrue(result.measurements.contains { $0.name == "dirty_line_invalidation_typing" })
    }
}
