import Testing
import Foundation
@testable import Music

struct NDJSONTransportTests {

    // Verifies that feed() correctly splits newline-delimited input into individual lines.
    @Test func splitsLinesCorrectly() {
        var lines: [String] = []
        let buffer = NDJSONLineBuffer { lines.append($0) }
        buffer.feed(Data("{\"type\":\"pause\"}\n{\"type\":\"resume\"}\n".utf8))
        #expect(lines == ["{\"type\":\"pause\"}", "{\"type\":\"resume\"}"])
    }

    // Verifies that a line split across two TCP reads is reassembled.
    @Test func handlesPartialLines() {
        var lines: [String] = []
        let buffer = NDJSONLineBuffer { lines.append($0) }
        buffer.feed(Data("{\"type\":\"pa".utf8))
        #expect(lines.isEmpty)
        buffer.feed(Data("use\"}\n".utf8))
        #expect(lines == ["{\"type\":\"pause\"}"])
    }

    // Verifies empty lines are ignored.
    @Test func ignoresEmptyLines() {
        var lines: [String] = []
        let buffer = NDJSONLineBuffer { lines.append($0) }
        buffer.feed(Data("\n\n{\"type\":\"next\"}\n\n".utf8))
        #expect(lines == ["{\"type\":\"next\"}"])
    }
}
