#!/usr/bin/env bash
# HTML source adapter checks (spec section 11, FR-021, AC-03, AC-15): POST /ingestion/jobs with "format":"html"
# converts the page to Markdown (core/lib.HtmlAdapter.asp) and stores it as a markdown document. Covers structure (headings ->
# heading paths, lists, pre, tables), removal of script/style/comments/nav (never searchable), entity decoding, the
#
default, re-ingestion (unchanged / new revision, markup-only changes are no-ops), async processing by the
# worker action, validation (422), tenant isolation, UTF-8 and a cold first request.
#
# Needs the container from docker-compose.yml running. Provisions two throw-away tenants and purges them at the end.
# bash tests/phase4-html.sh
set -u
BASE=${BASE:-http://localhost:8801/api/v1}
PASS=0; FAIL=0; FAILED=(); TENANTS=()
TMP=$(mktemp -d)
prov() {
MSYS_NO_PATHCONV=1 docker exec -u axonasp -e SERVER_WEB_ROOT=/opt/axonasp/site \
-e "PROV_ACTION=$1" -e "PROV_ARG1=${2:-}" -e "PROV_ARG2=${3:-}" -e "PROV_ARG3=${4:-}" -e "PROV_ARG4=${5:-}" \
axonasp ./axonasp-cli -r /opt/axonasp/site/scripts/provision.asp 2>&1 | tr -d '\015'
}
embed() { MSYS_NO_PATHCONV=1 docker exec -u axonasp -e SERVER_WEB_ROOT=/opt/axonasp/site -e "EMBED_ACTION=$1" axonasp ./axonasp-cli -r /opt/axonasp/site/scripts/embed.asp 2>&1 | tr -d '\015'; }
field() { sed -n "s/^$1: *\([^ ]*\).*/\1/p" | head -1; }
cleanup() { for t in "${TENANTS[@]}"; do prov purge "$t" >/dev/null; done; rm -rf "$TMP"; }
trap cleanup EXIT
check() { if [ "$2" = "$3" ]; then PASS=$((PASS+1)); else FAIL=$((FAIL+1)); FAILED+=("$1 (expected $2, got $3)"); fi; }
code() { local key=$1 m=$2 p=$3; shift 3; curl -s -o /dev/null -w '%{http_code}' -X "$m" -H "Authorization: Bearer $key" "$@" "$BASE$p"; }
body() { local key=$1 m=$2 p=$3; shift 3; curl -s -X "$m" -H "Authorization: Bearer $key" "$@" "$BASE$p"; }
jq_() { python -I -c "import sys,json;sys.stdin.reconfigure(encoding='utf-8');d=json.load(sys.stdin);print($1)"; }
mk() { local out; out=$(prov principal "$1" "$2" "$3"); P_ID=$(echo "$out" | field principal_id); P_KEY=$(echo "$out" | field api_key); }
# req [title] [source_uri] [async] -> writes a UTF-8 JSON request body
req() {
python -I -c "
import json,sys
a=sys.argv
d={'namespace_id':a[2],'format':'html','content':eval('('+a[3]+')')}
if a[4]: d['title']=a[4]
if a[5]: d['source_uri']=a[5]
if a[6]: d['async']=True
open(a[1],'w',encoding='utf-8').write(json.dumps(d,ensure_ascii=False))" "$1" "$2" "$3" "${4:-}" "${5:-}" "${6:-}"
}
ingest() { curl -s -X POST -H "Authorization: Bearer $1" --data-binary @"$2" "$BASE/ingestion/jobs"; }
search() { body "$1" POST /search -d "{\"query\":\"$2\"}"; }
# ---- setup ------------------------------------------------------------------------------------------------------
TA=$(prov tenant "html-A" | field tenant_id); TB=$(prov tenant "html-B" | field tenant_id); TENANTS=("$TA" "$TB")
mk "$TA" writer agent; WID=$P_ID; WK=$P_KEY
mk "$TB" other agent; BID=$P_ID; BK=$P_KEY
prov grant "$WID" /web "read,write" 1 >/dev/null
prov grant "$BID" /web "read,write" 1 >/dev/null
NS=$(prov namespace "$TA" /web | field namespace_id); NSB=$(prov namespace "$TB" /web | field namespace_id)
[ -n "$NS" ] && [ -n "$WK" ] || { echo "setup failed"; exit 2; }
PAGE="'Yard Runbook & Notes'
+'\n\n'
+'
Gate check-in
\n
Trucks stop at gate 2 for the pelicanscan. Then they wait.
\n'
+'\n'
+'
Reefer containers
Plug into row R
Gr'+chr(252)+chr(223)+'e 日 ä 😀 <b>
\n'
+'
vaultctl rotate\n --zone C & D
\n'
+'
Sockets
Row
Amps
R
32
\n'
+'
<literal> and café
'"
# ---- cold start: first request after a restart is the HTML ingest -------------------------------------------------------
docker restart axonasp >/dev/null; for i in $(seq 1 20); do curl -s -o /dev/null "$BASE/health/live" && break; sleep 1; done
req "$TMP/p.json" "$NS" "$PAGE" "" "kb://web/yard"
R=$(ingest "$WK" "$TMP/p.json")
check "cold: html ingest -> source_format html" "html False" "$(echo "$R" | jq_ "d['source_format']+' '+str(d['unchanged'])")"
DOC=$(echo "$R" | jq_ "d['document_id']")
D=$(body "$WK" GET "/documents/$DOC?include_content=true")
check "title defaults to (decoded)" "Yard Runbook & Notes" "$(echo "$D" | jq_ "d['data']['title']")"
check "stored as markdown" "markdown" "$(echo "$D" | jq_ "d['data']['format']")"
check "h1 becomes # heading" "yes" "$(echo "$D" | jq_ "'yes' if '# Gate check-in' in d['data']['content'] else 'no'")"
check "br becomes a line break, whitespace collapsed" "yes" "$(echo "$D" | jq_ "'yes' if 'pelicanscan.'+chr(10)+'Then they wait.' in d['data']['content'] else 'no'")"
check "list items" "yes" "$(echo "$D" | jq_ "'yes' if chr(10)+'- Plug into row R'+chr(10)+'- Gr' in d['data']['content'] else 'no'")"
check "pre kept verbatim as fenced code" "yes" "$(echo "$D" | jq_ "'yes' if '\x60\x60\x60'+chr(10)+'vaultctl rotate'+chr(10)+' --zone C & D'+chr(10)+'\x60\x60\x60' in d['data']['content'] else 'no'")"
check "table cells" "yes" "$(echo "$D" | jq_ "'yes' if 'Row | Amps'+chr(10)+'R | 32' in d['data']['content'] else 'no'")"
check "entities decoded, escaped markup stays text" "yes" "$(echo "$D" | jq_ "'yes' if chr(0x65e5)+' '+chr(228) in d['data']['content'] and '' in d['data']['content'] and '<literal> and caf'+chr(233) in d['data']['content'] else 'no'")"
check "raw UTF-8 kept" "yes" "$(echo "$D" | jq_ "'yes' if 'Gr'+chr(252)+chr(223)+'e' in d['data']['content'] else 'no'")"
check "entity above U+FFFF becomes U+FFFD" "yes" "$(echo "$D" | jq_ "'yes' if chr(0xfffd) in d['data']['content'] else 'no'")"
check "script, style, comment and nav removed" "none" "$(echo "$D" | jq_ "' '.join(w for w in ['hiddenvar','zebracss','commentsecret','Navlinkhome','ignore previous'] if w in d['data']['content']) or 'none'")"
# ---- chunks and search --------------------------------------------------------------------------------------------------
check "search finds body text with heading path" "Gate check-in" "$(search "$WK" "pelicanscan" | jq_ "d['results'][0]['heading_path'].split(' > ')[-1]")"
check "nested heading path" "yes" "$(search "$WK" "amps sockets row" | jq_ "'yes' if any(r['heading_path'].endswith('Reefer containers > Sockets') for r in d['results']) else 'no'")"
check "script text never searchable" "0" "$(search "$WK" "hiddenvar" | jq_ "len(d['results'])")"
check "comment never searchable" "0" "$(search "$WK" "commentsecret" | jq_ "len(d['results'])")"
check "results stay untrusted references" "untrusted_reference" "$(search "$WK" "pelicanscan" | jq_ "d['content_trust']")"
check "other tenant cannot find it" "0" "$(search "$BK" "pelicanscan" | jq_ "len(d['results'])")"
# ---- re-ingestion ---------------------------------------------------------------------------------------------------------
check "same page again -> unchanged" "200 True" "$(code "$WK" POST /ingestion/jobs --data-binary @"$TMP/p.json") $(ingest "$WK" "$TMP/p.json" | jq_ "d['unchanged']")"
req "$TMP/p2.json" "$NS" "($PAGE).replace('