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

geesawra.industries/old-fella

Old people's social media app

0
1
main
213 lines · 11.9 KB Raw History
import Observation
import XCTest

@testable import OldFella

extension AppModelTests {
    func testPreparedContentRefreshesWhenExpiryTimerFires() async throws {
        let service = ModerationTestService()
        let author = Post.Author(id: "other", name: "Other", handle: "other")
        let post = Post(
            id: "post", author: author, text: "hidden text", likeCount: 0, repostCount: 0, isLiked: false,
            isReposted: false, imageURLs: [], replyIDs: [])
        service.feedPage = FeedPage(posts: [post], cursor: nil)
        let model = makeAppModel(service: service)
        await model.signInWithDemo()
        service.mutedWords = [.init(value: "hidden", expiresAt: Date().addingTimeInterval(0.1))]
        await model.reloadContentPreferences()
        await model.loadFeed()
        XCTAssertTrue(model.timelinePosts.isEmpty)
        let refreshed = expectation(description: "Expired muted word refreshes the prepared feed")
        withObservationTracking {
            _ = model.timelinePosts
        } onChange: {
            Task { @MainActor in
                if model.timelinePosts.map(\.id) == [post.id] { refreshed.fulfill() }
            }
        }
        await fulfillment(of: [refreshed], timeout: 2)
        XCTAssertEqual(model.timelinePosts.map(\.id), [post.id])
    }

    func testPreparedContentRefreshesAtMutedWordAndLabelExpiry() async {
        let service = ModerationTestService()
        var time = Date(timeIntervalSince1970: 1_800_000_000)
        let author = Post.Author(id: "other", name: "Other", handle: "other")
        let muted = Post(
            id: "muted", author: author, text: "hidden text", likeCount: 0, repostCount: 0, isLiked: false,
            isReposted: false, imageURLs: [], replyIDs: [])
        var labeled = Post(
            id: "labeled", author: author, text: "safe text", likeCount: 0, repostCount: 0, isLiked: false,
            isReposted: false, imageURLs: [], replyIDs: [])
        labeled.moderationLabels = [
            .init(value: "!hide", sourceDID: author.id, expiresAt: time.addingTimeInterval(120))
        ]
        service.mutedWords = [.init(value: "hidden", expiresAt: time.addingTimeInterval(60))]
        service.feedPage = FeedPage(posts: [muted, labeled], cursor: nil)
        service.notificationPage = NotificationPage(
            notifications: [
                FeedNotification(
                    id: "notification", author: author, reason: .reply, indexedAt: time, isRead: false,
                    postID: muted.id, relatedPost: muted)
            ], cursor: nil)
        let model = makeAppModel(service: service, now: { time })
        await model.signInWithDemo()
        await model.loadNotifications()
        XCTAssertTrue(model.timelinePosts.isEmpty)
        XCTAssertFalse(model.hasUnreadNotifications)
        let labeledState = model.postViewState(for: labeled)
        let decisions = ObservationProbe { labeledState.decision }
        XCTAssertEqual(labeledState.decision?.action, .hide)
        time.addTimeInterval(60)
        model.refreshExpiredModeration()
        XCTAssertEqual(model.timelinePosts.map(\.id), [muted.id])
        XCTAssertTrue(model.hasUnreadNotifications)
        XCTAssertEqual(model.contentDecision(for: labeled)?.action, .hide)
        time.addTimeInterval(60)
        model.refreshExpiredModeration()
        XCTAssertEqual(model.timelinePosts.map(\.id), [muted.id, labeled.id])
        XCTAssertEqual(model.contentDecision(for: labeled)?.action, .allow)
        await Task.yield()
        XCTAssertEqual(labeledState.decision?.action, .allow)
        XCTAssertEqual(decisions.values.count, 1)
        let preparations = model.contentPreparationCount
        model.refreshExpiredModeration()
        XCTAssertEqual(model.contentPreparationCount, preparations)
        time.addTimeInterval(-120)
        model.refreshExpiredModeration()
        XCTAssertTrue(model.timelinePosts.isEmpty)
        XCTAssertFalse(model.hasUnreadNotifications)
        await model.signOut()
        assertSignedOutModeration(model, post: muted)
    }

