(local { : view } (require :fennel))

(local Command (require :command))

(var happened false)
(fn before [] (set happened false) (Command.reset-state))

(Command.define-command "no-args-command" #(set happened true))

(Command.define-command
 "multiply"
 (fn [{: a : b }] (set happened (* (tonumber a) (tonumber b))))
 {:a #(do "3") :b #(do "2")})

(before)
(let [(ok err)
      (match-try (Command.activate)
        {:active true} (Command.on-input "not-a-command")
        (where {:error e :active false} (e:match "can't find command")) true
        (catch
         x (values nil (view x))))]
  (assert ok err))

(before)
(let [(ok err)
      (match-try  (Command.activate)
        {:active true} (Command.on-input "multiply")
        {:active true :prompt p1} (Command.on-input "2")
        {:active true :prompt p2} (Command.on-input "3")
        (where {:active false} (= happened 6)) true
        (catch
         x (values nil (view x))))]
  (assert ok err))

(before)
(let [(ok err)
      (match
          (Command.invoke-interactively
           "multiply"
           {:a "7" :b "9"})
        (where {:active false} (= happened 63)) true
        x (values nil (.. "wrong answer " (view x) " " (view happened)))
        nil (values nil "???"))]
  (assert ok err))
