Skip to content

Commit

Permalink
Refactor and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
eahlberg committed Feb 2, 2025
1 parent b2edba8 commit f9b94c7
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 13 deletions.
2 changes: 1 addition & 1 deletion lib/Tete/Timer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ list :: Connection -> IO ()
list conn = do
timers <- getTimers conn
now <- getCurrentTime
putStrLn $ PrettyPrint.tasksText now timers
tputStrLn $ PrettyPrint.tasksText now timers

checkAndRunMigrations :: Connection -> IO ()
checkAndRunMigrations conn =
Expand Down
4 changes: 2 additions & 2 deletions lib/Tete/Timer/PrettyPrint.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ summaryText now tasks =
where
total = sum $ Stats.taskTotalTime now <$> tasks

tasksText :: UTCTime -> [Task] -> String
tasksText :: UTCTime -> [Task] -> Text
tasksText now tasks =
render $ vsep 1 left [taskBoxes now tasks, summaryText now tasks]
pack $ render $ vsep 1 left [taskBoxes now tasks, summaryText now tasks]

taskBoxes :: UTCTime -> [Task] -> Box
taskBoxes now = vsep 1 left . fmap (taskBox now)
Expand Down
2 changes: 1 addition & 1 deletion lib/Tete/Timer/Stats.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ timerTotalTime now Timer {..} =
in diffUTCTime stopTime timerStartTime

timersTotalTime :: UTCTime -> [Timer] -> NominalDiffTime
timersTotalTime now timers = sum $ timerTotalTime now <$> timers
timersTotalTime now = sum . fmap (timerTotalTime now)

taskTotalTime :: UTCTime -> Task -> NominalDiffTime
taskTotalTime now task = timersTotalTime now timers
Expand Down
45 changes: 36 additions & 9 deletions test/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,39 +20,66 @@ jan1 = UTCTime (fromGregorian 2020 1 1)
jan2 :: DiffTime -> UTCTime
jan2 = UTCTime (fromGregorian 2020 1 2)

jan3 :: DiffTime -> UTCTime
jan3 = UTCTime (fromGregorian 2020 1 3)

main :: IO ()
main = hspec $ do
describe "taskText" $ do
it "should return data for stopped timers" $ do
let timer =
let timer1 =
Timer
{ timerId = 2,
timerStartTime = jan1 0,
timerStopTime = Just $ jan1 60,
timerCreatedAt = dummyTime,
timerUpdatedAt = Nothing,
timerDescription = Just "stopped timer"
timerDescription = Just "first timer"
}

let task =
let task1 =
Task
{ taskId = 1,
taskName = "task name",
taskName = "first task",
taskCreatedAt = jan1 0,
taskUpdatedAt = Nothing,
taskPeriods = Set.fromList [timer]
taskPeriods = Set.fromList [timer1]
}

let timer2 =
Timer
{ timerId = 2,
timerStartTime = jan3 0,
timerStopTime = Just $ jan3 3600,
timerCreatedAt = dummyTime,
timerUpdatedAt = Nothing,
timerDescription = Just "second timer"
}

let task2 =
Task
{ taskId = 1,
taskName = "second task",
taskCreatedAt = jan1 0,
taskUpdatedAt = Nothing,
taskPeriods = Set.fromList [timer2]
}

let now = dummyTime
let actual = PrettyPrint.tasksText now [task]
let actual = PrettyPrint.tasksText now [task1, task2]

actual `shouldSatisfy` isInfixOf "task name"
actual `shouldSatisfy` isInfixOf "first task"
actual `shouldSatisfy` isInfixOf "second task"

actual `shouldSatisfy` isInfixOf "2020-01-01 00:00"
actual `shouldSatisfy` isInfixOf "2020-01-01 00:01"
actual `shouldSatisfy` isInfixOf "stopped timer"
actual `shouldSatisfy` isInfixOf "first timer"

actual `shouldSatisfy` isInfixOf "2020-01-03 00:00"
actual `shouldSatisfy` isInfixOf "2020-01-03 01:00"
actual `shouldSatisfy` isInfixOf "second timer"

actual `shouldSatisfy` isInfixOf "Total time: 1min"
actual `shouldSatisfy` isInfixOf "Total time: 1h 1min"

it "should return data for stopped and running timers" $ do
let stoppedTimer =
Expand Down

0 comments on commit f9b94c7

Please sign in to comment.