Skip to content

Commit a9ca5a2

Browse files
committed
[cli] Add more specific failures in UpdateTestRunFailure
1 parent dd93586 commit a9ca5a2

File tree

3 files changed

+70
-73
lines changed

3 files changed

+70
-73
lines changed

cli/src/Oracle/Validate/Requests/TestRun/Update.hs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ data UpdateTestRunFailure
4141
| UpdateTestRunAgentRejection AgentRejection
4242
| UpdateTestRunRequestNotFromAgent Owner
4343
| UpdateTestRunConfigNotAvailable
44+
| UpdateTestRunPreviousStateNotFound
45+
| UpdateTestRunTestRunIdNotResolved
4446
| UpdateTestRunWrongPreviousState
4547
deriving (Show, Eq)
4648

@@ -54,6 +56,10 @@ instance Monad m => ToJSON m UpdateTestRunFailure where
5456
object ["updateTestRunRequestNotFromAgent" .= show owner]
5557
UpdateTestRunConfigNotAvailable ->
5658
toJSON ("Token configuration is not available yet" :: String)
59+
UpdateTestRunPreviousStateNotFound ->
60+
toJSON ("Previous state for test run not found" :: String)
61+
UpdateTestRunTestRunIdNotResolved ->
62+
toJSON ("Test run ID is not resolved" :: String)
5763
UpdateTestRunWrongPreviousState ->
5864
toJSON ("Wrong previous state for test run" :: String)
5965

cli/src/Oracle/Validate/Types.hs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ module Oracle.Validate.Types
1515
, throwFalse
1616
, throwLeft
1717
, liftMaybe
18+
, hoistValidate
1819
) where
1920

2021
import Control.Monad.Catch (MonadCatch, MonadMask (..), MonadThrow)
@@ -36,9 +37,9 @@ newtype Validate e m a = Validate (ExceptT e m a)
3637
deriving
3738
(Functor, Applicative, Monad, MonadMask, MonadCatch, MonadThrow)
3839

39-
-- instance MonadMask m => MonadMask (Validate e m) where
40-
-- mask a = Validate $ mask $ \restore -> runExceptT (a (Validate . restore))
41-
-- uninterruptibleMask a = Validate $ uninterruptibleMask $ \restore -> runExceptT (a (Validate . restore))
40+
hoistValidate
41+
:: (forall x. m x -> n x) -> Validate e m a -> Validate e n a
42+
hoistValidate f (Validate a) = Validate $ ExceptT $ f $ runExceptT a
4243
instance MonadTrans (Validate e) where
4344
lift = Validate . lift
4445

cli/src/User/Agent/Cli.hs

Lines changed: 60 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ import Oracle.Validate.Types
6262
( AValidationResult (..)
6363
, Validate
6464
, Validated
65+
, hoistValidate
6566
, liftMaybe
6667
, runValidate
6768
)
@@ -86,63 +87,63 @@ import User.Types
8687
)
8788
import Validation (Validation)
8889

90+
type ValidateWithContext m a =
91+
Validate UpdateTestRunFailure (WithContext m) a
92+
8993
withExpectedState
9094
:: (FromJSON Maybe (TestRunState phase), Monad m)
9195
=> TokenId
9296
-> TestRun
93-
-> (TestRunState phase -> WithContext m result)
94-
-> WithContext m (Maybe result)
97+
-> (TestRunState phase -> ValidateWithContext m result)
98+
-> ValidateWithContext m result
9599
withExpectedState tokenId testRun cont = do
96-
facts <- withMPFS $ \mpfs -> mpfsGetTokenFacts mpfs tokenId
97-
jsonKeyValue <- toJSON testRun
98-
let
99-
hasKey (JSObject obj) =
100-
any (\(k, value) -> k == "key" && value == jsonKeyValue) obj
101-
hasKey _ = False
102-
case facts of
103-
JSArray objects -> do
104-
case filter hasKey objects of
105-
[JSObject object] -> do
106-
let value = find (\(k, _) -> k == "value") object
107-
case value >>= fromJSON . snd of
108-
Nothing -> pure Nothing
109-
Just x -> Just <$> cont x
110-
_ -> pure Nothing
111-
_ -> pure Nothing
100+
facts <- lift
101+
$ fmap parseFacts
102+
$ withMPFS
103+
$ \mpfs -> mpfsGetTokenFacts mpfs tokenId
104+
jsonKey <- toJSON testRun
105+
jPreviousState <-
106+
liftMaybe UpdateTestRunPreviousStateNotFound
107+
$ find ((== jsonKey) . factKey) facts
108+
value <-
109+
liftMaybe UpdateTestRunWrongPreviousState
110+
$ fromJSON
111+
$ factValue jPreviousState
112+
cont value
112113

113114
withResolvedTestRun
114115
:: Monad m
115116
=> TokenId
116117
-> TestRunId
117-
-> (TestRun -> WithContext m (Maybe a))
118-
-> WithContext m (Maybe a)
118+
-> (TestRun -> ValidateWithContext m a)
119+
-> ValidateWithContext m a
119120
withResolvedTestRun tk (TestRunId testRunId) cont = do
120-
facts <- withMPFS $ \mpfs -> mpfsGetTokenFacts mpfs tk
121-
let testRuns = parseFacts facts
122-
finder :: Fact TestRun JSValue -> Bool
123-
finder (Fact key _) = case keyHash key of
121+
facts <- lift
122+
$ fmap parseFacts
123+
$ withMPFS
124+
$ \mpfs -> mpfsGetTokenFacts mpfs tk
125+
let match :: Fact TestRun JSValue -> Bool
126+
match (Fact key _) = case keyHash key of
124127
Nothing -> False
125128
Just keyId -> keyId == testRunId
126-
let mtr = factKey <$> find finder testRuns
127-
case mtr of
128-
Nothing -> pure Nothing
129-
Just testRun -> cont testRun
129+
testRun <-
130+
liftMaybe UpdateTestRunTestRunIdNotResolved
131+
$ factKey <$> find match facts
132+
cont testRun
130133

