packages feed

hstratus-notes-0.1.2.0 / test / HStratus / Notes / ProtoSpec.hs

{-# LANGUAGE OverloadedStrings #-}{- |Module      : HStratus.Notes.ProtoSpecCopyright   : (c) 2026 Tim EmiolaMaintainer  : Tim Emiola <adetokunbo@emio.la>SPDX-License-Identifier: BSD-3-ClauseTests for the protobuf decoders in 'Network.HStratus.Internal.Notes.Proto'.-}module HStratus.Notes.ProtoSpec (spec) whereimport Data.ByteString (ByteString)import HStratus.Notes.Arbitraries ()import HStratus.Notes.TestHelperimport Network.HStratus.Internal.Notes.Protoimport Test.Hspecimport Test.Hspec.Benri (endsLeft_, endsRight)import Test.Hspec.QuickCheck (prop)import Test.QuickCheck (counterexample, (===))spec :: Specspec = describe "decodeNoteStoreProto" $ do  it "decodes a minimal note with text only" $    pure (decodeNoteStoreProto minimalNoteBytes)      `endsRight` ProtoNote{pnNoteText = "hello", pnAttributeRuns = []}  it "returns an error for empty input" $    endsLeft_ $      pure (decodeNoteStoreProto "")  it "decodes attribute run length and paragraph style" $    case decodeNoteStoreProto noteWithRunBytes of      Left err -> expectationFailure err      Right note -> do        pnNoteText note `shouldBe` "hi"        case pnAttributeRuns note of          [run] -> do            parLength run `shouldBe` 2            parParagraphStyle run              `shouldBe` Just                ProtoParagraphStyle                  { ppsStyleType = 1                  , ppsIndent = 0                  , ppsChecked = Nothing                  , ppsListStart = Nothing                  , ppsBlockQuote = False                  }          runs -> expectationFailure $ "expected 1 run, got " <> show (length runs)  it "decodes strikethrough field 7" $    case decodeNoteStoreProto noteWithStrikethroughBytes of      Left err -> expectationFailure err      Right note ->        case pnAttributeRuns note of          [run] -> do            parLength run `shouldBe` 2            parStrikethrough run `shouldBe` 1          runs -> expectationFailure $ "expected 1 run, got " <> show (length runs)  it "decodes indent_amount field 4 into ppsIndent" $    case decodeNoteStoreProto bulletIndent1Bytes of      Left err -> expectationFailure err      Right note ->        case pnAttributeRuns note of          [run] ->            fmap ppsIndent (parParagraphStyle run) `shouldBe` Just 1          runs -> expectationFailure $ "expected 1 run, got " <> show (length runs)  it "decodes checklist.done = 1 into ppsChecked = Just True" $    case decodeNoteStoreProto checklistDoneBytes of      Left err -> expectationFailure err      Right note ->        case pnAttributeRuns note of          [run] ->            fmap ppsChecked (parParagraphStyle run) `shouldBe` Just (Just True)          runs -> expectationFailure $ "expected 1 run, got " <> show (length runs)  it "decodes checklist.done = 0 into ppsChecked = Just False" $    case decodeNoteStoreProto checklistUndoneBytes of      Left err -> expectationFailure err      Right note ->        case pnAttributeRuns note of          [run] ->            fmap ppsChecked (parParagraphStyle run) `shouldBe` Just (Just False)          runs -> expectationFailure $ "expected 1 run, got " <> show (length runs)  it "decodes absent checklist into ppsChecked = Nothing" $    case decodeNoteStoreProto checklistAbsentBytes of      Left err -> expectationFailure err      Right note ->        case pnAttributeRuns note of          [run] ->            fmap ppsChecked (parParagraphStyle run) `shouldBe` Just Nothing          runs -> expectationFailure $ "expected 1 run, got " <> show (length runs)  it "decodes starting_list_item_number into ppsListStart = Just 3" $    case decodeNoteStoreProto numberedListStart3Bytes of      Left err -> expectationFailure err      Right note ->        case pnAttributeRuns note of          [run] ->            fmap ppsListStart (parParagraphStyle run) `shouldBe` Just (Just 3)          runs -> expectationFailure $ "expected 1 run, got " <> show (length runs)  it "decodes block_quote into ppsBlockQuote = True" $    case decodeNoteStoreProto blockQuoteBytes of      Left err -> expectationFailure err      Right note ->        case pnAttributeRuns note of          [run] ->            fmap ppsBlockQuote (parParagraphStyle run) `shouldBe` Just True          runs -> expectationFailure $ "expected 1 run, got " <> show (length runs)  prop "encode/decode roundtrip preserves ProtoParagraphStyle" $ \ps ->    case decodeNoteStoreProto (mkNote "x" [runWith 1 (encodeParagraphStyle ps)]) of      Left err -> counterexample err False      Right note -> case pnAttributeRuns note of        [run] -> parParagraphStyle run === Just ps        runs -> counterexample ("expected 1 run, got " <> show (length runs)) FalseminimalNoteBytes :: ByteStringminimalNoteBytes = mkNote "hello" []noteWithRunBytes :: ByteStringnoteWithRunBytes = mkNote "hi" [runWith 2 (psStyleType 1)]noteWithStrikethroughBytes :: ByteStringnoteWithStrikethroughBytes = mkNote "hi" [strikethroughRun 2]bulletIndent1Bytes :: ByteStringbulletIndent1Bytes = mkNote "hi" [runWith 1 (psIndentAmount 1 (psStyleType 100))]checklistDoneBytes :: ByteStringchecklistDoneBytes = mkNote "hi" [runWith 1 (psChecklist True (psStyleType 103))]checklistUndoneBytes :: ByteStringchecklistUndoneBytes = mkNote "hi" [runWith 1 (psChecklist False (psStyleType 103))]checklistAbsentBytes :: ByteStringchecklistAbsentBytes = mkNote "hi" [runWith 1 (psStyleType 103)]numberedListStart3Bytes :: ByteStringnumberedListStart3Bytes = mkNote "hi" [runWith 1 (psListStart 3 (psStyleType 102))]blockQuoteBytes :: ByteStringblockQuoteBytes = mkNote "hi" [runWith 1 psBlockQuote]