@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
swift 222 lines 8.08 KB
Raw
1 // Instrument decoder entry and accepted publication in the real serial queue.
2 private final class TokenDecodeProbe {
3 private let lock = NSLock()
4 private var entries = 0
5 private var publications = 0
6 private var onEntry: ((Int) -> Void)?
7
8 func reset(onEntry: ((Int) -> Void)? = nil) {
9 lock.lock()
10 entries = 0
11 publications = 0
12 self.onEntry = onEntry
13 lock.unlock()
14 }
15
16 func enter() {
17 lock.lock()
18 entries += 1
19 let entry = entries
20 let callback = onEntry
21 lock.unlock()
22 callback?(entry)
23 }
24
25 func publish() {
26 lock.lock()
27 publications += 1
28 lock.unlock()
29 }
30
31 func checkCounts(_ expectedEntries: Int, _ expectedPublications: Int, _ name: String) {
32 lock.lock()
33 defer { lock.unlock() }
34 check(entries == expectedEntries,
35 "\(name): expected \(expectedEntries) token decoder entries, got \(entries)")
36 check(publications == expectedPublications,
37 "\(name): expected \(expectedPublications) retained full publications, got \(publications)")
38 print("Passed tokens: \(name), decoder entries=\(entries), retained full publications=\(publications)")
39 }
40 }
41
42 private let tokenDecodeProbe = TokenDecodeProbe()
43
44 private func decodeTokensForTest(
45 _ type: [String: [ReviewDiffNativeToken]].Type, from data: Data
46 ) throws -> [String: [ReviewDiffNativeToken]] {
47 tokenDecodeProbe.enter()
48 return try JSONDecoder().decode(type, from: data)
49 }
50
51 private func tokenPayload(_ id: String) -> String {
52 "{\"\(id)\":[{\"content\":\"漢字 👨‍👩‍👧‍👦 é\",\"color\":\"#ff0000\",\"fontStyle\":3},{\"content\":\"tail\",\"fontStyle\":0}]}"
53 }
54
55 private func tokenPatch(_ id: String, key: String = "tokens", chunk: Int = 0) -> String {
56 "{\"resetKey\":\"\(key)\",\"chunkIndex\":\(chunk),\"tokensByRowId\":\(tokenPayload(id))}"
57 }
58
59 private extension T3ReviewDiffView {
60 static func afterTokens(on queue: DispatchQueue, _ completion: @escaping () -> Void) {
61 queue.async { DispatchQueue.main.async(execute: completion) }
62 }
63
64 func checkTokens(_ payloads: [String]) {
65 var expected: [String: [ReviewDiffNativeToken]] = [:]
66 for payload in payloads {
67 let decoded = try! JSONDecoder().decode([String: [ReviewDiffNativeToken]].self, from: Data(payload.utf8))
68 expected.merge(decoded) { _, next in next }
69 }
70 let describe = { (value: [String: [ReviewDiffNativeToken]]) in
71 value.mapValues { $0.map { String(reflecting: $0) } }
72 }
73 check(describe(contentView.tokensByRowId) == describe(expected), "Token fields/output order changed")
74 }
75
76 static func runQueuedTokenDecodeCase(_ index: Int) {
77 let names = ["A B C burst", "content reset", "reset then B", "same content key",
78 "token reset preserves full", "same token key", "current full", "current malformed",
79 "obsolete malformed", "full patch full", "full then patches", "patches then full",
80 "additive patches", "reset rejects old patch"]
81 guard index < names.count else { runReleasedTokenDecodeCase(); return }
82 tokenDecodeProbe.reset()
83 let view = T3ReviewDiffView()
84 view.setContentResetKey("content")
85 view.setTokensResetKey("tokens")
86 var errors: [String] = []
87 view.onDebug.onEvent = { payload in
88 if payload["message"] as? String == "tokens-decode-failed" {
89 errors.append(payload["error"] as! String)
90 }
91 }
92 view.contentView.tokensByRowId = try! JSONDecoder().decode(
93 [String: [ReviewDiffNativeToken]].self, from: Data(tokenPayload("seed").utf8)
94 )
95 var acknowledgements: [Bool] = []
96 let patch = { (id: String, chunk: Int) in
97 view.setTokensPatchJson(tokenPatch(id, chunk: chunk)) { acknowledgements.append($0) }
98 }
99 view.payloadDecodeQueue.suspend()
100 switch index {
101 case 0:
102 for id in ["a", "b", "c"] { view.setTokensJson(tokenPayload(id)) }
103 case 1, 2:
104 view.setTokensJson(tokenPayload("a"))
105 view.setContentResetKey("new")
106 if index == 2 { view.setTokensJson(tokenPayload("b")) }
107 case 3:
108 view.setTokensJson(tokenPayload("b"))
109 view.setContentResetKey("content")
110 case 4, 5:
111 view.setTokensJson(tokenPayload("b"))
112 view.setTokensResetKey(index == 4 ? "new" : "tokens")
113 case 6: view.setTokensJson(tokenPayload("b"))
114 case 7: view.setTokensJson("invalid JSON")
115 case 8:
116 view.setTokensJson("invalid JSON")
117 view.setTokensJson(tokenPayload("b"))
118 case 9:
119 view.setTokensJson(tokenPayload("a"))
120 patch("patch", 0)
121 view.setTokensJson(tokenPayload("b"))
122 case 10:
123 view.setTokensJson(tokenPayload("b"))
124 patch("one", 0)
125 patch("two", 1)
126 case 11:
127 patch("one", 0)
128 patch("two", 1)
129 view.setTokensJson(tokenPayload("b"))
130 case 12:
131 patch("one", 0)
132 patch("two", 1)
133 default:
134 patch("old", 0)
135 view.setContentResetKey("new")
136 view.setTokensJson(tokenPayload("b"))
137 }
138 view.payloadDecodeQueue.resume()
139 afterTokens(on: view.payloadDecodeQueue) {
140 let entries = index == 1 || index == 12 ? 0 : 1
141 let publications = entries
142 tokenDecodeProbe.checkCounts(entries, publications, names[index])
143 if index == 1 || index == 7 {
144 view.checkTokens([])
145 } else if index == 0 {
146 view.checkTokens([tokenPayload("c")])
147 } else if index == 10 {
148 view.checkTokens([tokenPayload("b"), tokenPayload("one"), tokenPayload("two")])
149 } else if index == 12 {
150 view.checkTokens([tokenPayload("seed"), tokenPayload("one"), tokenPayload("two")])
151 } else {
152 view.checkTokens([tokenPayload("b")])
153 }
154 check(acknowledgements == (index == 9 ? [true] : index == 10 || index == 11 || index == 12 ? [true, true] : index == 13 ? [false] : []),
155 "All additive chunks must retain their acknowledgement semantics")
156 if index == 7 {
157 do {
158 _ = try JSONDecoder().decode([String: [ReviewDiffNativeToken]].self, from: Data("invalid JSON".utf8))
159 } catch { check(errors == [error.localizedDescription], "Current error/fallback changed") }
160 } else { check(errors.isEmpty, "Obsolete errors must not publish") }
161 runQueuedTokenDecodeCase(index + 1)
162 }
163 }
164
165 static func runReleasedTokenDecodeCase() {
166 tokenDecodeProbe.reset()
167 var view: T3ReviewDiffView? = T3ReviewDiffView()
168 let queue = view!.payloadDecodeQueue
169 let released = { [weak view] in view == nil }
170 queue.suspend()
171 view!.setTokensJson(tokenPayload("released"))
172 view = nil
173 check(released(), "Queued token closure must not retain the view")
174 queue.resume()
175 afterTokens(on: queue) {
176 tokenDecodeProbe.checkCounts(0, 0, "release before entry")
177 runEnteredTokenDecodeCase(invalid: false)
178 }
179 }
180
181 static func runEnteredTokenDecodeCase(invalid: Bool) {
182 let view = T3ReviewDiffView()
183 let release = DispatchSemaphore(value: 0)
184 tokenDecodeProbe.reset { entry in
185 guard entry == 1 else { return }
186 DispatchQueue.main.async {
187 view.setContentResetKey("new")
188 view.setTokensJson(tokenPayload("b"))
189 release.signal()
190 afterTokens(on: view.payloadDecodeQueue) {
191 tokenDecodeProbe.checkCounts(2, 1, "entered \(invalid ? "error" : "success") then replacement")
192 view.checkTokens([tokenPayload("b")])
193 tokenDecodeProbe.reset()
194 if invalid { runReleasedEnteredTokenDecodeCase() } else { runEnteredTokenDecodeCase(invalid: true) }
195 }
196 }
197 release.wait()
198 }
199 view.setTokensJson(invalid ? "invalid JSON" : tokenPayload("a"))
200 }
201
202 static func runReleasedEnteredTokenDecodeCase() {
203 var view: T3ReviewDiffView? = T3ReviewDiffView()
204 let released = { [weak view] in view == nil }
205 let queue = view!.payloadDecodeQueue
206 let release = DispatchSemaphore(value: 0)
207 tokenDecodeProbe.reset { _ in
208 DispatchQueue.main.async {
209 view = nil
210 check(released(), "Entered decoder must not keep the view alive")
211 release.signal()
212 afterTokens(on: queue) {
213 tokenDecodeProbe.checkCounts(1, 0, "release after entry")
214 tokenDecodeProbe.reset()
215 exit(0)
216 }
217 }
218 release.wait()
219 }
220 view!.setTokensJson(tokenPayload("a"))
221 }
222 }