|
| 1 | +import Defaults |
| 2 | +@testable import Pika |
| 3 | +import XCTest |
| 4 | + |
| 5 | +/// Tests the hex-to-NSColor conversion and round-trip behaviour of ColorPair. |
| 6 | +/// ColorPair is the on-disk representation of a swatch in the auto-history |
| 7 | +/// and saved palettes, so drift here would corrupt user-visible history. |
| 8 | +final class ColorPairTests: XCTestCase { |
| 9 | + override func setUp() { |
| 10 | + super.setUp() |
| 11 | + Defaults[.colorSpace] = .sRGB |
| 12 | + } |
| 13 | + |
| 14 | + // MARK: - Construction and identity |
| 15 | + |
| 16 | + func test_equality_ignoresIdAndDate() { |
| 17 | + let a = ColorPair(id: UUID(), foregroundHex: "#ff0000", backgroundHex: "#00ff00", date: Date()) |
| 18 | + let b = ColorPair(id: UUID(), foregroundHex: "#ff0000", backgroundHex: "#00ff00", |
| 19 | + date: Date(timeIntervalSince1970: 0)) |
| 20 | + XCTAssertEqual(a, b) |
| 21 | + } |
| 22 | + |
| 23 | + func test_equality_distinguishesByHex() { |
| 24 | + let a = ColorPair(id: UUID(), foregroundHex: "#ff0000", backgroundHex: "#00ff00", date: Date()) |
| 25 | + let b = ColorPair(id: UUID(), foregroundHex: "#ff0001", backgroundHex: "#00ff00", date: Date()) |
| 26 | + XCTAssertNotEqual(a, b) |
| 27 | + } |
| 28 | + |
| 29 | + // MARK: - foregroundColor / backgroundColor |
| 30 | + |
| 31 | + func test_foregroundColor_parsesHashedHex() { |
| 32 | + let pair = ColorPair(id: UUID(), foregroundHex: "#ff0000", backgroundHex: "#000000", date: Date()) |
| 33 | + let color = pair.foregroundColor.usingColorSpace(.sRGB)! |
| 34 | + XCTAssertEqual(color.redComponent, 1.0, accuracy: 0.001) |
| 35 | + XCTAssertEqual(color.greenComponent, 0.0, accuracy: 0.001) |
| 36 | + XCTAssertEqual(color.blueComponent, 0.0, accuracy: 0.001) |
| 37 | + } |
| 38 | + |
| 39 | + func test_backgroundColor_parsesUnhashedHex() { |
| 40 | + let pair = ColorPair(id: UUID(), foregroundHex: "#000000", backgroundHex: "0000ff", date: Date()) |
| 41 | + let color = pair.backgroundColor.usingColorSpace(.sRGB)! |
| 42 | + XCTAssertEqual(color.redComponent, 0.0, accuracy: 0.001) |
| 43 | + XCTAssertEqual(color.greenComponent, 0.0, accuracy: 0.001) |
| 44 | + XCTAssertEqual(color.blueComponent, 1.0, accuracy: 0.001) |
| 45 | + } |
| 46 | + |
| 47 | + func test_hexRoundTrip_noChannelDrift() { |
| 48 | + // Stored hex should reconstruct to a color whose re-serialized hex matches. |
| 49 | + let original = "#3a7bd5" |
| 50 | + let pair = ColorPair(id: UUID(), foregroundHex: original, backgroundHex: "#000000", date: Date()) |
| 51 | + XCTAssertEqual(pair.foregroundColor.toHexString(style: .css), original) |
| 52 | + } |
| 53 | + |
| 54 | + // MARK: - Malformed input |
| 55 | + |
| 56 | + func test_invalidLength_fallsBackToBlack() { |
| 57 | + let pair = ColorPair(id: UUID(), foregroundHex: "#abc", backgroundHex: "#000000", date: Date()) |
| 58 | + let color = pair.foregroundColor.usingColorSpace(.sRGB)! |
| 59 | + XCTAssertEqual(color.redComponent, 0.0, accuracy: 0.001) |
| 60 | + XCTAssertEqual(color.greenComponent, 0.0, accuracy: 0.001) |
| 61 | + XCTAssertEqual(color.blueComponent, 0.0, accuracy: 0.001) |
| 62 | + } |
| 63 | + |
| 64 | + func test_emptyHex_fallsBackToBlack() { |
| 65 | + let pair = ColorPair(id: UUID(), foregroundHex: "", backgroundHex: "#000000", date: Date()) |
| 66 | + let color = pair.foregroundColor.usingColorSpace(.sRGB)! |
| 67 | + XCTAssertEqual(color.redComponent, 0.0, accuracy: 0.001) |
| 68 | + XCTAssertEqual(color.greenComponent, 0.0, accuracy: 0.001) |
| 69 | + XCTAssertEqual(color.blueComponent, 0.0, accuracy: 0.001) |
| 70 | + } |
| 71 | + |
| 72 | + func test_nonHexCharacters_fallBackToBlack() { |
| 73 | + let pair = ColorPair(id: UUID(), foregroundHex: "#zzzzzz", backgroundHex: "#000000", date: Date()) |
| 74 | + let color = pair.foregroundColor.usingColorSpace(.sRGB)! |
| 75 | + XCTAssertEqual(color.redComponent, 0.0, accuracy: 0.001) |
| 76 | + XCTAssertEqual(color.greenComponent, 0.0, accuracy: 0.001) |
| 77 | + XCTAssertEqual(color.blueComponent, 0.0, accuracy: 0.001) |
| 78 | + } |
| 79 | + |
| 80 | + // MARK: - Codable |
| 81 | + |
| 82 | + func test_isCodable_roundTripsThroughJSON() throws { |
| 83 | + let original = ColorPair( |
| 84 | + id: UUID(uuidString: "11111111-2222-3333-4444-555555555555")!, |
| 85 | + foregroundHex: "#123456", |
| 86 | + backgroundHex: "#abcdef", |
| 87 | + date: Date(timeIntervalSince1970: 1_700_000_000) |
| 88 | + ) |
| 89 | + let data = try JSONEncoder().encode(original) |
| 90 | + let decoded = try JSONDecoder().decode(ColorPair.self, from: data) |
| 91 | + XCTAssertEqual(decoded.id, original.id) |
| 92 | + XCTAssertEqual(decoded.foregroundHex, original.foregroundHex) |
| 93 | + XCTAssertEqual(decoded.backgroundHex, original.backgroundHex) |
| 94 | + XCTAssertEqual(decoded.date.timeIntervalSince1970, original.date.timeIntervalSince1970, accuracy: 0.001) |
| 95 | + } |
| 96 | + |
| 97 | + // MARK: - Palette |
| 98 | + |
| 99 | + func test_palette_isAutoHistory_whenNameIsNil() { |
| 100 | + let palette = Palette(id: UUID(), name: nil, pairs: [], createdAt: Date()) |
| 101 | + XCTAssertTrue(palette.isAutoHistory) |
| 102 | + } |
| 103 | + |
| 104 | + func test_palette_isAutoHistory_falseWhenNamed() { |
| 105 | + let palette = Palette(id: UUID(), name: "Brand", pairs: [], createdAt: Date()) |
| 106 | + XCTAssertFalse(palette.isAutoHistory) |
| 107 | + } |
| 108 | + |
| 109 | + func test_maxHistory_is20() { |
| 110 | + // Changing this bound without auditing the history/undo stack truncation |
| 111 | + // logic in Eyedroppers would silently drop user data. |
| 112 | + XCTAssertEqual(ColorPair.maxHistory, 20) |
| 113 | + } |
| 114 | +} |
0 commit comments