Skip to content

Commit cb434f3

Browse files
committed
chore: add smoke test script
1 parent efa1d1d commit cb434f3

5 files changed

Lines changed: 97 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
- Optional search filters for `lang`, `country`, `from`, `to`, and `sortBy`, with validation and normalized cache keys.
1313
- Prometheus metrics for article-search cache hit/miss behavior and upstream provider request latency/outcomes.
14+
- Curl-based `npm run smoke` check for a running instance.
1415

1516
## [1.2.0] — 2026-04-05
1617

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ Development with reload:
4040
npm run dev
4141
```
4242

43+
Smoke-test a running instance:
44+
45+
```bash
46+
BASE_URL=http://localhost:3000 QUERY=postgres npm run smoke
47+
# If CLIENT_API_KEYS is configured on the server:
48+
CLIENT_API_KEY=client-secret-one npm run smoke
49+
```
50+
4351
## Quick start (Docker)
4452

4553
```bash
@@ -96,6 +104,7 @@ Errors: `{ "error": "message" }`. Rate limit: `429` with standard rate-limit hea
96104
| `npm run test:watch` | Vitest watch. |
97105
| `npm run test:coverage` | Tests + coverage. |
98106
| `npm run lint` | ESLint. |
107+
| `npm run smoke` | Curl-based smoke test against a running instance (`BASE_URL`, `QUERY`, `COUNT`, optional `CLIENT_API_KEY`). |
99108

100109
## Project layout
101110

docs/OPERATIONS.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,22 @@ Or with Compose (requires `GNEWS_API_KEY` in `.env`):
6464
docker compose up --build
6565
```
6666

67+
## Smoke test
68+
69+
After the service is running and ready, run:
70+
71+
```bash
72+
BASE_URL=http://localhost:3000 QUERY=postgres npm run smoke
73+
```
74+
75+
If `CLIENT_API_KEYS` is configured, pass a matching client key:
76+
77+
```bash
78+
CLIENT_API_KEY=client-secret-one npm run smoke
79+
```
80+
81+
The smoke test checks `/health`, `/ready`, `/openapi.yaml`, `/api/articles`, and `/metrics`.
82+
6783
## Logs
6884

6985
Logs are **JSON** (Pino). Each response includes an `x-request-id` header for correlation.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"test": "vitest run",
1414
"test:watch": "vitest",
1515
"test:coverage": "vitest run --coverage",
16-
"lint": "eslint src test vitest.config.ts"
16+
"lint": "eslint src test vitest.config.ts",
17+
"smoke": "sh scripts/smoke-test.sh"
1718
},
1819
"keywords": [
1920
"news",

scripts/smoke-test.sh

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/bin/sh
2+
set -eu
3+
4+
BASE_URL="${BASE_URL:-http://localhost:3000}"
5+
QUERY="${QUERY:-technology}"
6+
COUNT="${COUNT:-3}"
7+
8+
TMP_BODY="$(mktemp)"
9+
trap 'rm -f "$TMP_BODY"' EXIT
10+
11+
url_encode() {
12+
node -e 'process.stdout.write(encodeURIComponent(process.argv[1]))' "$1"
13+
}
14+
15+
expect_status() {
16+
name="$1"
17+
expected="$2"
18+
url="$3"
19+
20+
if ! status="$(curl -sS -o "$TMP_BODY" -w "%{http_code}" "$url")"; then
21+
echo "FAIL $name: request failed"
22+
exit 1
23+
fi
24+
25+
if [ "$status" != "$expected" ]; then
26+
echo "FAIL $name: expected HTTP $expected, got HTTP $status"
27+
head -c 500 "$TMP_BODY"
28+
echo
29+
exit 1
30+
fi
31+
32+
echo "OK $name ($status)"
33+
}
34+
35+
expect_api_status() {
36+
name="$1"
37+
expected="$2"
38+
url="$3"
39+
40+
if [ -n "${CLIENT_API_KEY:-}" ]; then
41+
status="$(curl -sS -H "X-API-Key: $CLIENT_API_KEY" -o "$TMP_BODY" -w "%{http_code}" "$url")" || {
42+
echo "FAIL $name: request failed"
43+
exit 1
44+
}
45+
else
46+
status="$(curl -sS -o "$TMP_BODY" -w "%{http_code}" "$url")" || {
47+
echo "FAIL $name: request failed"
48+
exit 1
49+
}
50+
fi
51+
52+
if [ "$status" != "$expected" ]; then
53+
echo "FAIL $name: expected HTTP $expected, got HTTP $status"
54+
head -c 500 "$TMP_BODY"
55+
echo
56+
exit 1
57+
fi
58+
59+
echo "OK $name ($status)"
60+
}
61+
62+
ENCODED_QUERY="$(url_encode "$QUERY")"
63+
64+
echo "Smoke testing $BASE_URL"
65+
expect_status "health" "200" "$BASE_URL/health"
66+
expect_status "ready" "200" "$BASE_URL/ready"
67+
expect_status "openapi" "200" "$BASE_URL/openapi.yaml"
68+
expect_api_status "articles" "200" "$BASE_URL/api/articles?query=$ENCODED_QUERY&count=$COUNT"
69+
expect_status "metrics" "200" "$BASE_URL/metrics"

0 commit comments

Comments
 (0)