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.confirmStage = true 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.confirmStage == true) #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": "