packages feed

tricorder-1.1.0.0: test/Unit/Tricorder/Daemon/DispatchSpec.hs

module Unit.Tricorder.Daemon.DispatchSpec (test_Dispatch) where

import Test.Tasty (TestTree, testGroup)
import Test.Tasty.HUnit (assertBool, testCase, (@?=))

import Atelier.Effects.FileWatcher.Event qualified as Event
import Atelier.FileSystem.Path qualified as Path
import Data.Map.Strict qualified as Map
import Data.Set qualified as Set

import Tricorder.Build.Diagnostic (Diagnostic (..))
import Tricorder.Build.DiagnosticLocation (DiagnosticLocation (..))
import Tricorder.Daemon.Dispatch
    ( DispatchAction (..)
    , KnownTargetNames (..)
    , dispatch
    , fileMatchesAnyTarget
    , filterToWatchDirs
    , mergeDiagnostics
    , planActions
    , preserveFailureVisibility
    )
import Tricorder.Daemon.Ghci.LoadResult (LoadResult (..))
import Tricorder.Daemon.Ghci.LoadedModule (LoadedModule (..))
import Tricorder.Daemon.Ghci.Result (collectResult)
import Tricorder.Session.WatchDirs (WatchDirs (..))

import Tricorder.Build.Severity qualified as Severity


test_Dispatch :: TestTree
test_Dispatch =
    testGroup
        "Dispatch"
        [ testGroup "fileMatchesAnyTarget" testFileMatchesAnyTarget
        , testGroup "mergeDiagnostics" testMergeDiagnostics
        , testGroup "filterToWatchDirs" testFilterToWatchDirs
        , testGroup "planActions" testPlanActions
        , testGroup "dispatch" testDispatch
        ]


--------------------------------------------------------------------------------
-- fileMatchesAnyTarget tests
--------------------------------------------------------------------------------

testFileMatchesAnyTarget :: [TestTree]
testFileMatchesAnyTarget =
    [ testCase "matches when the path's uppercase-suffix equals a target" do
        fileMatchesAnyTarget
            (KnownTargetNames (Set.singleton "Tricorder.Version"))
            (Path.rel @"./tricorder/src/Tricorder/Version.hs")
            @?= True
    , testCase "matches a single-segment module" do
        fileMatchesAnyTarget
            (KnownTargetNames (Set.singleton "Main"))
            (Path.rel @"./app/Main.hs")
            @?= True
    , testCase "does not match when no uppercase-suffix equals a target" do
        fileMatchesAnyTarget
            (KnownTargetNames (Set.singleton "Other.Module"))
            (Path.rel @"./tricorder/src/Tricorder/Version.hs")
            @?= False
    , testCase "does not match a lowercase-prefix even if textually contained" do
        fileMatchesAnyTarget
            (KnownTargetNames (Set.singleton "src.Tricorder.Version"))
            (Path.rel @"./tricorder/src/Tricorder/Version.hs")
            @?= False
    , testCase "handles .lhs extension" do
        fileMatchesAnyTarget
            (KnownTargetNames (Set.singleton "Foo.Bar"))
            (Path.rel @"./src/Foo/Bar.lhs")
            @?= True
    , -- GHCi renders a target whose module name is ambiguous across home units
      -- (every executable/test 'Main') as its source path, e.g. "app/Main.hs".
      testCase "matches a path-shaped target on directory-segment boundaries" do
        fileMatchesAnyTarget
            (KnownTargetNames (Set.singleton "app/Main.hs"))
            (Path.rel @"./tricorder/app/Main.hs")
            @?= True
    , testCase "does not match a path-shaped target on a partial segment" do
        fileMatchesAnyTarget
            (KnownTargetNames (Set.singleton "pp/Main.hs"))
            (Path.rel @"./tricorder/app/Main.hs")
            @?= False
    , testCase "does not match a path-shaped target for a different file" do
        fileMatchesAnyTarget
            (KnownTargetNames (Set.singleton "daemon/Main.hs"))
            (Path.rel @"./tricorder/app/Main.hs")
            @?= False
    ]


