示例工程 58-capstone-data-and-navigation

58-capstone-data-and-navigation/Tests/Ch58KitTests/BookmarkTests.swift

swift · 4.2 KB

import SwiftData
import SwiftUI
import Testing
 
@testable import Ch58Kit
 
@Suite("书签数据")
struct BookmarkTests {
    private func makeContext() throws -> ModelContext {
        ModelContext(try Library.makeContainer())
    }
 
    @Test func addsAndFetchesSorted() throws {
        let context = try makeContext()
        Library.add(title: "B", url: "b", in: context)
        Library.add(title: "A", url: "a", in: context)
        #expect(try Library.all(in: context).map(\.title) == ["A", "B"])
    }
 
    @Test func searchesTitleURLAndTags() throws {
        let context = try makeContext()
        Library.add(title: "Swift", url: "https://swift.org", tags: ["语言"], in: context)
        Library.add(title: "Tool", url: "https://opencode.ai", tags: ["编辑器"], in: context)
        #expect(try Library.search("swift", in: context).count == 1)
        #expect(try Library.search("opencode", in: context).count == 1)
        #expect(try Library.search("语言", in: context).count == 1)
    }
 
    @Test func blankQueryReturnsAll() throws {
        let context = try makeContext()
        Library.add(title: "A", url: "a", in: context)
        Library.add(title: "B", url: "b", in: context)
        #expect(try Library.search("  ", in: context).count == 2)
    }
 
    @Test func deletesBookmark() throws {
        let context = try makeContext()
        let bookmark = Library.add(title: "Temp", url: "t", in: context)
        Library.delete(bookmark, in: context)
        #expect(try Library.all(in: context).isEmpty)
    }
 
    @Test func rowsExposeBadge() throws {
        let context = try makeContext()
        Library.add(title: "A", url: "a", tags: ["工具"], in: context)
        let rows = BookmarkListModel.rows(from: try Library.all(in: context))
        #expect(rows.first?.badge == "工具")
    }
}
 
@Suite("导航")
struct NavigationTests {
    @Test func pushAndPop() {
        var model = NavigationModel()
        model.open(.settings)
        #expect(model.path.count == 1)
        model.pop()
        #expect(model.path.isEmpty)
    }
 
    @Test func roundTrip() throws {
        var model = NavigationModel()
        let id = UUID()
        model.open(.bookmark(id))
        model.open(.settings)
        let restored = try NavigationModel.restored(try model.encoded())
        #expect(restored == model)
    }
 
    @Test func popOnEmptyIsSafe() {
        var model = NavigationModel()
        model.pop()
        #expect(model.path.isEmpty)
    }
}
 
@Suite("视图")
struct BoardTests {
    @Test @MainActor func boardRendersAtFixedSize() throws {
        let image = try #require(ImageRenderer(content: LibraryBoard()).cgImage)
        #expect(image.width == 340)
        #expect(image.height > 120)
    }
}
 
@Suite("截图")
struct ScreenshotTests {
    @MainActor
    private func writeShot(
        _ view: some View,
        name: String,
        expectedWidth: Int,
        minHeight: Int
    ) throws {
        let renderer = ImageRenderer(content: view)
        renderer.scale = 2
        let image = try #require(renderer.cgImage)
        #expect(image.width == expectedWidth)
        #expect(image.height >= minHeight)
 
        let url = URL(fileURLWithPath: #filePath)
            .deletingLastPathComponent()
            .deletingLastPathComponent()
            .deletingLastPathComponent()
            .appendingPathComponent("shots/\(name)")
        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)
    }
 
    @Test @MainActor func boardShot() throws {
        try writeShot(
            LibraryBoard(), name: "bookmark-board.png", expectedWidth: 680, minHeight: 300)
    }
 
    @Test @MainActor func emptyShot() throws {
        try writeShot(
            BookmarksEmptyBoard(), name: "bookmark-empty.png", expectedWidth: 680, minHeight: 200)
    }
 
    @Test @MainActor func searchShot() throws {
        try writeShot(
            BookmarksSearchBoard(), name: "bookmark-search.png", expectedWidth: 680, minHeight: 280)
    }
}