Files

261 lines
12 KiB
Swift

import HelderCore
import Testing
@Suite struct TerminalContextMenuTests {
private func labels(_ target: ContextTarget) -> [String] {
ContextActions.entries(for: target).compactMap { entry in
guard case let .action(_, label) = entry else { return nil }
return label
}
}
@Test func copyAppearsOnlyWithASelection() {
#expect(labels(.terminal(.agent, hasSelection: true)) == ["Copy", "Paste", "Read aloud selection", "Plak", "Wait what?"])
#expect(labels(.terminal(.shell, hasSelection: false)) == ["Paste"])
}
@Test func waitWhatAppearsOnlyForASelectionInTheAgentPane() {
#expect(!labels(.terminal(.agent, hasSelection: false)).contains("Wait what?"))
#expect(!labels(.terminal(.shell, hasSelection: true)).contains("Wait what?"))
}
@Test func theActionsNameTheirPane() {
#expect(ContextActions.entries(for: .terminal(.shell, hasSelection: true)) == [
.action(.copyTerminalSelection(.shell), label: "Copy"),
.action(.pasteIntoTerminal(.shell), label: "Paste"),
.separator,
.action(.readAloudTerminalSelection(.shell), label: "Read aloud selection"),
.separator,
.action(.plakTerminalSelection(.shell), label: "Plak"),
])
}
@Test func plakAppearsOnlyWithASelectionInBothPanes() {
#expect(labels(.terminal(.agent, hasSelection: true)).contains("Plak"))
#expect(labels(.terminal(.shell, hasSelection: true)).contains("Plak"))
#expect(!labels(.terminal(.agent, hasSelection: false)).contains("Plak"))
#expect(!labels(.terminal(.shell, hasSelection: false)).contains("Plak"))
}
@Test func readAloudAppearsOnlyWithASelection() {
#expect(!labels(.terminal(.agent, hasSelection: false)).contains("Read aloud selection"))
#expect(!labels(.terminal(.shell, hasSelection: false)).contains("Read aloud selection"))
}
@Test func theTailPaneOffersCopyButNoPasteOrWaitWhat() {
#expect(labels(.terminal(.tail, hasSelection: true)) == ["Copy", "Read aloud selection", "Plak"])
#expect(ContextActions.entries(for: .terminal(.tail, hasSelection: false)).isEmpty)
}
@Test func theMenuHasNoHeader() {
#expect(ContextActions.header(for: .terminal(.agent, hasSelection: false), projectName: "app").isEmpty)
#expect(ContextActions.header(for: .directory(path: "src"), projectName: "app").isEmpty)
}
}
@Suite struct EditorContextMenuTests {
private let reference = AgentReference(path: "src/a.php", startLine: 12)
@Test func theEditorMenuHasNoHeader() {
#expect(ContextActions.header(for: .editor(reference: reference, code: nil), projectName: "app").isEmpty)
#expect(ContextActions.header(for: .editor(reference: reference, code: "x", showDiffLine: 12), projectName: "app").isEmpty)
}
@Test func fileGitRowAndRootMenusKeepTheirHeader() {
#expect(ContextActions.header(for: .file(path: "src/a.php"), projectName: "app") == "src/a.php")
#expect(ContextActions.header(for: .gitRow(path: "src/a.php", isDeleted: false), projectName: "app") == "src/a.php")
#expect(ContextActions.header(for: .projectRoot, projectName: "app") == "app/")
}
@Test func aMarkedLineEndsWithShowDiffAtThatLine() {
#expect(ContextActions.entries(for: .editor(reference: reference, code: nil, showDiffLine: 12)) == [
.action(.passOnReference("src/a.php:12", code: nil), label: "Pass on reference"),
.action(.copyReference("src/a.php:12"), label: "Copy reference"),
.separator,
.action(.showDiff("src/a.php", line: 12), label: "Show diff"),
])
}
@Test func anUnchangedLineHasNoShowDiff() {
#expect(ContextActions.entries(for: .editor(reference: reference, code: nil)) == [
.action(.passOnReference("src/a.php:12", code: nil), label: "Pass on reference"),
.action(.copyReference("src/a.php:12"), label: "Copy reference"),
])
}
}
@Suite struct BouwenContextMenuTests {
private func hasBouwen(_ target: ContextTarget) -> Bool {
ContextActions.entries(for: target).contains { entry in
guard case .action(.bouwen, _) = entry else { return false }
return true
}
}
@Test func anOntwikkelingFolderStartsWithBouwen() {
let entries = ContextActions.entries(for: .directory(path: "ontwikkelingen/2026-10-02-001-x"))
#expect(Array(entries.prefix(3)) == [
.action(.bouwen("2026-10-02-001-x"), label: "Bouwen"),
.separator,
.action(.passOnReference("ontwikkelingen/2026-10-02-001-x/", code: nil), label: "Pass on reference"),
])
}
@Test func otherPathsHaveNoBouwen() {
#expect(!hasBouwen(.directory(path: "ontwikkelingen")))
#expect(!hasBouwen(.directory(path: "ontwikkelingen/ontwikkeld")))
#expect(!hasBouwen(.directory(path: "ontwikkelingen/a/b")))
#expect(!hasBouwen(.directory(path: "src/ontwikkelingen/a")))
#expect(!hasBouwen(.file(path: "ontwikkelingen/a")))
}
}
@Suite struct ReadAloudContextMenuTests {
private func readAloud(_ target: ContextTarget) -> [ContextMenuEntry] {
ContextActions.entries(for: target).filter { entry in
switch entry {
case .action(.readAloudFile, _), .action(.readAloudText, _): true
default: false
}
}
}
@Test func aMarkdownFileCanBeReadAloud() {
let entries = ContextActions.entries(for: .file(path: "docs/guide.md"))
#expect(Array(entries[4...6]) == [
.separator,
.action(.readAloudFile("docs/guide.md"), label: "Read aloud"),
.separator,
])
}
@Test func otherFilesCannot() {
#expect(readAloud(.file(path: "src/app.php")).isEmpty)
#expect(readAloud(.directory(path: "docs")).isEmpty)
}
@Test func aGitRowOpensInTheEditorThenPassesOnItsPath() {
#expect(ContextActions.entries(for: .gitRow(path: "src/a.php", isDeleted: false)) == [
.action(.openFile("src/a.php"), label: "Open in editor"),
.action(.openDiff("src/a.php"), label: "Open diff"),
.separator,
.action(.passOnReference("src/a.php", code: nil), label: "Pass on reference"),
.action(.copyReference("src/a.php"), label: "Copy reference"),
])
#expect(ContextActions.entries(for: .gitRow(path: "gone.php", isDeleted: true)) == [
.action(.passOnReference("gone.php", code: nil), label: "Pass on reference"),
.action(.copyReference("gone.php"), label: "Copy reference"),
])
#expect(ContextAction.openFile("src/a.php").isEmphasized)
#expect(!ContextAction.openDiff("src/a.php").isEmphasized)
#expect(!ContextAction.openDiff("src/a.php").isDestructive)
}
@Test func aSelectionCanBeReadAloudAfterCopyReference() {
let reference = AgentReference(path: "src/a.php", startLine: 3, endLine: 4)
#expect(ContextActions.entries(for: .editor(reference: reference, code: "$a = 1;", showDiffLine: 3)) == [
.action(.passOnReference("src/a.php:3-4", code: "$a = 1;"), label: "Pass on selection"),
.action(.copyReference("src/a.php:3-4"), label: "Copy reference"),
.separator,
.action(.readAloudText("$a = 1;"), label: "Read aloud selection"),
.separator,
.action(.plak("$a = 1;"), label: "Plak"),
.separator,
.action(.showDiff("src/a.php", line: 3), label: "Show diff"),
])
}
@Test func noSelectionNoReadAloud() {
let reference = AgentReference(path: "README.md", startLine: 1)
#expect(readAloud(.editor(reference: reference, code: nil)).isEmpty)
#expect(readAloud(.editor(reference: reference, code: "")).isEmpty)
}
@Test func selectedMarkdownIsReadAsProse() {
let reference = AgentReference(path: "README.md", startLine: 1)
#expect(readAloud(.editor(reference: reference, code: "## Setup\nRun **this**")) == [
.action(.readAloudText("Setup.\nRun this."), label: "Read aloud selection"),
])
#expect(readAloud(.editor(reference: reference, code: "```\nls\n```")) == [
.action(.readAloudText("```\nls\n```"), label: "Read aloud selection"),
])
}
}
@Suite struct PlakContextMenuTests {
private func plak(_ target: ContextTarget) -> [ContextMenuEntry] {
ContextActions.entries(for: target).filter { entry in
if case .action(.plak, _) = entry { true } else { false }
}
}
@Test func anEditorSelectionIsSentAsRawCode() {
let reference = AgentReference(path: "README.md", startLine: 1)
#expect(plak(.editor(reference: reference, code: "## Setup\nRun **this**")) == [
.action(.plak("## Setup\nRun **this**"), label: "Plak"),
])
}
@Test func noSelectionNoPlak() {
let reference = AgentReference(path: "src/a.php", startLine: 1)
#expect(plak(.editor(reference: reference, code: nil)).isEmpty)
#expect(plak(.editor(reference: reference, code: "")).isEmpty)
}
}
@Suite struct BranchContextMenuTests {
@Test func aCleanTreeOffersTheMergeIntoTheCurrentBranch() {
#expect(ContextActions.entries(for: .branch(name: "feature", remote: false, current: "dev", isDirty: false)) == [
.action(.mergeBranch("feature"), label: "Merge into dev"),
])
#expect(ContextActions.entries(for: .branch(name: "origin/feature", remote: true, current: "dev", isDirty: false)) == [
.action(.mergeBranch("origin/feature"), label: "Merge into dev"),
])
}
@Test func aDirtyTreeShowsTheMergeDisabled() {
let entries = ContextActions.entries(for: .branch(name: "feature", remote: false, current: "dev", isDirty: true))
#expect(entries == [
.disabled(.mergeBranch("feature"), label: "Merge into dev", hint: "Commit or discard your changes first"),
])
#expect(!entries[0].isSelectable)
}
@Test func theCurrentBranchHasNoMenuButItsUpstreamHas() {
#expect(ContextActions.entries(for: .branch(name: "dev", remote: false, current: "dev", isDirty: false)).isEmpty)
#expect(ContextActions.entries(for: .branch(name: "origin/dev", remote: true, current: "dev", isDirty: false)).count == 1)
}
@Test func theHeaderIsTheBranchName() {
#expect(ContextActions.header(for: .branch(name: "origin/feature", remote: true, current: "dev", isDirty: false),
projectName: "app") == "origin/feature")
}
}
@Suite struct TailLogContextMenuTests {
private func hasTailLog(_ target: ContextTarget) -> Bool {
ContextActions.entries(for: target).contains { entry in
guard case .action(.tailLog, _) = entry else { return false }
return true
}
}
@Test func aLogFileOffersTailLogInItsOwnGroupAfterTheCopyItems() {
let entries = ContextActions.entries(for: .file(path: "storage/logs/laravel.log"))
#expect(Array(entries[4...6]) == [
.separator,
.action(.tailLog("storage/logs/laravel.log"), label: "Tail log"),
.separator,
])
#expect(hasTailLog(.file(path: "LOGS/Error.LOG")))
}
@Test func otherFilesAndFoldersDoNot() {
#expect(!hasTailLog(.file(path: "src/app.php")))
#expect(!hasTailLog(.file(path: "notes.md")))
#expect(!hasTailLog(.file(path: "catalog")))
#expect(!hasTailLog(.directory(path: "storage/logs.log")))
#expect(!hasTailLog(.gitRow(path: "laravel.log", isDeleted: false)))
}
}