--------------------------------------------------------------------------------
-- mergeDiagnostics tests
--------------------------------------------------------------------------------

testMergeDiagnostics :: [TestTree]
testMergeDiagnostics =
    [ testCase "retains diagnostics from files not in compiledFiles" do
        -- Foo has an error, Bar has a warning.
        -- Only Foo is recompiled (and fixed). Bar is unchanged, so Bar's
        -- warning must survive.
        let prev = Map.fromList [(Location errPath, [errMsg]), (Location warnPath, [warnMsg])]
            result =
                LoadResult
                    { moduleCount = 2
                    , compiledFiles = Set.singleton errPath
                    , loadedModules = Map.empty
                    , targetNames = []
                    , diagnostics = []
                    }
        let merged = mergeDiagnostics prev result
        Map.lookup (Location warnPath) merged @?= Just [warnMsg]
    , testCase "drops retained errors for files not in compiledFiles" do
        -- Foo's error is stale if Foo was skipped (e.g. blocked behind a failing
        -- upstream module): GHC re-emits errors for every module it attempts.
        -- Warnings in the same file are kept.
        let prev = Map.fromList [(Location errPath, [errMsg, errMsg {severity = Severity.Warning}])]
            result =
                LoadResult
                    { moduleCount = 1
                    , compiledFiles = Set.empty
                    , loadedModules = Map.empty
                    , targetNames = []
                    , diagnostics = []
                    }
        let merged = mergeDiagnostics prev result
        Map.lookup (Location errPath) merged @?= Just [errMsg {severity = Severity.Warning}]
    , testCase "clears diagnostics when a recompiled file now has no issues" do
        let prev = Map.fromList [(Location errPath, [errMsg])]
            result =
                LoadResult
                    { moduleCount = 1
                    , compiledFiles = Set.singleton errPath
                    , loadedModules = Map.empty
                    , targetNames = []
                    , diagnostics = []
                    }
        let merged = mergeDiagnostics prev result
        Map.lookup (Location errPath) merged @?= Nothing
    , testCase "replaces diagnostics for recompiled files" do
        let newErr = errMsg {title = "new error", text = "new error\n"}
            prev = Map.fromList [(Location errPath, [errMsg])]
            result =
                LoadResult
                    { moduleCount = 1
                    , compiledFiles = Set.singleton errPath
                    , loadedModules = Map.empty
                    , targetNames = []
                    , diagnostics = [newErr]
                    }
        let merged = mergeDiagnostics prev result
        Map.lookup (Location errPath) merged @?= Just [newErr]
    , testCase "accumulates diagnostics for newly seen files" do
        let result =
                LoadResult
                    { moduleCount = 1
                    , compiledFiles = Set.singleton warnPath
                    , loadedModules = Map.empty
                    , targetNames = []
                    , diagnostics = [warnMsg]
                    }
        let merged = mergeDiagnostics Map.empty result
        Map.lookup (Location warnPath) merged @?= Just [warnMsg]
    , testGroup
        "when the cycle reports none"
        [ testCase "clears a stale location-less diagnostic" do
            -- <no location info> is never in compiledFiles, so without special
            -- handling it would persist forever. A cycle with no location-less
            -- diagnostic must evict it.
            let noLoc = errMsg {file = Locationless}
                prev = Map.fromList [(noLoc.file, [noLoc])]
                result =
                    LoadResult
                        { moduleCount = 1
                        , compiledFiles = Set.singleton errPath
                        , loadedModules = Map.empty
                        , targetNames = []
                        , diagnostics = []
                        }
            let merged = mergeDiagnostics prev result
            Map.lookup noLoc.file merged @?= Nothing
        ]
    , testCase "refreshes a location-less diagnostic that is still present" do
        let noLoc = errMsg {file = Locationless}
            prev = Map.fromList [(noLoc.file, [noLoc])]
            result =
                LoadResult
                    { moduleCount = 1
                    , compiledFiles = Set.empty
                    , loadedModules = Map.empty
                    , targetNames = []
                    , diagnostics = [noLoc]
                    }
        let merged = mergeDiagnostics prev result
        Map.lookup noLoc.file merged @?= Just [noLoc]
    ]


