plugins_basic_test.clj 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. (ns logseq.e2e.plugins-basic-test
  2. (:require
  3. [clojure.string :as string]
  4. [clojure.test :refer [deftest testing is use-fixtures]]
  5. [logseq.e2e.fixtures :as fixtures]
  6. [wally.main :as w]
  7. [wally.repl :as repl]))
  8. (use-fixtures :once fixtures/open-page)
  9. (use-fixtures :each fixtures/new-logseq-page)
  10. (defn- to-snake-case
  11. "Converts a string to snake_case. Handles camelCase, PascalCase, spaces, hyphens, and existing underscores.
  12. Examples:
  13. 'HelloWorld' -> 'hello_world'
  14. 'Hello World' -> 'hello_world'
  15. 'hello-world' -> 'hello_world'
  16. 'Hello__World' -> 'hello_world'"
  17. [s]
  18. (when (string? s)
  19. (-> s
  20. ;; Normalize input: replace hyphens/spaces with underscores, collapse multiple underscores
  21. (clojure.string/replace #"[-\s]+" "_")
  22. ;; Split on uppercase letters (except at start) and join with underscore
  23. (clojure.string/replace #"(?<!^)([A-Z])" "_$1")
  24. ;; Remove redundant underscores and trim
  25. (clojure.string/replace #"_+" "_")
  26. (clojure.string/trim)
  27. ;; Convert to lowercase
  28. (clojure.string/lower-case))))
  29. (defn- ls-api-call!
  30. [tag & args]
  31. (let [tag (name tag)
  32. ns' (string/split tag #"\.")
  33. ns? (and (seq ns') (= (count ns') 2))
  34. ns1 (string/lower-case (if ns? (str "sdk." (first ns')) "api"))
  35. name1 (if ns? (to-snake-case (last ns')) tag)]
  36. (w/eval-js
  37. (format "args => { const o=logseq.%1$s; return o['%2$s']?.apply(null, args || []); }" ns1 name1)
  38. (vec args))))
  39. (deftest apis-related-test
  40. (testing "block related apis"
  41. (ls-api-call! :ui.showMsg "hello world" "error")))