Skip to content

Commit 0309229

Browse files
committed
Test both Swagger and OpenAPI on precommit
1 parent 493bd99 commit 0309229

3 files changed

Lines changed: 65 additions & 11 deletions

File tree

.husky/pre-commit

Lines changed: 62 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,64 @@ echo "Running OpenAPI 3.2.0 Petstore test..."
3535
node dist/cli.js from_openapi to_sdk -i ../petstore_oas3.json -o .test_out_openapi || { echo "OpenAPI 3.2.0 Petstore test failed"; exit 1; }
3636
(cd .test_out_openapi && npm install && npm run build) || { echo "OpenAPI 3.2.0 SDK build failed"; exit 1; }
3737

38-
echo "Starting Prism mock servers (fallback non-JVM version) due to upstream/Docker issues..."
39-
npx @stoplight/prism-cli mock ../petstore.json -p 4010 -d >/dev/null 2>&1 &
40-
PRISM_SWAGGER=$!
38+
DOCKER_PORT=8080
39+
USE_PRISM=0
40+
41+
if command -v docker >/dev/null 2>&1; then
42+
echo "Starting JVM official petstore server in Docker..."
43+
docker run --rm -d -p $DOCKER_PORT:8080 --name petstore_server_precommit swaggerapi/petstore >/dev/null 2>&1 || USE_PRISM=1
44+
else
45+
USE_PRISM=1
46+
fi
47+
48+
if [ "$USE_PRISM" -eq 0 ]; then
49+
echo "Waiting for JVM Petstore server to be ready..."
50+
READY=0
51+
for i in {1..30}; do
52+
if curl -s -f http://127.0.0.1:$DOCKER_PORT/api/pet/findByStatus?status=available > /dev/null; then
53+
READY=1
54+
break
55+
fi
56+
sleep 2
57+
done
58+
59+
if [ "$READY" -eq 1 ]; then
60+
echo "JVM server is ready."
61+
cat << EOF > test-sdks.ts
62+
import { StoreClient as SwaggerStoreClient } from './.test_out_swagger/dist/services/store.client.js';
63+
import { StoreClient as OpenApiStoreClient } from './.test_out_openapi/dist/services/store.client.js';
64+
65+
async function run() {
66+
console.log("Testing Swagger 2.0 SDK against JVM Server...");
67+
const swaggerClient = new SwaggerStoreClient('http://127.0.0.1:$DOCKER_PORT/api');
68+
await swaggerClient.getInventory({ headers: { api_key: 'special-key' } });
69+
console.log("Swagger 2.0 OK");
70+
71+
console.log("Testing OpenAPI 3.0 SDK against JVM Server...");
72+
const openapiClient = new OpenApiStoreClient('http://127.0.0.1:$DOCKER_PORT/api');
73+
await openapiClient.getInventory({ headers: { api_key: 'special-key' } });
74+
console.log("OpenAPI 3.0 OK");
75+
}
76+
run().catch(e => { console.error(e); process.exit(1); });
77+
EOF
78+
else
79+
echo "JVM server failed to become ready. Falling back to Prism."
80+
docker stop petstore_server_precommit >/dev/null 2>&1 || true
81+
USE_PRISM=1
82+
fi
83+
fi
84+
85+
if [ "$USE_PRISM" -eq 1 ]; then
86+
echo "Starting Prism mock servers (fallback non-JVM version) due to upstream/Docker issues..."
87+
npx @stoplight/prism-cli mock ../petstore.json -p 4010 -d >/dev/null 2>&1 &
88+
PRISM_SWAGGER=$!
4189

42-
npx @stoplight/prism-cli mock ../petstore_oas3.json -p 4011 -d >/dev/null 2>&1 &
43-
PRISM_OAS3=$!
90+
npx @stoplight/prism-cli mock ../petstore_oas3.json -p 4011 -d >/dev/null 2>&1 &
91+
PRISM_OAS3=$!
4492

45-
sleep 5
93+
sleep 5
4694

47-
cat << 'EOF' > test-sdks.ts
95+
cat << 'EOF' > test-sdks.ts
4896
import { StoreClient as SwaggerStoreClient } from './.test_out_swagger/dist/services/store.client.js';
4997
import { StoreClient as OpenApiStoreClient } from './.test_out_openapi/dist/services/store.client.js';
5098
@@ -61,12 +109,17 @@ async function run() {
61109
}
62110
run().catch(e => { console.error(e); process.exit(1); });
63111
EOF
112+
fi
64113

65114
npx tsc test-sdks.ts --module NodeNext --moduleResolution NodeNext || { echo "test-sdks compilation failed"; exit 1; }
66115
node test-sdks.js || { echo "test-sdks execution failed"; exit 1; }
67116

68-
kill $PRISM_SWAGGER
69-
kill $PRISM_OAS3
117+
if [ "$USE_PRISM" -eq 0 ]; then
118+
docker stop petstore_server_precommit >/dev/null 2>&1 || true
119+
else
120+
kill $PRISM_SWAGGER || true
121+
kill $PRISM_OAS3 || true
122+
fi
70123

71124
rm -rf .test_out_swagger
72125
rm -rf .test_out_openapi

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
[![interactive WASM web demo](https://img.shields.io/badge/interactive-WASM_web_demo-blue.svg)](https://offscale.io/wasm_web_demo)
55
[![CI](https://github.com/offscale/cdd-ts/actions/workflows/ci.yml/badge.svg)](https://github.com/offscale/cdd-ts/actions)
66
<!-- TEST_COVERAGE_START -->
7-
![TEST_COVERAGE](https://img.shields.io/badge/Test%20Coverage-0%25-red)
7+
![TEST_COVERAGE](https://img.shields.io/badge/Test%20Coverage-100%25-brightgreen)
88
<!-- TEST_COVERAGE_END -->
99
<!-- DOC_COVERAGE_START -->
1010
![DOC_COVERAGE](https://img.shields.io/badge/Doc%20Coverage-100%25-brightgreen)

tests/80-fetch-generator/00-fetch.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ import { describe, expect, it } from "vitest";
88

99
describe("Fetch Implementation", () => {
1010
describe("Config Validation", () => {
11-
it("should execute fetch client generator and attempt admin UI generation when requested", async () => {
11+
it("should execute fetch client generator and attempt admin UI and MCP generation when requested", async () => {
1212
const config: GeneratorConfig = {
1313
input: "dummy",
1414
output: "dummy",
1515
options: {
1616
implementation: "fetch",
1717
admin: true,
18+
mcp: true,
1819
},
1920
};
2021

0 commit comments

Comments
 (0)