    func testTimelineRespectsGraphicMediaPreferenceOnPostWithMultipleLabels() async {
        let author = Post.Author(id: "did:example:other", name: "Other", handle: "other")
        let labels: [ModerationPolicy.Label] = [
            .init(value: "porn", sourceDID: author.id),
            .init(value: "graphic-media", sourceDID: author.id)
        ]
        let post = Post(
            id: "graphic", author: author, text: "", likeCount: 0, repostCount: 0, isLiked: false, isReposted: false,
            imageURLs: [URL(string: "https://example.com/image.jpg")!], replyIDs: [], moderationLabels: labels)
        for setting: ModerationPolicy.LabelSetting in [.ignore, .warn, .hide] {
            let service = ModerationTestService()
            service.preferenceOverride = .init(
                userDID: "demo", adultContentEnabled: true, labels: ["porn": .warn, "graphic-media": setting],
                labelDefinitions: ModerationPolicy.builtInDefinitions)
            service.feedPage = FeedPage(posts: [post], cursor: nil)
            let model = makeAppModel(service: service)
            await model.signIn(handle: "demo", appPassword: "password")
            XCTAssertEqual(model.timelineRowIDs, setting == .hide ? [] : ["graphic"], "setting: \(setting)")
        }
    }

    func testTimelineAdultMediaWarningsDoNotOverrideOtherWarnings() async {
        let author = Post.Author(id: "did:example:other", name: "Other", handle: "other")
        let image = URL(string: "https://example.com/image.jpg")!
        let adult = ModerationPolicy.Label(value: "sexual", sourceDID: author.id)
        let other = ModerationPolicy.Label(value: "icon-intolerant", sourceDID: author.id)
        let locked = ModerationPolicy.Label(value: "locked", sourceDID: author.id)
        let definition = ModerationPolicy.LabelDefinition(
            value: "locked", sourceDID: author.id, defaultSetting: .warn, noOverride: true, severity: .alert,
            blur: .media)
        func post(_ id: String, labels: [ModerationPolicy.Label], quote: Bool) -> Post {
            let quoted =
                quote
                ? Post.Quote(
                    postID: "quote-\(id)", author: author, text: "quote", imageURLs: [image], moderationLabels: labels)
                : nil
            return Post(
                id: id, author: author, text: "safe", likeCount: 0, repostCount: 0, isLiked: false, isReposted: false,
                imageURLs: quote ? [] : [image], quote: quoted, replyIDs: [], moderationLabels: quote ? [] : labels)
        }
        for setting: ModerationPolicy.LabelSetting in [.warn, .ignore, .hide] {
            let service = ModerationTestService()
            service.preferenceOverride = .init(
                userDID: "demo", adultContentEnabled: true, labels: ["sexual": setting, "icon-intolerant": .warn],
                labelDefinitions: ModerationPolicy.builtInDefinitions + [definition])
            service.feedPage = FeedPage(
                posts: [
                    post("adult", labels: [adult], quote: false), post("adult-quote", labels: [adult], quote: true),
                    post("mixed", labels: [adult, other], quote: false),
                    post("mixed-quote", labels: [adult, other], quote: true),
                    post("locked", labels: [adult, locked], quote: false),
                    post("locked-quote", labels: [adult, locked], quote: true)
                ], cursor: nil)
            let model = makeAppModel(service: service)
            await model.signIn(handle: "demo", appPassword: "password")
            XCTAssertEqual(
                model.timelineRowIDs, setting == .hide ? [] : ["adult", "adult-quote"], "setting: \(setting)")
        }
    }

