module Main (main) where

import Test.HUnit
import NonogramSolverPar (solveSequential)
import NonogramTypes (Nonogram(..))
import Parser (parseNonogram)
import Data.Array (Array, bounds, (!))
import System.IO.Silently (capture)

-- test single puzzle
testPuzzle :: FilePath -> Test
testPuzzle puzzlePath = TestCase $ do
    -- Parse the puzzle
    nonogram <- parseNonogram puzzlePath

    -- Check if a goal is provided
    let expectedSolution = goal nonogram
    if null expectedSolution
        then assertFailure $ "No solution provided in puzzle file: " ++ puzzlePath
        else do
            -- Capture console output
            (actualOutput, _) <- capture (solveSequential puzzlePath)

            -- Clean and compare output
            let actualSolution = cleanOutput actualOutput
            assertEqual ("Mismatch for puzzle: " ++ puzzlePath) expectedSolution actualSolution


-- Helper function to reformat captured output
cleanOutput :: String -> String
cleanOutput = concat . map (filter (`elem` "01")) . lines


-- test cases
tests :: Test
tests = TestList
    [ testPuzzle "test/test_cases/bloop.txt", 
        testPuzzle "test/test_cases/spade.txt" ]


-- Run the tests
main :: IO ()
main = do
    testResults <- runTestTT tests
    print testResults
