localhost1978's picture
download
raw
5.54 kB
import Foundation
import Testing
@testable import SSK_DocGen
private enum ACPProcessTestError: Error {
case executableNotFound(URL)
}
@Suite("ACP process")
struct ACPProcessTests {
@Test("Serve handles sequential requests and exits at EOF")
func servesSequentialRequests() throws {
let executableURL = productsDirectory.appendingPathComponent("SSK_DocGen")
guard FileManager.default.fileExists(atPath: executableURL.path) else {
throw ACPProcessTestError.executableNotFound(executableURL)
}
let process = Process()
process.executableURL = executableURL
process.arguments = ["serve", "--workspace", FileManager.default.temporaryDirectory.path]
let stdinPipe = Pipe()
let stdoutPipe = Pipe()
let stderrPipe = Pipe()
process.standardInput = stdinPipe
process.standardOutput = stdoutPipe
process.standardError = stderrPipe
try process.run()
let requests = [
"{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"initialize\",\"params\":{}}",
"{\"jsonrpc\":\"2.0\",\"id\":2,\"method\":\"tools/list\",\"params\":{}}",
"{\"jsonrpc\":\"2.0\",\"id\":3,\"method\":\"unknown\",\"params\":{}}"
].joined(separator: "\n") + "\n"
stdinPipe.fileHandleForWriting.write(Data(requests.utf8))
stdinPipe.fileHandleForWriting.closeFile()
process.waitUntilExit()
let output = stdoutPipe.fileHandleForReading.readDataToEndOfFile()
let standardError = stderrPipe.fileHandleForReading.readDataToEndOfFile()
#expect(process.terminationStatus == 0, "stderr: \(String(decoding: standardError, as: UTF8.self))")
let lines = String(decoding: output, as: UTF8.self)
.split(separator: "\n", omittingEmptySubsequences: true)
#expect(lines.count == 3)
let responses = try lines.map { line in
try JSONSerialization.jsonObject(with: Data(line.utf8)) as! [String: Any]
}
#expect(responses.map { $0["id"] as? Int } == [1, 2, 3])
#expect((responses[0]["result"] as? [String: Any])?["protocolVersion"] as? String == "2024-11-05")
#expect(((responses[2]["error"] as? [String: Any])?["code"] as? Int) == -32601)
}
@Test("Serve reads only an allowlisted vault resource")
func readsAllowlistedVaultResource() async throws {
let testRoot = FileManager.default.temporaryDirectory
.appendingPathComponent("SSKDocGen-\(UUID().uuidString)", isDirectory: true)
defer { try? FileManager.default.removeItem(at: testRoot) }
let workspace = testRoot.appendingPathComponent("workspace", isDirectory: true)
let vault = testRoot.appendingPathComponent("vault", isDirectory: true)
let ledger = vault.appendingPathComponent("ledger", isDirectory: true)
try FileManager.default.createDirectory(at: workspace, withIntermediateDirectories: true)
try FileManager.default.createDirectory(at: ledger, withIntermediateDirectories: true)
try "{\"file\":\"/private/example/Secret.swift\"}".write(
to: ledger.appendingPathComponent("docc_index.json"),
atomically: true,
encoding: .utf8
)
let executableURL = productsDirectory.appendingPathComponent("SSK_DocGen")
guard FileManager.default.fileExists(atPath: executableURL.path) else {
throw ACPProcessTestError.executableNotFound(executableURL)
}
let process = Process()
process.executableURL = executableURL
process.arguments = ["serve", "--workspace", workspace.path, "--vault", vault.path]
let stdinPipe = Pipe()
let stdoutPipe = Pipe()
let stderrPipe = Pipe()
process.standardInput = stdinPipe
process.standardOutput = stdoutPipe
process.standardError = stderrPipe
try process.run()
// Give the serve process time to initialize its ACP context and registry.
try await Task.sleep(nanoseconds: 500_000_000)
let request = "{\"jsonrpc\":\"2.0\",\"id\":4,\"method\":\"resources/read\",\"params\":{\"uri\":\"file:///SSK_Ledger_Vault/docc_index.json\"}}\n"
stdinPipe.fileHandleForWriting.write(Data(request.utf8))
stdinPipe.fileHandleForWriting.closeFile()
process.waitUntilExit()
let output = stdoutPipe.fileHandleForReading.readDataToEndOfFile()
let standardError = stderrPipe.fileHandleForReading.readDataToEndOfFile()
#expect(process.terminationStatus == 0, "stderr: \(String(decoding: standardError, as: UTF8.self))")
let response = try JSONSerialization.jsonObject(with: output) as! [String: Any]
let result = try #require(response["result"] as? [String: Any], "resources/read should return a result object, not an error")
let contents = try #require(result["contents"] as? [[String: Any]])
let resource = try #require(contents.first)
let text = try #require(resource["text"] as? String)
#expect(resource["uri"] as? String == "file:///SSK_Ledger_Vault/docc_index.json")
#expect(!text.contains("/private/example/Secret.swift"))
}
private var productsDirectory: URL {
#if os(macOS)
for bundle in Bundle.allBundles where bundle.bundlePath.hasSuffix(".xctest") {
return bundle.bundleURL.deletingLastPathComponent()
}
return Bundle.main.bundleURL
#else
return Bundle.main.bundleURL
#endif
}
}

Xet Storage Details

Size:
5.54 kB
·
Xet hash:
7be18fc67f4fdf0e32af9a9ce1b81787d1ba88662e30da0af42a038a362debe7

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.