#!/usr/bin/env bash # Offline tests for payment-record.sh's transaction_id normalization — the writer # side of the varchar(50) fix (Dolibarr's num_payment columns truncate at 50 chars, # Qonto ids run ~67). Uses tests/stub-dol-write.sh via the DOL_WRITE env hook, so # NOTHING is written to the sandbox or prod. # 1. Long Qonto id → POST carries the UUID suffix; JSON reports it; stderr says so. # 2. Wise numeric id → passes through untouched, no normalization notice. # 3. Id still >50 chars after normalization → refused BEFORE any POST, error # cites varchar(50) (never a silent truncation). set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PR="${SCRIPT_DIR}/../scripts/payment-record.sh" STUB="${SCRIPT_DIR}/stub-dol-write.sh" fail() { echo "FAIL: $*" >&2; exit 1; } bash -n "${PR}" || fail "bash -n payment-record.sh" bash -n "${STUB}" || fail "bash -n stub-dol-write.sh" STATE="$(mktemp -d -t prtest.XXXXXX)" trap 'rm -rf "${STATE}"' EXIT LONG="arcodange-1246-1-transaction-019f14c5-e254-7ac9-9e9f-307ed9-d55f44" SHORT="019f14c5-e254-7ac9-9e9f-307ed9-d55f44" # --- Case 1: long Qonto id is normalized to the UUID suffix --- OUT="$(printf '{"invoice_id":13,"kind":"supplier","account_id":1,"amount":96,"transaction_id":"%s"}' "${LONG}" \ | DOL_WRITE="${STUB}" STUB_STATE="${STATE}" bash "${PR}" 2>"${STATE}/stderr1")" \ || fail "long-qonto-id: expected success, got $?" grep -q "\"num_payment\": \"${SHORT}\"" "${STATE}/post_body.json" \ || fail "long-qonto-id: POST body must carry the SHORT num, got: $(cat "${STATE}/post_body.json")" python3 -c " import json, sys o = json.loads('''${OUT}''') assert o['transaction_id'] == '${SHORT}', o assert o['id'] == 77 and o['bank_transaction_id'] == 556, o " || fail "long-qonto-id: output JSON must report the normalized num, got: ${OUT}" grep -q 'normalized' "${STATE}/stderr1" || fail "long-qonto-id: normalization must be announced on stderr" # --- Case 2: Wise numeric id passes through unchanged --- OUT="$(printf '{"invoice_id":19,"account_id":2,"transaction_id":"2159468139"}' \ | DOL_WRITE="${STUB}" STUB_STATE="${STATE}" bash "${PR}" 2>"${STATE}/stderr2")" \ || fail "wise-id: expected success, got $?" grep -q '"num_payment": "2159468139"' "${STATE}/post_body.json" \ || fail "wise-id: POST body must carry the id untouched" grep -q 'normalized' "${STATE}/stderr2" && fail "wise-id: must NOT announce a normalization" # --- Case 3: >50 chars after normalization is refused before any POST --- rm -f "${STATE}/post_body.json" "${STATE}/post_endpoint" BAD="qonto-migration-batch-7-payment-reference-0123456789-0123456789" # 63 chars, no "transaction-" rc=0 printf '{"invoice_id":13,"kind":"supplier","account_id":1,"amount":96,"transaction_id":"%s"}' "${BAD}" \ | DOL_WRITE="${STUB}" STUB_STATE="${STATE}" bash "${PR}" >/dev/null 2>"${STATE}/stderr3" || rc=$? [[ "${rc}" -ne 0 ]] || fail "overlong-id: must exit non-zero" [[ ! -f "${STATE}/post_body.json" ]] || fail "overlong-id: must refuse BEFORE any POST" grep -q 'varchar(50)' "${STATE}/stderr3" || fail "overlong-id: error must cite the varchar(50) constraint" echo "OK: payment-record normalization tests passed (long→short, wise untouched, >50 refused pre-POST)"