Files

85 lines
3.4 KiB
Swift

import AppKit
import PlakCore
@testable import PlakUI
import Testing
/// Append from other apps: a new block at the end of the page.
@MainActor
@Suite struct AppendTests {
private func pasteboard(_ text: String) -> NSPasteboard {
let board = NSPasteboard(name: NSPasteboard.Name("plak-tests-\(UUID().uuidString)"))
board.clearContents()
board.setString(text, forType: .string)
return board
}
@Test("on an empty page the text becomes the page, dated now, with the end in the middle")
func emptyPage() throws {
let page = try TestPage()
let board = pasteboard("new")
defer { board.releaseGlobally() }
page.page.append(from: board)
#expect(page.fileText == "\(dateLine(2026, 10, 17, 10, 0))\nnew\n")
#expect(page.endOffsetFromMiddle <= 1)
#expect(page.textView.selectedRange() == NSRange(location: page.textView.length, length: 0))
}
@Test("after a block the text comes after a blank line, as a new block with its own date line")
func afterBlock() throws {
let old = dateLine(2026, 10, 16, 8, 55)
let page = try TestPage("\(old)\na\n")
let board = pasteboard("new\nline")
defer { board.releaseGlobally() }
page.caret(0)
page.page.append(from: board)
#expect(page.fileText == "\(old)\na\n\n\(dateLine(2026, 10, 17, 10, 0))\nnew\nline\n")
}
@Test("appended JSON becomes a fenced json code block at the end")
func json() throws {
let page = try TestPage("a\n")
let board = pasteboard(#"{"b":1}"#)
defer { board.releaseGlobally() }
page.page.append(from: board)
#expect(page.textView.string == "a\n\n\u{FFFC}")
#expect(page.fileText == "a\n\n\(dateLine(2026, 10, 17, 10, 0))\n```json\n{\n \"b\": 1\n}\n```\n")
}
@Test("after text that ends with a newline only one more newline is added")
func endsWithNewline() throws {
let page = try TestPage("a\n")
page.caretAtEnd()
page.type("\n")
let board = pasteboard("b")
defer { board.releaseGlobally() }
page.page.append(from: board)
#expect(page.textView.string == "a\n\nb")
#expect(page.fileText == "a\n\n\(dateLine(2026, 10, 17, 10, 0))\nb\n")
}
@Test("every block of a text with several blocks gets the created date of the append")
func severalBlocks() throws {
let old = dateLine(2026, 10, 16, 8, 55)
let page = try TestPage("\(old)\na\n")
let board = pasteboard("one\n\n=two\nline\n\n\nthree")
defer { board.releaseGlobally() }
page.page.append(from: board)
let now = dateLine(2026, 10, 17, 10, 0)
#expect(page.fileText == "\(old)\na\n\n\(now)\none\n\n\(now)\n=two\nline\n\n\n\(now)\nthree\n")
}
@Test("the help text appends with a date on every block, its json and csv as objects")
func helpText() throws {
let page = try TestPage("a\n")
let board = pasteboard(HelpText.text)
defer { board.releaseGlobally() }
page.page.append(from: board)
let help = PageFile(text: HelpText.text)
let now = dateLine(2026, 10, 17, 10, 0)
#expect(page.fileText.components(separatedBy: now).count - 1 == help.blocks.count)
#expect(page.fileText.contains("\(now)\n=shortcuts\n```csv\n"))
#expect(page.textView.string.filter { $0 == "\u{FFFC}" }.count == 2)
#expect(page.endOffsetFromMiddle <= 1)
}
}