import SwiftUI import Testing @testable import Ch39Kit @Suite("快捷键映射") struct KeymapTests { @Test func resolvesCommandN() { #expect(Keymap.resolve(KeyInput(key: "n", command: true)) == .newDocument) } @Test func resolvesSearch() { #expect(Keymap.resolve(KeyInput(key: "f", command: true)) == .search) } @Test func requiresExactModifiers() { #expect(Keymap.resolve(KeyInput(key: "n")) == nil) #expect(Keymap.resolve(KeyInput(key: "n", command: true, shift: true)) == nil) } @Test func unknownKeyReturnsNil() { #expect(Keymap.resolve(KeyInput(key: "z", command: true)) == nil) } @Test func everyCommandHasAnInput() { for command in KeyCommand.allCases { #expect(Keymap.input(for: command) != nil) } } @Test func inputsAreUnique() { let inputs = Keymap.entries.map(\.input) #expect(Set(inputs).count == inputs.count) } @Test func displayOrdersModifiers() { #expect(KeyInput(key: "/", command: true, shift: true).display == "⇧⌘/") #expect(KeyInput(key: "delete").display == "DELETE") } } @Suite("视图") struct KeyboardViewTests { @Test @MainActor func shortcutCardRendersAtFixedSize() throws { let image = try #require(ImageRenderer(content: ShortcutCard()).cgImage) #expect(image.width == 300) #expect(image.height > 120) } } @Suite("截图") struct ScreenshotTests { @Test @MainActor func shotMatchesExpectedWidth() throws { let renderer = ImageRenderer(content: ShortcutCard()) renderer.scale = 2 let image = try #require(renderer.cgImage) #expect(image.width == 600) let url = URL(fileURLWithPath: #filePath) .deletingLastPathComponent() .deletingLastPathComponent() .deletingLastPathComponent() .appendingPathComponent("shots/shortcut-card.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) } }