gitbayTests/ReplyByMailTests.swift
68 lines · 2558 bytes
7 symbols in this file
1import Foundation
2import Testing
3@testable import gitbay
4
5private func makeClient() throws -> (GitbayClient, StubProtocol.Box) {
6 let box = StubProtocol.box()
7 let client = GitbayClient(
8 instance: try GitbayInstance(url: "https://gitbay.org"),
9 token: "test-token",
10 session: box.session()
11 )
12 return (client, box)
13}
14
15private func show(_ fields: String) -> String {
16 #"{"protocol_version":1,"data":{"mail":true,"watch":false,"# + fields + #"},"exit_code":0}"#
17}
18
19@MainActor
20struct ReplyByMailTests {
21
22 @Test func theSwitchShowsWhereTheInstanceReadsReplies() async throws {
23 let (client, stub) = try makeClient()
24 stub.enqueue(.init(status: 200, json: show(#""reply":false,"reply_available":true"#), match: "argv=show"))
25 let model = AccountViewModel(client: client)
26
27 await model.loadNotificationPreferences()
28
29 #expect(model.replyOn == false)
30 #expect(model.offersReply)
31 }
32
33 @Test func noSwitchWhereTheInstanceDoesNot() async throws {
34 let (client, stub) = try makeClient()
35 stub.enqueue(.init(status: 200, json: show(#""reply":false,"reply_available":false"#), match: "argv=show"))
36 let model = AccountViewModel(client: client)
37
38 await model.loadNotificationPreferences()
39
40 #expect(!model.offersReply)
41 }
42
43 /// A server without reply_available says nothing either way: the
44 /// switch shows, and the refusal explains if it cannot be turned on.
45 @Test func anOlderServerShowsTheSwitch() async throws {
46 let (client, stub) = try makeClient()
47 stub.enqueue(.init(status: 200, json: show(#""reply":false"#), match: "argv=show"))
48 let model = AccountViewModel(client: client)
49
50 await model.loadNotificationPreferences()
51
52 #expect(model.offersReply)
53 }
54
55 @Test func turningItOnSendsReplyOn() async throws {
56 let (client, stub) = try makeClient()
57 stub.enqueue(.init(status: 200, json: #"{"protocol_version":1,"data":{},"exit_code":0}"#, match: "cmd"))
58 stub.enqueue(.init(status: 200, json: show(#""reply":true,"reply_available":true"#), match: "argv=show"))
59 let model = AccountViewModel(client: client)
60
61 await model.setReply(true)
62
63 let write = try #require(stub.seen.first { $0.method == "POST" })
64 let body = try #require(try JSONSerialization.jsonObject(with: write.body) as? [String: Any])
65 #expect(body["argv"] as? [String] == ["notifications", "settings", "reply", "on"])
66 #expect(model.replyOn == true)
67 }
68}