    func testTimelineOmitsHiddenAndRevealRequiredPosts() async {
        let service = ModerationTestService()
        let author = Post.Author(id: "did:example:other", name: "Other", handle: "other")
        func post(_ id: String, label: String? = nil, media: Bool = false) -> Post {
            var result = Post(
                id: id, author: author, text: id, likeCount: 0, repostCount: 0, isLiked: false, isReposted: false,
                imageURLs: media ? [URL(string: "https://example.com/image.jpg")!] : [], replyIDs: [])
            if let label { result.moderationLabels = [.init(value: label, sourceDID: author.id)] }
            return result
        }
        service.preferenceOverride = .init(
            userDID: "demo", adultContentEnabled: true, labels: ["nudity": .warn, "scam": .warn],
            labelDefinitions: ModerationPolicy.builtInDefinitions)
        service.feedPage = FeedPage(
            posts: [
                post("safe"), post("hidden", label: "!hide"), post("warning", label: "!warn"),
                post("media", label: "nudity", media: true), post("no-media", label: "nudity"),
                post("alert", label: "scam")
            ], cursor: nil)
        let model = makeAppModel(service: service)
        await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")
        XCTAssertEqual(model.timelinePosts.map(\.id), ["safe", "media", "no-media", "alert"])
    }

    func testTimelineOmitsPostsWithHiddenOrRevealRequiredQuotes() async {
        let service = ModerationTestService()
        let author = Post.Author(id: "other", name: "Other", handle: "other")
        let image = URL(string: "https://example.com/image.jpg")!
        func post(_ id: String, label: String? = nil, media: Bool = false) -> Post {
            let labels: [ModerationPolicy.Label] = label.map { [.init(value: $0, sourceDID: author.id)] } ?? []
            let quote = Post.Quote(
                postID: "quote-\(id)", author: author, text: "quoted text", imageURLs: media ? [image] : [],
                moderationLabels: labels)
            return Post(
                id: id, author: author, text: "safe", likeCount: 0, repostCount: 0, isLiked: false, isReposted: false,
                imageURLs: [], quote: quote, replyIDs: [])
        }
        let information = ModerationPolicy.LabelDefinition(
            value: "information", sourceDID: author.id, severity: .inform, blur: .none)
        service.preferenceOverride = .init(
            userDID: "demo", adultContentEnabled: true, labels: ["nudity": .warn, "scam": .warn, "information": .warn],
            labelDefinitions: ModerationPolicy.builtInDefinitions + [information])
        let safe = post("safe")
        let hidden = post("hidden", label: "!hide")
        let warning = post("warning", label: "!warn")
        let media = post("media", label: "nudity", media: true)
        let textOnly = post("text-only", label: "nudity")
        let alert = post("alert", label: "scam")
        let informational = post("informational", label: "information")
        service.feedPage = FeedPage(posts: [safe, hidden, textOnly, warning, alert, media, informational], cursor: nil)
        let model = makeAppModel(service: service)
        await model.signIn(handle: "demo.bsky.social", appPassword: "test-only")

        XCTAssertEqual(model.quoteContentDecision(hidden.quote!, fallbackPost: nil)?.action, .hide)
        XCTAssertTrue(model.quoteContentDecision(warning.quote!, fallbackPost: nil)?.ui(.contentList).blur == true)
        XCTAssertTrue(model.quoteContentDecision(media.quote!, fallbackPost: nil)?.ui(.contentMedia).blur == true)
        XCTAssertTrue(model.quoteContentDecision(alert.quote!, fallbackPost: nil)?.ui(.contentList).alert == true)
        XCTAssertTrue(
            model.quoteContentDecision(informational.quote!, fallbackPost: nil)?.ui(.contentList).inform == true)
        XCTAssertEqual(model.timelinePosts.map(\.id), ["safe", "text-only", "alert", "media", "informational"])
    }

    private func assertSignedOutModeration(_ model: AppModel, post: Post) {
        XCTAssertTrue(model.timelinePosts.isEmpty)
        XCTAssertTrue(model.visibleNotifications.isEmpty)
        XCTAssertTrue(model.notificationGroups.isEmpty)
        XCTAssertFalse(model.hasUnreadNotifications)
        XCTAssertNil(model.contentDecision(for: post))
    }
}