gitbayTests/RepoStateTests.swift
56 lines · 1937 bytes
1import Foundation
2import Testing
3@testable import gitbay
4
5struct RepoStateDecodingTests {
6
7 private func decode(_ json: String) throws -> RepoDetail {
8 try JSONDecoder().decode(RepoDetail.self, from: Data(json.utf8))
9 }
10
11 private let base = """
12 "path":"krz/gitbay","visibility":"public","default_branch":"main"
13 """
14
15 /// All three are omitempty. A repository you have expressed no
16 /// opinion about, which is not a fork, carries none of them.
17 @Test func aRepositoryWithNoStateDecodes() throws {
18 let repo = try decode("{\(base)}")
19 #expect(repo.watch == nil)
20 #expect(repo.bookmarked == false)
21 #expect(repo.forkOf == nil)
22 }
23
24 @Test func theCallersOwnStateDecodes() throws {
25 let repo = try decode("""
26 {\(base),"watch":"watching","bookmarked":true,"fork_of":"upstream/thing"}
27 """)
28 #expect(repo.watch == "watching")
29 #expect(repo.bookmarked)
30 #expect(repo.forkOf == "upstream/thing")
31 }
32
33 @Test func mutedDecodes() throws {
34 #expect(try decode("{\(base),\"watch\":\"muted\"}").watch == "muted")
35 }
36}
37
38struct WatchStateTests {
39
40 @Test func theThreeStatesMapFromTheWire() {
41 #expect(WatchState("watching") == .watching)
42 #expect(WatchState("muted") == .muted)
43 #expect(WatchState(nil) == .default)
44 // An unknown value the server might add later must not crash or
45 // masquerade as watching.
46 #expect(WatchState("something-new") == .default)
47 }
48
49 /// `repo unwatch` returns to the default from either state, so it is
50 /// offered from both and never from the default itself.
51 @Test func onlyTheReachableTransitionsAreOffered() {
52 #expect(WatchState.watching.available == [.unwatch, .mute])
53 #expect(WatchState.muted.available == [.watch, .unwatch])
54 #expect(WatchState.default.available == [.watch, .mute])
55 }
56}