|
| 1 | +const handler = require("../../api/spacecraft_missions"); |
| 2 | +const { mockReq, mockRes } = require("../helpers/mock"); |
| 3 | + |
| 4 | +const ISO_DATE = /^\d{4}-\d{2}-\d{2}$/; |
| 5 | +const VALID_STATUSES = new Set(["active", "decommissioned", "failed", "unknown"]); |
| 6 | +const VALID_ORBIT_TYPES = new Set(["LEO", "SSO", "GEO", "Lunar", "Interplanetary", "Failed"]); |
| 7 | + |
| 8 | +describe("GET /api/spacecraft_missions", () => { |
| 9 | + test("returns 200 with application/json header", async () => { |
| 10 | + const res = mockRes(); |
| 11 | + await handler(mockReq(), res); |
| 12 | + expect(res._status).toBe(200); |
| 13 | + expect(res._headers["Content-Type"]).toBe("application/json"); |
| 14 | + }); |
| 15 | + |
| 16 | + test("response has spacecraft_missions wrapper key with 64 records", async () => { |
| 17 | + const res = mockRes(); |
| 18 | + await handler(mockReq(), res); |
| 19 | + expect(res._body).toHaveProperty("spacecraft_missions"); |
| 20 | + expect(res._body.spacecraft_missions).toHaveLength(64); |
| 21 | + }); |
| 22 | + |
| 23 | + test("all records have valid status values", async () => { |
| 24 | + const res = mockRes(); |
| 25 | + await handler(mockReq(), res); |
| 26 | + for (const r of res._body.spacecraft_missions) { |
| 27 | + expect(VALID_STATUSES.has(r.status)).toBe(true); |
| 28 | + } |
| 29 | + }); |
| 30 | + |
| 31 | + test("all non-null orbit_type values are from known set", async () => { |
| 32 | + const res = mockRes(); |
| 33 | + await handler(mockReq(), res); |
| 34 | + for (const r of res._body.spacecraft_missions) { |
| 35 | + if (r.orbit_type !== null) { |
| 36 | + expect(VALID_ORBIT_TYPES.has(r.orbit_type)).toBe(true); |
| 37 | + } |
| 38 | + } |
| 39 | + }); |
| 40 | + |
| 41 | + test("all non-null launch_dates are ISO 8601 format", async () => { |
| 42 | + const res = mockRes(); |
| 43 | + await handler(mockReq(), res); |
| 44 | + for (const r of res._body.spacecraft_missions) { |
| 45 | + if (r.launch_date !== null) { |
| 46 | + expect(r.launch_date).toMatch(ISO_DATE); |
| 47 | + } |
| 48 | + } |
| 49 | + }); |
| 50 | + |
| 51 | + test("all non-null mass_kg values are positive numbers", async () => { |
| 52 | + const res = mockRes(); |
| 53 | + await handler(mockReq(), res); |
| 54 | + for (const r of res._body.spacecraft_missions) { |
| 55 | + if (r.mass_kg !== null) { |
| 56 | + expect(typeof r.mass_kg).toBe("number"); |
| 57 | + expect(r.mass_kg).toBeGreaterThan(0); |
| 58 | + } |
| 59 | + } |
| 60 | + }); |
| 61 | + |
| 62 | + test("all non-null power_watts values are positive numbers", async () => { |
| 63 | + const res = mockRes(); |
| 64 | + await handler(mockReq(), res); |
| 65 | + for (const r of res._body.spacecraft_missions) { |
| 66 | + if (r.power_watts !== null) { |
| 67 | + expect(typeof r.power_watts).toBe("number"); |
| 68 | + expect(r.power_watts).toBeGreaterThan(0); |
| 69 | + } |
| 70 | + } |
| 71 | + }); |
| 72 | + |
| 73 | + test("filters by status", async () => { |
| 74 | + const res = mockRes(); |
| 75 | + await handler(mockReq({ status: "active" }), res); |
| 76 | + expect(res._body.spacecraft_missions.length).toBeGreaterThan(0); |
| 77 | + for (const r of res._body.spacecraft_missions) { |
| 78 | + expect(r.status).toBe("active"); |
| 79 | + } |
| 80 | + }); |
| 81 | + |
| 82 | + test("filters by orbit_type", async () => { |
| 83 | + const res = mockRes(); |
| 84 | + await handler(mockReq({ orbit_type: "SSO" }), res); |
| 85 | + expect(res._body.spacecraft_missions.length).toBeGreaterThan(0); |
| 86 | + for (const r of res._body.spacecraft_missions) { |
| 87 | + expect(r.orbit_type).toBe("SSO"); |
| 88 | + } |
| 89 | + }); |
| 90 | + |
| 91 | + test("combined filters return intersection", async () => { |
| 92 | + const res = mockRes(); |
| 93 | + await handler(mockReq({ orbit_type: "GEO", status: "decommissioned" }), res); |
| 94 | + for (const r of res._body.spacecraft_missions) { |
| 95 | + expect(r.orbit_type).toBe("GEO"); |
| 96 | + expect(r.status).toBe("decommissioned"); |
| 97 | + } |
| 98 | + }); |
| 99 | +}); |
0 commit comments