refactor: return Either from loadConfig, add Config tests

This commit is contained in:
2026-05-21 10:16:41 -04:00
parent 1d5459ea1e
commit 5765829004
7 changed files with 1348 additions and 10 deletions
+55
View File
@@ -0,0 +1,55 @@
module Roux.ConfigSpec (spec) where
import System.FilePath ((</>))
import System.IO.Temp (withSystemTempDirectory)
import Test.Hspec (Spec, describe, expectationFailure, it, shouldBe)
import Roux.Config
spec :: Spec
spec = describe "loadConfig" $ do
it "returns Nothing config when file doesn't exist" $ do
result <- loadConfig "/tmp/nonexistent-config-file-for-roux-test.yaml"
result `shouldBe` Right (RouxConfig Nothing)
it "parses a minimal config with api_key" $ do
withSystemTempDirectory "roux-test" $ \dir -> do
let path = dir </> "config.yaml"
writeFile path "anthropic:\n api_key: sk-test-123\n"
result <- loadConfig path
case result of
Right (RouxConfig (Just ac)) -> do
acApiKey ac `shouldBe` "sk-test-123"
acBaseUrl ac `shouldBe` Nothing
acModel ac `shouldBe` Nothing
_ -> expectationFailure "Expected Right config"
it "parses a full config with all fields" $ do
withSystemTempDirectory "roux-test" $ \dir -> do
let path = dir </> "config.yaml"
writeFile path "anthropic:\n api_key: sk-test-456\n base_url: http://localhost:11434/v1\n model: local-model\n"
result <- loadConfig path
case result of
Right (RouxConfig (Just ac)) -> do
acApiKey ac `shouldBe` "sk-test-456"
acBaseUrl ac `shouldBe` Just "http://localhost:11434/v1"
acModel ac `shouldBe` Just "local-model"
_ -> expectationFailure "Expected Right config"
it "returns Left for malformed YAML" $ do
withSystemTempDirectory "roux-test" $ \dir -> do
let path = dir </> "config.yaml"
writeFile path "{{{{ invalid yaml }}}}\n"
result <- loadConfig path
case result of
Left _ -> pure () -- expected
Right _ -> expectationFailure "Expected Left error"
it "returns Left when api_key is missing" $ do
withSystemTempDirectory "roux-test" $ \dir -> do
let path = dir </> "config.yaml"
writeFile path "anthropic:\n model: foo\n"
result <- loadConfig path
case result of
Left _ -> pure () -- expected
Right _ -> expectationFailure "Expected Left error"
+4 -1
View File
@@ -3,19 +3,22 @@ module Main (main) where
import Test.Tasty (defaultMain, testGroup)
import Test.Tasty.Hspec (testSpec)
import qualified Roux.ConfigSpec
import qualified Roux.NYTimesSpec
import qualified Roux.ParserSpec
import qualified Roux.SchemaOrgSpec
main :: IO ()
main = do
configTests <- testSpec "Config" Roux.ConfigSpec.spec
parserTests <- testSpec "Parser" Roux.ParserSpec.spec
nytTests <- testSpec "NYTimes" Roux.NYTimesSpec.spec
schemaOrgTests <- testSpec "SchemaOrg" Roux.SchemaOrgSpec.spec
defaultMain $
testGroup
"roux-server"
[ parserTests
[ configTests
, parserTests
, nytTests
, schemaOrgTests
]