#!/usr/bin/env bash
# 05_embedding_contract.sh — GOLAKE-050.
# Verifies POST /v1/embed contract: dim=768, non-empty vector, model
# echoed back. Skips with explicit reason if Ollama is unreachable
# (per TEST_PROOF_SCOPE.md hard rule: skipped != passed).
#
# This is the contract subset of embedding. Semantic ranking lives in
# Phase C (05/06 integration cases) and asserts against a stored
# rankings fixture; this case stays embedding-implementation-agnostic.

set -uo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "${SCRIPT_DIR}/../lib/env.sh"
source "${SCRIPT_DIR}/../lib/http.sh"
source "${SCRIPT_DIR}/../lib/assert.sh"

CASE_ID="GOLAKE-050"
CASE_NAME="Embedding contract — dim=768, non-empty"
CASE_TYPE="contract"
if [ "${1:-}" = "--metadata-only" ]; then return 0 2>/dev/null || exit 0; fi

# One real request — short text, default model. If Ollama is up we
# get 200; if down we get 502 from embedd. Either way we record it.
proof_post "$CASE_ID" "embed_one_text" "${PROOF_GATEWAY_URL}/v1/embed" \
    "application/json" \
    '{"texts":["industrial staffing for welders in Chicago"],"model":"nomic-embed-text"}' \
    > /dev/null

status=$(proof_status_of "$CASE_ID" "embed_one_text")

# 502 from embedd = Ollama not reachable. Mark skip with reason; do
# not pretend to verify the contract.
if [ "$status" = "502" ]; then
    proof_skip "$CASE_ID" "Embedding contract — Ollama unreachable" \
        "POST /v1/embed returned 502; embedd cannot reach upstream Ollama. Run 'just doctor' to diagnose."
    return 0 2>/dev/null || exit 0
fi

proof_assert_eq "$CASE_ID" "POST /v1/embed → 200" "200" "$status"

body_path="${PROOF_REPORT_DIR}/raw/http/${CASE_ID}/embed_one_text.body"

# Dimension echoed back.
dim=$(jq -r '.dimension // empty' "$body_path")
proof_assert_eq "$CASE_ID" "response.dimension = 768" "768" "$dim"

# One vector returned.
n=$(jq -r '.vectors | length' "$body_path")
proof_assert_eq "$CASE_ID" "response.vectors length = 1" "1" "$n"

# Vector dim matches.
vec_len=$(jq -r '.vectors[0] | length' "$body_path")
proof_assert_eq "$CASE_ID" "vectors[0] length = 768" "768" "$vec_len"

# Vector is non-empty (sum of squared elements > 0). Cheap proxy for
# "not all zeros" without comparing every element.
sum_sq=$(jq -r '[.vectors[0][] | . * .] | add' "$body_path")
proof_assert_gt "$CASE_ID" "vectors[0] non-zero (sum of squares > 0)" "$sum_sq" "0"

# Model name echoed.
model=$(jq -r '.model // empty' "$body_path")
proof_assert_eq "$CASE_ID" "response.model = nomic-embed-text" "nomic-embed-text" "$model"
