// SourceKitLSPClientTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT #if os(macOS) || os(Linux) import Testing import Foundation @testable import LyrebirdLiveCompile @Suite("SourceKitLSPClient") struct SourceKitLSPClientTests { /// A server that never answers must make a request FAIL (`timedOut`) within /// the timeout — not hang. Uses `/bin/sleep` as a stand-in process that stays /// alive but speaks no LSP, so the `initialize` request in `start()` times /// out. Regression guard for the task-group/continuation deadlock where a /// dropped-but-not-resumed continuation left `request(...)` awaiting forever. @Test func requestTimesOutOnUnresponsiveServer() async { let client: SourceKitLSPClient = SourceKitLSPClient( executableURL: URL(fileURLWithPath: "/bin/sleep"), arguments: ["3600"], requestTimeout: 0.5 ) let start: Date = Date() await #expect(throws: SourceKitLSPError.self) { try await client.start(rootURI: "file:///tmp") } let elapsed: TimeInterval = Date().timeIntervalSince(start) // Must resolve near the 0.5 s timeout, not hang (the old bug never returned). #expect(elapsed < 5.0, "start() took \(elapsed)s — expected a prompt timeout") client.shutdown() } /// A server that launches then exits immediately (EOF on stdout) must fail /// an in-flight request promptly rather than hang the full timeout — the /// reader marks the client not-running and fails pending requests on EOF. /// `/usr/bin/true` exits 0 at once. @Test func serverExitFailsRequestPromptly() async { let client: SourceKitLSPClient = SourceKitLSPClient( executableURL: URL(fileURLWithPath: "/usr/bin/true"), requestTimeout: 10.0 ) let start: Date = Date() await #expect(throws: SourceKitLSPError.self) { try await client.start(rootURI: "file:///tmp") } let elapsed: TimeInterval = Date().timeIntervalSince(start) #expect(elapsed < 5.0, "server-exit took \(elapsed)s — expected a prompt failure, not a timeout") client.shutdown() } /// Launching a non-existent executable surfaces `.launchFailed` rather than /// crashing or hanging. @Test func launchFailureIsReported() async { let client: SourceKitLSPClient = SourceKitLSPClient( executableURL: URL(fileURLWithPath: "/nonexistent/sourcekit-lsp-xyz"), requestTimeout: 0.5 ) await #expect(throws: SourceKitLSPError.self) { try await client.start(rootURI: "file:///tmp") } client.shutdown() } } #endif