;; prelude-extra.sx — tests for Phase 6 prelude additions:
;; ord/isAlpha/isDigit/isSpace/isUpper/isLower/isAlphaNum/digitToInt
;; words/lines/unwords/unlines/sort/nub/splitAt/span/break
;; partition/intercalate/intersperse/isPrefixOf/isSuffixOf/isInfixOf

;; ── ord ──────────────────────────────────────────────────────
(hk-test "ord 'A'" (hk-eval-expr-source "ord 'A'") 65)
(hk-test "ord 'a'" (hk-eval-expr-source "ord 'a'") 97)
(hk-test "ord '0'" (hk-eval-expr-source "ord '0'") 48)

;; ── isAlpha / isDigit / isSpace / isUpper / isLower ──────────
(hk-test
  "isAlpha 'a' True"
  (hk-eval-expr-source "isAlpha 'a'")
  (list "True"))
(hk-test
  "isAlpha 'Z' True"
  (hk-eval-expr-source "isAlpha 'Z'")
  (list "True"))
(hk-test
  "isAlpha '3' False"
  (hk-eval-expr-source "isAlpha '3'")
  (list "False"))
(hk-test
  "isDigit '5' True"
  (hk-eval-expr-source "isDigit '5'")
  (list "True"))
(hk-test
  "isDigit 'a' False"
  (hk-eval-expr-source "isDigit 'a'")
  (list "False"))
(hk-test
  "isSpace ' ' True"
  (hk-eval-expr-source "isSpace ' '")
  (list "True"))
(hk-test
  "isSpace 'x' False"
  (hk-eval-expr-source "isSpace 'x'")
  (list "False"))
(hk-test
  "isUpper 'A' True"
  (hk-eval-expr-source "isUpper 'A'")
  (list "True"))
(hk-test
  "isUpper 'a' False"
  (hk-eval-expr-source "isUpper 'a'")
  (list "False"))
(hk-test
  "isLower 'z' True"
  (hk-eval-expr-source "isLower 'z'")
  (list "True"))
(hk-test
  "isLower 'Z' False"
  (hk-eval-expr-source "isLower 'Z'")
  (list "False"))
(hk-test
  "isAlphaNum '3' True"
  (hk-eval-expr-source "isAlphaNum '3'")
  (list "True"))
(hk-test
  "isAlphaNum 'b' True"
  (hk-eval-expr-source "isAlphaNum 'b'")
  (list "True"))
(hk-test
  "isAlphaNum '!' False"
  (hk-eval-expr-source "isAlphaNum '!'")
  (list "False"))

;; ── digitToInt ───────────────────────────────────────────────
(hk-test "digitToInt '0'" (hk-eval-expr-source "digitToInt '0'") 0)
(hk-test "digitToInt '7'" (hk-eval-expr-source "digitToInt '7'") 7)
(hk-test "digitToInt '9'" (hk-eval-expr-source "digitToInt '9'") 9)

;; ── words ────────────────────────────────────────────────────
(hk-test
  "words single"
  (hk-deep-force (hk-eval-expr-source "words \"hello\""))
  (list ":" "hello" (list "[]")))

(hk-test
  "words two"
  (hk-deep-force (hk-eval-expr-source "words \"hello world\""))
  (list ":" "hello" (list ":" "world" (list "[]"))))

(hk-test
  "words leading/trailing spaces"
  (hk-deep-force (hk-eval-expr-source "words \"  foo  bar  \""))
  (list ":" "foo" (list ":" "bar" (list "[]"))))

(hk-test
  "words empty string"
  (hk-deep-force (hk-eval-expr-source "words \"\""))
  (list "[]"))

;; ── lines ────────────────────────────────────────────────────
(hk-test
  "lines single no newline"
  (hk-deep-force (hk-eval-expr-source "lines \"hello\""))
  (list ":" "hello" (list "[]")))

(hk-test
  "lines two lines"
  (hk-deep-force (hk-eval-expr-source "lines \"a\\nb\""))
  (list ":" "a" (list ":" "b" (list "[]"))))

(hk-test
  "lines trailing newline"
  (hk-deep-force (hk-eval-expr-source "lines \"a\\n\""))
  (list ":" "a" (list "[]")))

(hk-test
  "lines empty string"
  (hk-deep-force (hk-eval-expr-source "lines \"\""))
  (list "[]"))

;; ── unwords / unlines ────────────────────────────────────────
(hk-test
  "unwords two"
  (hk-eval-expr-source "unwords [\"hello\", \"world\"]")
  "hello world")

