~ knotless Search/
↑↓ move↵ open⇧↵ all results

geesawra.industries/old-fella

Old people's social media app

0
1
main
218 lines · 10.3 KB Raw History
import Observation
import XCTest

@testable import OldFella

extension AppModelTests {
    func testOverlappingPreferenceRefreshesShareOneRequest() async {
        let service = ModerationTestService()
        let model = makeAppModel(service: service)
        await model.signIn(handle: "demo", appPassword: "password")
        let initialCalls = service.preferenceCalls
        let gate = ThreadGate()
        service.preferenceGate = gate
        service.mutedWords = [.init(value: "updated")]
        let first = Task { await model.reloadContentPreferences() }
        await gate.waitUntilStarted()
        var secondStarted = false
        let second = Task {
            secondStarted = true
            await model.reloadContentPreferences()
        }
        while !secondStarted { await Task.yield() }
        XCTAssertEqual(service.preferenceCalls, initialCalls + 1)
        await gate.release()
        await first.value
        await second.value
        XCTAssertEqual(service.preferenceCalls, initialCalls + 1)
        XCTAssertEqual(model.contentPreferences?.mutedWords.map(\.value), ["updated"])
        service.preferenceGate = nil
        await model.reloadContentPreferences()
        XCTAssertEqual(service.preferenceCalls, initialCalls + 2)
    }

    func testPreferenceRefreshCannotRestoreContentAfterSignOut() async {
        let service = ModerationTestService()
        let model = makeAppModel(service: service)
        await model.signIn(handle: "demo", appPassword: "password")
        let gate = ThreadGate()
        service.preferenceGate = gate
        let refresh = Task { await model.reloadContentPreferences() }
        await gate.waitUntilStarted()
        await model.signOut()
        await gate.release()
        await refresh.value
        XCTAssertNil(model.account)
        XCTAssertNil(model.contentPreferences)
        XCTAssertTrue(model.timelinePosts.isEmpty)
        service.preferenceGate = nil
        await model.signIn(handle: "demo", appPassword: "password")
        XCTAssertNotNil(model.contentPreferences)
    }

    func testNotificationGroupsKeepOrderAndSeparateReasonsAndPosts() {
        func notification(_ id: String, _ reason: FeedNotification.Reason, _ postID: String?) -> FeedNotification {
            FeedNotification(
                id: id, author: Post.Author(id: id, name: id, handle: id), reason: reason,
                indexedAt: .now, isRead: false, postID: postID)
        }
        let notifications = [
            notification("1", .like, "a"), notification("2", .like, "a"),
            notification("3", .repost, "a"), notification("4", .repost, "a"),
            notification("5", .like, "a"), notification("6", .like, "b"),
            notification("7", .like, nil), notification("8", .like, nil)
        ]
        XCTAssertEqual(
            NotificationGroup.make(from: notifications).map { $0.notifications.map(\.id) },
            [["1", "2", "5"], ["3", "4"], ["6"], ["7"], ["8"]])
        XCTAssertEqual(
            NotificationGroup.make(from: notifications, pageStarts: ["5"]).map { $0.notifications.map(\.id) },
            [["1", "2"], ["3", "4"], ["5"], ["6"], ["7"], ["8"]])
        XCTAssertTrue(NotificationGroup.make(from: []).isEmpty)
    }

    func testElapsedTimeRoundsDown() {
        let now = Date(timeIntervalSince1970: 2_000_000_000)
        XCTAssertEqual(
            PostElapsedTime.text(since: now.addingTimeInterval(-119), now: now),
            PostElapsedTime.text(since: now.addingTimeInterval(-60), now: now))
        XCTAssertNotEqual(
            PostElapsedTime.text(since: now.addingTimeInterval(-120), now: now),
            PostElapsedTime.text(since: now.addingTimeInterval(-119), now: now))
        XCTAssertEqual(
            PostElapsedTime.text(since: now.addingTimeInterval(20), now: now),
            PostElapsedTime.text(since: now, now: now))
    }

    func testElapsedTimeUsesCompactUnits() {
        let now = Date(timeIntervalSince1970: 2_000_000_000)
        XCTAssertEqual(PostElapsedTime.text(since: now.addingTimeInterval(-59), now: now), "59s")
        XCTAssertEqual(PostElapsedTime.text(since: now.addingTimeInterval(-60), now: now), "1m")
        XCTAssertEqual(PostElapsedTime.text(since: now.addingTimeInterval(-3_600), now: now), "1h")
        XCTAssertEqual(PostElapsedTime.text(since: now.addingTimeInterval(-86_400), now: now), "1d")
        XCTAssertEqual(PostElapsedTime.text(since: now.addingTimeInterval(-604_800), now: now), "1w")
        XCTAssertEqual(PostElapsedTime.text(since: now.addingTimeInterval(-31_536_000), now: now), "1y")
    }

    func testElapsedTimeStaysWithinFourCharactersForExtremeDates() {
        let now = Date(timeIntervalSince1970: 2_000_000_000)
        let farPast = PostElapsedTime.text(since: .distantPast, now: now)
        let farFuture = PostElapsedTime.text(since: .distantFuture, now: now)

        XCTAssertEqual(farPast, "999y")
        XCTAssertEqual(farFuture, "0s")
        XCTAssertLessThanOrEqual(farPast.count, 4)
        XCTAssertLessThanOrEqual(farFuture.count, 4)
    }

