module Test.FeatureFlags.ConferenceCalling where

import qualified Data.Aeson as A
import Test.FeatureFlags.Util
import Testlib.Prelude

testPatchConferenceCalling :: (HasCallStack) => App ()
testPatchConferenceCalling :: HasCallStack => App ()
testPatchConferenceCalling = do
  Domain
-> String
-> Value
-> (String -> Value -> Value -> App Value)
-> App ()
forall domain.
(HasCallStack, MakesValue domain) =>
domain
-> String
-> Value
-> (String -> Value -> Value -> App Value)
-> App ()
checkPatchWithComputeExpected Domain
OwnDomain String
"conferenceCalling" ([Pair] -> Value
object [String
"lockStatus" String -> String -> Pair
forall a. ToJSON a => String -> a -> Pair
.= String
"locked"]) String -> Value -> Value -> App Value
computeExpectedValue
  Domain
-> String
-> Value
-> (String -> Value -> Value -> App Value)
-> App ()
forall domain.
(HasCallStack, MakesValue domain) =>
domain
-> String
-> Value
-> (String -> Value -> Value -> App Value)
-> App ()
checkPatchWithComputeExpected Domain
OwnDomain String
"conferenceCalling" ([Pair] -> Value
object [String
"status" String -> String -> Pair
forall a. ToJSON a => String -> a -> Pair
.= String
"disabled"]) String -> Value -> Value -> App Value
computeExpectedValue
  Domain
-> String
-> Value
-> (String -> Value -> Value -> App Value)
-> App ()
forall domain.
(HasCallStack, MakesValue domain) =>
domain
-> String
-> Value
-> (String -> Value -> Value -> App Value)
-> App ()
checkPatchWithComputeExpected Domain
OwnDomain String
"conferenceCalling" ([Pair] -> Value
object [String
"lockStatus" String -> String -> Pair
forall a. ToJSON a => String -> a -> Pair
.= String
"locked", String
"status" String -> String -> Pair
forall a. ToJSON a => String -> a -> Pair
.= String
"disabled"]) String -> Value -> Value -> App Value
computeExpectedValue
  Domain
-> String
-> Value
-> (String -> Value -> Value -> App Value)
-> App ()
forall domain.
(HasCallStack, MakesValue domain) =>
domain
-> String
-> Value
-> (String -> Value -> Value -> App Value)
-> App ()
checkPatchWithComputeExpected Domain
OwnDomain String
"conferenceCalling" ([Pair] -> Value
object [String
"lockStatus" String -> String -> Pair
forall a. ToJSON a => String -> a -> Pair
.= String
"unlocked", String
"config" String -> Value -> Pair
forall a. ToJSON a => String -> a -> Pair
.= [Pair] -> Value
object [String
"useSFTForOneToOneCalls" String -> Value -> Pair
forall a. ToJSON a => String -> a -> Pair
.= Bool -> Value
forall a. ToJSON a => a -> Value
toJSON Bool
True]]) String -> Value -> Value -> App Value
computeExpectedValue
  where
    computeExpectedValue :: String -> Value -> Value -> App Value
    computeExpectedValue :: String -> Value -> Value -> App Value
computeExpectedValue String
"status" Value
_ Value
patch = do
      Maybe Value
statusFromPatch <- Value -> String -> App (Maybe Value)
forall a.
(HasCallStack, MakesValue a) =>
a -> String -> App (Maybe Value)
lookupField Value
patch String
"status"
      -- conference calling behaves differently than other features
      -- if unlocked, the status is set to enabled even thought the default is disabled
      App Value -> (Value -> App Value) -> Maybe Value -> App Value
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Value -> App Value
forall a. a -> App a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Text -> Value
A.String (String -> Text
forall a. IsString a => String -> a
fromString String
"enabled"))) Value -> App Value
forall a. a -> App a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe Value
statusFromPatch
    computeExpectedValue String
key Value
defFeature Value
patch = do
      Maybe Value
mValue <- Value -> String -> App (Maybe Value)
forall a.
(HasCallStack, MakesValue a) =>
a -> String -> App (Maybe Value)
lookupField Value
patch String
key
      App Value -> (Value -> App Value) -> Maybe Value -> App Value
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Value
defFeature Value -> String -> App Value
forall a. (HasCallStack, MakesValue a) => a -> String -> App Value
%. String
key) Value -> App Value
forall a. a -> App a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe Value
mValue

testConferenceCalling :: (HasCallStack) => APIAccess -> App ()
testConferenceCalling :: HasCallStack => APIAccess -> App ()
testConferenceCalling APIAccess
access = do
  Domain -> APIAccess -> FeatureTests -> App ()
forall domain.
(HasCallStack, MakesValue domain) =>
domain -> APIAccess -> FeatureTests -> App ()
runFeatureTests Domain
OwnDomain APIAccess
access
    (FeatureTests -> App ()) -> FeatureTests -> App ()
forall a b. (a -> b) -> a -> b
$ String -> FeatureTests
mkFeatureTests String
"conferenceCalling"
    FeatureTests -> (FeatureTests -> FeatureTests) -> FeatureTests
forall a b. a -> (a -> b) -> b
& Value -> FeatureTests -> FeatureTests
addUpdate (ConfCalling -> Value
confCalling ConfCalling
forall a. Default a => a
def {sft = toJSON True})
    FeatureTests -> (FeatureTests -> FeatureTests) -> FeatureTests
forall a b. a -> (a -> b) -> b
& Value -> FeatureTests -> FeatureTests
addUpdate (ConfCalling -> Value
confCalling ConfCalling
forall a. Default a => a
def {sft = toJSON False})
    FeatureTests -> (FeatureTests -> FeatureTests) -> FeatureTests
forall a b. a -> (a -> b) -> b
& Value -> FeatureTests -> FeatureTests
addInvalidUpdate (ConfCalling -> Value
confCalling ConfCalling
forall a. Default a => a
def {sft = toJSON (0 :: Int)})