Repository Public
1// SP Notes Core2// File: Tests/SPNotesSyncTests/CaptureInboxTests.swift3// Description: A capture created offline (a page with `inboxAt` and `captureKind`) is still a capture on the server and on the4// device after its first synchronisation; the pen width law and its inverse; the additive helpers of 2.0.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import Foundation9import Testing10import SPNotesDocument11import SPNotesStore12import SPNotesAPI13@testable import SPNotesSync14import SPNotesTestSupport1516private struct NoFiles: AttachmentFileLocating {17 func url(relativePath: String) -> URL { FileManager.default.temporaryDirectory.appendingPathComponent(relativePath) }18 func relativePath(id: String, filename: String) -> String { id }19 func remove(relativePath: String) {}20}2122@Suite struct CaptureInboxTests {23 @Test func anOfflineCaptureStaysInTheInboxOnTheServerAndOnTheDevice() async throws {24 let fake = FakeBackend(); defer { fake.shutdown() }25 let client = HTTPClient(configuration: APIConfiguration(baseURL: fake.baseURL, userAgent: "tests"), tokenStore: InMemoryTokenStore(token: fake.issueToken()), sessionConfiguration: FakeBackend.sessionConfiguration())26 let store = try NotesStore.inMemory(); let api = NotesAPI(client: client)27 let engine = SyncEngine(store: store, api: api, files: NoFiles())28 let at = Date(timeIntervalSince1970: 1_800_000_000)29 let page = try await store.createPage(NewPage(title: "Capturé hors ligne", inboxAt: at, captureKind: "audio"))30 #expect(await engine.sync() == .completed)31 let server = try #require(fake.pageSnapshot(page.id))32 #expect(server.inInbox, "the server knows it is a capture"); #expect(server.title == "Capturé hors ligne")33 // the feed row gives the same state back: still in the inbox, kind kept34 #expect(await engine.sync() == .completed)35 let local = try #require(try await store.page(id: page.id))36 #expect(local.inboxAt != nil); #expect(local.captureKind == "audio"); #expect(local.syncState == .synced)37 // an encoded draft without the two fields is what 2.x clients sent38 let plain = try APIJSON.encoder().encode(PageDraft(id: "x", title: "t")); #expect(!String(decoding: plain, as: UTF8.self).contains("inboxAt"))39 }4041 @Test func thePenWidthLawHasAnInverse() {42 for p in [0.0, 0.25, 0.5, 1.0] { #expect(abs(InkRenderer.pressure(forWidth: InkRenderer.penWidth(4, p), nominal: 4) - p) < 1e-9) }43 #expect(InkRenderer.penWidth(5, 1) == 5); #expect(abs(InkRenderer.penWidth(5, 0) - 1.75) < 1e-9)44 #expect(InkRenderer.pressure(forWidth: 99, nominal: 4) == 1); #expect(InkRenderer.pressure(forWidth: 0, nominal: 4) == 0); #expect(InkRenderer.pressure(forWidth: 3, nominal: 0) == 1)45 }4647 @Test func additiveHelpers() throws {48 #expect(NodeType.blockIdTypes.isSuperset(of: Set(NodeType.nextBlocks + [NodeType.listItem, NodeType.taskItem, NodeType.paragraph])))49 #expect(!NodeType.blockIdTypes.contains(NodeType.footnote)); #expect(TaskItemPriority.high.rawValue == TaskRowPriority.high.rawValue)50 func task(_ due: String?) throws -> TaskItem { try APIJSON.decoder().decode(TaskItem.self, from: Data(#"{"id":"a","title":"t","status":"todo","createdAt":"2026-11-01T10:00:00.000Z","updatedAt":"2026-11-01T10:00:00.000Z"\#(due.map { #","dueAt":"\#($0)""# } ?? "")}"#.utf8)) }51 #expect(try task("2026-11-21T12:00:00.000Z").isBareDay); #expect(try task("2026-11-21T12:00:00.000Z").dueDay(timeZone: TimeZone(identifier: "Pacific/Auckland")!) == "2026-11-21")52 #expect(try !task("2026-11-21T23:30:00.000Z").isBareDay); #expect(try task("2026-11-21T23:30:00.000Z").dueDay(timeZone: TimeZone(identifier: "Pacific/Auckland")!) == "2026-11-22")53 #expect(try task(nil).dueDay() == nil)54 var doc = NoteDocument(lines: [.paragraph(text: "a"), .footnote(id: "1"), .footnote(id: "7"), .footnote(id: "x")])55 #expect(doc.nextFootnoteLabel() == "8"); doc.lines.append(.footnote(id: doc.nextFootnoteLabel())); #expect(doc.schemaViolations().isEmpty)56 #expect(PMNode.footnoteRef(id: "8").attrs?["id"] == .string("8"))57 let q = LiveQuery(source: "tasks"); #expect(KnowledgeRepository.cacheKey(for: q) == KnowledgeRepository.cacheKey(for: q)); #expect(KnowledgeRepository.cacheKey(for: q) != KnowledgeRepository.cacheKey(for: LiveQuery(source: "pages")))58 }59}60