import log.{type log, Debug, Entry, Error, Info, Warn} import std/list import std/test.{assert, assert_equal} import std/time fn at :: -> time.Timestamp { time.from_unix_ms 1793887331000 // 2026-11-05T14:02:11.000Z } // ---- levels ------------------------------------------------------------------------ @test fn levels_rank_least_first :: { assert (Debug < Info && Info < Warn && Warn < Error) "debug < info < warn < error" } @test fn every_level_is_read_back_from_its_name :: { assert_equal ( [Debug, Info, Warn, Error] |> list.map fn l { log.level_named (log.name l) } ) [Some(Debug), Some(Info), Some(Warn), Some(Error)] } @test fn a_level_name_ignores_case_and_spaces :: { assert_equal (log.level_named " WARN ") Some(Warn) } @test fn an_unknown_level_name_is_none :: { assert_equal (log.level_named "verbose") None assert_equal (log.level_named "") None } // ---- the line ---------------------------------------------------------------------- @test fn a_line_has_the_time_the_padded_level_the_message_and_the_fields :: { assert_equal (log.line (at ()) Entry(Info, "GET / 200 8ms", [("client", "127.0.0.1")])) "2026-11-05T14:02:11.000Z info GET / 200 8ms client=127.0.0.1" } @test fn the_time_always_has_three_fraction_digits :: { let stamped = fn ms { log.line (time.from_unix_ms ms) Entry(Info, "m", []) } assert_equal (stamped 1793887331007) "2026-11-05T14:02:11.007Z info m" assert_equal (stamped 1793887331250) "2026-11-05T14:02:11.250Z info m" assert_equal (stamped (-1)) "1969-12-31T23:59:59.999Z info m" } @test fn a_line_without_fields_ends_with_the_message :: { assert_equal (log.line (at ()) Entry(Error, "boom", [])) "2026-11-05T14:02:11.000Z error boom" } @test fn a_value_that_would_not_split_cleanly_is_quoted :: { let fields = [ ("reason", "no TLS") , ("empty", "") , ("pair", "a=b") , ("said", "a \"b\"") , ("path", "C:\\x") , ("lines", "one\ntwo") ] assert_equal (log.line (at ()) Entry(Warn, "m", fields)) "2026-11-05T14:02:11.000Z warn m reason=\"no TLS\" empty=\"\" pair=\"a=b\" said=\"a \\\"b\\\"\" path=\"C:\\\\x\" lines=\"one\\ntwo\"" } @test fn a_message_stays_on_one_line :: { assert_equal (log.line (at ()) Entry(Debug, "one\ntwo", [])) "2026-11-05T14:02:11.000Z debug one\\ntwo" } // ---- handlers ---------------------------------------------------------------------- fn chatty :: -> Int / { log.debug "d" [] log.info "i" [("k", "v")] log.warn "w" [] log.error "e" [] 7 } @test fn captured_keeps_the_entries_in_order_with_the_result :: { assert_equal (log.captured chatty) ( 7 , [ Entry(Debug, "d", []) , Entry(Info, "i", [("k", "v")]) , Entry(Warn, "w", []) , Entry(Error, "e", []) ] ) } @test fn at_least_passes_the_entries_at_its_level_and_above_outward :: { let (result, entries) = log.captured fn { log.at_least Warn chatty } assert_equal result 7 assert_equal entries [Entry(Warn, "w", []), Entry(Error, "e", [])] } @test fn silently_drops_every_entry :: { assert_equal (log.silently chatty) 7 }