mirror of
https://github.com/yattee/yattee.git
synced 2026-11-04 10:57:18 +00:00
Only a deletion the app can point at moves the selection
`correctIfContextRemoved` read a locally missing account as a deleted one and reset the context with `setContext(.local)` — a new timestamp and a pending upload. Now that foreground activation calls it, it ran before the fetch that delivers the account record: a perfectly good remote choice was reset and then published over everybody else's. It now asks `AccountsManager` for a repair keyed on the deletion tombstone, which is the same repair a deletion performs — a signed-in login on the same server when there is one, the replaced choice's timestamp, nothing uploaded. The toast names where the selection actually landed. Adopting a synced context that named a deleted account repaired it but kept whatever upload the previous choice still owed, so the sync engine published the automatic fallback under that choice's timestamp — exactly what not stamping it "now" exists to prevent. The adoption clears the flag, and only on adoption: a local choice that outranks the repair keeps its own upload. The channel button's subscription lookup is fenced to the channel and the round it was asked in, so an answer that lands after the user has walked on — or after the account behind it changed — no longer writes itself into the button on screen. Context and credential changes ask again. A destination toggle is compared against the effective context rather than the raw selection: the menu offers Local precisely when the selected account cannot be written to, and that toggle has to be the one the button then shows. The destination rows forget their checkmarks when the logins change. The answers describe credentials that have been replaced, and the tvOS picker deliberately stays open across exactly that. The subscriptions settings screen counts and exports what it is actually reading from. The fetch behind both already followed the effective context while the branch and the footer followed the selection, so an unavailable account meant local data described as the server's. The legacy `subscriptionAccount` migration refused to adopt a context whose account had not synced yet and was never retried, leaving a fresh install on local until the next launch. A synced account arriving now retries it; the migration's own guards keep it a one-way move that never overrides a choice made in the meantime. The remaining negative-able Piped counts — likes, channel subscribers and videos, playlist video counts — go through `PipedSentinel.count` like the views and upload dates already did.
This commit is contained in:
@@ -157,6 +157,49 @@ struct AccountsManagerTests {
|
||||
#expect(manager2.currentContext == .local)
|
||||
}
|
||||
|
||||
@Test("A legacy selection migrates when its account finally syncs")
|
||||
func legacyContextMigratesOnLateAccountArrival() {
|
||||
let defaults = makeDefaults()
|
||||
let instanceID = UUID()
|
||||
|
||||
let legacy = try! JSONEncoder().encode(SubscriptionAccount.invidious(instanceID))
|
||||
defaults.set(legacy, forKey: "subscriptionAccount")
|
||||
|
||||
let manager = makeManager(defaults: defaults)
|
||||
// On a fresh install the accounts arrive from iCloud long after launch,
|
||||
// so the migration finds nothing to point at and adopts nothing.
|
||||
manager.migrateLegacyContextIfNeeded()
|
||||
#expect(manager.currentContext == .local)
|
||||
|
||||
// The account the legacy value names, arriving at last. A migrated
|
||||
// account reuses its instance's UUID, which is what the legacy mapping
|
||||
// resolves to.
|
||||
manager.applySyncedAccount(Account.migrated(from: instanceID, dateAdded: Date()))
|
||||
|
||||
#expect(manager.currentContext == .account(instanceID))
|
||||
}
|
||||
|
||||
@Test("A choice made while the account was in transit is never overridden")
|
||||
func lateAccountArrivalNeverOverridesAChoice() {
|
||||
let defaults = makeDefaults()
|
||||
let instanceID = UUID()
|
||||
|
||||
let legacy = try! JSONEncoder().encode(SubscriptionAccount.invidious(instanceID))
|
||||
defaults.set(legacy, forKey: "subscriptionAccount")
|
||||
|
||||
let manager = makeManager(defaults: defaults)
|
||||
manager.migrateLegacyContextIfNeeded()
|
||||
|
||||
// The user picked something in the meantime; a guess at what they meant
|
||||
// before the upgrade never replaces what they actually chose after it.
|
||||
let chosen = manager.addAccount(instanceID: UUID())
|
||||
manager.setContext(.account(chosen.id))
|
||||
|
||||
manager.applySyncedAccount(Account.migrated(from: instanceID, dateAdded: Date()))
|
||||
|
||||
#expect(manager.currentContext == .account(chosen.id))
|
||||
}
|
||||
|
||||
@Test("A legacy instance selection resolves to a real account")
|
||||
func legacySelectionResolvesToRealAccount() {
|
||||
let manager = makeManager()
|
||||
@@ -553,8 +596,8 @@ struct DataContextValidatorTests {
|
||||
instances.remove(instance)
|
||||
}
|
||||
|
||||
@Test("Only an account that is genuinely gone moves the context")
|
||||
func correctionRunsOnlyOnAbsence() {
|
||||
@Test("Only a deletion the app can point at moves the context")
|
||||
func correctionRequiresATombstone() {
|
||||
let instances = InstancesManager()
|
||||
var instance = Instance(type: .invidious, url: URL(string: "https://inv-correct.example.com")!)
|
||||
instances.add(instance)
|
||||
@@ -579,15 +622,48 @@ struct DataContextValidatorTests {
|
||||
validator.correctIfContextRemoved()
|
||||
#expect(accounts.currentContext == .account(account.id))
|
||||
|
||||
// An account the store has never heard of is the one case that counts.
|
||||
accounts.setContext(.account(UUID()))
|
||||
// An account the store has never heard of has not been proven deleted:
|
||||
// it may be a record the fetch has yet to deliver, and this runs on
|
||||
// foreground activation — before that fetch.
|
||||
let pending = UUID()
|
||||
accounts.setContext(.account(pending))
|
||||
validator.correctIfContextRemoved()
|
||||
#expect(accounts.currentContext == .local)
|
||||
#expect(accounts.currentContext == .account(pending))
|
||||
|
||||
// Persisted, not merely held in memory: the correction has to survive
|
||||
// the relaunch that would otherwise re-read the dead selection.
|
||||
let (reloaded, _) = makeValidator(instances: instances, defaults: defaults)
|
||||
#expect(reloaded.currentContext == .local)
|
||||
// Nor is it corrected on the way back in: a relaunch reads the same
|
||||
// absence, and the user's choice has to survive it.
|
||||
let (reloaded, reloadedValidator) = makeValidator(instances: instances, defaults: defaults)
|
||||
reloadedValidator.correctIfContextRemoved()
|
||||
#expect(reloaded.currentContext == .account(pending))
|
||||
}
|
||||
|
||||
@Test("A tombstoned selection is repaired without being published")
|
||||
func correctionFollowsTheTombstone() {
|
||||
let instances = InstancesManager()
|
||||
let instance = Instance(type: .invidious, url: URL(string: "https://inv-tombstone.example.com")!)
|
||||
instances.add(instance)
|
||||
defer { instances.remove(instance) }
|
||||
|
||||
let (accounts, validator) = makeValidator(instances: instances)
|
||||
let deleted = accounts.addAccount(instanceID: instance.id)
|
||||
let sibling = accounts.addAccount(instanceID: instance.id)
|
||||
accounts.setStatus(.signedIn, for: sibling.id)
|
||||
|
||||
// Deleted while this device pointed somewhere else, then selected again
|
||||
// by a stale choice that arrived afterwards — the case the deletion
|
||||
// paths themselves never see.
|
||||
accounts.setContext(.local)
|
||||
accounts.applyRemoteTombstone(deleted.id)
|
||||
accounts.setContext(.account(deleted.id))
|
||||
let chosenAt = accounts.storedContext.changedAt
|
||||
|
||||
validator.correctIfContextRemoved()
|
||||
|
||||
// Repaired like any deletion: the signed-in login on the same server,
|
||||
// the replaced choice's timestamp, and nothing left to publish.
|
||||
#expect(accounts.currentContext == .account(sibling.id))
|
||||
#expect(accounts.storedContext.changedAt == chosenAt)
|
||||
#expect(!accounts.pendingContextUpload)
|
||||
}
|
||||
|
||||
@Test("Only accounts the app can actually resolve are offered")
|
||||
@@ -742,6 +818,62 @@ struct DataContextRepairTests {
|
||||
|
||||
#expect(manager.currentContext == .local)
|
||||
}
|
||||
|
||||
@Test("Adopting a repaired synced context drops what the replaced choice owed")
|
||||
func syncedTombstoneRepairIsNeverPublished() {
|
||||
let instances = InstancesManager()
|
||||
let instance = makeInstance("inv")
|
||||
instances.add(instance)
|
||||
defer { instances.remove(instance) }
|
||||
|
||||
let manager = makeManager(instances: instances)
|
||||
let deleted = manager.addAccount(instanceID: instance.id)
|
||||
let sibling = manager.addAccount(instanceID: instance.id)
|
||||
manager.setStatus(.signedIn, for: sibling.id)
|
||||
|
||||
// A choice made here that has not reached the server yet.
|
||||
manager.setContext(.local)
|
||||
manager.removeAccount(deleted.id)
|
||||
#expect(manager.pendingContextUpload)
|
||||
|
||||
let incoming = StoredDataContext(
|
||||
context: .account(deleted.id),
|
||||
changedAt: Date().addingTimeInterval(60)
|
||||
)
|
||||
manager.applySyncedContext(incoming)
|
||||
|
||||
// The repair replaced that choice, so the upload it owed goes with it —
|
||||
// sending it now would publish the fallback under its timestamp.
|
||||
#expect(manager.currentContext == .account(sibling.id))
|
||||
#expect(!manager.pendingContextUpload)
|
||||
}
|
||||
|
||||
@Test("A local choice that outranks a repair keeps its upload")
|
||||
func repairThatLosesLeavesTheChoiceDirty() {
|
||||
let instances = InstancesManager()
|
||||
let instance = makeInstance("inv")
|
||||
instances.add(instance)
|
||||
defer { instances.remove(instance) }
|
||||
|
||||
let manager = makeManager(instances: instances)
|
||||
let deleted = manager.addAccount(instanceID: instance.id)
|
||||
let sibling = manager.addAccount(instanceID: instance.id)
|
||||
manager.setStatus(.signedIn, for: sibling.id)
|
||||
|
||||
manager.removeAccount(deleted.id)
|
||||
manager.setContext(.local)
|
||||
|
||||
// The other device's selection — and so its repair — is older than the
|
||||
// choice made here.
|
||||
let incoming = StoredDataContext(
|
||||
context: .account(deleted.id),
|
||||
changedAt: Date().addingTimeInterval(-60)
|
||||
)
|
||||
#expect(manager.applySyncedContext(incoming))
|
||||
|
||||
#expect(manager.currentContext == .local)
|
||||
#expect(manager.pendingContextUpload)
|
||||
}
|
||||
}
|
||||
|
||||
@Suite("Account capabilities")
|
||||
|
||||
@@ -41,14 +41,15 @@ private func pipedStreamJSON(
|
||||
return "{\(fields.joined(separator: ", "))}"
|
||||
}
|
||||
|
||||
/// Builds the first page of `/playlists/{id}`.
|
||||
private func pipedPlaylistPageJSON(streams: [String]) -> Data {
|
||||
/// Builds the first page of `/playlists/{id}`. `videos` defaults to the number
|
||||
/// of rows the page carries, which is what a healthy server sends.
|
||||
private func pipedPlaylistPageJSON(streams: [String], videos: Int64? = nil) -> Data {
|
||||
Data("""
|
||||
{
|
||||
"name": "Watch Later",
|
||||
"uploader": "arek",
|
||||
"uploaderUrl": "/channel/UCowner",
|
||||
"videos": \(streams.count),
|
||||
"videos": \(videos ?? Int64(streams.count)),
|
||||
"relatedStreams": [\(streams.joined(separator: ",\n"))],
|
||||
"nextpage": null
|
||||
}
|
||||
@@ -127,6 +128,44 @@ struct PipedSentinelTests {
|
||||
#expect(video.viewCount == 4200)
|
||||
#expect(video.publishedAt == Date(timeIntervalSince1970: 1_609_459_200))
|
||||
}
|
||||
|
||||
@Test("A playlist that says it holds -1 videos is counted by its rows")
|
||||
func playlistCountSentinelFallsBackToTheRows() throws {
|
||||
let page = pipedPlaylistPageJSON(
|
||||
streams: [
|
||||
pipedStreamJSON(videoId: "aaa", title: "First", views: 10, uploaded: nil),
|
||||
pipedStreamJSON(videoId: "bbb", title: "Second", views: 20, uploaded: nil)
|
||||
],
|
||||
videos: -1
|
||||
)
|
||||
|
||||
let snapshot = try PipedAPI.userPlaylistSnapshot(
|
||||
fromPageJSON: [page],
|
||||
playlistID: "playlist-uuid",
|
||||
instanceURL: pipedBaseURL
|
||||
)
|
||||
|
||||
// "-1 videos" would show as a row count in the list, and reads as a
|
||||
// playlist that lost everything in it.
|
||||
#expect(snapshot.playlist.videoCount == 2)
|
||||
}
|
||||
|
||||
@Test("A playlist count the server has beats the row count")
|
||||
func playlistCountIsTakenFromTheServer() throws {
|
||||
// More than the page carries — the rest is behind the pager.
|
||||
let page = pipedPlaylistPageJSON(
|
||||
streams: [pipedStreamJSON(videoId: "aaa", title: "First", views: 10, uploaded: nil)],
|
||||
videos: 42
|
||||
)
|
||||
|
||||
let snapshot = try PipedAPI.userPlaylistSnapshot(
|
||||
fromPageJSON: [page],
|
||||
playlistID: "playlist-uuid",
|
||||
instanceURL: pipedBaseURL
|
||||
)
|
||||
|
||||
#expect(snapshot.playlist.videoCount == 42)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Write Responses
|
||||
|
||||
Reference in New Issue
Block a user