From 5ed3d14f88d6d92be600f9ae52a98cb46354480e Mon Sep 17 00:00:00 2001 From: Ananovo Date: Mon, 9 Nov 2026 12:03:47 +0800 Subject: [PATCH] test(ios): capture intermittent transcript layout diagnostics (#2325) * test(ios): capture intermittent transcript layout diagnostics * fix(ci): keep iPad diagnostics running after failures * ci: retry iOS diagnostics after transient simulator failures --- .github/workflows/ios.yml | 8 +- ios/HapiTests/TranscriptTypographyTests.swift | 97 ++++++++++++++++++- .../HapiUI/Chat/AnchoredTranscriptList.swift | 33 +++++++ ios/scripts/test-transcript.sh | 8 +- 4 files changed, 141 insertions(+), 5 deletions(-) diff --git a/.github/workflows/ios.yml b/.github/workflows/ios.yml index 2d6d5b4a..6361f0da 100644 --- a/.github/workflows/ios.yml +++ b/.github/workflows/ios.yml @@ -47,10 +47,14 @@ jobs: - name: Build Hapi app for iOS Simulator run: xcodebuild build -project ios/Hapi.xcodeproj -scheme Hapi -destination 'generic/platform=iOS Simulator' CODE_SIGNING_ALLOWED=NO - name: Transcript layout regression tests + env: + HAPI_TEST_RESULT_BUNDLE_PATH: ${{ runner.temp }}/iphone-transcript.xcresult run: ios/scripts/test-transcript.sh - name: iPad navigation and layout regression tests + if: ${{ !cancelled() }} env: HAPI_TEST_DEVICE_TYPE: com.apple.CoreSimulator.SimDeviceType.iPad-Air-11-inch-M2 + HAPI_TEST_RESULT_BUNDLE_PATH: ${{ runner.temp }}/ipad-regression.xcresult run: >- ios/scripts/test-transcript.sh -only-testing:HapiTests/SessionNavigationTests @@ -89,11 +93,11 @@ jobs: -enableCodeCoverage NO -parallel-testing-enabled NO CODE_SIGNING_ALLOWED=NO -resultBundlePath "$RUNNER_TEMP/mac-transcript-geometry.xcresult" -only-testing:HapiTests/TranscriptGeometryTests - - name: Upload Mac regression test results + - name: Upload regression test results if: ${{ always() }} uses: actions/upload-artifact@v4 with: - name: mac-regression-test-results + name: ios-regression-test-results path: ${{ runner.temp }}/*.xcresult if-no-files-found: ignore retention-days: 7 diff --git a/ios/HapiTests/TranscriptTypographyTests.swift b/ios/HapiTests/TranscriptTypographyTests.swift index 61eedfed..8ae03d7a 100644 --- a/ios/HapiTests/TranscriptTypographyTests.swift +++ b/ios/HapiTests/TranscriptTypographyTests.swift @@ -94,6 +94,53 @@ final class TranscriptTypographyTests: XCTestCase { return try XCTUnwrap(view.cellForItem(at: IndexPath(item: index, section: 0))) } + private func diagnosticSample(anchorID: String, driver: Driver, view: UICollectionView, + phase: String, elapsedMilliseconds: Int) -> [String: Any] { + let layoutSnapshot = (view.collectionViewLayout as? TranscriptLayout)? + .diagnosticSnapshot(anchorID: anchorID, in: view) ?? ["layout": "not TranscriptLayout"] + let visibleRows = view.indexPathsForVisibleItems.sorted().map { indexPath -> [String: Any] in + let frame = view.cellForItem(at: indexPath)?.frame ?? .zero + return [ + "id": String(indexPath.item), + "item": indexPath.item, + "frame": [Double(frame.minX), Double(frame.minY), Double(frame.width), Double(frame.height)], + ] + } + return [ + "phase": phase, + "elapsedMilliseconds": elapsedMilliseconds, + "dynamicTypeSize": String(describing: driver.size), + "bodySize": (driver.typography.map { Double($0.bodySize) } as Any?) ?? NSNull(), + "layoutVersions": Array(driver.layouts.suffix(12)), + "visibleRows": visibleRows, + "layout": layoutSnapshot, + ] + } + + private func elapsedMilliseconds(since start: TimeInterval) -> Int { + Int(((ProcessInfo.processInfo.systemUptime - start) * 1_000).rounded()) + } + + private func attachDiagnosticTrace(_ samples: [[String: Any]], anchorID: String, + expectedOffset: CGFloat) { + let record: [String: Any] = [ + "anchorID": anchorID, + "expectedAnchorOffset": Double(expectedOffset), + "device": UIDevice.current.name, + "systemVersion": UIDevice.current.systemVersion, + "processVersion": ProcessInfo.processInfo.operatingSystemVersionString, + "samples": samples, + ] + guard let data = try? JSONSerialization.data(withJSONObject: record, options: [.sortedKeys, .prettyPrinted]) else { + return + } + print("HAPI_TRANSCRIPT_DIAGNOSTIC \(String(decoding: data, as: UTF8.self))") + let attachment = XCTAttachment(data: data, uniformTypeIdentifier: "public.json") + attachment.name = "growing-text-deep-offset-diagnostics" + attachment.lifetime = .keepAlways + add(attachment) + } + func testEqualItemsResizeAtLargeTextAndKeepTheHistoryAnchor() async throws { let driver = Driver() let (window, view) = try host(driver) @@ -159,11 +206,57 @@ final class TranscriptTypographyTests: XCTestCase { view.contentOffset.y = cell.frame.minY + deepOffset view.layoutIfNeeded() await settle() + + let anchorID = "25" + var diagnostics: [[String: Any]] = [] + diagnostics.append(diagnosticSample(anchorID: anchorID, driver: driver, view: view, + phase: "before-typography-change", elapsedMilliseconds: 0)) + defer { + attachDiagnosticTrace(diagnostics, anchorID: anchorID, expectedOffset: deepOffset) + } + let previousSize = try XCTUnwrap(driver.typography).bodySize + let elapsedStart = ProcessInfo.processInfo.systemUptime driver.size = .accessibility5 - await settle { (driver.typography?.bodySize ?? 0) > previousSize } + var observedTypographyChange = false + for _ in 0..<100 { + try? await Task.sleep(for: .milliseconds(20)) + diagnostics.append(diagnosticSample( + anchorID: anchorID, driver: driver, view: view, phase: "wait-for-typography", + elapsedMilliseconds: elapsedMilliseconds(since: elapsedStart) + )) + if (driver.typography?.bodySize ?? 0) > previousSize { + observedTypographyChange = true + break + } + } + XCTAssertTrue(observedTypographyChange, "Typography did not update within the existing 2-second bound") + + // Preserve the original assertion timing while sampling the 160 ms settle window. + for _ in 0..<8 { + try? await Task.sleep(for: .milliseconds(20)) + diagnostics.append(diagnosticSample( + anchorID: anchorID, driver: driver, view: view, phase: "settle-before-assertion", + elapsedMilliseconds: elapsedMilliseconds(since: elapsedStart) + )) + } let grown = try XCTUnwrap(view.cellForItem(at: IndexPath(item: 25, section: 0))) - XCTAssertEqual(view.contentOffset.y - grown.frame.minY, deepOffset, accuracy: 1) + let assertionOffset = view.contentOffset.y - grown.frame.minY + diagnostics.append(diagnosticSample( + anchorID: anchorID, driver: driver, view: view, phase: "assertion", + elapsedMilliseconds: elapsedMilliseconds(since: elapsedStart) + )) + + // This is diagnostic observation only. It does not delay or repair layout. + for _ in 0..<100 { + try? await Task.sleep(for: .milliseconds(20)) + diagnostics.append(diagnosticSample( + anchorID: anchorID, driver: driver, view: view, phase: "post-assertion", + elapsedMilliseconds: elapsedMilliseconds(since: elapsedStart) + )) + } + + XCTAssertEqual(assertionOffset, deepOffset, accuracy: 1) } func testReadingWidthAndBoldTextChangesKeepHistoryInPlace() async throws { diff --git a/ios/Packages/HapiKit/Sources/HapiUI/Chat/AnchoredTranscriptList.swift b/ios/Packages/HapiKit/Sources/HapiUI/Chat/AnchoredTranscriptList.swift index bf2a40b3..76173bec 100644 --- a/ios/Packages/HapiKit/Sources/HapiUI/Chat/AnchoredTranscriptList.swift +++ b/ios/Packages/HapiKit/Sources/HapiUI/Chat/AnchoredTranscriptList.swift @@ -256,6 +256,39 @@ final class TranscriptLayout: UICollectionViewLayout { context.contentOffsetAdjustment.y = y - view.contentOffset.y invalidateLayout(with: context) } + + #if DEBUG + /// Read-only state for CI diagnosis; callers must not use this to advance layout. + func diagnosticSnapshot(anchorID: String, in view: UICollectionView) -> [String: Any] { + let index = indicesByID[anchorID] + let frame = index.flatMap { frames.indices.contains($0) ? frames[$0] : nil } + let updateAnchor: Any = anchorForUpdate.map { + ["id": $0.id, "offset": Double($0.offset)] + } ?? NSNull() + let heightEstimate: Any = anchorHeightEstimate.map { + ["id": $0.id, "height": Double($0.height)] + } ?? NSNull() + let viewportTop = view.contentOffset.y + view.adjustedContentInset.top + return [ + "anchorItemID": anchorID, + "anchorIndex": index ?? -1, + "anchorFrame": (frame.map { + [Double($0.minX), Double($0.minY), Double($0.width), Double($0.height)] + } as Any?) ?? NSNull(), + "anchorOffsetFromViewportTop": (frame.map { Double($0.minY - viewportTop) } as Any?) ?? NSNull(), + "anchorMeasuredHeight": (heights[anchorID].map { Double($0) } as Any?) ?? NSNull(), + "anchorEstimatedHeight": Double(estimatedHeight(for: anchorID)), + "anchorHeightEstimate": heightEstimate, + "anchorForUpdate": updateAnchor, + "contentOffset": [Double(view.contentOffset.x), Double(view.contentOffset.y)], + "contentSize": [Double(collectionViewContentSize.width), Double(collectionViewContentSize.height)], + "viewportHeight": Double(view.bounds.height), + "adjustedTopInset": Double(view.adjustedContentInset.top), + "followsTail": followsTail, + "dirtyFromIndex": dirtyFromIndex ?? -1, + ] + } + #endif } private final class TranscriptCollectionView: UICollectionView { diff --git a/ios/scripts/test-transcript.sh b/ios/scripts/test-transcript.sh index 8a4eac39..d28dce15 100755 --- a/ios/scripts/test-transcript.sh +++ b/ios/scripts/test-transcript.sh @@ -17,7 +17,13 @@ print(max(items, key=lambda x: tuple(map(int, x["version"].split("."))))["identi device=$(xcrun simctl create "HAPI Transcript Tests" "$device_type" "$runtime") trap 'xcrun simctl delete "$device" >/dev/null 2>&1 || true' EXIT +result_bundle_args=() +if [[ -n "${HAPI_TEST_RESULT_BUNDLE_PATH:-}" ]]; then + result_bundle_args=(-resultBundlePath "$HAPI_TEST_RESULT_BUNDLE_PATH") +fi + xcodebuild test -project ios/Hapi.xcodeproj -scheme Hapi \ -destination "platform=iOS Simulator,id=$device" \ -derivedDataPath "${HAPI_TEST_DERIVED_DATA:-/tmp/hapi-transcript-tests}" \ - -parallel-testing-enabled NO CODE_SIGNING_ALLOWED=NO "$@" + -parallel-testing-enabled NO CODE_SIGNING_ALLOWED=NO \ + ${result_bundle_args[@]+"${result_bundle_args[@]}"} "$@"