Repository Public
1// SP Notes Core2// File: Tests/SPNotesFeaturesTests/AssistantResearchTests.swift3// Description: The research screen's view model, the citation picker and the bibliography against the fake backend4// (library from the entity feed, DOI lookup then add, BibTeX dry run then import, exports written as files,5// edit / delete / undo, offline read-only, server errors, empty workspace), and the pure reference formatting.6//7// Author: Simon-Pierre Boucher8// Contact: contact@spboucher.ai9import Foundation10import SPNotesAPI11import SPNotesSync12import SPNotesTestSupport13import Testing14@testable import SPNotesFeatures15@testable import SPNotesFeaturesCatalog1617private func reference(_ title: String, authors: [String], year: Int?, journal: String? = nil, doi: String? = nil, fields: String = "{}", key: String = "k", label: String? = nil) throws -> ResearchReference {18 let object: [String: Any] = ["id": "r1", "citeKey": key, "title": title, "authors": authors, "year": year as Any? ?? NSNull(), "journal": journal as Any? ?? NSNull(),19 "doi": doi as Any? ?? NSNull(), "fields": try JSONSerialization.jsonObject(with: Data(fields.utf8)), "label": label as Any? ?? NSNull()]20 return try APIJSON.decoder().decode(ResearchReference.self, from: JSONSerialization.data(withJSONObject: object))21}2223@Suite("Research: reference formatting")24struct ReferenceFormattingTests {25 @Test("APA authors: initials, particles, hyphenated given names, two, many")26 func authors() {27 #expect(ReferenceFormatter.apaAuthor("Eugene F. Fama") == "Fama, E. F.")28 #expect(ReferenceFormatter.apaAuthor("Ludwig van Beethoven") == "van Beethoven, L.")29 #expect(ReferenceFormatter.apaAuthor("Jean-Paul Sartre") == "Sartre, J.-P.")30 #expect(ReferenceFormatter.apaAuthor("Doe, Jane") == "Doe, J.")31 #expect(ReferenceFormatter.apaAuthor("OECD") == "OECD")32 #expect(ReferenceFormatter.apaAuthors(["Fischer Black", "Myron Scholes"]) == "Black, F., & Scholes, M.")33 #expect(ReferenceFormatter.apaAuthors(["A One", "B Two", "C Three"]) == "One, A., Two, B., & Three, C.")34 #expect(ReferenceFormatter.apaAuthors([]) == "")35 let many = (1...22).map { "A Name\($0)" }36 #expect(ReferenceFormatter.apaAuthors(many).hasSuffix(", … Name22, A.") && ReferenceFormatter.apaAuthors(many).contains("Name19, A."))37 }3839 @Test("APA entry: built only from the fields the reference has")40 func entry() throws {41 let engle = try reference("Autoregressive Conditional Heteroscedasticity", authors: ["Robert F. Engle"], year: 1982, journal: "Econometrica", doi: "10.2307/1912773",42 fields: #"{"volume":"50","number":4,"pages":"987--1007"}"#)43 #expect(ReferenceFormatter.apaPlain(engle) == "Engle, R. F. (1982). Autoregressive Conditional Heteroscedasticity. Econometrica, 50(4), 987–1007. https://doi.org/10.2307/1912773")44 #expect(ReferenceFormatter.apaMarkdown(engle).contains("*Econometrica*, *50*(4)"))45 let bare = try reference("A title without anything?", authors: [], year: nil)46 #expect(ReferenceFormatter.apaPlain(bare) == "A title without anything? (s. d.).")47 let starred = try reference("Stars * and _underscores_", authors: ["A B"], year: 2020)48 #expect(ReferenceFormatter.apaPlain(starred) == "B, A. (2020). Stars * and _underscores_.")49 #expect(ReferenceFormatter.field(engle, "number") == "4" && ReferenceFormatter.field(engle, "publisher") == nil)50 }5152 @Test("citation label, authors line and search")53 func labels() throws {54 #expect(ReferenceFormatter.label(try reference("T", authors: ["Jane Doe"], year: 2021)) == "Doe, 2021")55 #expect(ReferenceFormatter.label(try reference("T", authors: ["Jane Doe", "John Roe"], year: 2021)) == "Doe & Roe, 2021")56 #expect(ReferenceFormatter.label(try reference("T", authors: ["A One", "B Two", "C Three"], year: nil)) == "One et al.")57 #expect(ReferenceFormatter.label(try reference("T", authors: [], year: 2020, key: "anon2020")) == "anon2020, 2020")58 #expect(ReferenceFormatter.label(try reference("T", authors: ["Jane Doe"], year: 2021, label: "Doe (2021)")) == "Doe (2021)")59 #expect(ReferenceFormatter.authorsLine(try reference("T", authors: ["A One", "B Two", "C Three"], year: nil)) == "A One et 2 autres")60 let ref = try reference("Hétéroscédasticité conditionnelle", authors: ["Tim Bollerslev"], year: 1986, journal: "Journal of Econometrics")61 #expect(ReferenceFormatter.matches(ref, "heteroscedasticite 1986") && ReferenceFormatter.matches(ref, "BOLLER") && !ReferenceFormatter.matches(ref, "engle") && ReferenceFormatter.matches(ref, " "))62 #expect(ReferenceFormatter.isSupported(style: "APA") && !ReferenceFormatter.isSupported(style: "ieee"))63 }64}6566@Suite("Research: screen model")67@MainActor68struct ResearchModelTests {69 @Test("library: the device's references, the experiments and the papers; search; detail opens the link and the page")70 func happy() async throws {71 let world = try await FeatureWorld()72 let router = FeatureRouterRecorder()73 let model = ResearchModel(repositories: world.repositories, router: router)74 await model.load()75 #expect(model.phase == .loaded && !model.isOffline && model.canWrite)76 #expect(model.references.map(\.citeKey) == ["black1973pricing", "bollerslev1986generalized", "engle1982autoregressive", "glosten1993relation", "nelson1987parsimonious"])77 #expect(model.experiments.count == 1 && model.papers.count == 1)78 #expect(ResearchModel.keyValues(model.experiments[0].metrics) == "mse = 1.2 · qlike = 0.398")79 #expect(ResearchModel.status(experiment: model.experiments[0]) == "Terminée" && ResearchModel.status(paper: model.papers[0]) == "Rédaction")80 model.searchText = "yield siegel"81 #expect(model.filtered.map(\.citeKey) == ["nelson1987parsimonious"])82 model.searchText = ""83 let glosten = try #require(model.references.first { $0.citeKey == "glosten1993relation" })84 #expect(ResearchModel.verification(glosten)?.isAI == true) // AI-suggested references are marked85 model.select(glosten)86 #expect(model.panel == .detail(glosten.id) && model.selected?.id == glosten.id)87 model.openLink(of: glosten)88 model.openPage(of: glosten)89 model.open(paper: model.papers[0])90 #expect(router.calls == [.url(URL(string: "https://doi.org/10.1111/j.1540-6261.1993.tb05128.x")!), .page(id: AssistantSamples.projectId, blockId: nil),91 .page(id: AssistantSamples.projectId, blockId: nil)])92 }9394 @Test("DOI: the notice is previewed first, added only on the second step; an unknown DOI is explained")95 func doi() async throws {96 let world = try await FeatureWorld()97 let model = ResearchModel(repositories: world.repositories)98 await model.load()99 model.startDOI()100 #expect(await model.lookupDOI() == false && model.actionFailure != nil) // empty101 model.doiText = " https://doi.org/10.2307/2325486 "102 #expect(await model.lookupDOI())103 #expect(model.doiPreview?.title == "Efficient Capital Markets: A Review of Theory and Empirical Work" && model.doiPreview?.duplicate == nil)104 #expect(world.fake.count("POST", "^/api/research/references/doi") == 0 && model.references.count == 5)105 #expect(await model.addPreviewedDOI())106 #expect(world.fake.count("POST", "^/api/research/references/doi") == 1 && model.references.count == 6 && model.doiPreview == nil && model.doiText.isEmpty)107 #expect(model.selected?.citeKey == "fama1970efficient")108 model.startDOI()109 model.doiText = "10.9999/nope"110 #expect(await model.lookupDOI() == false)111 #expect(model.actionFailure?.message == "Ce DOI est inconnu du registre." && model.doiPreview == nil)112 #expect(ResearchModel.normalized(doi: "doi:10.1/x") == "10.1/x" && ResearchModel.normalized(doi: "HTTP://dx.doi.org/10.1/y") == "10.1/y")113 }114115 @Test("BibTeX: a dry run shows what would be created; nothing is written before the confirmation; editing the text asks for a new preview")116 func bibtex() async throws {117 let world = try await FeatureWorld()118 let bodies = Bodies()119 world.fake.stub("POST", path: "^/api/research/references/import-bibtex$") { request in bodies.add(request.body); return nil }120 let model = ResearchModel(repositories: world.repositories)121 await model.load()122 model.startBibTeX()123 #expect(await model.previewImport() == false) // nothing pasted124 #expect(await model.confirmImport() == false) // nothing previewed125 model.bibtexText = "@article{roe2019parity, title={Fixture: Rents and Rates}, year={2019}}"126 #expect(await model.previewImport())127 #expect(model.importPreview?.wouldCreate.count == 3 && model.importPreview?.dryRun == true)128 #expect(bodies.all.count == 1 && bodies.all[0]["dryRun"] as? Bool == true)129 model.bibtexTextChanged()130 #expect(model.importPreview == nil)131 #expect(await model.previewImport())132 #expect(await model.confirmImport())133 #expect(bodies.all.count == 3 && bodies.all[2]["dryRun"] == nil)134 #expect(model.importResult?.created.count == 3 && model.importPreview == nil && model.bibtexText.isEmpty)135 }136137 @Test("exports: BibTeX and LaTeX arrive as files with the server's name; LaTeX needs a project; the report is kept")138 func exports() async throws {139 let world = try await FeatureWorld()140 let library = ResearchModel(repositories: world.repositories)141 await library.load()142 #expect(await library.exportBibTeX())143 let bib = try #require(library.exports.first)144 #expect(bib.kind == .bibtex && bib.filename == "library.bib" && bib.count == 5 && library.panel == .export)145 #expect(try String(contentsOf: bib.fileURL, encoding: .utf8) == AssistantSamples.bibExport && bib.fileURL.lastPathComponent == "library.bib")146 #expect(await library.exportLaTeX() == false && library.actionFailure != nil) // no project147148 let project = ResearchModel(repositories: world.repositories, scope: .project(AssistantSamples.projectId))149 await project.load()150 #expect(project.references.count == 3 && !project.projectListIsPartial) // the server's project list151 #expect(await project.exportLaTeX())152 #expect(await project.exportBibTeX())153 #expect(project.exports.map(\.kind) == [.latex, .bibtex])154 let tex = try #require(project.exports.first)155 #expect(tex.filename == "volatilite-conditionnelle.tex" && tex.report?.missingKeys == 1 && tex.report?.unknownNodes == ["mermaid"])156 #expect(try String(contentsOf: tex.fileURL, encoding: .utf8).contains("\\begin{document}"))157 #expect(try ResearchModel.write("x", name: "../evil/../name.bib").lastPathComponent == "export.txt") // never outside its directory158 #expect(try ResearchModel.write("x", name: "a/b.bib").lastPathComponent == "a-b.bib")159 }160161 @Test("writing: create, edit (validation, a taken key de-duplicated by the server is reported), delete with undo")162 func writing() async throws {163 let world = try await FeatureWorld()164 let model = ResearchModel(repositories: world.repositories, scope: .page(AssistantSamples.projectId))165 await model.load()166 #expect(model.references.count == 3)167 model.startNew()168 #expect(await model.saveForm() == false && model.actionFailure?.message == "Le titre est obligatoire.")169 model.form.title = "Une note de travail"170 model.form.authors = "Jane Doe; John Roe\n"171 model.form.year = "deux mille"172 #expect(await model.saveForm() == false)173 model.form.year = "2026"174 #expect(await model.saveForm())175 #expect(model.references.count == 4 && model.selected?.title == "Une note de travail" && model.selected?.authors == ["Jane Doe", "John Roe"])176177 let engle = try #require(model.references.first { $0.citeKey == "engle1982autoregressive" })178 model.startEdit(engle)179 #expect(model.form.title == engle.title && model.form.year == "1982")180 model.form.citeKey = "bollerslev1986generalized"181 #expect(await model.saveForm()) // the server de-duplicates a taken key: said, not hidden182 #expect(model.reference(id: engle.id)?.citeKey == "bollerslev1986generalizedb" && model.notice?.contains("bollerslev1986generalizedb") == true)183 model.startEdit(try #require(model.reference(id: engle.id)))184 model.form.citeKey = "engle1982arch"185 model.form.journal = ""186 #expect(await model.saveForm())187 let updated = try #require(model.reference(id: engle.id))188 #expect(updated.citeKey == "engle1982arch" && updated.journal == nil && model.panel == .detail(engle.id) && model.notice == nil)189 model.startEdit(updated)190 model.cancelForm()191 #expect(model.panel == .detail(engle.id))192193 #expect(await model.delete(updated))194 #expect(model.reference(id: engle.id) == nil && model.lastDeleted?.id == engle.id && model.panel == nil)195 #expect(await model.undoDelete())196 #expect(model.reference(id: engle.id) != nil && model.lastDeleted == nil)197 }198199 @Test("offline: the library is readable from the device, cached lists carry their date, every write fails calmly and nothing is queued")200 func offline() async throws {201 let world = try await FeatureWorld()202 let model = ResearchModel(repositories: world.repositories)203 await model.load()204 world.setOffline(true)205 world.fake.clearLog()206 await model.load()207 #expect(model.phase == .loaded && model.isOffline && !model.canWrite)208 #expect(model.references.count == 5 && model.experiments.count == 1 && model.papers.count == 1 && model.freshness?.fromCache == true)209 model.doiText = AssistantSamples.famaDOI210 #expect(await model.lookupDOI() == false && model.actionFailure?.kind == .offline)211 #expect(await model.delete(model.references[0]) == false && model.references.count == 5)212 #expect(await model.exportBibTeX() == false && model.exports.isEmpty)213 #expect((try await world.store.entityOps()).isEmpty)214215 let project = ResearchModel(repositories: world.repositories, scope: .project(AssistantSamples.projectId))216 await project.load()217 #expect(project.phase == .loaded && project.projectListIsPartial && project.references.count == 3)218219 let cold = try await FeatureWorld(samples: false)220 cold.setOffline(true)221 let empty = ResearchModel(repositories: cold.repositories)222 await empty.load()223 #expect(empty.phase == .loaded && empty.references.isEmpty && empty.isOffline) // nothing cached: an empty, read-only library224 }225226 @Test("server error on the computed lists never hides the references; an empty workspace is an empty state")227 func errorsAndEmpty() async throws {228 let world = try await FeatureWorld()229 world.fake.stub("GET", path: "^/api/research/overview$") { _ in .error(500, code: "server_error", message: "boom") }230 let model = ResearchModel(repositories: world.repositories)231 await model.load()232 #expect(model.phase.failure?.kind == .server && model.references.count == 5 && !model.isOffline)233234 let cold = try await FeatureWorld(samples: false)235 cold.fake.stub("GET", path: "^/api/research/(experiments|overview)$", json: "[]")236 let empty = ResearchModel(repositories: cold.repositories)237 await empty.load()238 #expect(empty.phase == .loaded && empty.references.isEmpty && empty.experiments.isEmpty && empty.papers.isEmpty && empty.canWrite)239 }240}241242@Suite("Research: citation picker and bibliography")243@MainActor244struct CitationTests {245 @Test("picker: the page's references first, works offline; the choice carries refId, label and locator and is returned only by confirm()")246 func picker() async throws {247 let world = try await FeatureWorld()248 world.setOffline(true)249 let picked = Picked()250 let model = CitationPickerModel(repositories: world.repositories, pageId: AssistantSamples.projectId, onPick: { picked.values.append($0) })251 await model.load()252 #expect(model.phase == .loaded && model.references.count == 5)253 #expect(model.references.prefix(3).allSatisfy { $0.pageId == AssistantSamples.projectId })254 #expect(model.references.map(ReferenceFormatter.label) == ["Bollerslev, 1986", "Engle, 1982", "Glosten et al., 1993", "Black & Scholes, 1973", "Nelson & Siegel, 1987"])255 #expect(model.choice == nil && model.confirm() == nil && picked.values.isEmpty)256 model.searchText = "scholes"257 let black = try #require(model.filtered.first)258 model.select(black)259 model.locator = " p. 640 "260 #expect(picked.values.isEmpty)261 let choice = try #require(model.confirm())262 #expect(choice == CitationChoice(refId: black.id, label: "Black & Scholes, 1973", locator: "p. 640") && picked.values == [choice])263 #expect(CitationPickerModel.preview(choice) == "(Black & Scholes, 1973, p. 640)")264265 let editing = CitationPickerModel(repositories: world.repositories, current: CitationChoice(refId: black.id, label: "x", locator: "chap. 2"))266 await editing.load()267 #expect(editing.selected?.id == black.id && editing.locator == "chap. 2")268269 let cold = try await FeatureWorld(samples: false)270 let empty = CitationPickerModel(repositories: cold.repositories)271 await empty.load()272 #expect(empty.phase == .loaded && empty.references.isEmpty)273 }274275 @Test("bibliography: cited references resolved, sorted like a reference list, missing ones said missing; offline from the device; another style is declared substituted")276 func bibliography() async throws {277 let world = try await FeatureWorld()278 let router = FeatureRouterRecorder()279 let ids = [AssistantSamples.referenceId(1), AssistantSamples.referenceId(0), AssistantSamples.referenceId(1), "", AssistantSamples.referenceId(4), "00000000-0000-4000-9000-00000000dead"]280 let model = BibliographyModel(repositories: world.repositories, router: router, citedIds: ids, style: "", title: "")281 #expect(model.citedIds.count == 4 && model.style == "apa" && model.title == "Références" && !model.styleIsSubstituted)282 await model.load()283 #expect(model.phase == .loaded && model.entries.count == 4 && model.missingCount == 1)284 #expect(model.entries.map { $0.reference?.citeKey } == ["bollerslev1986generalized", "engle1982autoregressive", "glosten1993relation", nil])285 #expect(model.entries[0].plain == "Bollerslev, T. (1986). Generalized Autoregressive Conditional Heteroskedasticity. Journal of Econometrics, 31(3), 307–327. https://doi.org/10.1016/0304-4076(86)90063-1")286 model.open(model.entries[1])287 model.open(model.entries[3])288 #expect(router.calls == [.url(URL(string: "https://doi.org/10.2307/1912773")!)])289290 world.setOffline(true)291 let offline = BibliographyModel(repositories: world.repositories, citedIds: ids, style: "ieee", title: "Sources")292 await offline.load()293 #expect(offline.phase == .loaded && offline.entries.count == 4 && offline.styleIsSubstituted && offline.title == "Sources")294295 world.setOffline(false)296 world.fake.stub("GET", path: "^/api/research/references$") { _ in .error(500, code: "server_error", message: "boom") }297 await model.load()298 #expect(model.phase.failure?.kind == .server)299 let none = BibliographyModel(repositories: world.repositories, citedIds: [])300 await none.load()301 #expect(none.phase == .loaded && none.entries.isEmpty)302 }303}304305@MainActor306final class Picked {307 var values: [CitationChoice] = []308}309