1
0
Fork 0

Call function when a predefined prompt has one

Also test if the variable with predefined prompts is used properly by kagi-fastgpt-prompt.
This commit is contained in:
Bram Schoenmakers 2024-03-23 09:37:39 +01:00
parent 554a19285b
commit 9b58724986
Signed by: bram
GPG key ID: 0CCD19DFDC63258F
2 changed files with 16 additions and 2 deletions

View file

@ -77,6 +77,7 @@ The EXPECT-ARGS correspond to the arguments passed to the `expect' macro."
(spy-on #'kagi--call-api :and-return-value (kagi-test--dummy-output dummy-output)))
(describe "FastGPT"
(describe "kagi-fastgpt-prompt"
:var ((kagi-fastgpt-prompts))
(before-each
(spy-on #'message)
(spy-on #'kagi--fastgpt-display-result))
@ -177,7 +178,19 @@ https://www.example.com"
(spy-on #'kagi--get-text-for-prompt)
(call-interactively #'kagi-fastgpt-prompt)
(kagi-test--expect-arg #'kagi--fastgpt 0 :to-equal "foo%bar")
(expect #'kagi--get-text-for-prompt :not :to-have-been-called)))
(expect #'kagi--get-text-for-prompt :not :to-have-been-called))
(it "uses the cdr when the user enters a predefined prompt name"
(setq kagi-fastgpt-prompts '(("foo" . "bar")))
(spy-on #'kagi--fastgpt)
(spy-on #'completing-read :and-return-value "foo")
(call-interactively #'kagi-fastgpt-prompt)
(kagi-test--expect-arg #'kagi--fastgpt 0 :to-equal "bar"))
(it "calls the function when a predefined prompt has one"
(setq kagi-fastgpt-prompts '(("function" . (lambda () "result"))))
(spy-on #'kagi--fastgpt)
(spy-on #'completing-read :and-return-value "function")
(call-interactively #'kagi-fastgpt-prompt)
(kagi-test--expect-arg #'kagi--fastgpt 0 :to-equal "result")))
(describe "kagi-translate"
(before-each
(spy-on #'kagi-fastgpt-prompt))

View file

@ -424,7 +424,8 @@ interactively obtained from the user (the region, buffer content
or text input)."
(let* ((user-text)
(prompt-name (completing-read "fastgpt> " kagi-fastgpt-prompts))
(prompt-template (alist-get prompt-name kagi-fastgpt-prompts prompt-name nil #'string=)))
(prompt-cdr (alist-get prompt-name kagi-fastgpt-prompts prompt-name nil #'string=))
(prompt-template (if (functionp prompt-cdr) (funcall prompt-cdr) prompt-cdr)))
(replace-regexp-in-string (rx (seq "%" anychar))
(lambda (match)
(pcase match