#!/bin/bash
#
# character_test.sh — Regression tests for the ^-character forms the engine
# resolves at read time: the ^UUUU^ Unicode code point (4-6 hex digits,
# since 2026-08-23 covering the full range to U+10FFFF), and the character
# tables (mnemonics, diacritics) it must not disturb.
#
# SKS-independent: character handling is katomizer/engine machinery
# (mac/ktype.h nonascii katom, mac/character.cpp), so everything runs with
# --klammersets none under the default (general) target.
#
# The 2026-08-23 changes these cases pin:
#   - a 5-digit code point converts WHOLE: ^13000^ (EGYPTIAN HIEROGLYPH
#     A001) rendered as U+1300 followed by a literal "0" before, because
#     process_unicode_codepoint() re-scanned its capture at a fixed 4-digit
#     width;
#   - 6 digits are accepted (^10FFFD^ is the top of the range; the katom
#     regex capped at {1,5});
#   - a value above U+10FFFF renders as the "Invalid Unicode" text, the
#     same treatment as a surrogate.
#
# Note: expected strings use $'\xHH' UTF-8 BYTE escapes, not $'\U...' code
# points -- macOS ships bash 3.2, which does not expand \U (it failed there
# exactly as a naive check would).
#
# Usage: ./character_test.sh   (needs KLAMMERTEXT_HOME set; ktext on PATH)
# Exit code: 0 if all tests pass, 1 otherwise.

PASS=0
FAIL=0
KTEXT=ktext
K=${KLAMMERTEXT_HOME:?KLAMMERTEXT_HOME must be set}
ERR=$(mktemp)

red=$'\033[31m'
green=$'\033[32m'
bold=$'\033[1m'
reset=$'\033[0m'

trim() { awk '{ sub(/[ \t\r]+$/, "") } { line[NR]=$0 } END { f=1; while (f<=NR && line[f]=="") f++; l=NR; while (l>=1 && line[l]=="") l--; for (i=f;i<=l;i++) print line[i] }'; }

# check NAME EXPECTED INPUT — render INPUT (no klammer set, default
# target); exit 0 and stdout == EXPECTED.
check() {
    local name="$1" expected="$2" input="$3"
    local out status
    out=$("$KTEXT" --klammersets none -s "$input" -d 2>"$ERR"); status=$?
    out=$(printf '%s' "$out" | trim)
    if [ $status -ne 0 ]; then
        echo "${red}FAIL${reset} $name — ktext exited $status"
        head -3 "$ERR" | sed 's/^/       /'; FAIL=$((FAIL+1)); return
    fi
    if [ "$out" = "$expected" ]; then
        echo "${green}PASS${reset} $name"; PASS=$((PASS+1))
    else
        echo "${red}FAIL${reset} $name"
        echo "       expected: [$expected]"; echo "       got:      [$out]"; FAIL=$((FAIL+1))
    fi
}

echo "${bold}Character form tests (engine: ^UUUU^ code points and the tables)${reset}"
echo "================================================================="
echo

check " 1. 4-digit BMP code point"        $'A ☺ B'      'A ^263A^ B'
check " 2. 5-digit code point, whole"     $'A \xf0\x93\x80\x80 B'  'A ^13000^ B'
check " 3. 5-digit emoji"                 $'\xf0\x9f\x98\x80'  '^1F600^'
check " 4. 6-digit top of the range"      $'\xf4\x8f\xbf\xbd'  '^10FFFD^'
check " 5. above U+10FFFF is invalid"     'Invalid Unicode: 16777215' '^FFFFFF^'
check " 6. surrogate is invalid"          'Invalid Unicode: 55296'    '^D800^'
check " 7. mnemonic undisturbed"          'ß'                '^s^'
# The diacritic tables compose base + COMBINING mark (a U+0308), not the
# precomposed letter -- the expected string is built the same way.
check " 8. diacritic undisturbed"         $'Ma\xcc\x88dchen'  'M^a"dchen'
check " 9. code point beside quoted punctuation" $'☺ --' '^263A^ ^-^-'

echo
echo "================================================================="
echo "${bold}Results: $PASS passed, $FAIL failed${reset}"
rm -f "$ERR"
[ $FAIL -eq 0 ] && exit 0 || exit 1
