|
| 1 | +import { describe, it, expect, beforeAll, afterAll } from "vitest"; |
| 2 | +import { request } from "playwright"; |
| 3 | +import { startTestServer, stopTestServer } from "./utils/testServer.js"; |
| 4 | +import type { APIRequestContext } from "playwright"; |
| 5 | + |
| 6 | +describe("Health Endpoints", () => { |
| 7 | + let apiContext: APIRequestContext; |
| 8 | + let baseURL: string; |
| 9 | + |
| 10 | + beforeAll(async () => { |
| 11 | + // Start test server |
| 12 | + const { url } = await startTestServer(); |
| 13 | + baseURL = url; |
| 14 | + |
| 15 | + // Create Playwright API request context |
| 16 | + apiContext = await request.newContext({ |
| 17 | + baseURL, |
| 18 | + }); |
| 19 | + }); |
| 20 | + |
| 21 | + afterAll(async () => { |
| 22 | + // Clean up |
| 23 | + await apiContext.dispose(); |
| 24 | + await stopTestServer(); |
| 25 | + }); |
| 26 | + |
| 27 | + describe("GET /health", () => { |
| 28 | + it("should return ok: true and service name", async () => { |
| 29 | + const response = await apiContext.get("/health"); |
| 30 | + |
| 31 | + expect(response.ok()).toBe(true); |
| 32 | + expect(response.status()).toBe(200); |
| 33 | + |
| 34 | + const body = await response.json(); |
| 35 | + |
| 36 | + expect(body).toEqual({ |
| 37 | + ok: true, |
| 38 | + service: "refref-api", |
| 39 | + }); |
| 40 | + }); |
| 41 | + |
| 42 | + it("should have correct content-type header", async () => { |
| 43 | + const response = await apiContext.get("/health"); |
| 44 | + |
| 45 | + const contentType = response.headers()["content-type"]; |
| 46 | + expect(contentType).toContain("application/json"); |
| 47 | + }); |
| 48 | + |
| 49 | + it("should respond quickly", async () => { |
| 50 | + const startTime = Date.now(); |
| 51 | + const response = await apiContext.get("/health"); |
| 52 | + const endTime = Date.now(); |
| 53 | + |
| 54 | + expect(response.ok()).toBe(true); |
| 55 | + expect(endTime - startTime).toBeLessThan(1000); // Should respond within 1 second |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + describe("GET /", () => { |
| 60 | + it("should return ok: true and service name", async () => { |
| 61 | + const response = await apiContext.get("/"); |
| 62 | + |
| 63 | + expect(response.ok()).toBe(true); |
| 64 | + expect(response.status()).toBe(200); |
| 65 | + |
| 66 | + const body = await response.json(); |
| 67 | + |
| 68 | + expect(body).toEqual({ |
| 69 | + ok: true, |
| 70 | + service: "refref-api", |
| 71 | + }); |
| 72 | + }); |
| 73 | + |
| 74 | + it("should have correct content-type header", async () => { |
| 75 | + const response = await apiContext.get("/"); |
| 76 | + |
| 77 | + const contentType = response.headers()["content-type"]; |
| 78 | + expect(contentType).toContain("application/json"); |
| 79 | + }); |
| 80 | + |
| 81 | + it("should respond quickly", async () => { |
| 82 | + const startTime = Date.now(); |
| 83 | + const response = await apiContext.get("/"); |
| 84 | + const endTime = Date.now(); |
| 85 | + |
| 86 | + expect(response.ok()).toBe(true); |
| 87 | + expect(endTime - startTime).toBeLessThan(1000); // Should respond within 1 second |
| 88 | + }); |
| 89 | + }); |
| 90 | +}); |
0 commit comments