--------------------------------------------------------------------------------
-- filterToWatchDirs tests
--------------------------------------------------------------------------------

testFilterToWatchDirs :: [TestTree]
testFilterToWatchDirs =
    [ testCase "keeps diagnostics under a watched directory" do
        -- ./src/Foo.hs is what toRelative produces for an absolute project file
        let d = errMsg {file = Location errPath}
        filterToWatchDirs root watchDirs [d] @?= [d]
    , testCase "keeps diagnostics under \".\" watched directory" do
        let d = errMsg {file = Location (Path.Relative (Path.rel @"src/Foo.hs"))}
        filterToWatchDirs root (WatchDirs [root]) [d] @?= [d]
    , testCase "drops diagnostics from outside the project (e.g. Nix store .h files)" do
        let d = errMsg {file = Location (Path.Absolute (Path.abs @"/nix/store/abc123/ghcautoconf.h"))}
        filterToWatchDirs root watchDirs [d] @?= []
    , testCase "drops diagnostics with mangled CPP filenames" do
        -- The ghcid parser produces "In file included from <path>" as the file
        -- field for GCC-style CPP include-chain messages.
        let d = errMsg {file = Location (Path.Relative (Path.rel @"In file included from src/Foo.hs"))}
        filterToWatchDirs root watchDirs [d] @?= []
    , testCase "drops mangled CPP filenames when watchDirs is [\".\"] (project root)" do
        -- With watchDirs=["."], the watch dir resolves to projectRoot itself.
        -- A mangled path joined onto projectRoot would incorrectly start with
        -- projectRoot+"/", so this case requires an explicit guard.
        let d = errMsg {file = Location (Path.Relative (Path.rel @"In file included from src/Foo.hs"))}
        filterToWatchDirs root (WatchDirs [root]) [d] @?= []
    , testCase "passes everything through when watchDirs is empty" do
        let d = errMsg {file = Location (Path.Absolute (Path.abs @"/nix/store/abc123/ghcautoconf.h"))}
        filterToWatchDirs root (WatchDirs []) [d] @?= [d]
    , testCase "works with the '.' fallback watch dir (whole project root)" do
        let d = errMsg {file = Location errPath}
            nixD = errMsg {file = Location (Path.Absolute (Path.abs @"/nix/store/abc123/ghcautoconf.h"))}
        filterToWatchDirs root (WatchDirs [root]) [d, nixD] @?= [d]
    , testGroup
        "when diagnostic has no path it"
        [ testCase "keeps location-less <no location info> errors" do
            -- A home-unit GHC plugin that can't load under --enable-multi-repl
            -- produces a <no location info> error. It has no path to test against a
            -- watch dir, but must survive or the failed build reads as clean.
            let d = errMsg {file = Locationless}
            filterToWatchDirs root watchDirs [d] @?= [d]
        ]
    , testCase "does not treat a real <-prefixed path as a location-less marker" do
        -- isLocationLess requires a closing '>'. A real (if exotic) path that
        -- merely starts with '<' is an ordinary out-of-watch file and must be
        -- dropped, not kept as a build-level marker.
        let d = errMsg {file = Location (Path.Relative (Path.rel @"<generated>/Foo.hs"))}
        filterToWatchDirs root watchDirs [d] @?= []
    , testGroup
        "when its only error is out of watch dirs"
        [ testCase "a failed load does not read as clean" do
            -- collectResult only injects its synthetic failure when no SError is
            -- present. Here GHCi Failed with a single *located* error in a file
            -- outside the watch dirs, so collectResult adds no synthetic — and then
            -- filterToWatchDirs drops the out-of-watch error, leaving nothing. The
            -- Builder pipeline composes preserveFailureVisibility after filtering to
            -- re-attach the failure, so a failed build never survives with zero
            -- diagnostics.
            let reloadOutput =
                    [ "/other/Dep.hs:5:1: error: boom"
                    , "Failed, 0 modules loaded."
                    ]
                result = collectResult root reloadOutput [] []
                filtered = filterToWatchDirs root watchDirs result.diagnostics
            assertBool "expected at least one diagnostic"
                $ not (null (preserveFailureVisibility result.diagnostics filtered))
        ]
    ]
  where
    root = Path.absolute @"/project"
    watchDirs = WatchDirs [Path.absolute @"/project/src"]


