Tests/OrgCoreTests/FlashcardTests.swift
206 lines · 10693 bytes
10 symbols in this file
1import Foundation
2import Testing
3@testable import OrgCore
4
5struct FlashcardTests {
6 static let now = Date(timeIntervalSince1970: 1_795_000_000.25)
7
8 static func run(_ command: any OrgCommand, _ text: String, caret: Int = 0, now: Date = now, answers: [String: String] = [:]) -> (text: String, failed: String?) {
9 let context = EditContext(revision: 0, text: text, tree: OrgParser.parse(text), selection: [caret..<caret], now: now, answers: answers)
10 switch command.run(in: context) {
11 case .commit(let result):
12 var new = text
13 for edit in result.edits.sorted(by: { $0.range.lowerBound > $1.range.lowerBound }) { new = edit.apply(to: new) }
14 return (new, nil)
15 case .failed(let message): return (text, message)
16 case .prompt(let prompt): return (text, "prompt \(prompt.key)")
17 default: return (text, "other")
18 }
19 }
20
21 /// What `org-fc-type-normal-init` and `org-fc-type-cloze-init` wrote in Emacs 31.1 with
22 /// Org 9.8.7 and org-fc at fce1cb2, the clock at 1795000000.25.
23 @Test func makesCardsAsOrgFcDoes() {
24 let text = "* Q :x:\n\nans\n* Cloze\nThe {{cat}{animal}} sat on {{mat}}.\n** Back\nb\n"
25 var result = Self.run(MakeFlashcard(.normal) { "the-id" }, text)
26 #expect(result.failed == nil)
27 let cloze = (result.text as NSString).range(of: "* Cloze").location
28 #expect(Self.run(MakeFlashcard(.cloze) { "the-id" }, result.text, caret: cloze).failed == "prompt cloze-type")
29 result = Self.run(MakeFlashcard(.cloze) { "the-id" }, result.text, caret: cloze, answers: ["cloze-type": "deletion"])
30 #expect(result.failed == nil)
31 #expect(result.text == """
32 * Q :fc:x:
33 :PROPERTIES:
34 :FC_CREATED: 2026-11-18T11:06:40Z
35 :FC_TYPE: normal
36 :FC_ALGO: sm2
37 :ID: the-id
38 :END:
39 :REVIEW_DATA:
40 | position | ease | box | interval | due |
41 |----------+------+-----+----------+----------------------|
42 | front | 2.50 | 0 | 0.00 | 2026-11-18T11:06:40Z |
43 :END:
44
45 ans
46 * Cloze :fc:
47 :PROPERTIES:
48 :FC_CREATED: 2026-11-18T11:06:40Z
49 :FC_TYPE: cloze
50 :FC_ALGO: sm2
51 :ID: the-id
52 :FC_CLOZE_MAX: 1
53 :FC_CLOZE_TYPE: deletion
54 :END:
55 :REVIEW_DATA:
56 | position | ease | box | interval | due |
57 |----------+------+-----+----------+----------------------|
58 | 0 | 2.50 | 0 | 0.00 | 2026-11-18T11:06:40Z |
59 | 1 | 2.50 | 0 | 0.00 | 2026-11-18T11:06:40Z |
60 :END:
61 The {{cat}{animal}@0} sat on {{mat}@1}.
62 ** Back
63 b
64
65 """)
66 #expect(Self.run(MakeFlashcard(.normal), result.text).failed == "Headline is already a flashcard")
67 }
68
69 @Test func sm2FollowsOrgFc() {
70 let sm2 = Flashcards.SM2()
71 func next(_ ease: String, _ box: String, _ interval: String, _ rating: Flashcards.Rating, random: Double = 0.5) -> [String] {
72 let row = sm2.next(["ease": ease, "box": box, "interval": interval], rating: rating, now: Self.now, random: random)
73 return [row["ease"]!, row["box"]!, row["interval"]!]
74 }
75 // Learning boxes keep the ease and take fixed intervals.
76 #expect(next("2.50", "0", "0.00", .good) == ["2.50", "1", "0.01"])
77 #expect(next("2.50", "0", "0.00", .easy) == ["2.50", "2", "1.00"])
78 #expect(next("2.50", "1", "0.01", .again) == ["2.50", "0", "0.00"])
79 #expect(next("2.50", "2", "1.00", .good) == ["2.50", "3", "6.00"])
80 // Then the ease changes, and the interval is ease times the last, fuzzed.
81 #expect(next("2.50", "3", "6.00", .good, random: 0.5) == ["2.50", "4", "15.00"])
82 #expect(next("2.50", "3", "6.00", .hard, random: 0) == ["2.35", "4", "12.69"])
83 #expect(next("2.50", "3", "6.00", .easy, random: 1) == ["2.65", "4", "17.49"])
84 #expect(next("1.40", "5", "20.00", .again) == ["1.30", "0", "0.00"])
85 #expect(next("4.95", "5", "20.00", .easy) == ["5.00", "6", "100.00"])
86 #expect(sm2.next(["ease": "2.50", "box": "1", "interval": "0.01"], rating: .good, now: Self.now, random: 0)["due"] == "2026-11-19T11:06:40Z")
87 }
88
89 static let deck = """
90 * Normal :fc:
91 :PROPERTIES:
92 :FC_CREATED: 2026-11-01T00:00:00Z
93 :FC_TYPE: normal
94 :FC_ALGO: sm2
95 :ID: n
96 :END:
97 :REVIEW_DATA:
98 | position | ease | box | interval | due |
99 |----------+------+-----+----------+----------------------|
100 | front | 2.50 | 0 | 0.00 | 2026-11-18T00:00:00Z |
101 :END:
102 Answer text
103 * Double :fc:
104 :PROPERTIES:
105 :FC_TYPE: double
106 :ID: d
107 :END:
108 :REVIEW_DATA:
109 | position | ease | box | interval | due |
110 |----------+------+-----+----------+----------------------|
111 | front | 2.50 | 0 | 0.00 | 2026-11-19T00:00:00Z |
112 | back | 2.50 | 0 | 0.00 | 2026-11-18T00:00:00Z |
113 :END:
114 Hund
115 * Cloze {{one}@0} :fc:
116 :PROPERTIES:
117 :FC_TYPE: cloze
118 :FC_CLOZE_TYPE: enumeration
119 :ID: c
120 :END:
121 :REVIEW_DATA:
122 | position | ease | box | interval | due |
123 |----------+------+-----+----------+----------------------|
124 | 0 | 2.50 | 0 | 0.00 | 2026-11-18T00:00:00Z |
125 | 1 | 2.50 | 0 | 0.00 | 2026-11-18T00:00:00Z |
126 | 2 | 2.50 | 0 | 0.00 | 2026-12-30T00:00:00Z |
127 :END:
128 {{two}{hint}@1} and {{three}@2}, not {{four}}.
129 ** More
130 extra
131 * Suspended :suspended:fc:
132 :PROPERTIES:
133 :FC_TYPE: normal
134 :ID: s
135 :END:
136 :REVIEW_DATA:
137 | position | ease | box | interval | due |
138 |----------+------+-----+----------+----------------------|
139 | front | 2.50 | 0 | 0.00 | 2026-11-18T00:00:00Z |
140 :END:
141 * No drawer :fc:
142 :PROPERTIES:
143 :ID: x
144 :END:
145 """
146
147 @Test func readsCardsAndDuePositions() {
148 let cards = Flashcards.cards(in: Self.deck)
149 #expect(cards.map(\.id) == ["n", "d", "c", "s"])
150 #expect(cards[0].positions.map(\.name) == ["front"] && cards[0].positions[0].ease == 2.5 && cards[0].body == "Answer text")
151 #expect(cards[2].clozeType == .enumeration && cards[2].children == "** More\nextra")
152 #expect(cards[3].suspended)
153 let due = Flashcards.due(cards.map { (path: "/a.org", card: $0) }, at: Self.now, shuffle: false).map { "\($0.card.id) \($0.position)" }
154 // One position of the double card (the first due), every due one of the enumeration.
155 #expect(due == ["n front", "d back", "c 0", "c 1"])
156 #expect(Flashcards.dueCount(cards, at: Self.now) == 4)
157 #expect(Flashcards.dueCount(cards, at: Date(timeIntervalSince1970: 0)) == 0)
158 var generator = SeededGenerator(state: testSeed(7))
159 let shuffled = Flashcards.due(cards.map { (path: "/a.org", card: $0) }, at: Self.now, using: &generator).map { "\($0.card.id) \($0.position)" }
160 #expect(Set(shuffled) == Set(due))
161 #expect(shuffled.firstIndex(of: "c 0")! < shuffled.firstIndex(of: "c 1")!)
162 }
163
164 @Test func facesHideWhatOrgFcHides() throws {
165 let cards = Flashcards.cards(in: Self.deck)
166 let normal = try #require(Flashcards.face(cards[0], position: "front"))
167 #expect(normal.front == .init(title: "Normal", text: "") && normal.back.text == "Answer text")
168 let back = try #require(Flashcards.face(cards[1], position: "back"))
169 #expect(back.front.title == nil && back.front.text == "Hund" && back.back.title == "Double")
170 let first = try #require(Flashcards.face(cards[2], position: "1"))
171 #expect(first.front.title == "Cloze one" && first.front.text == "[...hint] and ..., not {{four}}.")
172 #expect(first.back.text == "two and ..., not {{four}}.\n\n** More\nextra")
173 let marked = ("Cloze one\n" + first.back.text) as NSString
174 #expect(marked.substring(with: NSRange(first.back.marks[0])) == "two")
175 var context = cards[2]
176 context.clozeType = .context
177 #expect(Flashcards.face(context, position: "0")?.front.title == "Cloze [...]")
178 #expect(Flashcards.face(context, position: "0")?.front.text == "two and ..., not {{four}}.")
179 context.clozeType = .single
180 #expect(Flashcards.face(context, position: "2")?.front.text == "... and [...], not {{four}}.")
181 #expect(Flashcards.face(cards[2], position: "9") == nil)
182 }
183
184 @Test func textInputComparesTheFirstLine() {
185 let text = "* Q :fc:\n:PROPERTIES:\n:FC_TYPE: text-input\n:ID: t\n:END:\n:REVIEW_DATA:\n| position | due |\n|-+-|\n| front | 2026-11-18T00:00:00Z |\n:END:\nThe *answer* here\nmore\n"
186 let card = Flashcards.cards(in: text)[0]
187 #expect(card.answerLine == "The *answer* here")
188 #expect(Flashcards.face(card, position: "front")?.expected == "answer")
189 #expect(Self.run(MakeFlashcard(.textInput), "* Q\n** Child\n").failed == "The card has no answer line")
190 }
191
192 @Test func historyLinesAsOrgFcWritesThem() {
193 let position = Flashcards.Position(name: "front", values: ["ease": "2.50", "box": "1", "interval": "0.01"])
194 #expect(Flashcards.historyLine(at: Self.now, path: "/n/a.org", id: "x", position: position, rating: .hard, seconds: 4.567)
195 == "2026-11-18T11:06:40Z\t/n/a.org\tx\tfront\t2.50\t1\t0.01\thard\t4.57\tsm2-v1\n")
196 }
197
198 @Test func ratingFindsTheCardByID() {
199 let rated = Self.run(RateFlashcard(card: "d", position: "back", rating: .easy, random: 0.5), Self.deck)
200 #expect(rated.failed == nil)
201 let card = Flashcards.cards(in: rated.text).first { $0.id == "d" }!
202 #expect(card.positions[1].values["box"] == "2" && card.positions[1].values["due"] == "2026-11-19T11:06:40Z")
203 #expect(Self.run(RateFlashcard(card: "gone", position: "front", rating: .good), Self.deck).failed == "The card is no longer in the file")
204 #expect(Self.run(RateFlashcard(card: "n", position: "back", rating: .good), Self.deck).failed == "No review data row found for this position")
205 }
206}