Files
helder/Tests/HelderUITests/State/CleanCopyTests.swift
T
2026-10-30 11:27:44 +01:00

113 lines
4.1 KiB
Swift

import Foundation
import HelderCore
import HelderServices
@testable import HelderUI
import Testing
@MainActor
@Suite struct CleanCopyTests {
private let file = CleanCopy.fileName
private func started(_ text: String? = nil) async -> WorkspaceHarness {
let harness = WorkspaceHarness(contents: text.map { [CleanCopy.fileName: $0] } ?? [:])
await harness.start()
return harness
}
private func agentWrites(_ text: String, _ harness: WorkspaceHarness) async {
harness.files.set(file, text)
await harness.model.handle(.cleanCopy)
await harness.model.settle()
}
@Test func theTextFoundOnProjectOpenIsTheBaselineAndNeverPops() async {
let harness = await started("left over from yesterday")
#expect(harness.model.overlay.current == .none)
await harness.model.handle(.cleanCopy)
#expect(harness.model.overlay.current == .none)
}
@Test func aChangedFileOpensThePopupWithItsText() async {
let harness = await started("old")
await agentWrites("git commit -m \"fix\"", harness)
#expect(harness.model.overlay.current == .cleanCopy)
#expect(harness.model.cleanCopy?.text == "git commit -m \"fix\"")
}
@Test func aFileThatAppearsAfterOpenPops() async {
let harness = await started()
await agentWrites("first", harness)
#expect(harness.model.overlay.current == .cleanCopy)
}
@Test func theSameTextAgainDoesNotPop() async {
let harness = await started()
await agentWrites("once", harness)
await harness.perform(.dismissOverlay)
await agentWrites("once", harness)
#expect(harness.model.overlay.current == .none)
}
@Test func anEmptyOrBlankFileDoesNotPop() async {
let harness = await started("old")
await agentWrites("", harness)
await agentWrites(" \n\t", harness)
#expect(harness.model.overlay.current == .none)
}
@Test func copyPutsTheEditedTextOnThePasteboardClosesAndToasts() async {
let harness = await started()
await agentWrites("draft", harness)
harness.model.cleanCopy?.text = "draft, edited"
await harness.perform(.copyCleanCopy)
#expect(harness.system.copied == ["draft, edited"])
#expect(harness.model.overlay.current == .none)
#expect(harness.toastTitles == ["Copied"])
}
@Test func escClosesWithoutCopying() async {
let harness = await started()
await agentWrites("draft", harness)
await harness.perform(.dismissOverlay)
#expect(harness.system.copied.isEmpty)
#expect(harness.model.overlay.current == .none)
}
@Test func newTextReplacesAnUneditedPopup() async {
let harness = await started()
await agentWrites("one", harness)
await agentWrites("two", harness)
#expect(harness.model.overlay.current == .cleanCopy)
#expect(harness.model.cleanCopy?.text == "two")
}
@Test func newTextLeavesAnEditedPopupAlone() async {
let harness = await started()
await agentWrites("one", harness)
harness.model.cleanCopy?.text = "one, mine"
await agentWrites("two", harness)
#expect(harness.model.cleanCopy?.text == "one, mine")
#expect(harness.model.cleanCopy?.pending == nil)
}
@Test func textWaitsForAnotherOverlayAndOnlyTheLatestShows() async {
let harness = await started()
await harness.perform(.showShortcuts)
await agentWrites("one", harness)
await agentWrites("two", harness)
#expect(harness.model.overlay.current == .shortcuts)
await harness.perform(.dismissOverlay)
#expect(await eventually { harness.model.overlay.current == .cleanCopy })
#expect(harness.model.cleanCopy?.text == "two")
}
@Test func theFileIsNeverWrittenOrDeleted() async {
let harness = await started()
await agentWrites("keep", harness)
await harness.perform(.copyCleanCopy)
#expect(harness.files.current(file) == "keep")
#expect(harness.files.deleted.isEmpty)
#expect(harness.files.writes.isEmpty)
}
}