Spaces:
Sleeping
Sleeping
Claude commited on
Add smoke tests and fixtures
Browse files- scripts/smoke_test.sh: dependency-light HTTP smoke test (curl + python3)
exercising health, tool list, a file-producing PPTX and PDF tool with
download, the report-only preview path, and wrong-file-type rejection.
- scripts/docker_smoke.sh: build image, run container, wait for health, run
the HTTP smoke test against it, tear down.
- tests/fixtures: small sample.pptx/sample.pdf plus _generate.py to recreate
them.
HTTP smoke test verified green against a local uvicorn (6/6).
Files changed (5)
- scripts/docker_smoke.shAdded+42
- scripts/smoke_test.shAdded+79
- tests/fixtures/_generate.pyAdded+47
- tests/fixtures/sample.pdfAdded+32
- tests/fixtures/sample.pptxAdded
123456789101112131415161718192021222324252627282930313233343536373839404142#!/usr/bin/env bash# Build the Docker image, run the container, wait for it to come up, run the# HTTP smoke test against it, then tear everything down.## Usage: scripts/docker_smoke.sh# Requires a running Docker daemon.set -euo pipefail
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"IMAGE="ppt-pdf-tools:smoke"NAME="ppt-pdf-tools-smoke"PORT="${PORT:-8123}"BASE_URL="http://localhost:$PORT"
cleanup() { docker rm -f "$NAME" >/dev/null 2>&1 || true}trap cleanup EXIT
echo "==> Building image ($IMAGE)…"docker build -t "$IMAGE" "$ROOT"
echo "==> Starting container on port $PORT…"cleanupdocker run -d --name "$NAME" -p "$PORT:8000" "$IMAGE" >/dev/null
echo "==> Waiting for health…"for i in $(seq 1 30); do if curl -fs "$BASE_URL/api/health" >/dev/null 2>&1; then echo " up after ${i}s" break fi if [ "$i" -eq 30 ]; then echo " container failed to become healthy; logs:" docker logs "$NAME" || true exit 1 fi sleep 1done
echo "==> Running HTTP smoke test…""$ROOT/scripts/smoke_test.sh" "$BASE_URL"12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879#!/usr/bin/env bash# HTTP smoke test for the PPTX/PDF tools web app.## Exercises the live API against a running server. Dependency-light: needs# only bash, curl, and python3 (stdlib) for JSON assertions.## Usage:# scripts/smoke_test.sh [BASE_URL]# Default BASE_URL: http://localhost:8000set -euo pipefail
BASE_URL="${1:-http://localhost:8000}"ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"FIX="$ROOT/tests/fixtures"TMP="$(mktemp -d)"trap 'rm -rf "$TMP"' EXIT
pass=0fail=0ok() { echo " PASS: $1"; pass=$((pass + 1)); }bad() { echo " FAIL: $1"; fail=$((fail + 1)); }
# jget FILE KEY -> prints value of top-level JSON key (or "" if missing)jget() { python3 -c "import json,sys; print(json.load(open(sys.argv[1])).get(sys.argv[2],''))" "$1" "$2"; }
echo "Smoke testing $BASE_URL"
# 1. Health ----------------------------------------------------------code="$(curl -s -o "$TMP/health.json" -w '%{http_code}' "$BASE_URL/api/health")"[ "$code" = "200" ] && [ "$(jget "$TMP/health.json" status)" = "ok" ] \ && ok "health" || bad "health (HTTP $code)"
# 2. Tool list -------------------------------------------------------code="$(curl -s -o "$TMP/tools.json" -w '%{http_code}' "$BASE_URL/api/tools")"count="$(python3 -c "import json;print(len(json.load(open('$TMP/tools.json'))['tools']))" 2>/dev/null || echo 0)"[ "$code" = "200" ] && [ "$count" = "9" ] \ && ok "tools list (9 tools)" || bad "tools list (HTTP $code, count $count)"
# 3. PPTX tool that produces a file: convert_caps + download ---------code="$(curl -s -o "$TMP/cc.json" -w '%{http_code}' \ -F "file=@$FIX/sample.pptx" -F "keep=API" "$BASE_URL/api/run/convert_caps")"rid="$(jget "$TMP/cc.json" result_id)"if [ "$code" = "200" ] && [ "$(jget "$TMP/cc.json" output_produced)" = "True" ] && [ -n "$rid" ]; then dcode="$(curl -s -o "$TMP/out.pptx" -w '%{http_code}' "$BASE_URL/api/result/$rid")" sz=$(wc -c < "$TMP/out.pptx") [ "$dcode" = "200" ] && [ "$sz" -gt 1000 ] \ && ok "convert_caps run + download ($sz bytes)" || bad "convert_caps download (HTTP $dcode, $sz bytes)"else bad "convert_caps run (HTTP $code)"fi
# 4. PDF tool that produces a file: clear_pdf_metadata --------------code="$(curl -s -o "$TMP/pm.json" -w '%{http_code}' \ -F "file=@$FIX/sample.pdf" "$BASE_URL/api/run/clear_pdf_metadata")"rid="$(jget "$TMP/pm.json" result_id)"if [ "$code" = "200" ] && [ -n "$rid" ]; then dcode="$(curl -s -o "$TMP/out.pdf" -w '%{http_code}' "$BASE_URL/api/result/$rid")" [ "$dcode" = "200" ] && ok "clear_pdf_metadata run + download" \ || bad "clear_pdf_metadata download (HTTP $dcode)"else bad "clear_pdf_metadata run (HTTP $code)"fi
# 5. Preview path (report-only, no file) -----------------------------code="$(curl -s -o "$TMP/ro.json" -w '%{http_code}' \ -F "file=@$FIX/sample.pdf" -F "report_only=true" -F "to=en-US" \ "$BASE_URL/api/run/fix_pdf_lang_tags")"[ "$code" = "200" ] && [ "$(jget "$TMP/ro.json" output_produced)" = "False" ] \ && ok "fix_pdf_lang_tags preview (no file)" || bad "lang preview (HTTP $code)"
# 6. Wrong file type is rejected -------------------------------------code="$(curl -s -o /dev/null -w '%{http_code}' \ -F "file=@$FIX/sample.pdf" "$BASE_URL/api/run/convert_caps")"[ "$code" = "400" ] && ok "wrong file type rejected (400)" \ || bad "wrong file type (expected 400, got $code)"
echoecho "Smoke test: $pass passed, $fail failed."[ "$fail" -eq 0 ]1234567891011121314151617181920212223242526272829303132333435363738394041424344454647#!/usr/bin/env python3"""Regenerate the smoke-test fixtures (sample.pptx, sample.pdf).
Run from the repo root: python tests/fixtures/_generate.pyRequires python-pptx and pikepdf (see requirements.txt)."""
from pathlib import Path
import pikepdffrom pptx import Presentationfrom pptx.util import Inches
HERE = Path(__file__).resolve().parent
def make_pptx(path: Path) -> None: prs = Presentation() slide = prs.slides.add_slide(prs.slide_layouts[5]) box = slide.shapes.add_textbox(Inches(1), Inches(1), Inches(8), Inches(3)) tf = box.text_frame
# Literal ALL-CAPS text (for replace_allcaps_pptx). tf.text = "HELLO WORLD FROM THE API"
# A run carrying All-Caps *formatting* (for convert_caps). p = tf.add_paragraph() run = p.add_run() run.text = "Formatted Caps Run" run._r.get_or_add_rPr().set("cap", "all")
prs.save(str(path))
def make_pdf(path: Path) -> None: pdf = pikepdf.new() pdf.add_blank_page(page_size=(200, 200)) pdf.docinfo[pikepdf.Name.Title] = "Secret Title" pdf.docinfo[pikepdf.Name.Author] = "Jane Doe" pdf.Root.Lang = pikepdf.String("de-DE") pdf.save(str(path))
if __name__ == "__main__": make_pptx(HERE / "sample.pptx") make_pdf(HERE / "sample.pdf") print("Wrote sample.pptx and sample.pdf to", HERE)1234567891011121314151617181920212223242526272829303132%PDF-1.3%����1 0 obj<< /Lang (de-DE) /Pages 3 0 R /Type /Catalog >>endobj2 0 obj<< /Author (Jane Doe) /Title (Secret Title) >>endobj3 0 obj<< /Count 1 /Kids [ 4 0 R ] /Type /Pages >>endobj4 0 obj<< /Contents 5 0 R /MediaBox [ 0 0 200 200 ] /Parent 3 0 R /Resources << >> /Type /Page >>endobj5 0 obj<< /Length 0 /Filter /FlateDecode >>stream
endstreamendobjxref0 60000000000 65535 f 0000000015 00000 n 0000000078 00000 n 0000000140 00000 n 0000000199 00000 n 0000000305 00000 n trailer << /Info 2 0 R /Root 1 0 R /Size 6 /ID [<d5243893af9d7dac21b18b0d42477d58><d5243893af9d7dac21b18b0d42477d58>] >>startxref375%%EOFscripts/docker_smoke.shAdded+42
scripts/smoke_test.shAdded+79
tests/fixtures/_generate.pyAdded+47
tests/fixtures/sample.pdfAdded+32
tests/fixtures/sample.pptxAdded