    func testProfileRevealKeyChangesWithActorDecisionAndRevision() {
        let revision = UUID()
        let decision = ModerationPolicy.Decision(action: .warn, reasons: [])
        let key = ProfileRevealKey(actorID: "one", decision: decision, contentRevision: revision)
        XCTAssertEqual(key, ProfileRevealKey(actorID: "one", decision: decision, contentRevision: revision))
        XCTAssertNotEqual(key, ProfileRevealKey(actorID: "two", decision: decision, contentRevision: revision))
        XCTAssertNotEqual(key, ProfileRevealKey(actorID: "one", decision: nil, contentRevision: revision))
        XCTAssertNotEqual(key, ProfileRevealKey(actorID: "one", decision: decision, contentRevision: UUID()))
    }

    func testForegroundRetryKeepsPreferenceErrorWhileLoading() async {
        let service = ModerationTestService()
        service.preferenceError = URLError(.notConnectedToInternet)
        let model = makeAppModel(service: service)
        await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
        let error = model.contentPreferencesError
        XCTAssertNotNil(error)
        let gate = ThreadGate()
        service.preferenceGate = gate
        model.sceneBecameActive()
        await gate.waitUntilStarted()
        XCTAssertNil(model.contentPreferences)
        XCTAssertEqual(model.contentPreferencesError, error)
        XCTAssertTrue(model.timelinePosts.isEmpty)
        await gate.release()
    }

    func testUnavailableLabelerBlocksContentUntilPreferencesReload() async {
        let service = ModerationTestService()
        service.missingLabeler = true
        let model = makeAppModel(service: service)
        await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
        XCTAssertNil(model.contentPreferences)
        XCTAssertTrue(model.contentPreferencesError?.contains("Manage your moderation preferences in Bluesky") == true)
        XCTAssertTrue(model.timelinePosts.isEmpty)
        service.missingLabeler = false
        await model.reloadContentPreferences()
        XCTAssertNil(model.contentPreferencesError)
        XCTAssertNotNil(model.contentPreferences)
        await model.loadFeed()
        XCTAssertFalse(model.timelinePosts.isEmpty)
    }

    func testFailedContentPreferenceRefreshRemovesPreviousDecision() async {
        let service = ModerationTestService()
        let model = makeAppModel(service: service)
        await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
        let post = model.posts[0]
        XCTAssertNotNil(model.contentDecision(for: post))
        service.failPreferences = true
        await model.reloadContentPreferences()
        XCTAssertNil(model.contentDecision(for: post))
        XCTAssertNotNil(model.contentPreferencesError)
        XCTAssertTrue(model.posts.isEmpty)
        XCTAssertTrue(model.timelinePosts.isEmpty)
        XCTAssertTrue(model.visibleNotifications.isEmpty)
    }

    func testUnchangedPreferenceReloadKeepsCachedContent() async {
        let service = ModerationTestService()
        let model = makeAppModel(service: service)
        await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
        await model.loadProfile(actorID: "mae")
        _ = await model.loadNotifications()
        await model.loadPost(id: "post-2")
        let oldRevision = model.contentRevision
        XCTAssertFalse(model.posts.isEmpty)
        await model.reloadContentPreferences()
        XCTAssertEqual(model.contentRevision, oldRevision)
        XCTAssertFalse(model.posts.isEmpty)
        XCTAssertFalse(model.profiles.isEmpty)
        XCTAssertFalse(model.authorPosts.isEmpty)
        XCTAssertFalse(model.threadContexts.isEmpty)
        XCTAssertFalse(model.notifications.isEmpty)
        await model.loadFeed()
        XCTAssertFalse(model.timelinePosts.isEmpty)
    }

    func testConnectionFailureKeepsLoadedPreferencesAndContent() async {
        let service = ModerationTestService()
        let model = makeAppModel(service: service)
        await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
        let ids = model.posts.map(\.id)
        let revision = model.contentRevision
        service.preferenceError = URLError(.notConnectedToInternet)
        await model.reloadContentPreferences()
        XCTAssertNotNil(model.contentPreferences)
        XCTAssertNil(model.contentPreferencesError)
        XCTAssertNotNil(model.contentPreferencesRefreshError)
        XCTAssertEqual(model.posts.map(\.id), ids)
        XCTAssertEqual(model.contentRevision, revision)
        XCTAssertNil(model.errorMessage)
        service.preferenceError = nil
        await model.reloadContentPreferences()
        XCTAssertNil(model.contentPreferencesRefreshError)
    }

    func testExpiredPreferencesDoNotSurviveConnectionFailure() async {
        let service = ModerationTestService()
        var time = Date(timeIntervalSince1970: 1_700_000_000)
        let model = makeAppModel(service: service, now: { time })
        await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
        time.addTimeInterval(3600)
        service.preferenceError = URLError(.notConnectedToInternet)
        await model.reloadContentPreferences()
        XCTAssertNil(model.contentPreferences)
        XCTAssertNotNil(model.contentPreferencesError)
        XCTAssertNil(model.contentPreferencesRefreshError)
        XCTAssertTrue(model.posts.isEmpty)
    }
}