Repository Public
1// SP Notes Core2// File: Tests/SPNotesFeaturesTests/WorkInboxTests.swift3// Description: The inbox view model against the fake backend: list, capture, triage and undo, offline (last list, no4// action), server errors, and the Smart Inbox rules — nothing asked to a model without the explicit5// request, nothing ticked for the user, only the ticked subset applied.6//7// Author: Simon-Pierre Boucher8// Contact: contact@spboucher.ai9import Foundation10import SPNotesAPI11import SPNotesSync12import Testing13@testable import SPNotesFeatures14@testable import SPNotesFeaturesCatalog1516@Suite("Work · inbox")17@MainActor18struct WorkInboxTests {19 @Test("happy path: the captures, triage as a task, undo")20 func triageAndUndo() async throws {21 let world = try await FeatureWorld()22 let router = FeatureRouterRecorder()23 let model = InboxModel(repositories: world.repositories, router: router, now: { FeatureSamples.now })24 await model.load()25 #expect(model.phase == .loaded)26 #expect(model.items.count == 5)27 #expect(model.freshness?.fromCache == false)28 #expect(!model.databases.isEmpty)29 let tasks = try #require(model.items.first { $0.kind == "tasks" })30 #expect(await model.triage(tasks.id, .task()))31 #expect(model.items.count == 4)32 #expect(model.lastAction?.message == "2 tâches créées.")33 #expect(await model.undo())34 #expect(model.items.count == 5)35 #expect(model.lastAction == nil)36 model.open(tasks)37 #expect(router.calls == [.page(id: tasks.id, blockId: nil)])38 }3940 @Test("capture: a note, a link with its note; the box empties on success")41 func capture() async throws {42 let world = try await FeatureWorld()43 let model = InboxModel(repositories: world.repositories)44 await model.load()45 model.captureText = "Vérifier le taux de la marge"46 #expect(model.canCapture)47 #expect(await model.capture())48 #expect(model.captureText.isEmpty)49 #expect(model.items.first?.title == "Vérifier le taux de la marge")50 model.captureMode = .link51 model.captureText = "https://example.org/a Article à lire"52 #expect(await model.capture())53 #expect(model.items.first?.url == "https://example.org/a")54 #expect(model.items.count == 7)55 }5657 @Test("offline: the last list with its date, the actions disabled and calm")58 func offline() async throws {59 let world = try await FeatureWorld()60 let model = InboxModel(repositories: world.repositories)61 await model.load()62 world.setOffline(true)63 await model.load()64 #expect(model.items.count == 5)65 #expect(model.freshness?.fromCache == true)66 #expect(model.phase.failure?.isOffline == true)67 #expect(model.isOffline)68 model.captureText = "hors ligne"69 #expect(!model.canCapture)70 let first = try #require(model.items.first)71 #expect(await model.triage(first.id, .archive) == false)72 #expect(model.actionFailure?.kind == .offline)73 #expect(model.items.count == 5)74 }7576 @Test("offline and never loaded: a typed failure, not a spinner")77 func offlineNeverLoaded() async throws {78 let world = try await FeatureWorld()79 world.setOffline(true)80 let model = InboxModel(repositories: world.repositories)81 await model.load()82 #expect(model.items.isEmpty)83 #expect(model.phase.failure?.kind == .notCached)84 }8586 @Test("server error on triage: surfaced, the list unchanged")87 func serverError() async throws {88 let world = try await FeatureWorld()89 let model = InboxModel(repositories: world.repositories)90 await model.load()91 let first = try #require(model.items.first)92 world.fake.inject(.status(500), method: "POST", path: "^/api/inbox/")93 #expect(await model.triage(first.id, .trash) == false)94 #expect(model.actionFailure?.kind == .server)95 #expect(model.items.count == 5)96 world.fake.inject(.status(500), method: "GET", path: "^/api/inbox$")97 let fresh = InboxModel(repositories: world.repositories)98 await fresh.load()99 #expect(fresh.phase.failure?.kind == .server)100 }101102 @Test("empty inbox")103 func empty() async throws {104 let world = try await FeatureWorld(samples: false)105 let model = InboxModel(repositories: world.repositories)106 await model.load()107 #expect(model.phase == .loaded)108 #expect(model.items.isEmpty)109 }110111 @Test("Smart Inbox: selecting a capture only reads the cache; the model is asked on the explicit request only")112 func suggestionsAreExplicit() async throws {113 let world = try await FeatureWorld()114 let model = InboxModel(repositories: world.repositories)115 await model.load()116 world.fake.clearLog()117 await model.select(FeatureSamples.workInboxPlainId)118 #expect(model.suggestionState == .notGenerated)119 let reads = world.fake.requests.filter { $0.path.hasSuffix("/suggestions") }120 #expect(reads.count == 1)121 #expect(reads.allSatisfy { $0.query.contains("cachedOnly=1") })122 await model.askSuggestions()123 guard case .ready(let suggestions) = model.suggestionState else { Issue.record("expected suggestions, got \(model.suggestionState)"); return }124 #expect(suggestions.model == "claude-haiku-5-5")125 #expect(world.fake.requests.filter { $0.path.hasSuffix("/suggestions") && !$0.query.contains("cachedOnly") }.count == 1)126 }127128 @Test("Smart Inbox: nothing is ticked for the user; only the ticked, edited subset is applied; undo reverses it")129 func checklist() async throws {130 let world = try await FeatureWorld()131 let model = InboxModel(repositories: world.repositories)132 await model.load()133 await model.select(FeatureSamples.workInboxSuggestedId)134 guard case .ready(let suggestions) = model.suggestionState else { Issue.record("expected cached suggestions"); return }135 #expect(InboxModel.hasContent(suggestions))136 #expect(model.choice.isEmpty)137 #expect(model.selection().isEmpty)138 #expect(await model.applySuggestions() == false) // nothing ticked: nothing sent139 #expect(world.fake.count("POST", "/apply$") == 0)140 model.actionFailure = nil141 model.choice.useTitle = true142 model.choice.title = "Rappeler la banque (corrigé)"143 model.choice.tags = ["triplex"]144 let selection = model.selection()145 #expect(selection.title == "Rappeler la banque (corrigé)")146 #expect(selection.tags == ["triplex"])147 #expect(selection.destination == nil)148 #expect(selection.relations == nil)149 #expect(selection.summary == nil)150 #expect(await model.applySuggestions())151 #expect(model.items.first { $0.id == FeatureSamples.workInboxSuggestedId }?.title == "Rappeler la banque (corrigé)")152 #expect(model.items.first { $0.id == FeatureSamples.workInboxSuggestedId }?.tags == ["triplex"])153 #expect(model.choice.isEmpty)154 #expect(model.lastAction != nil)155 #expect(await model.undo())156 #expect(model.items.first { $0.id == FeatureSamples.workInboxSuggestedId }?.title == "rappeler la banque triplex")157 }158159 @Test("Smart Inbox: ticking the destination files the capture")160 func destinationFiles() async throws {161 let world = try await FeatureWorld()162 let model = InboxModel(repositories: world.repositories)163 await model.load()164 await model.select(FeatureSamples.workInboxSuggestedId)165 model.choice.useDestination = true166 model.choice.relations = [FeatureSamples.workJournalPageId]167 model.choice.useSummary = true168 let selection = model.selection()169 #expect(selection.destination?.section == "projects")170 #expect(selection.relations?.map(\.pageId) == [FeatureSamples.workJournalPageId])171 #expect(selection.summary?.isEmpty == false)172 #expect(await model.applySuggestions())173 #expect(model.items.count == 4)174 #expect(model.selectedId == nil)175 }176177 @Test("Smart Inbox: a server without the route says so; a provider failure is shown, not hidden")178 func suggestionFailures() async throws {179 let world = try await FeatureWorld()180 let model = InboxModel(repositories: world.repositories)181 await model.load()182 world.fake.stub("GET", path: "^/api/inbox/[^/]+/suggestions$") { _ in .init(status: 404, body: Data("<html>404</html>".utf8), headers: ["content-type": "text/html"]) }183 await model.select(FeatureSamples.workInboxPlainId)184 #expect(model.suggestionState == .unavailable)185 world.fake.stub("GET", path: "^/api/inbox/[^/]+/suggestions$") { request in186 request.query["cachedOnly"] != nil ? .error(404, code: "not_generated") : .error(502, code: "provider_error", message: "Provider failed")187 }188 await model.select(FeatureSamples.workInboxPlainId)189 #expect(model.suggestionState == .notGenerated)190 await model.askSuggestions()191 guard case .failed(let failure) = model.suggestionState else { Issue.record("expected a failure"); return }192 #expect(failure.code == "provider_error")193 }194195 @Test("labels of capture kinds and sections")196 func labels() {197 #expect(InboxModel.kindLabel("audio") == "Audio")198 #expect(InboxModel.kindSymbol("link") == "link")199 #expect(InboxModel.kindLabel(nil) == "Note")200 #expect(InboxModel.sectionLabel("projects") == "Projets")201 }202}203