|
| 1 | +import XCTest |
| 2 | +import Vizzly |
| 3 | + |
| 4 | +#if os(macOS) |
| 5 | +import AppKit |
| 6 | +#endif |
| 7 | + |
| 8 | +final class VizzlyE2ETests: XCTestCase { |
| 9 | + override func setUpWithError() throws { |
| 10 | + try super.setUpWithError() |
| 11 | + |
| 12 | + guard ProcessInfo.processInfo.environment["VIZZLY_E2E"] == "1" else { |
| 13 | + throw XCTSkip("Set VIZZLY_E2E=1 to run Swift SDK E2E tests") |
| 14 | + } |
| 15 | + } |
| 16 | + |
| 17 | + func testUploadsScreenshotThroughRunningTddServer() throws { |
| 18 | + let client = VizzlyClient() |
| 19 | + XCTAssertTrue(client.isReady, "Expected VizzlyClient to discover the TDD server") |
| 20 | + |
| 21 | + let firstResult = try XCTUnwrap( |
| 22 | + client.screenshot( |
| 23 | + name: "swift-sdk-e2e-home", |
| 24 | + image: try Self.makeTestPng(), |
| 25 | + properties: [ |
| 26 | + "platform": "swift-e2e", |
| 27 | + "screen": "home" |
| 28 | + ], |
| 29 | + threshold: 1.5, |
| 30 | + minClusterSize: 3, |
| 31 | + fullPage: true |
| 32 | + ) |
| 33 | + ) |
| 34 | + |
| 35 | + XCTAssertTrue( |
| 36 | + Self.successStatuses.contains(Self.status(from: firstResult)), |
| 37 | + "Expected first upload to create or match a baseline, got: \(firstResult)" |
| 38 | + ) |
| 39 | + XCTAssertEqual(firstResult["name"] as? String, "swift-sdk-e2e-home") |
| 40 | + XCTAssertNotNil(firstResult["current"]) |
| 41 | + XCTAssertNotNil(firstResult["baseline"]) |
| 42 | + } |
| 43 | + |
| 44 | + func testSecondUploadMatchesExistingBaseline() throws { |
| 45 | + let client = VizzlyClient() |
| 46 | + |
| 47 | + let firstResult = try XCTUnwrap( |
| 48 | + client.screenshot( |
| 49 | + name: "swift-sdk-e2e-repeatable", |
| 50 | + image: try Self.makeTestPng(), |
| 51 | + properties: ["case": "repeatable"] |
| 52 | + ) |
| 53 | + ) |
| 54 | + XCTAssertTrue( |
| 55 | + Self.successStatuses.contains(Self.status(from: firstResult)), |
| 56 | + "Expected first upload to create or match a baseline, got: \(firstResult)" |
| 57 | + ) |
| 58 | + |
| 59 | + let secondResult = try XCTUnwrap( |
| 60 | + client.screenshot( |
| 61 | + name: "swift-sdk-e2e-repeatable", |
| 62 | + image: try Self.makeTestPng(), |
| 63 | + properties: ["case": "repeatable"] |
| 64 | + ) |
| 65 | + ) |
| 66 | + XCTAssertEqual(Self.status(from: secondResult), "match") |
| 67 | + } |
| 68 | + |
| 69 | + private static let successStatuses: Set<String> = ["new", "match"] |
| 70 | + |
| 71 | + private static func status(from result: [String: Any]) -> String { |
| 72 | + return result["status"] as? String ?? "" |
| 73 | + } |
| 74 | + |
| 75 | + private static func makeTestPng() throws -> Data { |
| 76 | + #if os(macOS) |
| 77 | + let image = NSImage(size: NSSize(width: 2, height: 2)) |
| 78 | + image.lockFocus() |
| 79 | + NSColor.systemBlue.setFill() |
| 80 | + NSRect(x: 0, y: 0, width: 2, height: 2).fill() |
| 81 | + image.unlockFocus() |
| 82 | + |
| 83 | + let tiffData = try XCTUnwrap(image.tiffRepresentation) |
| 84 | + let bitmap = try XCTUnwrap(NSBitmapImageRep(data: tiffData)) |
| 85 | + return try XCTUnwrap(bitmap.representation(using: .png, properties: [:])) |
| 86 | + #else |
| 87 | + throw XCTSkip("Swift SDK E2E PNG fixture generation currently runs on macOS") |
| 88 | + #endif |
| 89 | + } |
| 90 | +} |
0 commit comments