(hk-test "unwords empty" (hk-eval-expr-source "unwords []") "")

(hk-test "unlines two" (hk-eval-expr-source "unlines [\"a\", \"b\"]") "a\nb\n")

;; ── sort / nub ───────────────────────────────────────────────
(hk-test
  "sort ascending"
  (hk-deep-force (hk-eval-expr-source "sort [3,1,2]"))
  (list ":" 1 (list ":" 2 (list ":" 3 (list "[]")))))

(hk-test
  "sort already sorted"
  (hk-deep-force (hk-eval-expr-source "sort [1,2,3]"))
  (list ":" 1 (list ":" 2 (list ":" 3 (list "[]")))))

(hk-test
  "nub removes duplicates"
  (hk-deep-force (hk-eval-expr-source "nub [1,2,1,3,2]"))
  (list ":" 1 (list ":" 2 (list ":" 3 (list "[]")))))

(hk-test
  "nub no duplicates unchanged"
  (hk-deep-force (hk-eval-expr-source "nub [1,2,3]"))
  (list ":" 1 (list ":" 2 (list ":" 3 (list "[]")))))

;; ── splitAt ──────────────────────────────────────────────────
(hk-test
  "splitAt 2"
  (hk-deep-force (hk-eval-expr-source "splitAt 2 [1,2,3,4]"))
  (list
    "Tuple"
    (list ":" 1 (list ":" 2 (list "[]")))
    (list ":" 3 (list ":" 4 (list "[]")))))

(hk-test
  "splitAt 0"
  (hk-deep-force (hk-eval-expr-source "splitAt 0 [1,2,3]"))
  (list
    "Tuple"
    (list "[]")
    (list ":" 1 (list ":" 2 (list ":" 3 (list "[]"))))))

;; ── span / break ─────────────────────────────────────────────
(hk-test
  "span digits"
  (hk-deep-force (hk-eval-expr-source "span (\\x -> x < 3) [1,2,3,4]"))
  (list
    "Tuple"
    (list ":" 1 (list ":" 2 (list "[]")))
    (list ":" 3 (list ":" 4 (list "[]")))))

(hk-test
  "break digits"
  (hk-deep-force (hk-eval-expr-source "break (\\x -> x >= 3) [1,2,3,4]"))
  (list
    "Tuple"
    (list ":" 1 (list ":" 2 (list "[]")))
    (list ":" 3 (list ":" 4 (list "[]")))))

;; ── partition ────────────────────────────────────────────────
(hk-test
  "partition even/odd"
  (hk-deep-force
    (hk-eval-expr-source "partition (\\x -> x `mod` 2 == 0) [1,2,3,4,5]"))
  (list
    "Tuple"
    (list ":" 2 (list ":" 4 (list "[]")))
    (list ":" 1 (list ":" 3 (list ":" 5 (list "[]"))))))

;; ── intercalate / intersperse ────────────────────────────────
(hk-test
  "intercalate"
  (hk-eval-expr-source "intercalate \", \" [\"a\", \"b\", \"c\"]")
  "a, b, c")

(hk-test
  "intersperse"
  (hk-deep-force (hk-eval-expr-source "intersperse 0 [1,2,3]"))
  (list
    ":"
    1
    (list
      ":"
      0
      (list ":" 2 (list ":" 0 (list ":" 3 (list "[]")))))))

;; ── isPrefixOf / isSuffixOf / isInfixOf ──────────────────────
(hk-test
  "isPrefixOf True"
  (hk-deep-force (hk-eval-expr-source "isPrefixOf [1,2] [1,2,3]"))
  (list "True"))

(hk-test
  "isPrefixOf False"
  (hk-deep-force (hk-eval-expr-source "isPrefixOf [2,3] [1,2,3]"))
  (list "False"))

(hk-test
  "isSuffixOf True"
  (hk-deep-force (hk-eval-expr-source "isSuffixOf [2,3] [1,2,3]"))
  (list "True"))

(hk-test
  "isInfixOf True"
  (hk-deep-force (hk-eval-expr-source "isInfixOf [2,3] [1,2,3,4]"))
  (list "True"))

(hk-test
  "isInfixOf False"
  (hk-deep-force (hk-eval-expr-source "isInfixOf [5,6] [1,2,3,4]"))
  (list "False"))

{:fails hk-test-fails :pass hk-test-pass :fail hk-test-fail}
