-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathTerminalOutputDeliveryQueueTests.swift
More file actions
177 lines (151 loc) · 6.67 KB
/
Copy pathTerminalOutputDeliveryQueueTests.swift
File metadata and controls
177 lines (151 loc) · 6.67 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import CMUXMobileCore
import Foundation
import Testing
@testable import CmuxMobileShell
@Test func terminalOutputQueueDeliversFirstChunkImmediately() {
var queue = TerminalOutputDeliveryQueue()
let first = TerminalOutputDelivery(bytes: Data("first".utf8), replaceable: false)
#expect(queue.enqueue(first) == first)
#expect(queue.pendingCount == 0)
}
@Test func terminalOutputQueueIgnoresCompletionWhenNothingIsInFlight() {
var queue = TerminalOutputDeliveryQueue()
#expect(queue.completeInFlight() == nil)
#expect(queue.isIdle)
}
@MainActor
@Test func staleStreamAckDoesNotAdvanceReplacementOutputQueue() async throws {
let store = MobileShellComposite.preview()
let surfaceID = "terminal"
var oldIterator = store.terminalOutputStream(surfaceID: surfaceID).makeAsyncIterator()
store.deliverTerminalBytes(Data("old-first".utf8), surfaceID: surfaceID)
let oldChunk = try #require(await oldIterator.next())
var currentIterator = store.terminalOutputStream(surfaceID: surfaceID).makeAsyncIterator()
store.deliverTerminalBytes(Data("new-first".utf8), surfaceID: surfaceID)
let currentChunk = try #require(await currentIterator.next())
store.deliverTerminalBytes(Data("new-second".utf8), surfaceID: surfaceID)
store.terminalOutputDidProcess(surfaceID: surfaceID, streamToken: oldChunk.streamToken)
#expect(store.terminalOutputQueuesBySurfaceID[surfaceID]?.pendingCount == 1)
store.terminalOutputDidProcess(surfaceID: surfaceID, streamToken: currentChunk.streamToken)
let secondChunk = try #require(await currentIterator.next())
#expect(String(decoding: secondChunk.data, as: UTF8.self) == "new-second")
}
@Test func terminalOutputQueueCoalescesReplaceableViewportFramesBehindBackpressure() {
var queue = TerminalOutputDeliveryQueue()
let inFlight = TerminalOutputDelivery(bytes: Data("in-flight".utf8), replaceable: false)
let oldViewport = TerminalOutputDelivery(bytes: Data("old viewport".utf8), replaceable: true)
let latestViewport = TerminalOutputDelivery(bytes: Data("latest viewport".utf8), replaceable: true)
#expect(queue.enqueue(inFlight) == inFlight)
#expect(queue.enqueue(oldViewport) == nil)
#expect(queue.enqueue(latestViewport) == nil)
#expect(queue.pendingCount == 1)
#expect(queue.completeInFlight() == latestViewport)
#expect(queue.completeInFlight() == nil)
#expect(queue.isIdle)
}
@Test func terminalOutputQueueCoalescesRenderGridFramesBeforeSynthesizingBytes() throws {
var queue = TerminalOutputDeliveryQueue()
let inFlight = TerminalOutputDelivery(bytes: Data("in-flight".utf8), replaceable: false)
let oldFrame = try MobileTerminalRenderGridFrame.fromPlainRows(
surfaceID: "terminal",
stateSeq: 1,
columns: 12,
rows: 2,
text: "old\nviewport",
full: false,
changedRows: [0, 1]
)
let latestFrame = try MobileTerminalRenderGridFrame.fromPlainRows(
surfaceID: "terminal",
stateSeq: 2,
columns: 12,
rows: 2,
text: "latest\nviewport",
full: false,
changedRows: [0, 1]
)
#expect(queue.enqueue(inFlight) == inFlight)
#expect(queue.enqueue(TerminalOutputDelivery(renderGrid: oldFrame, replaceable: true)) == nil)
#expect(queue.enqueue(TerminalOutputDelivery(renderGrid: latestFrame, replaceable: true)) == nil)
let maybeDelivered = queue.completeInFlight()
let delivered = try #require(maybeDelivered)
let vt = try #require(String(data: delivered.bytes, encoding: .utf8))
#expect(vt.contains("latest"))
#expect(!vt.contains("old"))
}
@Test func terminalOutputDeliveryCarriesRenderGridActiveScreen() throws {
let frame = try MobileTerminalRenderGridFrame(
surfaceID: "terminal",
stateSeq: 1,
columns: 12,
rows: 2,
rowSpans: [],
activeScreen: .alternate
)
let delivery = TerminalOutputDelivery(renderGrid: frame, replaceable: false)
let rawDelivery = TerminalOutputDelivery(bytes: Data("raw".utf8), replaceable: false)
#expect(delivery.activeScreen == .alternate)
#expect(rawDelivery.activeScreen == nil)
}
@Test func terminalOutputQueuePreservesNonreplaceableBarriers() {
var queue = TerminalOutputDeliveryQueue()
let inFlight = TerminalOutputDelivery(bytes: Data("in-flight".utf8), replaceable: false)
let viewport = TerminalOutputDelivery(bytes: Data("viewport".utf8), replaceable: true)
let rawBytes = TerminalOutputDelivery(bytes: Data("raw".utf8), replaceable: false)
let laterViewport = TerminalOutputDelivery(bytes: Data("later viewport".utf8), replaceable: true)
#expect(queue.enqueue(inFlight) == inFlight)
#expect(queue.enqueue(viewport) == nil)
#expect(queue.enqueue(rawBytes) == nil)
#expect(queue.enqueue(laterViewport) == nil)
#expect(queue.pendingCount == 3)
#expect(queue.completeInFlight() == viewport)
#expect(queue.completeInFlight() == rawBytes)
#expect(queue.completeInFlight() == laterViewport)
#expect(queue.completeInFlight() == nil)
}
@Test func terminalOutputQueueDrainsRawFallbackBacklogInOrder() {
var queue = TerminalOutputDeliveryQueue()
let inFlight = TerminalOutputDelivery(bytes: Data("in-flight".utf8), replaceable: false)
#expect(queue.enqueue(inFlight) == inFlight)
for index in 0..<128 {
let delivery = TerminalOutputDelivery(bytes: Data("raw-\(index)".utf8), replaceable: false)
#expect(queue.enqueue(delivery) == nil)
}
#expect(queue.pendingCount == 128)
for index in 0..<128 {
let expected = TerminalOutputDelivery(bytes: Data("raw-\(index)".utf8), replaceable: false)
#expect(queue.completeInFlight() == expected)
}
#expect(queue.completeInFlight() == nil)
#expect(queue.isIdle)
}
@Test func renderGridViewportPatchIsReplaceableOnlyWhenEveryRowIsCleared() throws {
let fullFrame = try MobileTerminalRenderGridFrame.fromPlainRows(
surfaceID: "terminal",
stateSeq: 1,
columns: 12,
rows: 3,
text: "a\nb\nc"
)
let fullViewportDelta = try MobileTerminalRenderGridFrame.fromPlainRows(
surfaceID: "terminal",
stateSeq: 2,
columns: 12,
rows: 3,
text: "d\ne\nf",
full: false,
changedRows: [0, 1, 2]
)
let partialDelta = try MobileTerminalRenderGridFrame.fromPlainRows(
surfaceID: "terminal",
stateSeq: 3,
columns: 12,
rows: 3,
text: "d\ne\nf",
full: false,
changedRows: [1]
)
#expect(!fullFrame.isReplaceableViewportPatchForMobileDelivery)
#expect(fullViewportDelta.isReplaceableViewportPatchForMobileDelivery)
#expect(!partialDelta.isReplaceableViewportPatchForMobileDelivery)
}