#!/usr/bin/env bash
# Copyright 2026 ddbsh reimplementation contributors.
# SPDX-License-Identifier: Apache-2.0
#
# devtools/run-web-smoke — build the browser edition and run its quick gates
# (P9 §4.5).
#
# The browser edition is ddbsh.wasm + the vendored AWS SDK for JavaScript v3,
# calling DynamoDB directly from the tab with user-supplied short-term
# credentials. This script is the quick companion to devtools/run-web-golden
# (which runs the whole golden suite through the same path):
#
#   1. locate emsdk + Node >= 20, build the wasm-release preset (stages the
#      static bundle into build-preset/wasm-release/web/);
#   2. the offline web gates (web_offline_gates in devtools/lib-web.sh, the
#      same set CI runs): glue no-eval check (the page CSP has no
#      'unsafe-eval'), app-core.test.mjs, sdk-bridge.test.mjs (real SDK, stub
#      HTTP handler), streaming.test.mjs (the built engine, stub transport
#      hooks), package.test.mjs, the browser-storage grep gate
#      (D-P9-4), and the check that deploy.sh and docs/HOSTING.md carry the
#      same seven upload commands (P10);
#   3. a LIVE UI-path smoke (web/live-smoke.mjs, DdbshSession.run and
#      runStreaming) against real DynamoDB with STS-minted short-term
#      credentials for the default profile: creates and drops one
#      ddbsh_web_smoke_<pid> table.
#
# Output: discussions/test-websmoke-<HEAD-short>.log.
#
# Env: AWS_REGION (default us-east-1), DDBSH_WEB_CRED_SECONDS, plus
# devtools/lib-web.sh's (EMSDK, EMSDK_PYTHON, DDBSH_WASM_BUILD_DIR).
#
# WARNING: step 3 talks to a real AWS account (one small table, dropped).

set -euo pipefail

REPO_DIR="$(cd "$(dirname "$0")/.." && pwd)"
# shellcheck source=devtools/lib-web.sh
. "$REPO_DIR/devtools/lib-web.sh"

HASH="$(cd "$REPO_DIR" && git rev-parse --short HEAD 2>/dev/null || echo unknown)"
DISCUSSIONS_DIR="$REPO_DIR/discussions"
mkdir -p "$DISCUSSIONS_DIR"
LOG_FILE="$DISCUSSIONS_DIR/test-websmoke-${HASH}.log"
export AWS_REGION="${AWS_REGION:-${AWS_DEFAULT_REGION:-us-east-1}}"

web_setup_emsdk
{
    echo "=== Web smoke ($HASH) ==="
    echo "  emcc:   $(emcc --version 2>/dev/null | head -1)"
    echo "  node:   $NODE_BIN ($("$NODE_BIN" --version))"
    echo "  region: $AWS_REGION"
} | tee "$LOG_FILE"

echo "=== Building wasm-release ===" | tee -a "$LOG_FILE"
web_build_bundle "$REPO_DIR" 2>&1 | tee -a "$LOG_FILE"
[ "${PIPESTATUS[0]}" -eq 0 ] || exit 1
WEB_OUT="${DDBSH_WASM_BUILD_DIR:-$REPO_DIR/build-preset/wasm-release}/web"

RC=0
echo "=== Offline web gates ===" | tee -a "$LOG_FILE"
set +e
web_offline_gates 2>&1 | tee -a "$LOG_FILE"
[ "${PIPESTATUS[0]}" -eq 0 ] || RC=1
set -e

echo "=== Live UI-path smoke (real DynamoDB) ===" | tee -a "$LOG_FILE"
CRED_DIR="$(mktemp -d)"
chmod 700 "$CRED_DIR"
trap 'rm -rf "$CRED_DIR"' EXIT
web_mint_credentials "$CRED_DIR" default
export DDBSH_WEB_CREDS_FILE="$CRED_DIR/creds.json"
{
    echo "  endpoint:      dynamodb.${AWS_REGION}.amazonaws.com (real DynamoDB)"
    echo "  access key id: $(web_creds_field "$DDBSH_WEB_CREDS_FILE" default AccessKeyId) (minted, short-term)"
    echo "  caller arn:    $(web_caller_arn "$DDBSH_WEB_CREDS_FILE" default)"
} | tee -a "$LOG_FILE"
if tail -n 3 "$LOG_FILE" | grep -q "(sts unavailable)"; then
    echo "error: the minted credentials could not be verified with sts get-caller-identity." | tee -a "$LOG_FILE" >&2
    exit 1
fi
set +e
(cd "$WEB_OUT" && "$NODE_BIN" live-smoke.mjs) 2>&1 | tee -a "$LOG_FILE"
[ "${PIPESTATUS[0]}" -eq 0 ] || RC=1
set -e

echo "=== Web smoke exit: $RC ===" | tee -a "$LOG_FILE"
echo "  artifact: $LOG_FILE"
exit $RC
