#!/bin/sh # Grade 4: upload → restart API replicas (volumes kept) → download same bytes. set -eu . "$(dirname "$0")/lib.sh" COMPOSE="docker compose --env-file $ROOT/.env -f $ROOT/compose.yaml" png="$FIXTURES/320x200.png" [ -f "$png" ] || die "missing $png" want_sha=$(sha256_file "$png") register_user alice ALICE_ID=$USER_ID create_chat 'persist' "[\"$ALICE_ID\"]" upload_file "$CHAT_ID" "$png" 'sample.png' 'image/png' expect_code 201 AID=$(printf '%s' "$HTTP_BODY" | jq -r .id) assert_eq "$(printf '%s' "$HTTP_BODY" | jq -r .sha256)" "$want_sha" upload_sha # Restart only API (and LB); keep pgdata + miniodata. $COMPOSE restart api # Wait for both replicas healthy + LB ready. i=0 while [ "$i" -lt 60 ]; do http_json GET /health/ready || true if [ "${HTTP_CODE:-}" = "200" ]; then break fi i=$((i + 1)) sleep 1 done [ "${HTTP_CODE:-}" = "200" ] || die "stack not ready after api restart" # Session may still be in Redis (AOF); reuse token. http_json GET "/api/v1/attachments/$AID" -H "Authorization: Bearer $TOKEN" expect_code 200 assert_eq "$(printf '%s' "$HTTP_BODY" | jq -r .sha256)" "$want_sha" meta_sha out=$(mktemp) code=$(curl -sS -m 60 -o "$out" -w '%{http_code}' \ -H "Authorization: Bearer $TOKEN" \ "$BASE_URL/api/v1/attachments/$AID/content") assert_eq "$code" 200 content_status assert_eq "$(sha256_file "$out")" "$want_sha" content_sha rm -f "$out" printf 'PASS test_attachment_persistence\n'