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

geesawra.industries/old-fella

Old people's social media app

0
1
main
85 lines · 3.2 KB Raw History
import Foundation
import XCTest

@testable import OldFella

final class NotificationBatchFixture {
    let suiteName: String
    let defaults: UserDefaults
    let session: URLSession
    let token: String
    var failPostURI: String? = NotificationBatchFixture.postURI(25)
    var omitPostURI: String?
    var omitOnlyFromBatch = false
    var mutedPostURI: String?
    var malformedPostURI: String?
    var recordedBatchSizes: [Int] = []
    let batchSizesLock = NSLock()
    var batchSizes: [Int] {
        get { batchSizesLock.withLock { recordedBatchSizes } }
        set { batchSizesLock.withLock { recordedBatchSizes = newValue } }
    }
    lazy var service = ATProtoBlueskyService(defaults: defaults, session: session)

    init() throws {
        suiteName = "OldFellaNotificationBatchTests.\(UUID().uuidString)"
        defaults = try XCTUnwrap(UserDefaults(suiteName: suiteName))
        let configuration = URLSessionConfiguration.ephemeral
        configuration.protocolClasses = [StubURLProtocol.self]
        session = URLSession(configuration: configuration)
        let futureExpiry = Int(Date().addingTimeInterval(3600).timeIntervalSince1970)
        let payload = Data("{\"exp\":\(futureExpiry)}".utf8).base64EncodedString()
            .replacingOccurrences(of: "+", with: "-")
            .replacingOccurrences(of: "/", with: "_")
            .replacingOccurrences(of: "=", with: "")
        token = "header.\(payload).signature"
    }

    func install() {
        StubURLProtocol.clearRequests()
        StubURLProtocol.handler = response
    }

    func cleanUp() {
        StubURLProtocol.errorHandler = nil
        StubURLProtocol.handler = nil
        session.invalidateAndCancel()
        defaults.removePersistentDomain(forName: suiteName)
    }

    func response(_ request: URLRequest) throws -> (Int, Data) {
        switch request.url?.path {
        case "/xrpc/com.atproto.identity.resolveHandle":
            return (200, Data("{\"did\":\"did:plc:reader\"}".utf8))
        case "/did:plc:reader":
            return (
                200,
                Data(
                    ("{\"id\":\"did:plc:reader\",\"service\":[{\"id\":\"#atproto_pds\","
                        + "\"type\":\"AtprotoPersonalDataServer\","
                        + "\"serviceEndpoint\":\"https://pds.example.test\"}]}")
                        .utf8)
            )
        case "/xrpc/com.atproto.server.createSession":
            return (
                200,
                Data(
                    ("{\"handle\":\"reader.example\",\"did\":\"did:plc:reader\","
                        + "\"accessJwt\":\"\(token)\",\"refreshJwt\":\"refresh-token\"}")
                        .utf8)
            )
        case "/xrpc/app.bsky.actor.getProfile":
            return (
                200,
                Data("{\"did\":\"did:plc:reader\",\"handle\":\"reader.example\",\"displayName\":\"Reader\"}".utf8)
            )
        case "/xrpc/app.bsky.notification.listNotifications":
            let response = try JSONSerialization.data(withJSONObject: ["notifications": Self.notifications])
            return (200, response)
        case "/xrpc/app.bsky.feed.getPosts":
            return try posts(request)
        default:
            return (404, Data("{\"error\":\"NotFound\"}".utf8))
        }
    }
}