Repository Public
1// SP Notes Core2// File: Tests/SPNotesAPITests/LiveNextSupport.swift3// Description: Shared environment of the live tests of the 3.0 routes (LiveNext*Tests): the SCRATCH backend only, never4// production; skipped unless SPN_LIVE_URL and SPN_LIVE_TOKEN are set; what a test creates is removed.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8//9// Run (scratch instance of API-NEXT §7, through a tunnel):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 LiveNext13//14// Origin: the server refuses every mutation whose `Origin` is not its own. Through a tunnel the base URL15// (`http://127.0.0.1:18483`) is not the origin the server knows (`http://localhost:8483`): the helper sends16// SPN_LIVE_ORIGIN when it is set, otherwise the first origin the server announces in `GET /api/auth/state`.17import Foundation18import XCTest19@testable import SPNotesAPI2021/// A client on the scratch backend and the cleanup of what a live test creates.22struct LiveNext {23 var api: NotesAPI24 var client: HTTPClient25 var configuration: APIConfiguration2627 static func environment(_ name: String) -> String? {28 guard let value = ProcessInfo.processInfo.environment[name], !value.isEmpty else { return nil }29 return value30 }3132 /// Builds the client, or skips the test when the live environment is not set.33 static func make() async throws -> LiveNext {34 guard let base = environment("SPN_LIVE_URL"), let url = URL(string: base) else { throw XCTSkip("SPN_LIVE_URL is not set") }35 guard let host = url.host, !host.hasSuffix("spboucher.ai") else {36 XCTFail("The live tests never run against production")37 throw XCTSkip("refusing to use \(base)")38 }39 guard let token = environment("SPN_LIVE_TOKEN") else { throw XCTSkip("SPN_LIVE_TOKEN is not set") }40 var configuration = APIConfiguration(baseURL: url, userAgent: APIConfiguration.userAgent(appVersion: "0.0.1"))41 if let origin = environment("SPN_LIVE_ORIGIN") {42 configuration.origin = origin43 } else {44 let probe = NotesAPI(client: HTTPClient(configuration: configuration, tokenStore: InMemoryTokenStore(token: token)))45 if let origins = try await probe.authState().origins, !origins.contains(configuration.origin), let first = origins.first {46 configuration.origin = first47 }48 }49 let client = HTTPClient(configuration: configuration, tokenStore: InMemoryTokenStore(token: token))50 return LiveNext(api: NotesAPI(client: client), client: client, configuration: configuration)51 }5253 /// The server must be a 3.0 one with this feature; otherwise the test is skipped (not failed).54 func require(_ feature: String) async throws {55 let meta = try await api.meta()56 guard meta.api >= 3, meta.has(feature) else { throw XCTSkip("the server does not announce the feature \(feature)") }57 }5859 static func document(_ text: String) -> RawJSON {60 RawJSON(string: #"{"type":"doc","content":[{"type":"paragraph","content":[{"type":"text","text":"\#(text)"}]}]}"#)61 }6263 /// Creates a page titled `Live · <title>` and registers its removal (trash, then purge when the session is fresh64 /// enough — a dev token is for 5 minutes; after that the page stays in the trash).65 @discardableResult66 func page(_ test: XCTestCase, _ title: String, parentId: String? = nil, section: PageSection = .notes, type: PageType? = nil, content: RawJSON? = nil) async throws -> Page {67 let page = try await api.createPage(PageDraft(id: NotesAPI.newID(), title: "Live · \(title)", parentId: parentId, section: section, type: type, content: content)).value68 let api = api69 test.addTeardownBlock {70 _ = try? await api.trashPage(id: page.id)71 _ = try? await api.purgePage(id: page.id)72 }73 return page74 }75}76