SPB Git

Repository Public

TestBackend.swift 2.4 KB 52 lines swift

1// SP Drive iOS2// File: Tests/TestBackend.swift3// Description: Direct access to the disposable backend from the tests, to prepare data and to check what really reached the server.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import Foundation8@testable import SPDrive910enum TestBackend {11    static var url: URL? { ProcessInfo.processInfo.environment["SPDRIVE_BASE_URL"].flatMap(URL.init(string:)) }12    static var token: String? { ProcessInfo.processInfo.environment["SPDRIVE_TEST_TOKEN"].flatMap { $0.isEmpty ? nil : $0 } }13    static var available: Bool { url != nil && token != nil }1415    /// A second client on the same account: plays « another device ».16    static func api() -> DriveAPI { DriveAPI(baseURL: url!, tokens: MemoryTokenStore(token)) }1718    static func upload(_ data: Data, named name: String, into parent: String, api: DriveAPI) async throws -> DriveItem {19        let dir = FileManager.default.temporaryDirectory.appendingPathComponent("seed-\(UUID().uuidString)", isDirectory: true)20        try FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true)21        let file = dir.appendingPathComponent(name)22        try data.write(to: file)23        return try await DirectTransfer.upload(file: file, remote: api, store: nil, resumeKey: nil, scratch: dir.appendingPathComponent("chunk")) { size in24            try await api.createUpload(parentID: parent, name: name, size: size, onConflict: .rename, fileID: nil, baseVersionID: nil)25        }26    }2728    static func children(of folder: String, api: DriveAPI) async throws -> [DriveItem] {29        var all: [DriveItem] = []30        var since: Int64 = 031        while true {32            let page = try await api.changes(since: since, limit: 2000)33            all += page.items34            since = page.cursor35            if !page.more { break }36        }37        // the feed lists an item once per change: keep the latest state38        var latest: [String: DriveItem] = [:]39        for i in all { latest[i.id] = i }40        return latest.values.filter { $0.parentID == folder && !$0.isTrashed }41    }4243    @MainActor static func eventually(_ timeout: TimeInterval = 30, _ condition: @MainActor () async -> Bool) async -> Bool {44        let deadline = Date.now.addingTimeInterval(timeout)45        while Date.now < deadline {46            if await condition() { return true }47            try? await Task.sleep(for: .milliseconds(250))48        }49        return await condition()50    }51}52
Files main
Menu

Theme