Tests/KeycaskCoreTests/EntryIDTests.swift
55 lines · 1759 bytes
1import Foundation
2import Testing
3
4@testable import KeycaskCore
5
6@Suite struct EntryIDTests {
7 @Test func randomIDsHaveLengthEightFromTheAlphabet() {
8 let allowed = Set(EntryID.alphabet)
9 for _ in 0..<200 {
10 let id = EntryID.random()
11 #expect(id.rawValue.count == 8)
12 #expect(id.rawValue.allSatisfy { allowed.contains($0) })
13 }
14 }
15
16 @Test func alphabetExcludesAmbiguousCharacters() {
17 let alphabet = Set(EntryID.alphabet)
18 #expect(alphabet.count == 32)
19 for bad in ["l", "o", "0", "1"] {
20 #expect(!alphabet.contains(Character(bad)))
21 }
22 }
23
24 @Test func parsingValidatesLengthAndAlphabet() {
25 #expect(EntryID("abcd2345") != nil)
26 #expect(EntryID("abcd234") == nil)
27 #expect(EntryID("abcd23456") == nil)
28 #expect(EntryID("abcd234l") == nil)
29 #expect(EntryID("ABCD2345") == nil)
30 }
31
32 @Test func codableIsABareString() throws {
33 let id = EntryID("abcd2345")!
34 let data = try JSONEncoder().encode([id])
35 #expect(String(decoding: data, as: UTF8.self) == "[\"abcd2345\"]")
36 let back = try JSONDecoder().decode([EntryID].self, from: data)
37 #expect(back == [id])
38 #expect(throws: DecodingError.self) {
39 try JSONDecoder().decode([EntryID].self, from: Data("[\"bad\"]".utf8))
40 }
41 }
42
43 @Test func seededGeneratorIsDeterministic() {
44 struct Counter: RandomNumberGenerator {
45 var n: UInt64 = 0
46 mutating func next() -> UInt64 {
47 n += 1
48 return n
49 }
50 }
51 var a = Counter()
52 var b = Counter()
53 #expect(EntryID.random(using: &a) == EntryID.random(using: &b))
54 }
55}