1
0
Fork 0

Compare commits

...

2 commits

View file

@ -57,6 +57,20 @@ TEXT is the output text, optionally with a list of REFERENCES."
:expect-match-phrase "Expected `%A' to be equal (incl. properties) to %b, but `%A' was %a."
:expect-mismatch-phrase "Expected `%A' not to be equal (incl. properties) to %b, but `%A' was %a.")
(defmacro kagi-test--expect-arg (function-symbol num &rest expect-args)
"Check the argument NUM of the first call of FUNCTION-SYMBOL.
The EXPECT-ARGS correspond to the arguments passed to the `expect' macro."
`(let ((args (spy-calls-args-for ,function-symbol 0)))
(expect (nth ,num args) ,@expect-args)))
(defmacro kagi-test--expect-object (function-symbol key &rest expect-args)
"Check the argument NUM of the first call of FUNCTION-SYMBOL.
The EXPECT-ARGS correspond to the arguments passed to the `expect' macro."
`(let ((args (car (spy-calls-args-for ,function-symbol 0))))
(expect (map-elt args ,key) ,@expect-args)))
(describe "kagi.el"
:var ((dummy-output "text"))
(before-each
@ -144,21 +158,19 @@ https://www.example.com"
(it "calls kagi-fastgpt-prompt non-interactively with target language in prompt"
(kagi-translate "hello" "toki pona")
(expect #'kagi-fastgpt-prompt :to-have-been-called-times 1)
(let ((args (spy-calls-args-for #'kagi-fastgpt-prompt 0)))
;; not going to test the exact phrasing of the prompt, but
;; at least 'toki pona' has to appear.
(expect (nth 0 args) :to-match "toki pona")
;; called non-interactively
(expect (nth 2 args) :to-equal nil)))
;; not going to test the exact phrasing of the prompt, but
;; at least 'toki pona' has to appear.
(kagi-test--expect-arg #'kagi-fastgpt-prompt 0 :to-match "toki pona")
;; called non-interactively
(kagi-test--expect-arg #'kagi-fastgpt-prompt 2 :to-equal nil))
(it "calls kagi-fastgpt-prompt non-interactively with source and target language in prompt"
(kagi-translate "bonjour" "toki pona" "French")
(let ((args (spy-calls-args-for #'kagi-fastgpt-prompt 0)))
;; has 'toki pona' in the prompt
(expect (nth 0 args) :to-match "toki pona")
;; and has French in the prompt
(expect (nth 0 args) :to-match "French")
;; called non-interactively
(expect (nth 2 args) :to-equal nil)))
;; has 'toki pona' in the prompt
(kagi-test--expect-arg #'kagi-fastgpt-prompt 0 :to-match "toki pona")
;; and has French in the prompt
(kagi-test--expect-arg #'kagi-fastgpt-prompt 0 :to-match "French")
;; called non-interactively
(kagi-test--expect-arg #'kagi-fastgpt-prompt 2 :to-equal nil))
(it "passes the region text to kagi-fastgpt-prompt, if active"
(spy-on #'use-region-p :and-return-value t)
(spy-on #'buffer-substring-no-properties :and-return-value "region text")
@ -166,19 +178,17 @@ https://www.example.com"
(spy-on #'region-end)
(spy-on #'kagi--read-language :and-return-value "toki pona")
(call-interactively #'kagi-translate)
(let ((args (spy-calls-args-for #'kagi-fastgpt-prompt 0)))
(expect (nth 0 args) :to-match "region text")
(expect (nth 0 args) :to-match "toki pona")
(expect (nth 2 args) :to-equal t)))
(kagi-test--expect-arg #'kagi-fastgpt-prompt 0 :to-match "region text")
(kagi-test--expect-arg #'kagi-fastgpt-prompt 0 :to-match "toki pona")
(kagi-test--expect-arg #'kagi-fastgpt-prompt 2 :to-equal t))
(it "passes the user input if the region is inactive"
(spy-on #'use-region-p)
(spy-on #'kagi--read-language :and-return-value "toki pona")
(spy-on #'read-buffer :and-return-value "user text")
(spy-on #'get-buffer)
(call-interactively #'kagi-translate)
(let ((args (spy-calls-args-for #'kagi-fastgpt-prompt 0)))
(expect (nth 0 args) :to-match "user text")
(expect (nth 2 args) :to-equal t)))
(kagi-test--expect-arg #'kagi-fastgpt-prompt 0 :to-match "user text")
(kagi-test--expect-arg #'kagi-fastgpt-prompt 2 :to-equal t))
(it "passes the buffer text if buffer is selected"
(spy-on #'use-region-p)
(spy-on #'kagi--read-language :and-return-value "toki pona")
@ -187,9 +197,8 @@ https://www.example.com"
(spy-on #'set-buffer)
(spy-on #'buffer-string :and-return-value "buffer text")
(call-interactively #'kagi-translate)
(let ((args (spy-calls-args-for #'kagi-fastgpt-prompt 0)))
(expect (nth 0 args) :to-match "buffer text")
(expect (nth 2 args) :to-equal t)))
(kagi-test--expect-arg #'kagi-fastgpt-prompt 0 :to-match "buffer text")
(kagi-test--expect-arg #'kagi-fastgpt-prompt 2 :to-equal t))
(it "raises an error when no text is given"
(spy-on #'use-region-p)
(spy-on #'kagi--read-language :and-return-value "toki pona")
@ -202,25 +211,22 @@ https://www.example.com"
(it "calls kagi-fastgpt-prompt"
(kagi-proofread "foo")
(expect #'kagi-fastgpt-prompt :to-have-been-called)
(let ((args (spy-calls-args-for #'kagi-fastgpt-prompt 0)))
(expect (nth 0 args) :to-match "foo")))
(kagi-test--expect-arg #'kagi-fastgpt-prompt 0 :to-match "foo"))
(it "passes the region text to kagi-fastgpt-prompt, if active"
(spy-on #'use-region-p :and-return-value t)
(spy-on #'buffer-substring-no-properties :and-return-value "region text")
(spy-on #'region-beginning)
(spy-on #'region-end)
(call-interactively #'kagi-proofread)
(let ((args (spy-calls-args-for #'kagi-fastgpt-prompt 0)))
(expect (nth 0 args) :to-match "region text")
(expect (nth 2 args) :to-equal t)))
(kagi-test--expect-arg #'kagi-fastgpt-prompt 0 :to-match "region text")
(kagi-test--expect-arg #'kagi-fastgpt-prompt 2 :to-equal t))
(it "passes the user input if the region is inactive"
(spy-on #'use-region-p)
(spy-on #'read-buffer :and-return-value "user text")
(spy-on #'get-buffer)
(call-interactively #'kagi-proofread)
(let ((args (spy-calls-args-for #'kagi-fastgpt-prompt 0)))
(expect (nth 0 args) :to-match "user text")
(expect (nth 2 args) :to-equal t)))
(kagi-test--expect-arg #'kagi-fastgpt-prompt 0 :to-match "user text")
(kagi-test--expect-arg #'kagi-fastgpt-prompt 2 :to-equal t))
(it "passes the buffer text if buffer is selected"
(spy-on #'use-region-p)
(spy-on #'read-buffer)
@ -228,9 +234,8 @@ https://www.example.com"
(spy-on #'set-buffer)
(spy-on #'buffer-string :and-return-value "buffer text")
(call-interactively #'kagi-proofread)
(let ((args (spy-calls-args-for #'kagi-fastgpt-prompt 0)))
(expect (nth 0 args) :to-match "buffer text")
(expect (nth 2 args) :to-equal t)))
(kagi-test--expect-arg #'kagi-fastgpt-prompt 0 :to-match "buffer text")
(kagi-test--expect-arg #'kagi-fastgpt-prompt 2 :to-equal t))
(it "raises an error when no text is given"
(spy-on #'use-region-p)
(spy-on #'read-buffer :and-return-value "")
@ -269,33 +274,26 @@ https://www.example.com"
(expect (kagi-summarize nil) :to-throw))
(it "returns a summary for a valid language code"
(expect (kagi-summarize just-enough-text-input "NL" :to-equal dummy-output))
(let ((args (car (spy-calls-args-for #'kagi--call-summarizer 0))))
(expect (map-elt args "target_language") :to-equal "NL")))
(kagi-test--expect-object #'kagi--call-summarizer "target_language" :to-equal "NL"))
(it "returns a summary for a valid language code with wrong capitalization"
(expect (kagi-summarize just-enough-text-input "nL" :to-equal dummy-output))
(let ((args (car (spy-calls-args-for #'kagi--call-summarizer 0))))
(expect (map-elt args "target_language") :to-equal "NL")))
(kagi-test--expect-object #'kagi--call-summarizer "target_language" :to-equal "NL"))
(it "returns a summary for a valid language name"
(expect (kagi-summarize just-enough-text-input "Dutch" :to-equal dummy-output))
(let ((args (car (spy-calls-args-for #'kagi--call-summarizer 0))))
(expect (map-elt args "target_language") :to-equal "NL")))
(kagi-test--expect-object #'kagi--call-summarizer "target_language" :to-equal "NL"))
(it "returns a summary for a valid language name with different capitalization"
(expect (kagi-summarize just-enough-text-input "dUtch" :to-equal dummy-output))
(let ((args (car (spy-calls-args-for #'kagi--call-summarizer 0))))
(expect (map-elt args "target_language") :to-equal "NL")))
(kagi-test--expect-object #'kagi--call-summarizer "target_language" :to-equal "NL"))
(it "falls back to the default language for invalid language codes"
(expect (kagi-summarize just-enough-text-input "VL") :to-equal dummy-output)
(let ((args (car (spy-calls-args-for #'kagi--call-summarizer 0))))
(expect (map-elt args "target_language") :to-equal "NL")))
(kagi-test--expect-object #'kagi--call-summarizer "target_language" :to-equal "NL"))
(it "falls back to the default language for invalid language names"
(expect (kagi-summarize just-enough-text-input "Valyrian") :to-equal dummy-output)
(let ((args (car (spy-calls-args-for #'kagi--call-summarizer 0))))
(expect (map-elt args "target_language") :to-equal "NL")))
(kagi-test--expect-object #'kagi--call-summarizer "target_language" :to-equal "NL"))
(it "falls back to English if the default language is invalid"
(setq kagi-summarizer-default-language "XY")
(expect (kagi-summarize just-enough-text-input "Valyrian") :to-equal dummy-output)
(let ((args (car (spy-calls-args-for #'kagi--call-summarizer 0))))
(expect (map-elt args "target_language") :to-equal "EN")))
(kagi-test--expect-object #'kagi--call-summarizer "target_language" :to-equal "EN"))
(it "returns a summary for an HTTPS URL"
(expect (kagi-summarize dummy-https-url) :to-equal dummy-output))
(it "returns a summary for an uppercase HTTPS URL"
@ -306,30 +304,24 @@ https://www.example.com"
(expect (kagi-summarize dummy-ftp-url) :to-throw))
(it "returns a summary for a valid engine with different capitalization"
(expect (kagi-summarize dummy-https-url nil "Daphne") :to-equal dummy-output)
(let ((args (car (spy-calls-args-for #'kagi--call-summarizer 0))))
(expect (map-elt args "engine") :to-equal "daphne"))
)
(kagi-test--expect-object #'kagi--call-summarizer "engine" :to-equal "daphne"))
(it "uses kagi-summarizer-engine variable for invalid engine values"
(setq kagi-summarizer-engine "Daphne")
(expect (kagi-summarize dummy-https-url nil "bram") :to-equal dummy-output)
(let ((args (car (spy-calls-args-for #'kagi--call-summarizer 0))))
(expect (map-elt args "engine") :to-equal "daphne")))
(kagi-test--expect-object #'kagi--call-summarizer "engine" :to-equal "daphne"))
(it "uses the cecil engine when an invalid engine is configured"
(setq kagi-summarizer-engine "steve")
(expect (kagi-summarize dummy-https-url) :to-equal dummy-output)
(let ((args (car (spy-calls-args-for #'kagi--call-summarizer 0))))
(expect (map-elt args "engine") :to-equal "cecil")))
(kagi-test--expect-object #'kagi--call-summarizer "engine" :to-equal "cecil"))
(it "returns a summary when the take-away style is requested"
(expect (kagi-summarize just-enough-text-input nil nil 'takeaway) :to-equal dummy-output))
(it "uses the summary style when an invalid format is given"
(kagi-summarize just-enough-text-input nil nil 'invalid)
(let ((args (car (spy-calls-args-for #'kagi--call-summarizer 0))))
(expect (map-elt args "summary_type") :to-equal 'summary)))
(kagi-test--expect-object #'kagi--call-summarizer "summary_type" :to-equal 'summary))
(it "uses the summary style when an invalid format is configured"
(setq kagi-summarizer-default-summary-format 'invalid)
(kagi-summarize just-enough-text-input)
(let ((args (car (spy-calls-args-for #'kagi--call-summarizer 0))))
(expect (map-elt args "summary_type") :to-equal 'summary))))
(kagi-test--expect-object #'kagi--call-summarizer "summary_type" :to-equal 'summary)))
(describe "kagi-summarize-buffer"
(before-each
(spy-on #'read-buffer)
@ -365,9 +357,8 @@ https://www.example.com"
(expect #'kagi-summarize :to-have-been-called)
(expect #'kagi--display-summary :not :to-have-been-called)
(expect #'kagi--insert-summary :to-have-been-called)
(let ((args (spy-calls-args-for #'kagi-summarize 0)))
(expect (nth 1 args) :to-equal 'lang)
(expect (nth 2 args) :to-equal 'bram)
(expect (nth 3 args) :to-equal 'random))))))
(kagi-test--expect-arg #'kagi-summarize 1 :to-equal 'lang)
(kagi-test--expect-arg #'kagi-summarize 2 :to-equal 'bram)
(kagi-test--expect-arg #'kagi-summarize 3 :to-equal 'random)))))
;;; kagi-test.el ends here