#!/bin/bash
# CHECK_DL: gate for wizard-work/beam/damerau_levenshtein.py (Q133-DL).
#
# Why this file exists (2026-10-31).  The DL module and its cells landed in
# 5bded80, but nothing pinned the *property* the module exists for: the cells
# are module-level asserts that run only when the file is imported, and
# `run_lane_tests.py --list` reports `test_damerau_levenshtein.py unittest=0
# bare=0` -- zero named cases, so the gate's per-test count never moves when
# they break.  This gate names the work: the shipped cells run as a script, and
# an independent BFS oracle (oracle_dl.py) grades a deterministic corpus against
# the module and proves, in the same run, that the corpus can red the two
# implementations people ship by mistake (OSA and plain Levenshtein).
#
# Exit codes follow the repo's tools/ contract:
#   0 clean, 1 graded a fault, 2 could not grade.

set -u
set -o pipefail
cd "$(dirname "$(readlink -f "$0")")" || exit 2

if ! command -v python3 >/dev/null 2>&1; then
    echo "CHECK_DL: python3 not found -- cannot grade." >&2
    exit 2
fi

echo "== shipped cells (test_damerau_levenshtein.py) =="
python3 test_damerau_levenshtein.py
rc=$?
if [ "$rc" -ne 0 ]; then
    echo "CHECK_DL: shipped cells failed (rc=$rc)." >&2
    exit 1
fi

echo "== independent BFS oracle (oracle_dl.py) =="
python3 oracle_dl.py
rc=$?
case $rc in
    0) ;;
    1) echo "CHECK_DL: oracle graded a fault -- see MISMATCH/FAIL above." >&2; exit 1 ;;
    2) echo "CHECK_DL: oracle could not grade -- see message above." >&2; exit 2 ;;
    *) echo "CHECK_DL: oracle exited $rc -- internal error (a fault)." >&2; exit 1 ;;
esac

echo "CHECK_DL: green"
