Repository navigation
Fix a crash when Stats details list the same entry twice #26464
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
crazytonyli
wants to merge
1
commit into
trunk
Choose a base branch
from
bugfix/stats-duplicate-top-level-rows
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 133 additions & 0 deletions
133
Tests/KeystoneTests/Tests/Features/Stats/SiteStatsDetailsDuplicateRowsTests.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| import Foundation | ||
| import Testing | ||
| import UIKit | ||
| import WordPressKit | ||
| import WordPressSharedObjC | ||
| @testable import WordPress | ||
|
|
||
| /// Covers detail tables where the Stats API lists the same top-level entry twice, with the | ||
| /// same name, count and link. | ||
| @MainActor | ||
| struct SiteStatsDetailsDuplicateRowsTests { | ||
|
|
||
| @Test func categoryListedTwiceShowsBothCopies() throws { | ||
| let table = DetailsTable(section: .insightsTagsAndCategories) | ||
| table.setTags(travelViews: [4, 4]) | ||
|
|
||
| #expect(rowNames(in: try table.snapshot()) == ["Recipes", "Travel", "Travel", "Garden"]) | ||
| } | ||
|
|
||
| @Test func linkListedTwiceInClicksShowsBothCopies() throws { | ||
| let table = DetailsTable(section: .periodClicks) | ||
| let link = ["name": "example.com/same/", "url": "https://example.com/same/", "views": 5] as [String: Any] | ||
| table.periodStore.state.topClicks = StatsTopClicksTimeIntervalData( | ||
| date: Date(), | ||
| period: .week, | ||
| jsonDictionary: [ | ||
| "days": [ | ||
| "2026-11-23": [ | ||
| "clicks": [ | ||
| ["name": "example.com/top/", "views": 20], link, link, | ||
| ["name": "example.com/end/", "views": 1] | ||
| ], | ||
| "total_clicks": 30, | ||
| "other_clicks": 0 | ||
| ] | ||
| ] | ||
| ] as [String: AnyObject] | ||
| ) | ||
| table.periodStore.state.topClicksStatus = .success | ||
|
|
||
| #expect( | ||
| rowNames(in: try table.snapshot()) == [ | ||
| "example.com/top/", "example.com/same/", "example.com/same/", "example.com/end/" | ||
| ] | ||
| ) | ||
| } | ||
|
|
||
| @Test func newCountForOneCopyKeepsTheOtherCopy() throws { | ||
| let table = DetailsTable(section: .insightsTagsAndCategories) | ||
| table.setTags(travelViews: [4, 4]) | ||
| let before = rowItems(in: try table.snapshot()) | ||
|
|
||
| table.setTags(travelViews: [4, 5]) | ||
| let after = try table.snapshot() | ||
|
|
||
| #expect(rowNames(in: after) == ["Recipes", "Travel", "Travel", "Garden"]) | ||
| // The diffable data source replaces an item only when it no longer finds an equal one. | ||
| #expect(before.map { after.indexOfItem($0) != nil } == [true, true, false, true]) | ||
| } | ||
| } | ||
|
|
||
| // MARK: - Helpers | ||
|
|
||
| private extension SiteStatsDetailsDuplicateRowsTests { | ||
| func rowItems(in snapshot: ImmuTableDiffableDataSourceSnapshot) -> [AnyHashableImmuTableRow] { | ||
| snapshot.itemIdentifiers.filter { $0.immuTableRow is DetailExpandableRow } | ||
| } | ||
|
|
||
| func rowNames(in snapshot: ImmuTableDiffableDataSourceSnapshot) -> [String] { | ||
| rowItems(in: snapshot).compactMap { ($0.immuTableRow as? DetailExpandableRow)?.rowData.name } | ||
| } | ||
| } | ||
|
|
||
| /// A stats detail table backed by real stores. | ||
| /// | ||
| /// Each test creates its own instance and releases it on the main actor: the stores' | ||
| /// `Dispatcher` asserts when their receipts are released on another thread. | ||
| @MainActor | ||
| private final class DetailsTable { | ||
| let insightsStore = StatsInsightsStore() | ||
| let periodStore = StatsPeriodStore() | ||
| private let viewModel: SiteStatsDetailsViewModel | ||
| // Strong references: the view model keeps its delegates weak. | ||
| private let detailsHandler = MockDetailsDelegate() | ||
| private let referrerHandler = MockReferrerDelegate() | ||
|
|
||
| /// Set the store state after `init`: `fetchDataFor` can mark the section as loading. | ||
| init(section: StatSection) { | ||
| viewModel = SiteStatsDetailsViewModel( | ||
| detailsDelegate: detailsHandler, | ||
| referrerDelegate: referrerHandler, | ||
| insightsStore: insightsStore, | ||
| periodStore: periodStore | ||
| ) | ||
| viewModel.fetchDataFor(statSection: section, selectedDate: Date(), selectedPeriod: .week) | ||
| } | ||
|
|
||
| /// Single-category groups, as in the crash reports: "Recipes", "Travel" once per count, | ||
| /// then "Garden". The last row draws a different separator, so the copies sit before it. | ||
| func setTags(travelViews: [Int]) { | ||
| let category = { (name: String, views: Int) in | ||
| [ | ||
| "views": views, | ||
| "tags": [ | ||
| ["name": name, "type": "category", "link": "https://example.com/?taxonomy=category&term=\(name)"] | ||
| ] | ||
| ] as [String: Any] | ||
| } | ||
| insightsStore.state.allTagsAndCategories = StatsTagsAndCategoriesInsight(jsonDictionary: [ | ||
| "tags": ([category("Recipes", 30)] + travelViews.map { category("Travel", $0) } + [category("Garden", 1)]) | ||
| as AnyObject | ||
| ]) | ||
| insightsStore.state.allTagsAndCategoriesStatus = .success | ||
| } | ||
|
|
||
| /// Returns the view model's snapshot. | ||
| /// | ||
| /// UIKit raises `NSInternalInconsistencyException` for equal snapshot items. Catching it | ||
| /// fails the test instead of ending the test process. | ||
| func snapshot() throws -> ImmuTableDiffableDataSourceSnapshot { | ||
| var snapshot = ImmuTableDiffableDataSourceSnapshot() | ||
| try WPException.objcTry { | ||
| snapshot = self.viewModel.tableViewSnapshot() | ||
| } | ||
| return snapshot | ||
| } | ||
|
|
||
| private final class MockDetailsDelegate: SiteStatsDetailsDelegate {} | ||
|
|
||
| private final class MockReferrerDelegate: SiteStatsReferrerDelegate { | ||
| func showReferrerDetails(_ data: StatsTotalRowData) {} | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Blocker: Expanding a repeated entry that has two or more children still crashes.
rowExpandedlooks up the row's name, so tapping either copy expands both. Each copy then appends the sameDetailExpandableChildRows (and nestedparentRow(rowData: childRowData…)rows), and those have nocopyIndex. With two or more children the first ones match exactly, and the snapshot hits the same duplicate-identifier exception.On trunk this data crashed when the list opened, so nobody is worse off. But the tap crash is only reachable with this PR, so the release note's "Fix a crash when opening … details that list the same entry twice" holds only until the user taps.
Evidence:
NSInternalInconsistencyExceptionin-[__UIDiffableDataSourceSnapshot appendItemsWithIdentifiers:intoSectionWithIdentifier:], viaStatsTotalRow.didTapDisclosureButton→SiteStatsDetailTableViewController.toggleChildRowsForRow→SiteStatsDetailsViewModel.tableViewSnapshot.SiteStatsDetailsDuplicateRowsTests, a repeated two-tag group loads collapsed. AfterStatsDataHelper.expandedRowLabelsDetails[.insightsTagsAndCategories] = ["Travel, Food"],tableViewSnapshot()throwsNSInternalInconsistencyException(_UIDiffableDataSourceUpdate.m:81).The same code is in
SiteStatsInsightsDetailsViewModelat line 1196. A small fix that doesn't need #26462: pass the parent'scopyIndexto the child and nested parent rows it appends, and addcopyIndextoDetailExpandableChildRow's==. Then the two expanded copies stay distinct, even though both still expand together.