feat(write-skill): GED attach op — upload the source document onto its invoice (erp#43)
document-attach.sh uploads a source piece (the supplier's own PDF) onto an invoice's GED via POST /documents/upload — idempotent by (object, filename, sha256): before any POST the object's GED is listed and a same-named entry is downloaded back and sha256-compared. Identical → deduped no-op; different content → ABORT (refuse-never-repair, overwriteifexists always 0, never Dolibarr's overwrite flag). Read-back after upload: re-list + download + sha256-verify. Module-relative download paths are derived from the listing's fullname (supplier invoices carry an id-derived get_exdir prefix like 9/2/FAF2026013/…, so reconstruction would be wrong). Promote integration: new `attach` op in promote-plan/promote-apply (OP_SCRIPT), object_id resolvable via @ref and #supplierinvoice lookups; a relative `file` resolves against the manifest's directory (replay packs carry pdfs/ beside the manifest, gitignored — README documents the books@ re-fetch message ids). promote-plan prints each file's sha256 (or a loud MISSING) at review time. CLI: `arcodange sandbox attach`. Proof: offline case 12 in tests/run-tests.sh (upload body, dedupe, conflict abort, field refusal, manifest-relative resolution via stubbed /documents); live: manifest-C-ged-attach.json applied twice on the sandbox — run 1 four created, run 2 four deduped, one GED file per FAF2026010-013, stored sha256s equal to the re-fetched sources; tests/replay-idempotency.sh extended with an attach op (4 created → 4 deduped, ged_files count unchanged) and a live same-name/different-bytes abort verified. Closes erp#43 Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01VRShc4QhLLU73FLHx9vskh
This commit is contained in:
@@ -28,6 +28,12 @@
|
||||
# an existing fiche missing the requested role ABORTS; a miss creates.
|
||||
# 11. a deduped DRAFT with validate:true is validated on replay (converges an
|
||||
# op that died between create and validate).
|
||||
# GED attach (erp#43):
|
||||
# 12. document-attach.sh uploads with overwriteifexists=0 + read-back sha256;
|
||||
# an identical stored file dedupes (no POST); the same filename with
|
||||
# DIFFERENT content ABORTS (never an overwrite); unknown fields refused;
|
||||
# promote-apply resolves a manifest-relative "file" and promote-plan
|
||||
# prints its sha256.
|
||||
set -euo pipefail
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
PR="${SCRIPT_DIR}/../scripts/payment-record.sh"
|
||||
@@ -326,4 +332,83 @@ grep -q '/supplierinvoices/5/validate' "${S11}/validated_endpoint" \
|
||||
|| fail "draft-converge: the matched draft must be validated"
|
||||
echo "OK: draft convergence — replay validates the half-done invoice instead of duplicating it"
|
||||
|
||||
# --- Case 12: document-attach (erp#43) — upload, sha256 dedupe, conflict abort ---
|
||||
DA="${SCRIPT_DIR}/../scripts/document-attach.sh"
|
||||
bash -n "${DA}" || fail "bash -n document-attach.sh"
|
||||
S12="$(mktemp -d -t datest.XXXXXX)"; trap 'rm -rf "${STATE}" "${S4}" "${S5}" "${S6}" "${S7}" "${S8}" "${S9}" "${S10}" "${S11}" "${S12}"' EXIT
|
||||
printf 'ged43 offline fixture' > "${S12}/src.pdf"
|
||||
SRC_SHA="$(python3 -c "import hashlib,sys; print(hashlib.sha256(open(sys.argv[1],'rb').read()).hexdigest())" "${S12}/src.pdf")"
|
||||
SRC_B64="$(python3 -c "import base64,sys; print(base64.b64encode(open(sys.argv[1],'rb').read()).decode())" "${S12}/src.pdf")"
|
||||
printf '%s' '{"id":"29","ref":"FAF2026013","ref_supplier":"F1045","statut":"1"}' \
|
||||
> "${S12}/invoice_detail_29.json"
|
||||
# 12a — fresh attach: 404 listing → upload (overwriteifexists=0) → read-back sha
|
||||
OUT="$(printf '{"modulepart":"facture_fournisseur","object_id":29,"file":"%s"}' "${S12}/src.pdf" \
|
||||
| DOL_WRITE="${STUB}" STUB_STATE="${S12}" bash "${DA}" 2>/dev/null)" \
|
||||
|| fail "attach-fresh: expected success, got $?"
|
||||
python3 -c "
|
||||
import json
|
||||
o = json.loads('''${OUT}''')
|
||||
assert o['deduped'] is False and o['sha256'] == '${SRC_SHA}' and o['ref'] == 'FAF2026013' \
|
||||
and o['filename'] == 'src.pdf' and o['object_id'] == 29, o
|
||||
" || fail "attach-fresh: bad output: ${OUT}"
|
||||
python3 -c "
|
||||
import json
|
||||
b = json.load(open('${S12}/upload_body.json'))
|
||||
assert b['overwriteifexists'] == '0' and b['fileencoding'] == 'base64' \
|
||||
and b['ref'] == 'FAF2026013' and b['filecontent'] == '${SRC_B64}', b
|
||||
" || fail "attach-fresh: upload body must carry base64 content + overwriteifexists=0"
|
||||
# 12b — re-attach identical content (staged listing + download, NO upload state) → dedupe
|
||||
S12B="$(mktemp -d -t datest12b.XXXXXX)"; trap 'rm -rf "${STATE}" "${S4}" "${S5}" "${S6}" "${S7}" "${S8}" "${S9}" "${S10}" "${S11}" "${S12}" "${S12B}"' EXIT
|
||||
cp "${S12}/src.pdf" "${S12B}/src.pdf"; cp "${S12}/invoice_detail_29.json" "${S12B}/"
|
||||
printf '%s' '[{"name":null,"relativename":"src.pdf","type":"file","level1name":"FAF2026013",
|
||||
"fullname":"/var/www/documents/fournisseur/facture/0/3/FAF2026013/src.pdf","size":21}]' \
|
||||
> "${S12B}/documents.json"
|
||||
printf '{"filename":"src.pdf","content-type":"application/pdf","filesize":21,"content":"%s"}' "${SRC_B64}" \
|
||||
> "${S12B}/document_download.json"
|
||||
OUT="$(printf '{"modulepart":"facture_fournisseur","object_id":29,"file":"%s"}' "${S12B}/src.pdf" \
|
||||
| DOL_WRITE="${STUB}" STUB_STATE="${S12B}" bash "${DA}" 2>/dev/null)" \
|
||||
|| fail "attach-dedupe: expected success, got $?"
|
||||
python3 -c "
|
||||
import json
|
||||
o = json.loads('''${OUT}''')
|
||||
assert o['deduped'] is True and o['sha256'] == '${SRC_SHA}', o
|
||||
" || fail "attach-dedupe: identical content must dedupe, got: ${OUT}"
|
||||
[[ ! -f "${S12B}/upload_body.json" ]] || fail "attach-dedupe: must NOT upload on a sha256 match"
|
||||
# 12c — same filename, DIFFERENT content → abort, no upload
|
||||
OTHER_B64="$(printf 'ged43 DIFFERENT bytes' | python3 -c "import base64,sys; print(base64.b64encode(sys.stdin.buffer.read()).decode())")"
|
||||
printf '{"filename":"src.pdf","content-type":"application/pdf","filesize":21,"content":"%s"}' "${OTHER_B64}" \
|
||||
> "${S12B}/document_download.json"
|
||||
rc=0
|
||||
printf '{"modulepart":"facture_fournisseur","object_id":29,"file":"%s"}' "${S12B}/src.pdf" \
|
||||
| DOL_WRITE="${STUB}" STUB_STATE="${S12B}" bash "${DA}" >/dev/null 2>"${S12B}/stderr12c" || rc=$?
|
||||
[[ "${rc}" -ne 0 ]] || fail "attach-conflict: same name + different content must abort"
|
||||
grep -q 'DIFFERENT content' "${S12B}/stderr12c" || fail "attach-conflict: error must say DIFFERENT content"
|
||||
[[ ! -f "${S12B}/upload_body.json" ]] || fail "attach-conflict: must NOT upload on a conflict"
|
||||
# 12d — unknown field refused before any request
|
||||
rc=0
|
||||
printf '{"modulepart":"facture_fournisseur","object_id":29,"file":"%s","overwrite":true}' "${S12}/src.pdf" \
|
||||
| DOL_WRITE="${STUB}" STUB_STATE="${S12}" bash "${DA}" >/dev/null 2>"${S12}/stderr12d" || rc=$?
|
||||
[[ "${rc}" -ne 0 ]] || fail "attach-unknown-field: must refuse unknown fields"
|
||||
grep -q 'overwrite' "${S12}/stderr12d" || fail "attach-unknown-field: error must name the offender"
|
||||
# 12e — promote-apply resolves a manifest-relative file; promote-plan prints its sha
|
||||
S12E="$(mktemp -d -t datest12e.XXXXXX)"; trap 'rm -rf "${STATE}" "${S4}" "${S5}" "${S6}" "${S7}" "${S8}" "${S9}" "${S10}" "${S11}" "${S12}" "${S12B}" "${S12E}"' EXIT
|
||||
mkdir -p "${S12E}/pack/pdfs"
|
||||
cp "${S12}/src.pdf" "${S12E}/pack/pdfs/src.pdf"
|
||||
printf '%s' '{"id":"29","ref":"FAF2026013","ref_supplier":"F1045","statut":"1"}' \
|
||||
> "${S12E}/invoice_detail_29.json"
|
||||
cat > "${S12E}/pack/manifest.json" <<'JSON'
|
||||
[ { "op": "attach",
|
||||
"input": { "modulepart": "facture_fournisseur", "object_id": 29,
|
||||
"file": "pdfs/src.pdf" } } ]
|
||||
JSON
|
||||
( cd "${S12E}" \
|
||||
&& bash "${PP}" pack/manifest.json > plan.out 2>&1 \
|
||||
&& DOL_WRITE="${STUB}" STUB_STATE="${S12E}" bash "${PA}" pack/manifest.json --target sandbox > apply.out 2>&1 ) \
|
||||
|| fail "attach-promote: plan/apply failed: $(cat "${S12E}/plan.out" "${S12E}/apply.out" 2>/dev/null)"
|
||||
grep -q "sha256=${SRC_SHA}" "${S12E}/plan.out" || fail "attach-promote: plan must print the file sha256"
|
||||
grep -q 'attach' "${S12E}/apply.out" || fail "attach-promote: apply must run the attach op"
|
||||
grep -q '(1 created)' "${S12E}/apply.out" || fail "attach-promote: summary must say (1 created)"
|
||||
[[ -f "${S12E}/upload_body.json" ]] || fail "attach-promote: the manifest-relative file must reach the upload"
|
||||
echo "OK: document-attach — upload + read-back, sha256 dedupe, conflict abort, field refusal, manifest-relative file"
|
||||
|
||||
echo "OK: all offline tests passed"
|
||||
|
||||
Reference in New Issue
Block a user