{-# LANGUAGE OverloadedStrings #-}{- |Module : HStratus.NotesSpecCopyright : (c) 2026 Tim EmiolaMaintainer : Tim Emiola <adetokunbo@emio.la>SPDX-License-Identifier: BSD-3-ClauseTests for the top-level Notes API ('Network.HStratus.Notes').-}module HStratus.NotesSpec (spec) whereimport Control.Exception (displayException)import Data.Aeson (object)import qualified Data.ByteString as BSimport qualified Data.ByteString.Char8 as BS8import qualified Data.ByteString.Lazy as LBSimport Data.IORef (IORef, newIORef, readIORef, writeIORef)import qualified Data.Map.Strict as Mapimport qualified Data.Text as Textimport Network.HStratus.Http (mkApiWith)import Network.HStratus.Http.Endpoints (Endpoints (..))import Network.HStratus.Notes ( NotesApi , NotesError (..) , getNote , mkNotesApi , noteFolders , notesInFolder , recentNotes )import Network.HStratus.Notes.Noteimport Network.HStratus.Session (AccountData (..), Credentials (..), Session (..), Webservice (..))import Network.HTTP.Client (Request (..), defaultManagerSettings, defaultRequest, newManager)import Network.HTTP.Types (HeaderName, hContentType, methodPost, status200, status404)import Network.Wai (Application, rawPathInfo, responseLBS)import Network.Wai.Handler.Warp (testWithApplication)import System.IO.Temp (withSystemTempDirectory)import Test.Hspecimport Test.Hspec.Benri (endsNothing)spec :: Specspec = describe "Network.HStratus.Notes" $ do describe "NotesError displayException" $ do it "NotesHttpError" $ displayException (NotesHttpError 404) `shouldBe` "iCloud Notes: HTTP error 404" it "NotesParseError" $ displayException (NotesParseError "bad json") `shouldBe` "iCloud Notes: parse error: bad json" describe "noteFolders" $ do it "returns NoteFolder list from a query response" $ withNotesMock queryFoldersJson "/records/query" $ \na -> do folders <- noteFolders na case folders of [f] -> do nfId f `shouldBe` FolderId "Folder/FOLDER-FIXTURE" nfName f `shouldBe` Just "Synthetic Folder" _ -> expectationFailure $ "expected 1 folder, got " <> show (length folders) describe "recentNotes" $ do it "returns NoteSummary list from a query response" $ withNotesMock queryNotesJson "/records/query" $ \na -> do notes <- recentNotes na case notes of [n] -> do nsId n `shouldBe` NoteId "Note/NOTE-FIXTURE" nsTitle n `shouldBe` Just "Synthetic note" _ -> expectationFailure $ "expected 1 note, got " <> show (length notes) it "accumulates results across paginated responses" $ withPaginatedMock $ \na -> do notes <- recentNotes na length notes `shouldBe` 2 describe "getNote" $ do it "returns Just Note with decoded body for a live record" $ withNotesMock lookupNoteJson "/records/lookup" $ \na -> do result <- getNote na (NoteId "Note/NOTE-FIXTURE") case result of Nothing -> expectationFailure "expected Just Note" Just n -> noteBodyBytes n `shouldBe` "synthetic note body" it "returns Nothing for a tombstone record" $ withNotesMock tombstoneLookupJson "/records/lookup" $ \na -> endsNothing $ getNote na (NoteId "Note/NOTE-DELETED-FIXTURE") describe "notesInFolder" $ do it "returns only notes in the given folder, excluding other folders and deleted notes" $ withNotesMock folderChangesJson "/changes/zone" $ \na -> do notes <- notesInFolder na (FolderId "Folder/FOLDER-FIXTURE") case notes of [n] -> nsId n `shouldBe` NoteId "Note/NOTE-FIXTURE" _ -> expectationFailure $ "expected 1 note, got " <> show (length notes)-- Mock serverswithNotesMock :: LBS.ByteString -> BS.ByteString -> (NotesApi -> IO a) -> IO awithNotesMock json pathSuffix action = withSystemTempDirectory "icloud-notes-mock" $ \tmpDir -> testWithApplication (pure (simpleApp json pathSuffix)) $ \serverPort -> do na <- mkTestNotesApi serverPort tmpDir action nawithPaginatedMock :: (NotesApi -> IO a) -> IO awithPaginatedMock action = withSystemTempDirectory "icloud-notes-paginated" $ \tmpDir -> do callRef <- newIORef (0 :: Int) testWithApplication (pure (paginatedApp callRef)) $ \serverPort -> do na <- mkTestNotesApi serverPort tmpDir action nasimpleApp :: LBS.ByteString -> BS.ByteString -> ApplicationsimpleApp json pathSuffix req respond | pathSuffix `BS.isSuffixOf` rawPathInfo req = respond $ responseLBS status200 jsonHeaders json | otherwise = respond $ responseLBS status404 [] "not found"paginatedApp :: IORef Int -> ApplicationpaginatedApp callRef _req respond = do n <- readIORef callRef writeIORef callRef (n + 1) respond $ responseLBS status200 jsonHeaders (if n == 0 then page1Json else page2Json)mkTestNotesApi :: Int -> FilePath -> IO NotesApimkTestNotesApi serverPort tmpDir = do let baseUrl = Text.pack $ "http://127.0.0.1:" ++ show serverPort mgr <- newManager defaultManagerSettings api <- mkApiWith (testSession tmpDir) (testAuthEndpoints serverPort) mgr mkNotesApi (testAccountData baseUrl) (testSession tmpDir) api-- FixturestestAccountData :: Text.Text -> AccountDatatestAccountData baseUrl = AccountData { adHsaVersion = 2 , adHsaChallengeRequired = False , adHsaTrustedBrowser = Just True , adWebservices = Map.fromList [("ckdatabasews", Webservice baseUrl Nothing)] , adRaw = object [] }testSession :: FilePath -> SessiontestSession topDir = Session { sessionCreds = Credentials{credAccountName = "test@example.com", credPassword = "test-pass"} , sessionTopDir = topDir , sessionClientId = "auth-test-client-id" }testAuthEndpoints :: Int -> EndpointstestAuthEndpoints serverPort = Endpoints { epHome = "http://127.0.0.1:" <> BS8.pack (show serverPort) , epAuth = dummyReq "/appleauth/auth" , epSetup = dummyReq "/setup/ws/1" , epWidgetKey = "test-widget-key" } where dummyReq p = defaultRequest { host = "127.0.0.1" , port = serverPort , secure = False , method = methodPost , path = p }jsonHeaders :: [(HeaderName, BS8.ByteString)]jsonHeaders = [(hContentType, "application/json")]queryFoldersJson :: LBS.ByteStringqueryFoldersJson = "{\"records\":[{\"recordName\":\"Folder/FOLDER-FIXTURE\"\ \,\"recordType\":\"Folder\"\ \,\"fields\":{\"TitleEncrypted\":{\"type\":\"ENCRYPTED_BYTES\",\"value\":\"U3ludGhldGljIEZvbGRlcg==\"}}}]\ \,\"continuationMarker\":null}"queryNotesJson :: LBS.ByteStringqueryNotesJson = "{\"records\":[{\"recordName\":\"Note/NOTE-FIXTURE\"\ \,\"recordType\":\"Note\"\ \,\"fields\":{\ \\"TitleEncrypted\":{\"type\":\"ENCRYPTED_BYTES\",\"value\":\"U3ludGhldGljIG5vdGU=\"}\ \,\"Deleted\":{\"type\":\"INT64\",\"value\":0}}}]\ \,\"continuationMarker\":null}"lookupNoteJson :: LBS.ByteStringlookupNoteJson = "{\"records\":[{\"recordName\":\"Note/NOTE-FIXTURE\"\ \,\"recordType\":\"Note\"\ \,\"fields\":{\ \\"TitleEncrypted\":{\"type\":\"ENCRYPTED_BYTES\",\"value\":\"U3ludGhldGljIG5vdGU=\"}\ \,\"TextDataEncrypted\":{\"type\":\"ENCRYPTED_BYTES\",\"value\":\"c3ludGhldGljIG5vdGUgYm9keQ==\"}\ \,\"Deleted\":{\"type\":\"INT64\",\"value\":0}}}]}"tombstoneLookupJson :: LBS.ByteStringtombstoneLookupJson = "{\"records\":[{\"recordName\":\"Note/NOTE-DELETED-FIXTURE\",\"deleted\":true}]}"folderChangesJson :: LBS.ByteStringfolderChangesJson = "{\"zones\":[{\"zoneID\":{\"zoneName\":\"Notes\",\"zoneType\":\"REGULAR_CUSTOM_ZONE\"}\ \,\"syncToken\":\"sync-1\",\"moreComing\":false\ \,\"records\":[\ \{\"recordName\":\"Note/NOTE-FIXTURE\",\"recordType\":\"Note\"\ \,\"fields\":{\"TitleEncrypted\":{\"type\":\"ENCRYPTED_BYTES\",\"value\":\"U3ludGhldGljIG5vdGU=\"}\ \,\"Deleted\":{\"type\":\"INT64\",\"value\":0}\ \,\"Folder\":{\"type\":\"REFERENCE\",\"value\":{\"recordName\":\"Folder/FOLDER-FIXTURE\",\"action\":\"VALIDATE\"}}}}\ \,{\"recordName\":\"Note/NOTE-OTHER\",\"recordType\":\"Note\"\ \,\"fields\":{\"TitleEncrypted\":{\"type\":\"ENCRYPTED_BYTES\",\"value\":\"T3RoZXIgTm90ZQ==\"}\ \,\"Deleted\":{\"type\":\"INT64\",\"value\":0}\ \,\"Folder\":{\"type\":\"REFERENCE\",\"value\":{\"recordName\":\"Folder/OTHER-FOLDER\",\"action\":\"VALIDATE\"}}}}\ \,{\"recordName\":\"Note/NOTE-DELETED\",\"recordType\":\"Note\"\ \,\"fields\":{\"TitleEncrypted\":{\"type\":\"ENCRYPTED_BYTES\",\"value\":\"RGVsZXRlZCBOb3Rl\"}\ \,\"Deleted\":{\"type\":\"INT64\",\"value\":1}\ \,\"Folder\":{\"type\":\"REFERENCE\",\"value\":{\"recordName\":\"Folder/FOLDER-FIXTURE\",\"action\":\"VALIDATE\"}}}}]}]}"page1Json :: LBS.ByteStringpage1Json = "{\"records\":[{\"recordName\":\"Note/NOTE-1\"\ \,\"recordType\":\"Note\"\ \,\"fields\":{\ \\"TitleEncrypted\":{\"type\":\"ENCRYPTED_BYTES\",\"value\":\"Tm90ZSAx\"}\ \,\"Deleted\":{\"type\":\"INT64\",\"value\":0}}}]\ \,\"continuationMarker\":\"page-marker-1\"}"page2Json :: LBS.ByteStringpage2Json = "{\"records\":[{\"recordName\":\"Note/NOTE-2\"\ \,\"recordType\":\"Note\"\ \,\"fields\":{\ \\"TitleEncrypted\":{\"type\":\"ENCRYPTED_BYTES\",\"value\":\"Tm90ZSAy\"}\ \,\"Deleted\":{\"type\":\"INT64\",\"value\":0}}}]\ \,\"continuationMarker\":null}"