Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- Updated instructions for `/login` command and invalid input handling.

## 0.75.4

- Fix 0.75.3 regression on custom openai-chat providers.
Expand Down
20 changes: 11 additions & 9 deletions src/eca/features/login.clj
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,18 @@
;; No provider selected
(defmethod login-step [nil :login/start] [{:keys [db* chat-id input send-msg!] :as ctx}]
(let [provider (string/trim input)
providers (->> @db* :auth keys sort)]
providers (->> @db* :auth keys sort)
provider-list-str (reduce (fn [s p] (str s "- " p "\n")) "" providers)]
(if (get-in @db* [:auth provider])
(do (swap! db* assoc-in [:chats chat-id :login-provider] provider)
(swap! db* assoc-in [:auth provider] {:step :login/start})
(login-step (assoc ctx :provider provider)))
(send-msg! (reduce
(fn [s provider]
(str s "- " provider "\n"))
"Choose a provider:\n"
providers)))))
(do
(swap! db* assoc-in [:chats chat-id :login-provider] provider)
(swap! db* assoc-in [:auth provider] {:step :login/start})
(login-step (assoc ctx :provider provider)))
(if (string/blank? provider)
(send-msg! (str "Please type the name of your chosen provider and press Enter:\n" provider-list-str))
(send-msg! (str "Sorry, \"" provider "\" is not a valid provider.\n"
"Please type the name of your chosen provider and press Enter:\n"
provider-list-str))))))

(defn handle-step [{:keys [message chat-id]} db* messenger config metrics]
(let [provider (get-in @db* [:chats chat-id :login-provider])
Expand Down
64 changes: 64 additions & 0 deletions test/eca/features/login_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
(ns eca.features.login-test
(:require [clojure.test :refer [deftest is testing]]
[eca.features.login :as login]
[matcher-combinators.test :refer [match?]]))

(deftest test-login-step
(let [msg-log (atom [])
send-msg! (fn [msg] (swap! msg-log conj msg))

db* (atom {:auth {"google" {}
"github-copilot" {}}
:chats {1 {}}})]

(testing "user just typed /login for the first time"
(login/login-step {:provider nil
:step :login/start
:chat-id 0
:input ""
:db* db*
:send-msg! send-msg!})

(testing "should ask to choose a provider"
(is (= "Please type the name of your chosen provider and press Enter:\n- github-copilot\n- google\n"
(last @msg-log)))))

(testing "user is confused"
(login/login-step {:provider nil
:step :login/start
:chat-id 0
:input "/login github"
:db* db*
:send-msg! send-msg!})

(testing "should ask to choose a provider and provide instructions"
(is (= "Sorry, \"/login github\" is not a valid provider.\nPlease type the name of your chosen provider and press Enter:\n- github-copilot\n- google\n"
(last @msg-log)))

(testing "state didn't change"
(is (= {:auth {"github-copilot" {}
"google" {}}
:chats {1 {}}}
@db*)))))

(testing "valid input is provided"

(login/login-step {:provider nil
:step :login/start
:chat-id 0
:input "github-copilot"
:db* db*
:send-msg! send-msg!})

(testing "should proceed to the next step"
(is (re-find #"(?m)Open your browser at `https://github.com/login/device` and authenticate using the code: `.+`\nThen type anything in the chat and send it to continue the authentication."
(last @msg-log)))

(testing "state is update to reflect in-progress login"
(is (match? {:auth {"github-copilot" {;; skipping :device-code attr
:step :login/waiting-user-confirmation}
"google" {}}
:chats {0 {:login-provider "github-copilot"}
1 {}}}

@db*)))))))
Loading