Files
helder/Tests/HelderUITests/State/WorkspaceModelTests.swift
T
2026-10-30 13:21:45 +01:00

837 lines
36 KiB
Swift

import CoreGraphics
import Foundation
import HelderCore
import HelderServices
@testable import HelderUI
import Testing
@MainActor
@Suite struct WorkspaceModelLifecycleTests {
@Test func startingAProjectLoadsEverythingOnceAndWatchesIt() async {
let harness = WorkspaceHarness(git: snapshot([("a.php", .modified, false)]))
await harness.start()
#expect(harness.model.isReady)
#expect(harness.config.resolves == 1)
#expect(harness.git.loads == 1)
#expect(harness.files.treeLoads == 1)
#expect(harness.model.sourceControl?.state.changes.map(\.path) == ["a.php"])
#expect(harness.model.explorer?.tree != nil)
#expect(harness.watchers.last?.root == harness.root)
#expect(harness.recents.projects.first?.path == harness.root.path)
#expect(harness.model.sourceControl?.isPolling == true)
}
@Test func startingTwiceDoesNotLoadTwice() async {
let harness = WorkspaceHarness()
await harness.start()
await harness.start()
#expect(harness.git.loads == 1)
}
@Test func theSessionIsRestoredOnlyWhenTheConfigSaysSo() async {
let session = FakeSession()
session.sessions["/fake/project"] = SessionState(active: "README.md")
var config = HelderConfig.defaults
config.session.restoreOnLaunch = false
let off = WorkspaceHarness(config: config, contents: ["README.md": "# hi"], session: session)
await off.start()
#expect(off.model.editor?.activePath == nil)
let on = WorkspaceHarness(contents: ["README.md": "# hi"], session: session)
await on.start()
#expect(on.model.editor?.activePath == "README.md")
#expect(on.model.editor?.text(for: "README.md") == "# hi")
}
@Test func theConfigReachesEveryStore() async {
var config = HelderConfig.defaults
config.git.confirmDiscard = false
config.git.defaultDiffMode = .diff
config.editor.tabSize = 2
let harness = WorkspaceHarness(config: config)
await harness.start()
#expect(harness.model.sourceControl?.config.confirmDiscard == false)
#expect(harness.model.editor?.defaultMode == .diff)
#expect(harness.model.editor?.editorConfig.tabSize == 2)
#expect(harness.model.terminal.isConfigured)
}
@Test func aConfigChangeIsReappliedEverywhere() async {
let harness = WorkspaceHarness()
await harness.start()
var changed = HelderConfig.defaults
changed.ai.command = "other-agent"
changed.git.confirmDiscard = false
changed.editor.tabSize = 8
harness.config.set(changed)
await harness.model.handle(.config)
#expect(harness.model.config.ai.command == "other-agent")
#expect(harness.model.terminal.config.ai.command == "other-agent")
#expect(harness.model.sourceControl?.config.confirmDiscard == false)
#expect(harness.model.editor?.editorConfig.tabSize == 8)
}
@Test func withoutAProjectTheWindowIsTheLauncher() async {
let recents = [RecentProject(path: "/code/a", name: "a")]
let harness = WorkspaceHarness(root: nil, recents: recents)
await harness.start()
#expect(harness.model.root == nil)
#expect(harness.model.isReady)
#expect(harness.model.launcher.recents == recents)
#expect(harness.git.loads == 0)
#expect(harness.watchers.made.isEmpty)
}
@Test func openingAProjectFromTheLauncherSwitchesTheWindow() async {
let harness = WorkspaceHarness(root: nil)
await harness.start()
let next = URL(fileURLWithPath: "/fake/other")
await harness.model.openProject(next)
#expect(harness.model.root == next)
#expect(harness.model.name == "other")
#expect(harness.model.editor != nil)
#expect(harness.git.loads == 1)
#expect(harness.recents.projects.map(\.path) == [next.path])
}
@Test func switchingProjectsEndsTheOldProjectCleanly() async {
let harness = WorkspaceHarness(contents: ["a.php": "<?php"])
await harness.start()
harness.model.open("a.php")
await harness.model.settle()
let firstWatcher = harness.watchers.last
await harness.model.openProject(URL(fileURLWithPath: "/fake/other"))
#expect(firstWatcher?.stopped == true)
#expect(harness.watchers.made.count == 2)
#expect(harness.agent.terminations == 1)
#expect(harness.session.sessions["/fake/project"]?.active == "a.php")
}
@Test func closingTheWindowSavesAndEndsTheTerminals() async {
let harness = WorkspaceHarness(contents: ["a.php": "<?php"])
await harness.start()
harness.model.open("a.php")
harness.model.windowWillClose()
await harness.model.settle()
#expect(harness.agent.terminations == 1)
#expect(harness.session.sessions["/fake/project"]?.active == "a.php")
#expect(harness.model.sourceControl?.isPolling == false)
}
@Test func reopeningTheSameProjectOnlyClosesTheLauncher() async {
let harness = WorkspaceHarness()
await harness.start()
harness.model.perform(.showRecentProjects)
await harness.model.openProject(harness.root)
#expect(harness.model.overlay.current == .none)
#expect(harness.git.loads == 1)
}
@Test func theClassListLoadsAfterLaunch() async {
let harness = WorkspaceHarness()
harness.symbols.declared = ["User"]
await harness.start()
#expect(harness.model.editor?.knownSymbols == ["User"])
}
}
@MainActor
@Suite struct WorkspaceModelRefreshTests {
@Test func aBurstOfFileEventsBecomesOneRefresh() async {
let harness = WorkspaceHarness()
await harness.start()
for _ in 0..<5 { await harness.model.handle(.project) }
await harness.model.handle(.git)
await harness.model.settle()
#expect(harness.git.loads == 2)
#expect(harness.files.treeLoads == 2)
#expect(harness.symbols.invalidations == 1)
}
@Test func aProjectEventRereadsOpenFilesAndTheClassList() async {
let harness = WorkspaceHarness(contents: ["a.php": "one"])
await harness.start()
harness.model.open("a.php")
await harness.model.settle()
harness.files.set("a.php", "two")
harness.symbols.declared = ["Order"]
await harness.model.handle(.project)
await harness.model.settle()
#expect(harness.model.editor?.text(for: "a.php") == "two")
#expect(harness.model.editor?.knownSymbols == ["Order"])
}
@Test func aProjectEventKeepsUnsavedEdits() async {
let harness = WorkspaceHarness(contents: ["a.php": "one"])
await harness.start()
harness.model.open("a.php")
await harness.model.settle()
harness.model.editor?.edit("typing")
harness.files.set("a.php", "agent")
await harness.model.handle(.project)
await harness.model.settle()
#expect(harness.model.editor?.text(for: "a.php") == "typing")
}
@Test func aGitEventRefreshesOnlyGit() async {
let harness = WorkspaceHarness()
await harness.start()
await harness.model.handle(.git)
await harness.model.settle()
#expect(harness.git.loads == 2)
#expect(harness.files.treeLoads == 1)
}
@Test func watcherEventsArriveFromAnyThread() async throws {
let harness = WorkspaceHarness()
await harness.start()
nonisolated(unsafe) let watcher = try #require(harness.watchers.last)
await Task.detached { watcher.emit(.git) }.value
#expect(await eventually { harness.git.loads == 2 })
}
@Test func commandRLetsTheDiskWinAndFlashes() async {
let harness = WorkspaceHarness(contents: ["a.php": "one"])
await harness.start()
harness.model.open("a.php")
await harness.model.settle()
harness.model.editor?.edit("typing")
harness.files.set("a.php", "agent")
await harness.perform(.refresh)
#expect(harness.model.refreshTick == 1)
#expect(harness.model.editor?.text(for: "a.php") == "agent")
#expect(harness.git.loads == 2)
#expect(harness.files.treeLoads == 2)
}
@Test func openingTheGitViewRefreshesGitOnce() async {
let harness = WorkspaceHarness()
await harness.start()
harness.model.perform(.toggleGit)
harness.model.perform(.focusCommitMessage)
await harness.model.settle()
#expect(harness.git.loads == 2)
}
@Test func savingAFileRefreshesGit() async {
let harness = WorkspaceHarness(contents: ["a.php": "one"])
await harness.start()
harness.model.open("a.php")
await harness.model.settle()
harness.model.editor?.edit("two")
await harness.perform(.save)
#expect(harness.files.writes.map(\.text) == ["two"])
#expect(harness.git.loads == 2)
#expect(harness.toastTitles == ["Saved"])
}
}
@MainActor
@Suite struct WorkspaceModelActionTests {
@Test func openingAFileRevealsItAndWidensTheEditor() async {
let harness = WorkspaceHarness(contents: ["src/Http/Kernel.php": "a\nb\nc\nd"])
await harness.start()
await harness.perform(.open("src/Http/Kernel.php", OpenOptions(line: 4)))
#expect(harness.model.editor?.activePath == "src/Http/Kernel.php")
#expect(harness.model.editor?.cursor?.line == 4)
#expect(harness.model.explorer?.expanded == ["src", "src/Http"])
#expect(harness.model.explorer?.selectedPath == "src/Http/Kernel.php")
#expect(harness.model.layout.focusZone == .editor)
}
@Test func aTooBigFileIsSelectedButNotOpened() async {
let harness = WorkspaceHarness()
await harness.start()
await harness.perform(.open("composer.lock", OpenOptions()))
#expect(harness.model.editor?.activePath == nil)
#expect(harness.model.editor?.tooBig?.path == "composer.lock")
#expect(harness.model.explorer?.selectedPath == "composer.lock")
}
@Test func copyReferenceUsesThePasteboardAndToastsOnce() async {
let harness = WorkspaceHarness()
await harness.start()
harness.model.perform(.showContextMenu(.file(path: "a/b.php"), anchor: .zero))
await harness.perform(.context(.copyReference("a/b.php")))
#expect(harness.system.copied == ["a/b.php"])
#expect(harness.model.overlay.current == .none)
#expect(harness.toastTitles == ["Copied reference"])
}
@Test func deleteAsksFirstThenForgetsTheFile() async {
let harness = WorkspaceHarness(contents: ["README.md": "# hi"])
await harness.start()
harness.model.open("README.md")
await harness.perform(.context(.delete("README.md", isDirectory: false)))
guard case let .confirm(request) = harness.model.overlay.current else {
Issue.record("expected a confirm overlay")
return
}
#expect(request.title == "Delete file?")
#expect(harness.files.deleted.isEmpty)
await harness.perform(.confirmPending)
#expect(harness.model.overlay.current == .none)
#expect(harness.files.deleted == ["README.md"])
#expect(harness.model.editor?.activePath == nil)
#expect(harness.toastTitles == ["Deleted file"])
}
@Test func discardAsksUnderTheConfigThenReloadsTheFile() async {
let harness = WorkspaceHarness(git: snapshot([("a.php", .modified, false)]), contents: ["a.php": "edited"])
await harness.start()
harness.model.open("a.php")
await harness.model.settle()
await harness.perform(.context(.gitDiscard(["a.php"])))
guard case let .confirm(request) = harness.model.overlay.current else {
Issue.record("expected a confirm overlay")
return
}
#expect(request.confirmLabel == "Discard")
#expect(harness.git.executed.isEmpty)
harness.files.set("a.php", "committed")
await harness.perform(.confirmPending)
#expect(harness.git.executed == [["restore", "--", "a.php"]])
#expect(harness.model.editor?.text(for: "a.php") == "committed")
}
@Test func stagingRunsGitStraightAway() async {
let harness = WorkspaceHarness(git: snapshot([("a.php", .modified, false), ("b.php", .modified, false)]))
await harness.start()
await harness.perform(.stage(["a.php"]))
#expect(harness.git.executed == [["add", "--", "a.php"]])
#expect(harness.model.overlay.current == .none)
}
@Test func revealInFinderResolvesAgainstTheProject() async {
let harness = WorkspaceHarness()
await harness.start()
await harness.perform(.context(.revealInFinder("")))
await harness.perform(.context(.revealInFinder("src/app.php")))
#expect(harness.system.revealed.map(\.path) == [harness.root.path, harness.root.appendingPathComponent("src/app.php").path])
}
@Test func aNewFileFromThePromptIsCreatedAndOpened() async {
let harness = WorkspaceHarness()
await harness.start()
harness.model.perform(.showContextMenu(.directory(path: "src"), anchor: CGPoint(x: 10, y: 20)))
await harness.perform(.context(.newFile(inDirectory: "src")))
guard case let .namePrompt(request) = harness.model.overlay.current else {
Issue.record("expected a name prompt")
return
}
#expect(request.anchor == CGPoint(x: 10, y: 20))
await harness.perform(.submitName("Router.php"))
#expect(harness.files.created == ["src/Router.php"])
#expect(harness.model.editor?.activePath == "src/Router.php")
#expect(harness.toastTitles == ["Created file"])
}
@Test func renamingCarriesTheOpenFileOver() async {
let harness = WorkspaceHarness(contents: ["src/app.php": "<?php"])
await harness.start()
harness.model.open("src/app.php")
await harness.perform(.context(.rename("src/app.php", isDirectory: false)))
guard case let .namePrompt(request) = harness.model.overlay.current else {
Issue.record("expected a name prompt")
return
}
#expect(request.initialName == "app.php")
await harness.perform(.submitName("main.php"))
#expect(harness.model.editor?.activePath == "src/main.php")
#expect(harness.model.explorer?.selectedPath == "src/main.php")
#expect(harness.toastTitles == ["Renamed file"])
}
@Test func commitAndPushToastOnce() async {
let harness = WorkspaceHarness(git: snapshot([("a.php", .modified, true)]))
await harness.start()
harness.model.sourceControl?.setCommitMessage("fix it")
await harness.perform(.commit)
#expect(harness.git.executed == [["commit", "-m", "fix it"]])
await harness.perform(.push)
#expect(harness.git.executed.last == ["push"])
#expect(harness.toastTitles == ["Committed 1 file", "Pushed to origin/main"])
}
@Test func commitWithoutAMessageStillAsksGitThenFocusesTheMessage() async {
let harness = WorkspaceHarness(git: snapshot([("a.php", .modified, true)]))
await harness.start()
harness.git.setRun(FakeGitRun(exitCode: 1, lines: [("Aborting commit due to empty commit message.", true)]), forCommand: "commit")
await harness.perform(.commit)
#expect(harness.git.executed == [["commit", "-m", ""]])
#expect(harness.model.focusRequest?.target == .commitMessage)
#expect(harness.toastTitles == ["Commit failed"])
}
@Test func theRowMenuOpensTheFileAndClosesTheGitView() async {
let harness = WorkspaceHarness(git: snapshot([("a.php", .modified, false)]), contents: ["a.php": "<?php"])
await harness.start()
harness.model.perform(.toggleGit)
await harness.model.settle()
harness.model.perform(.showContextMenu(.gitRow(path: "a.php", isDeleted: false), anchor: CGPoint(x: 10, y: 20)))
await harness.perform(.context(.openFile("a.php")))
#expect(!harness.model.layout.isGitViewOpen)
#expect(harness.model.overlay.current == .none)
#expect(harness.model.editor?.activePath == "a.php")
}
@Test func thePassOnPromptInsertsIntoTheAgent() async {
let harness = WorkspaceHarness()
await harness.start()
harness.model.perform(.context(.passOnReference("a.php:3", code: nil)))
await harness.perform(.passToAgent(text: "a.php:3", reference: "a.php:3"))
#expect(harness.agent.sent.count == 1)
#expect(harness.model.overlay.current == .none)
#expect(harness.toastTitles == ["Passed to agent"])
}
@Test func bouwenInsertsTheCommandIntoTheAgent() async {
let harness = WorkspaceHarness()
await harness.start()
await harness.perform(.context(.bouwen("2026-10-02-001-x")))
#expect(harness.agent.sent == [BracketedPaste.wrap("/bouwen 2026-10-02-001-x", composerHasText: false)])
#expect(harness.toastTitles == ["Passed to agent"])
}
@Test func aDirtyTabAsksBeforeItCloses() async {
let harness = WorkspaceHarness(contents: ["a.php": "one"])
await harness.start()
harness.model.open("a.php")
await harness.model.settle()
harness.model.editor?.edit("two")
harness.system.unsavedCloseChoice = .cancel
await harness.perform(.closeFileOrWindow)
#expect(harness.system.unsavedCloseQuestions == ["a.php"])
#expect(harness.model.editor?.activePath == "a.php")
harness.system.unsavedCloseChoice = .discard
await harness.perform(.closeFileOrWindow)
#expect(harness.model.editor?.activePath == nil)
}
@Test func cancellingTheUnsavedPromptLeavesTheExplorerAndFocusAlone() async {
let harness = WorkspaceHarness(contents: ["a.php": "one", "b.php": "b"])
await harness.start()
harness.model.open("a.php")
await harness.model.settle()
harness.model.editor?.edit("two")
harness.model.layout.setFocusZone(.terminal)
harness.system.unsavedCloseChoice = .cancel
harness.model.open("b.php")
await harness.model.settle()
#expect(harness.system.unsavedCloseQuestions == ["a.php"])
#expect(harness.model.editor?.activePath == "a.php")
#expect(harness.model.explorer?.selectedPath == "a.php")
#expect(harness.model.layout.focusZone == .terminal)
}
@Test func aFailedSaveLeavesTheExplorerOnTheDirtyFile() async {
let harness = WorkspaceHarness(contents: ["a.php": "one", "b.php": "b"])
await harness.start()
harness.model.open("a.php")
await harness.model.settle()
harness.model.editor?.edit("two")
harness.system.unsavedCloseChoice = .save
harness.files.failWrites = true
harness.model.open("b.php")
await harness.model.settle()
#expect(harness.model.editor?.activePath == "a.php")
#expect(harness.model.explorer?.selectedPath == "a.php")
}
@Test func savingOnTheUnsavedPromptThenRevealsTheNewFile() async {
let harness = WorkspaceHarness(contents: ["a.php": "one", "b.php": "b"])
await harness.start()
harness.model.open("a.php")
await harness.model.settle()
harness.model.editor?.edit("two")
harness.system.unsavedCloseChoice = .save
harness.model.open("b.php")
await harness.model.settle()
#expect(harness.files.current("a.php") == "two")
#expect(harness.model.editor?.activePath == "b.php")
#expect(harness.model.explorer?.selectedPath == "b.php")
}
@Test func commandWWithoutAnOpenFileClosesTheWindow() async {
let harness = WorkspaceHarness()
await harness.start()
var closed = false
harness.model.hooks.closeWindow = { closed = true }
harness.model.perform(.closeFileOrWindow)
#expect(closed)
}
}
@MainActor
@Suite struct WorkspaceModelFocusTests {
@Test func focusCommandsRequestFirstResponder() async {
let harness = WorkspaceHarness()
await harness.start()
harness.model.perform(.focus(.explorer))
#expect(harness.model.layout.activePanel == .explorer)
#expect(harness.model.focusRequest?.target == .panel(.explorer))
harness.model.perform(.focusNeighbour(step: 1))
#expect(harness.model.focusRequest?.target == .panel(.editor))
}
@Test func commandCOpensTheGitViewOnItsMessageField() async {
let harness = WorkspaceHarness()
await harness.start()
harness.model.perform(.focusCommitMessage)
#expect(harness.model.layout.isGitViewOpen)
#expect(harness.model.focusRequest?.target == .commitMessage)
harness.model.focusTaken(.commitMessage)
harness.model.perform(.focusCommitMessage)
#expect(harness.model.layout.isGitViewOpen)
#expect(harness.model.focusRequest?.target == .commitMessage)
}
@Test func commandGTogglesTheGitViewAndClosingGivesTheKeyboardBack() async {
let harness = WorkspaceHarness()
await harness.start()
harness.model.perform(.focus(.terminal))
harness.model.perform(.toggleGit)
#expect(harness.model.layout.isGitViewOpen)
#expect(harness.model.focusRequest?.target == .commitMessage)
#expect(harness.model.layout.visibility == .all)
harness.model.perform(.toggleGit)
#expect(!harness.model.layout.isGitViewOpen)
#expect(harness.model.focusRequest?.target == .panel(.terminal))
}
@Test func closingTheGitViewWithNoActiveColumnDropsTheFieldRequest() async {
let harness = WorkspaceHarness()
await harness.start()
harness.model.perform(.toggleGit)
harness.model.perform(.closeGitView)
#expect(!harness.model.layout.isGitViewOpen)
#expect(harness.model.focusRequest == nil)
harness.model.perform(.closeGitView)
#expect(!harness.model.layout.isGitViewOpen)
}
@Test func paneTogglesLeaveTheGitViewAlone() async {
let harness = WorkspaceHarness()
await harness.start()
harness.model.perform(.toggleGit)
harness.model.perform(.togglePane(.explorer))
#expect(!harness.model.layout.isVisible(.explorer))
#expect(harness.model.layout.isGitViewOpen)
}
@Test func aPaneToggleHidesItsColumnAndAHiddenColumnTakesNoFocus() async {
let harness = WorkspaceHarness()
await harness.start()
harness.model.perform(.focus(.terminal))
harness.model.perform(.togglePane(.terminal))
#expect(!harness.model.layout.isVisible(.terminal))
#expect(harness.model.layout.activePanel == nil)
harness.model.perform(.focus(.editor))
harness.model.perform(.focus(.terminal))
#expect(harness.model.layout.activePanel == .editor)
#expect(harness.model.focusRequest?.target == .panel(.editor))
harness.model.perform(.togglePane(.terminal))
#expect(harness.model.layout.isVisible(.terminal))
}
@Test func rowCursorKeysDriveOnlyTheExplorer() async {
let harness = WorkspaceHarness(contents: ["README.md": "# hi"])
await harness.start()
let before = harness.model.explorer?.selectedPath
harness.model.perform(.focus(.editor))
harness.model.perform(.moveRowCursor(1))
#expect(harness.model.explorer?.selectedPath == before)
harness.model.perform(.focus(.explorer))
harness.model.perform(.moveRowCursor(10))
harness.model.perform(.moveRowCursor(10))
#expect(harness.model.explorer?.selectedPath == "README.md")
await harness.perform(.openSelectedRow)
#expect(harness.model.editor?.activePath == "README.md")
}
@Test func commandRightOnARowOpensItsMenuBesideTheColumn() async {
let harness = WorkspaceHarness()
await harness.start()
harness.model.perform(.focus(.explorer))
harness.model.perform(.moveRowCursor(1))
harness.model.perform(.openRowMenu)
guard case let .contextMenu(menu) = harness.model.overlay.current else {
Issue.record("expected a context menu")
return
}
#expect(menu.target == .directory(path: "src"))
#expect(Double(menu.anchor.x) == harness.model.layout.span(of: .explorer)?.upperBound)
}
@Test func comingBackToTheWindowFlashesBranchAndProjectButLaunchDoesNot() async {
let harness = WorkspaceHarness()
await harness.start()
harness.model.windowDidBecomeKey()
#expect(!harness.model.isFocusFlashVisible)
harness.model.windowDidResignKey()
harness.model.windowDidBecomeKey()
#expect(harness.model.isFocusFlashVisible)
#expect(await eventually { !harness.model.isFocusFlashVisible })
}
}
@MainActor
@Suite struct WorkspaceModelOverlayTests {
@Test func escClosesOnlyTheOneOpenOverlay() {
let harness = WorkspaceHarness()
harness.model.perform(.showShortcuts)
harness.model.perform(.showSearch(seed: "x"))
#expect(harness.model.overlay.current == .search(seed: "x"))
harness.model.perform(.dismissOverlay)
#expect(harness.model.overlay.current == .none)
}
@Test func aContextMenuActionReplacesTheMenuWithItsPrompt() {
let harness = WorkspaceHarness()
harness.model.perform(.showContextMenu(.file(path: "src/a.php"), anchor: CGPoint(x: 300, y: 120)))
harness.model.perform(.context(.passOnReference("src/a.php", code: nil)))
#expect(harness.model.overlay.current == .passOn(PassOnRequest(reference: "src/a.php", anchor: CGPoint(x: 300, y: 120))))
}
@Test func theDiffOverlayGivesBackTheModeItCovered() async {
let harness = WorkspaceHarness(git: snapshot([("a.md", .modified, false)]), contents: ["a.md": "b"])
await harness.start()
let editor = harness.model.editor
harness.model.open("a.md")
editor?.setMode(.preview, for: "a.md")
editor?.setMode(.diff, for: "a.md")
#expect(harness.model.overlay.current == .diff(DiffOverlayRequest(path: "a.md", side: nil)))
harness.model.perform(.dismissOverlay)
#expect(editor?.mode(for: "a.md") == .preview)
}
@Test func showDiffOpensTheDiffOfTheOpenFileAtThatLine() async {
let harness = WorkspaceHarness(git: snapshot([("a.php", .modified, false)]), contents: ["a.php": "b"])
await harness.start()
harness.model.open("a.php")
await harness.perform(.context(.showDiff("a.php", line: 3)))
#expect(harness.model.overlay.current == .diff(DiffOverlayRequest(path: "a.php", side: nil, focusLine: 3)))
}
@Test func theRecentProjectListLeavesOutTheOpenProject() async {
let harness = WorkspaceHarness(recents: [RecentProject(path: "/elsewhere", name: "elsewhere")])
await harness.start()
await harness.perform(.showRecentProjects)
#expect(harness.model.overlay.current == .launcher)
#expect(harness.model.otherRecentProjects.map(\.path) == ["/elsewhere"])
}
@Test func activatingTheNewProjectRowAsksForAFolder() async {
let harness = WorkspaceHarness(root: nil)
await harness.start()
let folder = URL(fileURLWithPath: "/fake/chosen")
harness.system.chosenFolder = folder
await harness.perform(.launcherActivate)
#expect(harness.model.root == folder)
}
@Test func searchTakesTheTypedSeed() {
let harness = WorkspaceHarness()
harness.model.perform(.showSearch(seed: "typed"))
#expect(harness.model.overlay.current == .search(seed: "typed"))
}
}
@MainActor
@Suite struct TerminalContextMenuModelTests {
@Test func aRightClickInATerminalOpensTheAppMenuAtThePointer() {
let harness = WorkspaceHarness()
harness.model.terminal.contextMenuRequested?(.agent, true, CGPoint(x: 900, y: 300))
guard case let .contextMenu(menu) = harness.model.overlay.current else {
Issue.record("no context menu")
return
}
#expect(menuLabels(menu) == ["Copy", "Paste", "Read aloud selection", "Plak", "Wait what?"])
#expect(menu.header.isEmpty)
#expect(menu.anchor == CGPoint(x: 900, y: 300))
}
@Test func copyPutsTheSelectionOnThePasteboard() async {
let harness = WorkspaceHarness()
harness.agent.selectedText = "npm test"
await harness.perform(.context(.copyTerminalSelection(.agent)))
#expect(harness.system.copied == ["npm test"])
}
@Test func waitWhatInsertsTheSelectionAsAWaitWhatCommand() async {
let harness = WorkspaceHarness()
harness.agent.selectedText = " the reviewer was unsure\n"
await harness.perform(.context(.waitWhat(.agent)))
#expect(harness.agent.sent == [BracketedPaste.wrap("/wait-what `the reviewer was unsure`", composerHasText: false)])
#expect(harness.toastTitles == ["Passed to agent"])
}
@Test func readAloudSelectionReadsTheSelectionAsProse() async {
let harness = WorkspaceHarness()
harness.agent.selectedText = " Run **npm test** first.\n"
await harness.perform(.context(.readAloudTerminalSelection(.agent)))
#expect(harness.speech.spoken == ["Run npm test first."])
#expect(harness.model.readAloud.isReading)
}
@Test func readAloudSelectionReadsTheShellSelection() async {
let harness = WorkspaceHarness()
let shell = FakeTerminal()
harness.model.terminal.attach(shell, as: .shell)
shell.selectedText = "```\nls -la\n```"
await harness.perform(.context(.readAloudTerminalSelection(.shell)))
#expect(harness.speech.spoken == ["```\nls -la\n```"])
}
@Test func readAloudSelectionWithBlankTextSaysSo() async {
let harness = WorkspaceHarness()
harness.agent.selectedText = " \n"
await harness.perform(.context(.readAloudTerminalSelection(.agent)))
#expect(harness.speech.spoken.isEmpty)
#expect(harness.toastTitles == ["Nothing to read"])
}
@Test func plakSendsTheTrimmedSelection() async {
let harness = WorkspaceHarness()
harness.agent.selectedText = " npm test\n"
await harness.perform(.context(.plakTerminalSelection(.agent)))
#expect(harness.system.plakked == ["npm test"])
}
@Test func plakSendsTheShellSelection() async {
let harness = WorkspaceHarness()
let shell = FakeTerminal()
harness.model.terminal.attach(shell, as: .shell)
shell.selectedText = "ls -la"
await harness.perform(.context(.plakTerminalSelection(.shell)))
#expect(harness.system.plakked == ["ls -la"])
}
@Test func plakWithBlankSelectionSaysSo() async {
let harness = WorkspaceHarness()
harness.agent.selectedText = " \n"
await harness.perform(.context(.plakTerminalSelection(.agent)))
#expect(harness.system.plakked.isEmpty)
#expect(harness.toastTitles == ["Nothing to send"])
}
@Test func plakSendsTheEditorSelection() async {
let harness = WorkspaceHarness()
await harness.perform(.context(.plak("\n $a = 1;\n")))
#expect(harness.system.plakked == ["$a = 1;"])
}
@Test func plakWithBlankEditorTextSaysSo() async {
let harness = WorkspaceHarness()
await harness.perform(.context(.plak(" \n ")))
#expect(harness.system.plakked.isEmpty)
#expect(harness.toastTitles == ["Nothing to send"])
}
@Test func pasteSendsThePasteboardToThatTerminal() async {
let harness = WorkspaceHarness()
let shell = FakeTerminal()
harness.model.terminal.attach(shell, as: .shell)
harness.system.clipboard = "git status"
await harness.perform(.context(.pasteIntoTerminal(.shell)))
#expect(shell.pasted == ["git status"])
#expect(harness.agent.pasted.isEmpty)
}
@Test func menuIconsUseThePlainTint() {
#expect(ContextMenuOverlay.tint(for: .copyTerminalSelection(.agent)) == .plain)
#expect(ContextMenuOverlay.tint(for: .pasteIntoTerminal(.shell)) == .plain)
#expect(ContextMenuOverlay.symbol(for: .copyTerminalSelection(.agent)) == "doc.on.doc")
#expect(ContextMenuOverlay.symbol(for: .pasteIntoTerminal(.agent)) == "doc.on.clipboard")
#expect(ContextMenuOverlay.tint(for: .readAloudTerminalSelection(.agent)) == .plain)
#expect(ContextMenuOverlay.symbol(for: .readAloudTerminalSelection(.shell)) == "speaker.wave.2")
#expect(ContextMenuOverlay.tint(for: .plakTerminalSelection(.agent)) == .plain)
#expect(ContextMenuOverlay.tint(for: .plak("x")) == .plain)
#expect(ContextMenuOverlay.symbol(for: .plakTerminalSelection(.shell)) == "note.text.badge.plus")
#expect(ContextMenuOverlay.symbol(for: .plak("x")) == "note.text.badge.plus")
}
@Test func aProjectSwitchKeepsTheTerminalMenuWired() async {
let harness = WorkspaceHarness()
await harness.start()
await harness.model.openProject(URL(fileURLWithPath: "/fake/other"))
#expect(harness.model.terminal.contextMenuRequested != nil)
}
}
@MainActor
@Suite struct ReadAloudModelTests {
@Test func aMarkdownFileIsReadAsPlainText() async {
let harness = WorkspaceHarness(contents: ["README.md": "# Hello\n\nSome **bold** [link](https://x.com)\n\n```\ncode\n```"])
await harness.start()
await harness.perform(.context(.readAloudFile("README.md")))
#expect(harness.speech.spoken == ["Hello.\nSome bold link."])
#expect(harness.model.readAloud.isReading)
}
@Test func theOpenFileIsReadWithItsUnsavedEdits() async {
let harness = WorkspaceHarness(contents: ["README.md": "On disk"])
await harness.start()
harness.model.open("README.md")
await harness.model.settle()
harness.model.editor?.edit("Typed")
await harness.perform(.context(.readAloudFile("README.md")))
#expect(harness.speech.spoken == ["Typed."])
}
@Test func aFileWithoutProseReadsNothingAndSaysSo() async {
let harness = WorkspaceHarness(contents: ["README.md": "```\nonly code\n```"])
await harness.start()
await harness.perform(.context(.readAloudFile("README.md")))
#expect(harness.speech.spoken.isEmpty)
#expect(!harness.model.readAloud.isReading)
#expect(harness.toastTitles == ["Nothing to read"])
}
@Test func aSelectionIsReadAsItIs() async {
let harness = WorkspaceHarness()
await harness.start()
await harness.perform(.context(.readAloudText("let a = 1")))
#expect(harness.speech.spoken == ["let a = 1"])
}
@Test func aDollarSignIsReadAsDollar() async {
let harness = WorkspaceHarness()
await harness.start()
await harness.perform(.context(.readAloudText("echo $HOME costs $5")))
#expect(harness.speech.spoken == ["echo dollar HOME costs $5"])
}
@Test func stopEndsTheReading() async {
let harness = WorkspaceHarness()
await harness.start()
await harness.perform(.context(.readAloudText("words")))
await harness.perform(.stopReading)
#expect(harness.speech.stops == 1)
#expect(!harness.model.readAloud.isReading)
}
@Test func readingToTheEndClearsTheState() async {
let harness = WorkspaceHarness()
await harness.start()
await harness.perform(.context(.readAloudText("words")))
harness.speech.finish()
#expect(!harness.model.readAloud.isReading)
}
@Test func aProjectSwitchStopsTheReading() async {
let harness = WorkspaceHarness()
await harness.start()
await harness.perform(.context(.readAloudText("words")))
await harness.model.openProject(URL(fileURLWithPath: "/fake/other"))
#expect(!harness.model.readAloud.isReading)
#expect(harness.speech.stops == 1)
}
@Test func readAloudUsesTheSpeakerIcon() {
#expect(ContextMenuOverlay.symbol(for: .readAloudFile("README.md")) == "speaker.wave.2")
#expect(ContextMenuOverlay.tint(for: .readAloudText("x")) == .plain)
}
}