Sapling/Tests/SaplingEditorTests/DocumentSessionStoreTests.swift

68 lines
2.4 KiB
Swift
Raw Normal View History

import Foundation
import SaplingCore
import SaplingEditor
import XCTest
@MainActor
final class DocumentSessionStoreTests: XCTestCase {
func testOpeningSameFileReusesExistingSession() throws {
let url = URL(fileURLWithPath: "/tmp/Sapling/Notes.md")
var loadCount = 0
let store = DocumentSessionStore()
let first = try store.openDocument(at: url) { url in
loadCount += 1
return MarkdownDocument(url: url, title: "Notes", content: "# Notes")
}
let second = try store.openDocument(at: url.standardizedFileURL) { url in
loadCount += 1
return MarkdownDocument(url: url, title: "Notes", content: "# Reloaded")
}
XCTAssertIdentical(first, second)
XCTAssertIdentical(store.activeSession, first)
XCTAssertEqual(store.sessions.count, 1)
XCTAssertEqual(loadCount, 1)
XCTAssertEqual(first.viewModel.document.content, "# Notes")
}
func testOpeningDifferentFilesCreatesSeparateSessionsAndActivatesLatest() throws {
let firstURL = URL(fileURLWithPath: "/tmp/Sapling/One.md")
let secondURL = URL(fileURLWithPath: "/tmp/Sapling/Two.md")
let store = DocumentSessionStore()
let first = try store.openDocument(at: firstURL) { url in
MarkdownDocument(url: url, title: "One", content: "# One")
}
let second = try store.openDocument(at: secondURL) { url in
MarkdownDocument(url: url, title: "Two", content: "# Two")
}
XCTAssertEqual(store.sessions.count, 2)
XCTAssertIdentical(store.activeSession, second)
XCTAssertTrue(store.activateDocument(at: firstURL))
XCTAssertIdentical(store.activeSession, first)
}
func testActivateMissingDocumentReturnsFalse() {
let store = DocumentSessionStore()
XCTAssertFalse(store.activateDocument(at: URL(fileURLWithPath: "/tmp/missing.md")))
XCTAssertNil(store.activeSession)
}
func testCloseAllRemovesSessionsAndActiveDocument() throws {
let store = DocumentSessionStore()
try store.openDocument(at: URL(fileURLWithPath: "/tmp/Sapling/Notes.md")) { url in
MarkdownDocument(url: url, title: "Notes", content: "# Notes")
}
store.closeAll()
XCTAssertTrue(store.sessions.isEmpty)
XCTAssertNil(store.activeSession)
}
}