SPB Git

Repository Public

LiveParitySupport.swift 3.9 KB 76 lines swift

1// SP Notes Core2// File: Tests/SPNotesAPITests/LiveParitySupport.swift3// Description: Environment of the live "parity" tests: a client for the SCRATCH backend (never production), with the4//              Origin the server really accepts, and the clean-up of what a test creates.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8//9// Run:10//   ssh -f -N -L 18483:127.0.0.1:8483 M4M64c11//   SPN_LIVE_URL=http://127.0.0.1:18483 SPN_LIVE_TOKEN=<dev token> [SPN_LIVE_ORIGIN=http://localhost:8483] \12//   SPN_ONLY=API swift test --scratch-path .build-api --filter LiveParityTests13import Foundation14import XCTest15@testable import SPNotesAPI1617/// A client for the scratch backend. Skips the test unless `SPN_LIVE_URL` and `SPN_LIVE_TOKEN` are set; refuses any18/// `*.spboucher.ai` host.19struct LiveParity {20    var api: NotesAPI21    var client: HTTPClient22    var configuration: APIConfiguration23    /// A marker unique to this run, to put in titles and find them again.24    let marker = "spnparity" + UUID().uuidString.replacingOccurrences(of: "-", with: "").prefix(10).lowercased()2526    private static func environment(_ name: String) -> String? {27        guard let value = ProcessInfo.processInfo.environment[name], !value.isEmpty else { return nil }28        return value29    }3031    /// Builds the client. The server only accepts mutations whose `Origin` is its own APP_URL (`http://localhost:8483`32    /// for the scratch instance), which is not the URL of an SSH tunnel: the origin comes from `SPN_LIVE_ORIGIN`, else33    /// from what the server itself announces in `GET /api/auth/state` (`origins`).34    static func make() async throws -> LiveParity {35        guard let base = environment("SPN_LIVE_URL"), let url = URL(string: base) else { throw XCTSkip("SPN_LIVE_URL is not set") }36        guard let host = url.host, !host.hasSuffix("spboucher.ai") else {37            XCTFail("The live tests never run against production")38            throw XCTSkip("refusing to use \(base)")39        }40        guard let token = environment("SPN_LIVE_TOKEN") else { throw XCTSkip("SPN_LIVE_TOKEN is not set") }41        var configuration = APIConfiguration(baseURL: url, userAgent: APIConfiguration.userAgent(appVersion: "0.0.1"))42        if let origin = environment("SPN_LIVE_ORIGIN") {43            configuration.origin = origin44        } else {45            let probe = HTTPClient(configuration: configuration, tokenStore: InMemoryTokenStore(token: token))46            let state = try await NotesAPI(client: probe).authState()47            if let origins = state.origins, !origins.contains(configuration.origin), let accepted = origins.first {48                configuration.origin = accepted49            }50        }51        let client = HTTPClient(configuration: configuration, tokenStore: InMemoryTokenStore(token: token))52        return LiveParity(api: NotesAPI(client: client), client: client, configuration: configuration)53    }5455    func title(_ text: String) -> String { "Fixture · live \(text) \(marker)" }5657    /// Creates a page that is trashed (and purged when the session is fresh enough) at the end of the test.58    func page(_ test: XCTestCase, _ text: String, type: PageType? = nil, section: PageSection? = nil, parentId: String? = nil, content: RawJSON? = nil) async throws -> Page {59        let created = try await api.createPage(PageDraft(id: NotesAPI.newID(), title: title(text), parentId: parentId, section: section, type: type, content: content))60        cleanUp(test, page: created.value.id)61        return created.value62    }6364    func cleanUp(_ test: XCTestCase, page id: String) {65        let api = api66        test.addTeardownBlock {67            _ = try? await api.trashPage(id: id)68            _ = try? await api.purgePage(id: id)69        }70    }7172    func document(_ text: String) -> RawJSON {73        RawJSON(string: #"{"type":"doc","content":[{"type":"paragraph","content":[{"type":"text","text":"\#(text)"}]}]}"#)74    }75}76
Files main
Menu

Theme