xtool.git
Publicgit clone https://git.maproj.org/xtool.gitxtool.git / Tests / XToolTests / CommandInputTests.swift
CommandInputTests.swift104 lines · 3.8 KiBRaw
| 1 | @testable import XCLIUtils |
| 2 | import XUtils |
| 3 | import ConcurrencyExtras |
| 4 | import Foundation |
| 5 | import Testing |
| 6 | |
| 7 | @Suite(.timeLimit(.minutes(1))) |
| 8 | struct CommandInputTests { |
| 9 | @Test(arguments: [Data("bar".utf8), Data([0, 255, 128, 13, 10])]) |
| 10 | func rawReadCoalescesBytesAfterLine(remainder: Data) async throws { |
| 11 | let channel = DataChannel() |
| 12 | let input = CommandInput(channel: channel) |
| 13 | channel.write(Data("foo\n".utf8) + remainder) |
| 14 | channel.write(Data("next\nlast".utf8)) |
| 15 | channel.finish(error: nil) |
| 16 | |
| 17 | #expect(try await input.readLine() == "foo") |
| 18 | #expect(try await input.read() == remainder + Data("next\nlast".utf8)) |
| 19 | #expect(try await input.read() == nil) |
| 20 | #expect(try await input.readLine() == nil) |
| 21 | } |
| 22 | |
| 23 | @Test |
| 24 | func lineReadsAfterRawReadPreserveFollowingLines() async throws { |
| 25 | let channel = DataChannel() |
| 26 | let input = CommandInput(channel: channel) |
| 27 | channel.write(Data("raw".utf8)) |
| 28 | #expect(try await input.read() == Data("raw".utf8)) |
| 29 | channel.write(Data("foo\r\n\nbar\nlast".utf8)) |
| 30 | channel.finish(error: nil) |
| 31 | |
| 32 | #expect(try await input.readLine() == "foo") |
| 33 | #expect(try await input.readLine() == "") |
| 34 | #expect(try await input.readLine() == "bar") |
| 35 | #expect(try await input.read() == Data("last".utf8)) |
| 36 | #expect(try await input.readLine() == nil) |
| 37 | } |
| 38 | |
| 39 | @Test |
| 40 | func lineReadAccumulatesSplitUTF8AndCRLF() async throws { |
| 41 | let channel = DataChannel() |
| 42 | let input = CommandInput(channel: channel) |
| 43 | // Split both a Unicode scalar and the CRLF terminator across reads. |
| 44 | let chunks = LockIsolated([ |
| 45 | Data([0xF0, 0x9F]), |
| 46 | Data([0x98, 0x80, 13]), |
| 47 | Data("\n\nlast".utf8), |
| 48 | ]) |
| 49 | channel.onRequest { [channel] in |
| 50 | let chunk = chunks.withValue { $0.isEmpty ? nil : $0.removeFirst() } |
| 51 | if let chunk { |
| 52 | channel.write(chunk) |
| 53 | } else { |
| 54 | channel.finish(error: nil) |
| 55 | } |
| 56 | } |
| 57 | try await withInputTestTimeout { () async throws in |
| 58 | #expect(try await input.readLine() == "😀") |
| 59 | #expect(try await input.readLine() == "") |
| 60 | #expect(try await input.readLine() == "last") |
| 61 | #expect(try await input.readLine() == nil) |
| 62 | } cleanup: { |
| 63 | channel.finish(error: nil) |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | @Test |
| 68 | @available(anyAppleOS 26.0, *) |
| 69 | func cancellingLineDiscardsConsumedPrefixAndPreservesFutureInput() async throws { |
| 70 | let channel = DataChannel() |
| 71 | defer { channel.finish(error: nil) } |
| 72 | let input = CommandInput(channel: channel) |
| 73 | channel.write(Data("discarded prefix".utf8)) |
| 74 | let reading = Task.immediate { try await input.readLine() } |
| 75 | defer { reading.cancel() } |
| 76 | |
| 77 | try await withInputTestTimeout { () async throws in |
| 78 | reading.cancel() |
| 79 | await #expect(throws: CancellationError.self) { try await reading.value } |
| 80 | channel.write(Data("next\nkept".utf8)) |
| 81 | #expect(try await input.readLine() == "next") |
| 82 | #expect(try await input.read() == Data("kept".utf8)) |
| 83 | } cleanup: { |
| 84 | channel.finish(error: nil) |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | @Test |
| 89 | @available(anyAppleOS 26.0, *) |
| 90 | func finishingChannelWakesLineReadAndReturnsItsUnterminatedBytes() async throws { |
| 91 | let channel = DataChannel() |
| 92 | let input = CommandInput(channel: channel) |
| 93 | channel.write(Data("partial".utf8)) |
| 94 | let reading = Task.immediate { try await input.readLine() } |
| 95 | defer { reading.cancel(); channel.finish(error: nil) } |
| 96 | try await withInputTestTimeout { () async throws in |
| 97 | channel.finish(error: nil) |
| 98 | #expect(try await reading.value == "partial") |
| 99 | #expect(try await input.read() == nil) |
| 100 | } cleanup: { |
| 101 | channel.finish(error: nil) |
| 102 | } |
| 103 | } |
| 104 | } |