File size: 1,473 Bytes
4fc4790
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import Foundation
import Testing
@testable import OpenClaw

@Suite struct FileHandleSafeReadTests {
    @Test func readToEndSafelyReturnsEmptyForClosedHandle() {
        let pipe = Pipe()
        let handle = pipe.fileHandleForReading
        try? handle.close()

        let data = handle.readToEndSafely()
        #expect(data.isEmpty)
    }

    @Test func readSafelyUpToCountReturnsEmptyForClosedHandle() {
        let pipe = Pipe()
        let handle = pipe.fileHandleForReading
        try? handle.close()

        let data = handle.readSafely(upToCount: 16)
        #expect(data.isEmpty)
    }

    @Test func readToEndSafelyReadsPipeContents() {
        let pipe = Pipe()
        let writeHandle = pipe.fileHandleForWriting
        writeHandle.write(Data("hello".utf8))
        try? writeHandle.close()

        let data = pipe.fileHandleForReading.readToEndSafely()
        #expect(String(data: data, encoding: .utf8) == "hello")
    }

    @Test func readSafelyUpToCountReadsIncrementally() {
        let pipe = Pipe()
        let writeHandle = pipe.fileHandleForWriting
        writeHandle.write(Data("hello world".utf8))
        try? writeHandle.close()

        let readHandle = pipe.fileHandleForReading
        let first = readHandle.readSafely(upToCount: 5)
        let second = readHandle.readSafely(upToCount: 32)

        #expect(String(data: first, encoding: .utf8) == "hello")
        #expect(String(data: second, encoding: .utf8) == " world")
    }
}