<commit_before>{-# LANGUAGE CPP #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
#ifndef Bluez
{-# OPTIONS_GHC -fno-warn-unused-binds #-}
#endif
module BluetoothSpec (spec) where

import Bluetooth
import Control.Monad.IO.Class
import DBus
import Test.Hspec

import qualified Data.ByteString as BS


spec :: Spec
spec = do
#ifndef Bluez
  return ()
#else
  registerApplicationSpec
  advertiseSpec
  {-notifySpec-}
#endif

registerApplicationSpec :: Spec
registerApplicationSpec = describe "registerApplication" $ before connect $ do

  it "registers the service with bluez" $ \conn -> do
    v <- runBluetoothM (registerApplication testApp) conn
    v `shouldBe` Right ()
    -- We verify that the application is in fact registered by checking that
    -- attempting to register it again throws an AlreadyExists error.
    Left err <- runBluetoothM (registerApplication testApp) conn
    show err `shouldContain` "Already Exists"

advertiseSpec :: Spec
advertiseSpec = describe "advertise" $ before connect $ do

  let checkAdvert ad conn = do
        v <- runBluetoothM (advertise ad) conn
        v `shouldBe` Right ()
        -- We verify that the advertisement was registered by checking that
        -- attempting to register it again throws an AlreadyExists error.
        Left err <- runBluetoothM (advertise ad) conn
        show err `shouldContain` "Already Exists"

  it "adverstises a set of services" $ \conn -> checkAdvert testAdv conn

  {-it "works with service data" $ \conn -> do-}
    {-let adv = testAdv-}
         {-& value . serviceData . at "351930f8"-}
         {-?~ "hi"-}
    {-checkAdvert adv conn-}

  it "works with manufacturer data" $ \conn -> do
    let adv = testAdv & value . manufacturerData . at 1 ?~ "hi"
    checkAdvert adv conn

{-notifySpec :: Spec-}
{-notifySpec = describe "notification" $ before connect $ do-}

  {-it "accepts StartNotify" $ \conn -> do-}
    {-readIORef isNotifying `shouldReturn` False-}
    {-v <- runBluetoothM (registerApplication testApp) conn-}
    {-_ <- runBluetoothM (testCharacteristic ^. startNotify) conn-}
    {-readIORef isNotifying `shouldReturn` True-}

  {-it "accepts StopNotify" $ \conn -> do-}
    {-readIORef isNotifying `shouldReturn` False-}
    {-v <- runBluetoothM (registerApplication testApp) conn-}<commit_msg>Fix tests<commit_after>{-# LANGUAGE CPP #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
#ifndef Bluez
{-# OPTIONS_GHC -fno-warn-unused-binds #-}
#endif
module BluetoothSpec (spec) where

import Bluetooth
import Control.Monad.IO.Class
import Data.Either            (isRight)
import DBus
import Test.Hspec

import qualified Data.ByteString as BS


spec :: Spec
spec = do
#ifndef Bluez
  return ()
#else
  registerApplicationSpec
  advertiseSpec
  {-notifySpec-}
#endif

registerApplicationSpec :: Spec
registerApplicationSpec = describe "registerApplication" $ before connect $ do

  it "registers the service with bluez" $ \conn -> do
    v <- runBluetoothM (registerApplication testApp) conn
    v `shouldSatisfy` isRight
    -- We verify that the application is in fact registered by checking that
    -- attempting to register it again throws an AlreadyExists error.
    Left err <- runBluetoothM (registerApplication testApp) conn
    show err `shouldContain` "Already Exists"

advertiseSpec :: Spec
advertiseSpec = describe "advertise" $ before connect $ do

  let checkAdvert ad conn = do
        v <- runBluetoothM (advertise ad) conn
        v `shouldSatisfy` isRight
        -- We verify that the advertisement was registered by checking that
        -- attempting to register it again throws an AlreadyExists error.
        Left err <- runBluetoothM (advertise ad) conn
        show err `shouldContain` "Already Exists"

  it "adverstises a set of services" $ \conn -> checkAdvert testAdv conn

  {-it "works with service data" $ \conn -> do-}
    {-let adv = testAdv-}
         {-& value . serviceData . at "351930f8"-}
         {-?~ "hi"-}
    {-checkAdvert adv conn-}

  it "works with manufacturer data" $ \conn -> do
    let adv = testAdv & value . manufacturerData . at 1 ?~ "hi"
    checkAdvert adv conn

{-notifySpec :: Spec-}
{-notifySpec = describe "notification" $ before connect $ do-}

  {-it "accepts StartNotify" $ \conn -> do-}
    {-readIORef isNotifying `shouldReturn` False-}
    {-v <- runBluetoothM (registerApplication testApp) conn-}
    {-_ <- runBluetoothM (testCharacteristic ^. startNotify) conn-}
    {-readIORef isNotifying `shouldReturn` True-}

  {-it "accepts StopNotify" $ \conn -> do-}
    {-readIORef isNotifying `shouldReturn` False-}
    {-v <- runBluetoothM (registerApplication testApp) conn-}
{'fraction_non_alphanumeric': 0.10657155209684392, 'fraction_numerical': 0.0034587116299178555, 'mean_word_length': 3.2025431425976385, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6776468', 'n_tokens_mistral': 1471, 'n_tokens_neox': 1376, 'n_words': 475}