示例工程 41-system-integration
41-system-integration/Tests/Ch41KitTests/IntegrationTests.swift
import Foundation
import SwiftUI
import Testing
import UniformTypeIdentifiers
@testable import Ch41Kit
@Suite("文档读写")
struct DocumentTests {
@Test func readsPlainText() throws {
let document = try TextDocument.parse(Data("你好".utf8))
#expect(document.text == "你好")
}
@Test func writesUTF8() {
let document = TextDocument(text: "swift")
#expect(String(data: document.encoded(), encoding: .utf8) == "swift")
}
@Test func rejectsInvalidUTF8() {
#expect(throws: (any Error).self) {
_ = try TextDocument.parse(Data([0xFF, 0xFE]))
}
}
}
@Suite("书签")
struct BookmarkTests {
@Test func roundTripsToTheSameFile() throws {
let url = FileManager.default.temporaryDirectory
.appendingPathComponent("ch41-test-\(UUID().uuidString).txt")
try "x".write(to: url, atomically: true, encoding: .utf8)
defer { try? FileManager.default.removeItem(at: url) }
let data = try BookmarkStore.make(for: url)
let resolved = try BookmarkStore.resolve(data)
#expect(resolved.lastPathComponent == url.lastPathComponent)
}
}
@Suite("深链")
struct DeepLinkTests {
private let uuid = UUID(uuidString: "11111111-2222-3333-4444-555555555555")!
@Test func parsesBookRoute() {
let url = URL(string: "myapp://book/\(uuid.uuidString)")!
#expect(DeepLink.parse(url) == .book(uuid))
}
@Test func parsesSettings() {
#expect(DeepLink.parse(URL(string: "myapp://settings")!) == .settings)
}
@Test func rejectsWrongScheme() {
#expect(DeepLink.parse(URL(string: "https://example.com")!) == nil)
}
@Test func rejectsMalformedUUID() {
#expect(DeepLink.parse(URL(string: "myapp://book/not-a-uuid")!) == nil)
}
}
@Suite("载荷与通知")
struct PayloadTests {
@Test func payloadCodableRoundTrip() throws {
let payload = BookPayload(title: "Swift", author: "Apple")
let data = try JSONEncoder().encode(payload)
let decoded = try JSONDecoder().decode(BookPayload.self, from: data)
#expect(decoded == payload)
}
@Test func notificationValidation() {
#expect(NotificationRequest(title: "a", body: "b").isValid)
#expect(!NotificationRequest(title: " ", body: "b").isValid)
#expect(!NotificationRequest(title: "a", body: "b", delay: -1).isValid)
#expect(!NotificationRequest(title: "a", body: "b", delay: 120).isValid)
}
}
@Suite("视图")
struct IntegrationViewTests {
@Test @MainActor func boardRendersAtFixedSize() throws {
let image = try #require(ImageRenderer(content: IntegrationBoard()).cgImage)
#expect(image.width == 360)
#expect(image.height > 150)
}
}
@Suite("截图")
struct ScreenshotTests {
@Test @MainActor func shotMatchesExpectedWidth() throws {
let renderer = ImageRenderer(content: IntegrationBoard())
renderer.scale = 2
let image = try #require(renderer.cgImage)
#expect(image.width == 720)
let url = URL(fileURLWithPath: #filePath)
.deletingLastPathComponent()
.deletingLastPathComponent()
.deletingLastPathComponent()
.appendingPathComponent("shots/integration-board.png")
guard !FileManager.default.fileExists(atPath: url.path) else { return }
let data = try #require(
NSBitmapImageRep(cgImage: image).representation(using: .png, properties: [:])
)
try FileManager.default.createDirectory(
at: url.deletingLastPathComponent(),
withIntermediateDirectories: true
)
try data.write(to: url)
}
}