131134
updateTestRunState
132135
:: (Monad m, FromJSON Maybe (TestRunState phase))
133136
=> TokenId
134137
-> TestRunId
135138
-> ( TestRun
136139
-> TestRunState phase
137-
-> WithContext m (AValidationResult UpdateTestRunFailure a)
140+
-> ValidateWithContext m a
138141
)
139142
-> WithContext m (AValidationResult UpdateTestRunFailure a)
140143
updateTestRunState tokenId key f = do
141-
mResult <- withResolvedTestRun tokenId key $ \testRun ->
142-
withExpectedState tokenId testRun $ f testRun
143-
pure $ case mResult of
144-
Nothing -> ValidationFailure UpdateTestRunWrongPreviousState
145-
Just result -> result
144+
runValidate
145+
$ withResolvedTestRun tokenId key
146+
$ \testRun -> withExpectedState tokenId testRun $ f testRun
146147

147148
agentCmd
148149
:: MonadIO m
@@ -347,29 +348,27 @@ signAndSubmitAnUpdate
347348
-> key
348349
-> old
349350
-> new
350-
-> WithContext
351-
m
352-
(AValidationResult UpdateTestRunFailure (WithTxHash new))
351+
-> ValidateWithContext m (WithTxHash new)
353352
signAndSubmitAnUpdate validate tokenId wallet testRun oldState newState = do
354353
let requester = owner wallet
355-
validation <- askValidation $ Just tokenId
356-
mconfig <- askConfig tokenId
357-
Submission submit <- ($ wallet) <$> askSubmit
358-
mpfs <- askMpfs
359-
lift $ runValidate $ do
360-
Config{configAgent} <-
361-
liftMaybe UpdateTestRunConfigNotAvailable mconfig
362-
void
363-
$ validate validation configAgent requester
364-
$ Change (Key testRun)
365-
$ Update oldState newState
366-
wtx <- lift $ submit $ \address -> do
367-
key <- toJSON testRun
368-
oldValue <- toJSON oldState
369-
newValue <- toJSON newState
370-
mpfsRequestUpdate mpfs address tokenId
371-
$ RequestUpdateBody{key, oldValue, newValue}
372-
pure $ wtx $> newState
354+
validation <- lift $ askValidation $ Just tokenId
355+
mconfig <- lift $ askConfig tokenId
356+
Submission submit <- lift $ ($ wallet) <$> askSubmit
357+
mpfs <- lift askMpfs
358+
Config{configAgent} <-
359+
liftMaybe UpdateTestRunConfigNotAvailable mconfig
360+
void
361+
$ hoistValidate lift
362+
$ validate validation configAgent requester
363+
$ Change (Key testRun)
364+
$ Update oldState newState
365+
wtx <- lift $ lift $ submit $ \address -> do
366+
key <- toJSON testRun
367+
oldValue <- toJSON oldState
368+
newValue <- toJSON newState
369+
mpfsRequestUpdate mpfs address tokenId
370+
$ RequestUpdateBody{key, oldValue, newValue}
371+
pure $ wtx $> newState
373372

374373
reportCommand
375374
:: Monad m
@@ -379,12 +378,9 @@ reportCommand
379378
-> TestRunState RunningT
380379
-> Duration
381380
-> URL
382-
-> WithContext
381+
-> ValidateWithContext
383382
m
384-
( AValidationResult
385-
UpdateTestRunFailure
386-
(WithTxHash (TestRunState DoneT))
387-
)
383+
(WithTxHash (TestRunState DoneT))
388384
reportCommand tokenId wallet testRun oldState duration url =
389385
signAndSubmitAnUpdate
390386
validateToDoneUpdate
@@ -401,12 +397,9 @@ rejectCommand
401397
-> TestRun
402398
-> TestRunState PendingT
403399
-> [TestRunRejection]
404-
-> WithContext
400+
-> ValidateWithContext
405401
m
406-
( AValidationResult
407-
UpdateTestRunFailure
408-
(WithTxHash (TestRunState DoneT))
409-
)
402+
(WithTxHash (TestRunState DoneT))
410403
rejectCommand tokenId wallet testRun testRunState reason =
411404
signAndSubmitAnUpdate
412405
validateToDoneUpdate
@@ -422,12 +415,9 @@ acceptCommand
422415
-> Wallet
423416
-> TestRun
424417
-> TestRunState PendingT
425-
-> WithContext
418+
-> ValidateWithContext
426419
m
427-
( AValidationResult
428-
UpdateTestRunFailure
429-
(WithTxHash (TestRunState RunningT))
430-
)
420+
(WithTxHash (TestRunState RunningT))
431421
acceptCommand tokenId wallet testRun testRunState =
432422
signAndSubmitAnUpdate
433423
validateToRunningUpdate

0 commit comments

Comments
 (0)