ForgejoKit/Tests/ForgejoKitTests/FileContentTests.swift
2026-06-04 11:29:23 +02:00

80 lines
2.6 KiB
Swift

@testable import ForgejoKit
import Foundation
import Testing
struct FileContentTests {
private func makeFileContent(content: String?, encoding: String?) -> FileContent {
FileContent(
name: "test.txt",
path: "test.txt",
sha: "abc",
size: 100,
url: "https://example.com",
htmlUrl: "https://example.com",
gitUrl: "https://example.com",
downloadUrl: nil,
type: "file",
content: content,
encoding: encoding,
)
}
// MARK: - Base64 decoding
@Test func decodesBase64Content() {
let original = "Hello, World!"
let base64 = Data(original.utf8).base64EncodedString()
let file = makeFileContent(content: base64, encoding: "base64")
#expect(file.decodedContent == "Hello, World!")
}
@Test func decodesBase64WithNewlines() {
let original = "Line one\nLine two\nLine three"
let base64 = Data(original.utf8).base64EncodedString()
let wrappedBase64 = String(base64.prefix(20)) + "\n" + String(base64.dropFirst(20))
let file = makeFileContent(content: wrappedBase64, encoding: "base64")
#expect(file.decodedContent == original)
}
@Test func returnsNilContentWhenContentIsNil() {
let file = makeFileContent(content: nil, encoding: "base64")
#expect(file.decodedContent == nil)
}
@Test func returnsRawContentWhenEncodingIsNil() {
let file = makeFileContent(content: "raw text", encoding: nil)
#expect(file.decodedContent == "raw text")
}
@Test func returnsRawContentWhenEncodingIsNotBase64() {
let file = makeFileContent(content: "raw text", encoding: "utf-8")
#expect(file.decodedContent == "raw text")
}
@Test func returnsNilForInvalidBase64() {
let file = makeFileContent(content: "!!!not-base64!!!", encoding: "base64")
#expect(file.decodedContent == nil)
}
// MARK: - JSON decoding
@Test func decodesFileContentFromJSON() throws {
let json = """
{
"name": "readme.md",
"path": "readme.md",
"sha": "xyz789",
"size": 50,
"url": "https://example.com/api",
"html_url": "https://example.com/html",
"git_url": "https://example.com/git",
"type": "file",
"content": "SGVsbG8=",
"encoding": "base64"
}
"""
let file = try JSONDecoder().decode(FileContent.self, from: Data(json.utf8))
#expect(file.name == "readme.md")
#expect(file.decodedContent == "Hello")
}
}