emulate -L zsh
zle() {
[[ "$1" == -* ]] && return 0
(( $+functions[$1] )) && "$1"
}
bindkey() { : }
export NIGHTHAWK_DEBOUNCE_MS=foo
_nh_tmpcfg=$(mktemp -d)
export XDG_CONFIG_HOME=$_nh_tmpcfg
_nh_plugin="${0:A:h}/../nighthawk.zsh"
if [[ ! -r "$_nh_plugin" ]]; then
print -u2 "async.test.zsh: cannot read $_nh_plugin"
rm -rf "$_nh_tmpcfg"
exit 2
fi
source "$_nh_plugin"
rm -rf "$_nh_tmpcfg"
if (( ! $+functions[_nh_handle_response] || ! $+functions[_nh_reply_is_fresh] || ! $+functions[_nh_has_ctrl_char] )); then
print -u2 "async.test.zsh: FAIL — async functions not defined after sourcing (renamed? deps missing?)"
exit 2
fi
typeset -gi _nh_pass=0 _nh_fail=0
check() { if [[ "$2" == "$3" ]]; then
(( _nh_pass++ ))
else
(( _nh_fail++ ))
print -r -- "FAIL: $1 — expected '$2' got '$3'"
fi
}
reset_state() { _nh_suggestion=""
_nh_replace_start=""
_nh_replace_end=""
_nh_diff_ops=""
_nh_original_buffer=""
_nh_has_highlight=0
region_highlight=()
unset POSTDISPLAY
_nh_inflight_buffer="$1"
_nh_inflight_cursor="$2"
BUFFER="$1"
CURSOR="$2"
}
DEL=$'\x7f'
reset_state "git ch" 6
_nh_handle_response '{"suggestions":[{"text":"git checkout","replace_start":0,"replace_end":6}]}'
check "prefix: suggestion published" "git checkout" "$_nh_suggestion"
check "prefix: ghost is the suffix" "eckout" "$POSTDISPLAY"
reset_state "gti" 3
_nh_handle_response '{"suggestions":[{"text":"git status","replace_start":0,"replace_end":3}]}'
check "replacement: suggestion published" "git status" "$_nh_suggestion"
check "replacement: rendered as hint" " $_nh_hint_arrow git status" "$POSTDISPLAY"
reset_state "gti" 3
_nh_handle_response '{"suggestions":[{"text":"git status","replace_start":0,"replace_end":3,"diff_ops":[{"op":"keep","ch":"g"},{"op":"insert","ch":"i"}]}]}'
check "fuzzy hint: suggestion published" "git status" "$_nh_suggestion"
check "fuzzy hint: hint rendered" " $_nh_hint_arrow git status" "$POSTDISPLAY"
reset_state "ls" 2
_nh_handle_response '{"suggestions":[{"text":"ls'"${DEL}"'x","replace_start":0,"replace_end":2}]}'
check "ctrl in text: nothing published" "" "$_nh_suggestion"
check "ctrl in text: no ghost" "" "${POSTDISPLAY-}"
reset_state "gti" 3
_nh_handle_response '{"suggestions":[{"text":"git status","replace_start":0,"replace_end":3,"diff_ops":[{"op":"insert","ch":"'"${DEL}"'"}]}]}'
check "ctrl in diff_ops: nothing published" "" "$_nh_suggestion"
check "ctrl in diff_ops: no ghost" "" "${POSTDISPLAY-}"
reset_state "ls" 2
_nh_handle_response '{"suggestions":[{"text":"ls -la","replace_start":0,"replace_end":99}]}'
check "oob range: nothing published" "" "$_nh_suggestion"
check "oob range: no ghost" "" "${POSTDISPLAY-}"
reset_state "git" 3
_nh_handle_response '{"suggestions":[]}'
check "empty list: nothing published" "" "$_nh_suggestion"
ctrl_of() { _nh_has_ctrl_char "$1" && print detected || print clean }
check "ctrl primitive: 0x01" detected "$(ctrl_of $'\x01')"
check "ctrl primitive: 0x1f" detected "$(ctrl_of $'\x1f')"
check "ctrl primitive: 0x7f (DEL)" detected "$(ctrl_of $'\x7f')"
check "ctrl primitive: newline" detected "$(ctrl_of $'a\nb')"
check "ctrl primitive: clean text" clean "$(ctrl_of 'git checkout --force')"
fresh_of() { _nh_gen=$1 _nh_dispatch_gen=$2 BUFFER="$3" _nh_inflight_buffer="$4" CURSOR=$5 _nh_inflight_cursor=$6
_nh_reply_is_fresh && print fresh || print stale
}
check "fresh: all match" fresh "$(fresh_of 5 5 git git 3 3)"
check "stale: generation moved" stale "$(fresh_of 6 5 git git 3 3)"
check "fresh: buffer diff ignored" fresh "$(fresh_of 5 5 gitx git 4 3)"
check "fresh: cursor diff ignored" fresh "$(fresh_of 5 5 git git 2 3)"
check "debounce_ms clamped from non-numeric" 200 "$_nh_debounce_ms"
reset_state "git ch" 6
_nh_gen=20 _nh_dispatch_gen=20
_nh_resp_accum=""
_nh_t=$(mktemp)
print -r -- '{"suggestions":[{"text":"git checkout","replace_start":0,"replace_end":6}]}' > "$_nh_t"
exec {_nh_fd}< "$_nh_t"; _nh_resp_fd=$_nh_fd
_nh_on_response $_nh_fd ""
check "on_response: full line renders ghost" "eckout" "${POSTDISPLAY-}"
check "on_response: fd torn down after finalize" 0 "$_nh_resp_fd"
rm -f "$_nh_t"
reset_state "git ch" 6
_nh_gen=21 _nh_dispatch_gen=21
_nh_resp_accum='{"suggestions":[{"text":"git che'
_nh_t=$(mktemp)
print -r -- 'ckout","replace_start":0,"replace_end":6}]}' > "$_nh_t"
exec {_nh_fd}< "$_nh_t"; _nh_resp_fd=$_nh_fd
_nh_on_response $_nh_fd ""
check "on_response: accumulates across chunks" "eckout" "${POSTDISPLAY-}"
rm -f "$_nh_t"
reset_state "ls" 2
_nh_gen=22 _nh_dispatch_gen=22
_nh_resp_accum=""
_nh_fifo=$(mktemp -u); mkfifo "$_nh_fifo"
exec {_nh_fd}<> "$_nh_fifo" print -rn -- '{"suggestions":[{"text":"ls' >&$_nh_fd
_nh_resp_fd=$_nh_fd
_nh_on_response $_nh_fd ""
check "on_response: partial w/o newline does not finalize" "" "${POSTDISPLAY-}"
check "on_response: partial keeps fd registered" "$_nh_fd" "$_nh_resp_fd"
exec {_nh_fd}<&-; rm -f "$_nh_fifo"
reset_state "git ch" 6
_nh_gen=24 _nh_dispatch_gen=23 _nh_resp_accum=""
_nh_t=$(mktemp)
print -r -- '{"suggestions":[{"text":"git checkout","replace_start":0,"replace_end":6}]}' > "$_nh_t"
exec {_nh_fd}< "$_nh_t"; _nh_resp_fd=$_nh_fd
_nh_on_response $_nh_fd ""
check "on_response: stale generation renders nothing" "" "${POSTDISPLAY-}"
rm -f "$_nh_t"
print -r -- "async.test.zsh: $_nh_pass passed, $_nh_fail failed"
(( _nh_fail == 0 ))