Tests/OrgAppTests/FlashcardReviewTests.swift
79 lines · 4161 bytes
4 symbols in this file
1import Foundation
2import OrgCore
3import OrgDocument
4@testable import OrgIndex
5import Testing
6@testable import OrgApp
7
8@MainActor
9struct FlashcardReviewTests {
10 static func card(_ title: String, id: String, type: String = "normal", rows: [(String, String)], tags: String = ":fc:") -> String {
11 """
12 * \(title) \(tags)
13 :PROPERTIES:
14 :FC_CREATED: 2026-11-01T00:00:00Z
15 :FC_TYPE: \(type)
16 :FC_ALGO: sm2
17 :ID: \(id)
18 :END:
19 :REVIEW_DATA:
20 | position | ease | box | interval | due |
21 |----------+------+-----+----------+----------------------|
22 \(rows.map { "| \($0.0) | 2.50 | 0 | 0.00 | \($0.1) |" }.joined(separator: "\n"))
23 :END:
24 Answer of \(title)
25
26 """
27 }
28
29 static let now = Date(timeIntervalSince1970: 1_795_000_000)
30 static let past = "2026-11-17T00:00:00Z", future = "2026-12-30T00:00:00Z"
31
32 @Test func reviewRatesThroughTheSessionAndLogsTheHistory() async throws {
33 let notes = try Folder()
34 let deck = try notes.write("deck.org", Self.card("One", id: "one", rows: [("front", Self.past)])
35 + Self.card("Two", id: "two", type: "double", rows: [("front", Self.past), ("back", Self.past)])
36 + Self.card("Later", id: "later", rows: [("front", Self.future)]))
37 _ = try notes.write("other.org", "* Not a card\n")
38 _ = try notes.write("held.org", Self.card("Held", id: "held", rows: [("front", Self.past)], tags: ":suspended:fc:"))
39 let workspace = try WorkspaceModel(directory: nil)
40 try await workspace.addRoot(notes.url)
41 let session = DocumentSession(recovery: FileRecoveryStore(directory: notes.url.appendingPathComponent(".r")))
42 #expect(try workspace.index.files(withLocalTag: "fc") == [deck.path, notes.url.appendingPathComponent("held.org").path].sorted())
43
44 let review = FlashcardReview(workspace: workspace, session: session)
45 review.history = notes.url.appendingPathComponent("logs/reviews.tsv")
46 await review.start(now: Self.now)
47 // One position of the double card; not the card due later or the suspended one.
48 #expect(review.queue.map(\.card.id).sorted() == ["one", "two"])
49 let first = try #require(review.current)
50 #expect(review.face?.front.title == first.card.title)
51 review.flip()
52 review.rate(.again, now: Self.now)
53 #expect(review.queue.map(\.card.id) == [review.queue[0].card.id, first.card.id])
54 let second = try #require(review.current)
55 review.rate(.easy, now: Self.now)
56 #expect(review.queue.map(\.card.id) == [first.card.id] && review.reviewed == 2)
57
58 let text = try String(contentsOf: deck, encoding: .utf8)
59 let cards = Flashcards.cards(in: text)
60 let card = try #require(cards.first { $0.id == second.card.id })
61 let rated = try #require(card.positions.first { $0.name == second.position })
62 #expect(rated.values["box"] == "2" && rated.values["due"] == "2026-11-19T11:06:40Z")
63 let history = try String(contentsOf: review.history, encoding: .utf8).split(separator: "\n")
64 #expect(history.count == 2)
65 #expect(history[0].hasPrefix("2026-11-18T11:06:40Z\t\(deck.path)\t\(first.card.id)\t\(first.position)\t2.50\t0\t0.00\tagain\t"))
66 #expect(history[1].contains("\t\(second.card.id)\t\(second.position)\t2.50\t0\t0.00\teasy\t") && history[1].hasSuffix("\tsm2-v1"))
67
68 review.suspend()
69 #expect(review.queue.isEmpty && review.face == nil)
70 #expect(Flashcards.cards(in: try String(contentsOf: deck, encoding: .utf8)).first { $0.id == first.card.id }?.suspended == true)
71 }
72
73 @Test func snapshotCountsAsTimePasses() {
74 let snapshot = FlashcardSnapshot(due: [Self.now.addingTimeInterval(60), Self.now.addingTimeInterval(-60), Self.now.addingTimeInterval(60)])
75 #expect(snapshot.count(at: Self.now) == 1)
76 #expect(snapshot.count(at: Self.now.addingTimeInterval(61)) == 3)
77 #expect(snapshot.changes(after: Self.now) == [Self.now.addingTimeInterval(61)])
78 }
79}