testPlanActions :: [TestTree]
testPlanActions =
    [ testCase "no actions plans nothing" do
        planActions [] @?= []
    , testCase "repeated reloads collapse into one" do
        planActions [Reload, Reload, Reload] @?= [Reload]
    , testCase "a reload is kept when it is the only action" do
        planActions [Reload] @?= [Reload]
    , testCase "an add subsumes a reload, whichever came first" do
        planActions [Reload, Add fileA] @?= [Add fileA]
        planActions [Add fileA, Reload] @?= [Add fileA]
    , testCase "an unadd subsumes a reload" do
        planActions [Reload, Unadd "A"] @?= [Unadd "A"]
    , testCase "target changes keep their order and are de-duplicated" do
        planActions [Add fileA, Unadd "B", Reload, Add fileA] @?= [Add fileA, Unadd "B"]
    ]


testDispatch :: [TestTree]
testDispatch =
    [ testCase "removing a loaded module unadds it by name and path" do
        dispatch noTargets (Just loadedA) fileA Event.Removed
            @?= Just (Unadd ("A " <> toText fileA))
    , testCase "removing a target that failed to compile unadds it by path" do
        dispatch (KnownTargetNames (Set.singleton "A")) Nothing fileA Event.Removed
            @?= Just (Unadd (toText fileA))
    , testCase "removing a path-form target that failed to compile unadds it" do
        dispatch (KnownTargetNames (Set.singleton "src/A.hs")) Nothing fileA Event.Removed
            @?= Just (Unadd (toText fileA))
    , testCase "removing a file GHCi never saw does nothing" do
        dispatch noTargets Nothing fileA Event.Removed @?= Nothing
    , testCase "modifying an unknown file adds it" do
        dispatch noTargets Nothing fileA Event.Modified @?= Just (Add fileA)
    ]
  where
    noTargets = KnownTargetNames mempty
    loadedA = LoadedModule {relPath = Path.rel @"./src/A.hs", moduleName = "A"}


--------------------------------------------------------------------------------
-- Helpers
--------------------------------------------------------------------------------

errPath :: Path.Any
errPath = Path.Relative (Path.rel @"./src/Foo.hs")


warnPath :: Path.Any
warnPath = Path.Relative (Path.rel @"./src/Bar.hs")


errMsg :: Diagnostic
errMsg =
    Diagnostic
        { severity = Severity.Error
        , file = Location errPath
        , line = 1
        , col = 1
        , endLine = 1
        , endCol = 5
        , title = "Variable not in scope: foo"
        , text = "Variable not in scope: foo"
        }


warnMsg :: Diagnostic
warnMsg =
    Diagnostic
        { severity = Severity.Warning
        , file = Location warnPath
        , line = 10
        , col = 3
        , endLine = 10
        , endCol = 8
        , title = "Unused import"
        , text = "Unused import"
        }


fileA :: Path.Any
fileA = Path.Relative $ Path.rel @"./src/A.hs"