ForgejoKit/Tests/ForgejoKitTests/RepositoryServiceURLTests.swift
Stefan Hausotte 897a8ebedd feat: initial commit
Intitial commit for ForgejoKit, a native Swift library to interact with
the Frogejo API
2026-02-28 20:25:57 +01:00

63 lines
2.6 KiB
Swift

import Foundation
import Testing
@testable import ForgejoKit
struct RepositoryServiceURLTests {
/// Creates a client with dummy credentials for URL construction tests.
private func makeClient() -> ForgejoClient {
ForgejoClient(serverURL: "https://forgejo.example.com", username: "user", password: "pass")
}
// MARK: - fetchContents URL construction
@Test func makeURLContentsWithoutRef() throws {
let client = makeClient()
let url = try client.makeURL(path: "/api/v1/repos/owner/repo/contents")
#expect(url.absoluteString == "https://forgejo.example.com/api/v1/repos/owner/repo/contents")
}
@Test func makeURLContentsWithRef() throws {
let client = makeClient()
let queryItems = [URLQueryItem(name: "ref", value: "develop")]
let url = try client.makeURL(path: "/api/v1/repos/owner/repo/contents", queryItems: queryItems)
#expect(url.absoluteString == "https://forgejo.example.com/api/v1/repos/owner/repo/contents?ref=develop")
}
@Test func makeURLContentsWithRefAndSubpath() throws {
let client = makeClient()
let queryItems = [URLQueryItem(name: "ref", value: "feature/login")]
let url = try client.makeURL(path: "/api/v1/repos/owner/repo/contents/src/main.py", queryItems: queryItems)
#expect(url.query?.contains("ref=feature/login") == true)
#expect(url.path == "/api/v1/repos/owner/repo/contents/src/main.py")
}
@Test func makeURLFileContentWithRef() throws {
let client = makeClient()
let queryItems = [URLQueryItem(name: "ref", value: "v1.0")]
let url = try client.makeURL(path: "/api/v1/repos/owner/repo/contents/README.md", queryItems: queryItems)
#expect(url.absoluteString == "https://forgejo.example.com/api/v1/repos/owner/repo/contents/README.md?ref=v1.0")
}
@Test func makeURLWithEmptyQueryItems() throws {
let client = makeClient()
let url = try client.makeURL(path: "/api/v1/repos/owner/repo/contents", queryItems: [])
#expect(url.query == nil)
}
// MARK: - ref parameter nil-coalescence (same pattern used by fetchContents and fetchFileContent)
@Test func optionalRefProducesEmptyQueryItems() {
let ref: String? = nil
let queryItems = ref.map { [URLQueryItem(name: "ref", value: $0)] } ?? []
#expect(queryItems.isEmpty)
}
@Test func presentRefProducesQueryItem() {
let ref: String? = "main"
let queryItems = ref.map { [URLQueryItem(name: "ref", value: $0)] } ?? []
#expect(queryItems.count == 1)
#expect(queryItems.first?.name == "ref")
#expect(queryItems.first?.value == "main")
}
}