DayByDayTests/ProfileSessionTests.swift
91 lines · 3073 bytes
11 symbols in this file
1import Foundation
2import Testing
3@testable import DayByDay
4
5struct ProfileSessionTests {
6 let container = ProfileStore.makeContainer(inMemory: true)
7 var store: ProfileStore { ProfileStore(context: container.mainContext) }
8 let t0 = Date(timeIntervalSince1970: 1_000_000)
9
10 @discardableResult
11 func add(_ name: String) -> Profile {
12 try! store.create(name: name, avatar: .cat, color: .red, birthday: nil)
13 }
14
15 @Test func noProfilesShowsSetup() {
16 #expect(ProfileSession(store: store).screen == .setup)
17 }
18
19 @Test func singleProfileAutoSelects() {
20 let maya = add("Maya")
21 let session = ProfileSession(store: store)
22 #expect(session.screen == .home)
23 #expect(session.current?.id == maya.id)
24 }
25
26 @Test func multipleProfilesShowPicker() {
27 add("Maya"); add("Sam")
28 let session = ProfileSession(store: store)
29 #expect(session.screen == .picker)
30 session.select(store.all()[1])
31 #expect(session.screen == .home)
32 #expect(session.current?.name == "Sam")
33 }
34
35 @Test func shortBackgroundKeepsCurrent() {
36 add("Maya"); add("Sam")
37 let session = ProfileSession(store: store)
38 session.select(store.all()[0])
39 session.didEnterBackground(at: t0)
40 session.willEnterForeground(at: t0.addingTimeInterval(599))
41 #expect(session.screen == .home)
42 }
43
44 @Test func longBackgroundShowsPickerWithSiblings() {
45 add("Maya"); add("Sam")
46 let session = ProfileSession(store: store)
47 session.select(store.all()[0])
48 session.didEnterBackground(at: t0)
49 session.willEnterForeground(at: t0.addingTimeInterval(ProfileSession.pickerAfterBackground))
50 #expect(session.screen == .picker)
51 }
52
53 @Test func longBackgroundKeepsSoleProfile() {
54 add("Maya")
55 let session = ProfileSession(store: store)
56 session.didEnterBackground(at: t0)
57 session.willEnterForeground(at: t0.addingTimeInterval(3600))
58 #expect(session.screen == .home)
59 }
60
61 @Test func deletingCurrentClearsIt() {
62 add("Maya"); add("Sam"); add("Lee")
63 let session = ProfileSession(store: store)
64 session.select(store.all()[0])
65 store.delete(store.all()[0])
66 session.profilesChanged()
67 #expect(session.current == nil)
68 #expect(session.screen == .picker)
69 session.select(store.all()[0])
70 store.delete(store.all()[0])
71 session.profilesChanged()
72 #expect(session.screen == .home)
73 #expect(session.current?.name == "Lee")
74 }
75
76 @Test func deletingLastProfileShowsSetup() {
77 add("Maya")
78 let session = ProfileSession(store: store)
79 store.delete(store.all()[0])
80 session.profilesChanged()
81 #expect(session.current == nil)
82 #expect(session.screen == .setup)
83 }
84
85 @Test func addingFirstProfileSelectsIt() {
86 let session = ProfileSession(store: store)
87 add("Maya")
88 session.profilesChanged()
89 #expect(session.screen == .home)
90 }
91}