Tests/Unit/QuestionListTests.swift (2454B)
1import Foundation
2import Testing
3
4@testable import Reportage
5
6@MainActor
7@Suite("QuestionList")
8struct QuestionListTests {
9
10 private static func question(_ id: String, _ askOn: SurveyKinds, archived: Bool = false) -> Question {
11 Question(
12 id: id, prompt: id, kind: .yesNo, askOn: askOn, order: 0,
13 archivedAt: archived ? Date(timeIntervalSince1970: 0) : nil,
14 modifiedAt: Date(timeIntervalSince1970: 0))
15 }
16
17 @Test("Moving within a survey leaves other questions where they were")
18 func reordered() {
19 let questions = [
20 Self.question("sleep", .wake),
21 Self.question("doing", .random),
22 Self.question("note", .bedtime),
23 Self.question("work", .random),
24 Self.question("mood", [.random, .bedtime]),
25 ]
26
27 let moved = QuestionList.reordered(questions, in: .prompted, from: [2], to: 0)
28 #expect(moved.map(\.id) == ["sleep", "mood", "note", "doing", "work"])
29 #expect(moved.map(\.order) == [0, 1, 2, 3, 4])
30 }
31
32 @Test("Archived questions aren't asked, and asking one brings it back in that survey alone")
33 func asking() {
34 var question = Self.question("Q", [.random, .wake], archived: true)
35 #expect(!question.isAsked(in: .prompted))
36 question.setAsked(true, in: .bedtime)
37 #expect(question.archivedAt == nil)
38 #expect(question.askOn == .bedtime)
39 question.setAsked(false, in: .bedtime)
40 #expect(question.askOn.isEmpty)
41 }
42
43 @Test("Archived questions are listed apart from those not asked")
44 func grouping() async throws {
45 let store = ReportStore(persistence: makeTestPersistence())
46 try await store.save([
47 Self.question("asked", .random),
48 Self.question("off", []),
49 Self.question("archived", .random, archived: true),
50 ])
51 let list = QuestionList()
52 await list.load(from: store)
53 #expect(list.bySurvey[.prompted]?.map(\.id) == ["asked"])
54 #expect(list.notAsked.map(\.id) == ["off"])
55 #expect(list.archived.map(\.id) == ["archived"])
56
57 var archived = try #require(list.question(id: "archived"))
58 archived.setAsked(true, in: .wake)
59 list.update(archived)
60 #expect(list.archived.isEmpty)
61 #expect(list.bySurvey[.wake]?.map(\.id) == ["archived"])
62 }
63}