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
| 1 | // Geometry tests exercise the production content view without an Expo app or device. |
| 2 | private extension ReviewDiffContentView { |
| 3 | func checkSparseGeometry(expectedNativeRows: Int) { |
| 4 | check(codeLayoutsByRowId.count == expectedNativeRows, |
| 5 | "Offscreen ASCII must not retain per-row layout objects or wrap-start arrays (retained \(codeLayoutsByRowId.count))") |
| 6 | } |
| 7 | |
| 8 | func checkReferenceGeometry() { |
| 9 | let font = codeFont |
| 10 | let characterWidth = monospaceCharacterWidth(font: font) |
| 11 | let width = codeLayoutWidthLimit(font: font) |
| 12 | var offset: CGFloat = 0 |
| 13 | var widths: [String: CGFloat] = [:] |
| 14 | for (index, row) in rows.enumerated() { |
| 15 | check(rowOffsets[index] == offset, "Wrapped offsets changed for \(row.id)") |
| 16 | var rowHeight = height(for: row) |
| 17 | if row.kind == "line", let width, let content = row.content { |
| 18 | let reference = ReviewDiffCodeLayout(text: content, font: font, width: width, characterWidth: characterWidth) |
| 19 | if rowHeight > 0 { |
| 20 | rowHeight = max(rowHeight, reference.firstLineHeight) + reference.extraHeight |
| 21 | } |
| 22 | widths[row.resolvedFileId] = max(widths[row.resolvedFileId] ?? 0, reference.usedWidth) |
| 23 | } else if row.kind == "hunk" { |
| 24 | widths[row.resolvedFileId] = max(widths[row.resolvedFileId] ?? 0, textWidth(row.text ?? "", font: hunkFont)) |
| 25 | } |
| 26 | check(height(at: index) == rowHeight, "Row height changed for \(row.id)") |
| 27 | if rowHeight > 0 { |
| 28 | check(rowIndex(at: offset + rowHeight / 2) == index, "Hit testing must use the published wrapped offsets") |
| 29 | } |
| 30 | offset += rowHeight |
| 31 | } |
| 32 | check(contentHeight == offset, "Content height must include every row") |
| 33 | check(contentWidthsByFileId == widths.mapValues { ceil($0) + style.codePadding * 2 }, |
| 34 | "The end of every unwrapped line must remain reachable") |
| 35 | } |
| 36 | |
| 37 | func preparedOffsetAddress() -> UInt { |
| 38 | guard let geometry = preparedRowGeometry else { fatalError("Geometry was not prepared") } |
| 39 | return geometry.offsets.withUnsafeBufferPointer { UInt(bitPattern: $0.baseAddress) } |
| 40 | } |
| 41 | |
| 42 | func checkPublishedOffsetAddress(_ address: UInt) { |
| 43 | check(rowOffsets.withUnsafeBufferPointer { UInt(bitPattern: $0.baseAddress) } == address, |
| 44 | "Publication must reuse the prepared offsets without rebuilding on the UI thread") |
| 45 | } |
| 46 | |
| 47 | func checkCancellationInsideLoop() { |
| 48 | var checks = 0 |
| 49 | let input = ReviewDiffRowGeometryInput( |
| 50 | configuration: geometryConfiguration, font: codeFont, hunkFont: hunkFont, |
| 51 | characterWidth: codeCharacterWidth, layoutWidth: codeLayoutWidthLimit(font: codeFont), cached: [:] |
| 52 | ) |
| 53 | let geometry = Self.buildRowGeometry( |
| 54 | rows, input: input, isCancelled: { |
| 55 | checks += 1 |
| 56 | return checks == 2 |
| 57 | } |
| 58 | ) |
| 59 | check(geometry == nil && checks == 2, "Obsolete work must abort at the next batch, before visiting every row") |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | private let geometryRows = try JSONDecoder().decode([ReviewDiffNativeRow].self, from: JSONSerialization.data(withJSONObject: |
| 64 | [["kind": "file", "id": "a:header", "fileId": "a"], |
| 65 | ["kind": "hunk", "id": "a:hunk:0", "fileId": "a", "text": "@@ context\twith tabs @@"]] + (0..<4096).map { index in |
| 66 | ["kind": "line", "id": "a:line:\(index)", "fileId": "a", |
| 67 | "content": String(repeating: "printable ASCII ", count: index % 30), "newLineNumber": index + 1] |
| 68 | } + [["kind": "line", "id": "a:unicode", "fileId": "a", "content": "漢字\t👨👩👧👦"], |
| 69 | ["kind": "comment", "id": "a:comment", "fileId": "a"], |
| 70 | ["kind": "file", "id": "b:header", "fileId": "b"], |
| 71 | ["kind": "line", "id": "b:line:0", "fileId": "b", "content": "e\u{301}\tمرحبا"]] |
| 72 | )) |
| 73 | private let geometryView = ReviewDiffContentView(frame: CGRect(x: 0, y: 0, width: 390, height: 844)) |
| 74 | private func geometryStyle(wrap: Bool, fontSize: Int = 12, weight: String = "regular", rowHeight: Int = 24) throws -> ReviewDiffNativeStyle { |
| 75 | let payload = try JSONDecoder().decode(ReviewDiffNativeStylePayload.self, from: Data( |
| 76 | "{\"wordWrap\":\(wrap),\"codeFontSize\":\(fontSize),\"codeFontWeight\":\"\(weight)\",\"rowHeight\":\(rowHeight)}".utf8 |
| 77 | )) |
| 78 | return ReviewDiffNativeStyle.resolve(payload) |
| 79 | } |
| 80 | geometryView.style = try geometryStyle(wrap: true) |
| 81 | geometryView.viewportWidth = 390 |
| 82 | geometryView.rows = geometryRows |
| 83 | geometryView.checkSparseGeometry(expectedNativeRows: 2) |
| 84 | geometryView.checkReferenceGeometry() |
| 85 | geometryView.checkCancellationInsideLoop() |
| 86 | for width: CGFloat in [400, 280] { |
| 87 | geometryView.viewportWidth = width |
| 88 | geometryView.checkReferenceGeometry() |
| 89 | } |
| 90 | geometryView.collapsedFileIds = ["b"] |
| 91 | geometryView.collapsedCommentIds = ["a:comment"] |
| 92 | geometryView.checkReferenceGeometry() |
| 93 | geometryView.style = try geometryStyle(wrap: true, fontSize: 28, weight: "bold", rowHeight: 8) |
| 94 | geometryView.checkReferenceGeometry() |
| 95 | geometryView.style = try geometryStyle(wrap: false) |
| 96 | geometryView.checkReferenceGeometry() |
| 97 | geometryView.collapsedFileIds = [] |
| 98 | geometryView.collapsedCommentIds = [] |
| 99 | geometryView.style = try geometryStyle(wrap: true) |
| 100 | geometryView.checkReferenceGeometry() |
| 101 | let originalHeight = geometryView.contentHeight |
| 102 | geometryView.mergeTokensByRowId(["a:line:29": [ReviewDiffNativeToken(content: geometryRows[31].content ?? "", color: "#ff0000", fontStyle: 3)]]) |
| 103 | geometryView.selectedRowIds = ["a:line:29"] |
| 104 | check(geometryView.contentHeight == originalHeight, "Tokens and selection must preserve geometry") |
| 105 | |
| 106 | // The runner instruments the production resolver in its temporary compile input only. |
| 107 | private enum HeaderLookupReads { |
| 108 | static var enabled = false |
| 109 | static var headers = 0 |
| 110 | static var code = 0 |
| 111 | static func record(_ row: ReviewDiffNativeRow) { |
| 112 | guard enabled else { return } |
| 113 | if row.kind == "file" { headers += 1 } else { code += 1 } |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | private extension ReviewDiffContentView { |
| 118 | func checkHeaderPathLimits() throws { |
| 119 | viewportWidth = 390 |
| 120 | let longPath = String(repeating: "long/directory/", count: 12) |
| 121 | func decode(_ input: [[String: Any]]) throws -> [ReviewDiffNativeRow] { |
| 122 | try JSONDecoder().decode([ReviewDiffNativeRow].self, from: JSONSerialization.data(withJSONObject: input)) |
| 123 | } |
| 124 | var fixture: [[String: Any]] = [] |
| 125 | for file in 0..<10 { |
| 126 | fixture.append(["kind": "file", "id": "f\(file):header", "fileId": "f\(file)", |
| 127 | "filePath": longPath + "new-\(file).swift", "previousPath": "old-\(file).swift", |
| 128 | "additions": file + 1, "deletions": file * 2]) |
| 129 | for line in 0..<100 { |
| 130 | fixture.append(["kind": "line", "id": "f\(file):line:\(line)", "fileId": "f\(file)", |
| 131 | "content": String(repeating: "code ", count: 100)]) |
| 132 | } |
| 133 | } |
| 134 | // A code row sharing the target ID must not hide the first header. |
| 135 | fixture.insert(["kind": "line", "id": "impostor", "fileId": "f9", "content": "value"], at: 0) |
| 136 | fixture += [ |
| 137 | ["kind": "file", "id": "duplicate:header", "fileId": "f9", "filePath": "short.swift"], |
| 138 | ["kind": "file", "id": "empty:header", "fileId": "", "filePath": longPath + "empty.swift"], |
| 139 | ["kind": "file", "id": "derived:header", "filePath": longPath + "derived.swift"], |
| 140 | ["kind": "file", "id": "path-fallback", "filePath": longPath + "fallback.swift"], |
| 141 | ["kind": "file", "id": "id-only"], |
| 142 | ["kind": "line", "id": "orphan:line:0", "fileId": "orphan", "content": String(repeating: "code ", count: 100)] |
| 143 | ] |
| 144 | let input = try decode(fixture) |
| 145 | rows = input |
| 146 | for wrap in [false, true] { |
| 147 | style = try geometryStyle(wrap: wrap) |
| 148 | func verify(_ label: String) { |
| 149 | for (caseIndex, id) in ["f0", "f5", "f9", "", "derived", longPath + "fallback.swift", "id-only", "orphan", "missing"].enumerated() { |
| 150 | let header = rows.first { $0.kind == "file" && $0.resolvedFileId == id } |
| 151 | let expected = header.map { maxHeaderPathOffset(for: $0) } |
| 152 | ?? maxHorizontalOffset(for: (fileId: id, kind: .code)) |
| 153 | let actual = maxHorizontalOffset(for: (fileId: id, kind: .fileHeaderPath)) |
| 154 | print("HEADER_LIMIT iOS wrap=\(wrap) state=\(label) case=\(caseIndex) limit=\(actual)") |
| 155 | check(actual == expected, "\(label) wrap=\(wrap) id=\(id): header path limit changed") |
| 156 | if ["f0", "f5", "f9", "", "derived"].contains(id) { |
| 157 | check(actual > 0, "Long rename/header path must overflow in either wrap mode") |
| 158 | } |
| 159 | // This is the same clamp called after token, style, and viewport changes. |
| 160 | setHorizontalOffset(.greatestFiniteMagnitude, for: id, kind: .fileHeaderPath) |
| 161 | clampHorizontalOffsets() |
| 162 | check(horizontalOffset(for: id, kind: .fileHeaderPath) == expected, "Header clamp changed") |
| 163 | setHorizontalOffset(13, for: id, kind: .code) |
| 164 | clampHorizontalOffsets() |
| 165 | check(horizontalOffset(for: id, kind: .code) == min(13, maxHorizontalOffset(for: (id, .code))), |
| 166 | "Code bounds changed") |
| 167 | } |
| 168 | } |
| 169 | verify("expanded") |
| 170 | collapsedFileIds = ["f0", "f5"] |
| 171 | verify("collapsed") |
| 172 | collapsedFileIds = [] |
| 173 | verify("reexpanded") |
| 174 | } |
| 175 | // Known fixture: ten headers separated by 100 code rows; repeatedly clamp the last. |
| 176 | rows = try decode(Array(fixture[1..<1011])) |
| 177 | style = try geometryStyle(wrap: false) |
| 178 | headerPathOffsetsByFileId = ["f9": 1] |
| 179 | horizontalOffsetsByFileId = [:] |
| 180 | HeaderLookupReads.headers = 0 |
| 181 | HeaderLookupReads.code = 0 |
| 182 | HeaderLookupReads.enabled = true |
| 183 | for _ in 0..<120 { clampHorizontalOffsets() } |
| 184 | HeaderLookupReads.enabled = false |
| 185 | print("HEADER_LOOKUP iOS updates=120 headerVisits=\(HeaderLookupReads.headers) codeVisits=\(HeaderLookupReads.code)") |
| 186 | // Finish semantic checks before rejecting baseline scan work. |
| 187 | rows = try decode([["kind": "file", "id": "replacement:header", "fileId": "f9", "filePath": "short.swift"]]) |
| 188 | check(headerPathOffsetsByFileId.isEmpty && horizontalOffsetsByFileId.isEmpty, "Replacement clears offsets") |
| 189 | check(maxHorizontalOffset(for: ("f9", .fileHeaderPath)) == 0, "Replacement must use current header") |
| 190 | rows = [] |
| 191 | check(fileHeaderRowIndices.isEmpty && headerPathOffsetsByFileId.isEmpty, "Reset must clear old index/offsets") |
| 192 | check(maxHorizontalOffset(for: ("f9", .fileHeaderPath)) == maxHorizontalOffset(for: ("f9", .code)), |
| 193 | "Missing-header fallback after reset changed") |
| 194 | print("Passed iOS header limits: first/middle/last, duplicate, empty/absent IDs, rename, collapse/reexpand, replacement/reset, wrap and code bounds") |
| 195 | check(HeaderLookupReads.code == 0, "Header clamps must not inspect code rows") |
| 196 | check(HeaderLookupReads.headers == 1200, "Header clamps must visit only preceding headers") |
| 197 | } |
| 198 | } |
| 199 | try ReviewDiffContentView(frame: CGRect(x: 0, y: 0, width: 390, height: 600)).checkHeaderPathLimits() |
| 200 | |
| 201 | // Configuration changes before publication must restart preparation from a fresh snapshot. |
| 202 | private let geometryQueue = DispatchQueue(label: "review-diff.geometry-tests") |
| 203 | geometryQueue.suspend() |
| 204 | geometryView.prepareRows(geometryRows, on: geometryQueue, isCurrent: { true }, completion: { |
| 205 | let address = geometryView.preparedOffsetAddress() |
| 206 | geometryView.rows = geometryRows |
| 207 | geometryView.checkPublishedOffsetAddress(address) |
| 208 | geometryView.checkSparseGeometry(expectedNativeRows: 2) |
| 209 | geometryView.checkReferenceGeometry() |
| 210 | check(geometryView.style.codeFontSize == 28 && geometryView.viewportWidth == 400) |
| 211 | |
| 212 | // A reset/new payload can cancel work before its worker is scheduled, without publishing it. |
| 213 | geometryQueue.suspend() |
| 214 | geometryView.prepareRows(geometryRows, on: geometryQueue, isCurrent: { true }, completion: { |
| 215 | check(false, "Cancelled geometry must never publish") |
| 216 | }) |
| 217 | geometryView.rows = [] |
| 218 | geometryQueue.resume() |
| 219 | geometryQueue.async { |
| 220 | DispatchQueue.main.async { |
| 221 | check(geometryView.rows.isEmpty && geometryView.contentHeight == 0, "Reset must remain empty after obsolete work drains") |
| 222 | print("Passed sparse geometry, reference heights/widths, font/wrap/collapse, hit testing, direct publication, and cancellation") |
| 223 | runRowDecodeTests() |
| 224 | } |
| 225 | } |
| 226 | }) |
| 227 | geometryView.style = try geometryStyle(wrap: true, fontSize: 28, weight: "bold", rowHeight: 8) |
| 228 | geometryView.viewportWidth = 400 |
| 229 | geometryView.collapsedFileIds = ["b"] |
| 230 | geometryView.collapsedCommentIds = ["a:comment"] |
| 231 | geometryQueue.resume() |
| 232 | dispatchMain() |