| 1 | import { existsSync, readdirSync, readFileSync } from 'node:fs'; |
| 2 | import path from 'node:path |
| 3 | |
| 4 | import { buildSchema, parse |
| 5 | import { describe, expect |
| 6 | |
| 7 | import { githubValidationRules } |
| 8 | |
| 9 | type Capture |
| 10 | command: string |
| 11 | requests: { |
| 12 | }[]; |
| 13 | |
| 14 | const root = path |
| 15 | const fixtures = path |
| 16 | const schema = buildSchema |
| 17 | readFileSync(path.join |
| 18 | ); |
| 19 | const openapi: { |
| 20 | readFileSync(path.join |
| 21 | ); |
| 22 | const routes = Object |
| 23 | Object.keys(methods |
| 24 | method: method.toUpperCase |
| 25 | // Express answers a route with or without a trailing slash; gh asks for `/api/v3/`. |
| 26 | pattern: new RegExp |
| 27 | })), |
| 28 | ); |
| 29 | |
| 30 | /** Routes gh calls that Ghost does not serve, and why gh copes without them. */ |
| 31 | const UNSERVED: Record |
| 32 | 'GET /api/v3/user/ssh_signing_keys': |
| 33 | 'Ghost has SSH authentication keys (/user/keys) but no SSH signing keys, since it verifies commit signatures with GPG only; gh ssh-key list lists the authentication keys alone when this answers 404. #121 adds them.', |
| 34 | }; |
| 35 | |
| 36 | const captures = existsSync |
| 37 | ? readdirSync(fixtures) |
| 38 | .filter((file) |
| 39 | .map( |
| 40 | (file) => |
| 41 | [ |
| 42 | file, |
| 43 | JSON.parse( |
| 44 | readFileSync(path.join |
| 45 | ) as Capture, |
| 46 | ] as const, |
| 47 | ) |
| 48 | : []; |
| 49 | |
| 50 | const requestsIn = ( |
| 51 | capture.flatMap(({ |
| 52 | requests.map((request |
| 53 | ); |
| 54 | |
| 55 | const queryOf = ( |
| 56 | typeof body === ' |
| 57 | body !== null && |
| 58 | 'query' in body |
| 59 | typeof body.query |
| 60 | ? body.query |
| 61 | : null; |
| 62 | |
| 63 | /** What real gh sent in the gh suite (test/gh-capture.ts), checked against what Ghost serves, so a field or route gh needs and Ghost lacks fails here before it fails a user. */ |
| 64 | describe('gh capture', () |
| 65 | it('has captures to check', |
| 66 | expect(captures.length) |
| 67 | }); |
| 68 | |
| 69 | describe.each(captures |
| 70 | it('asks only for GraphQL that Ghost validates', |
| 71 | for (const { |
| 72 | (request) => |
| 73 | )) { |
| 74 | const query = |
| 75 | if (!query) |
| 76 | throw new Error( |
| 77 | expect( |
| 78 | validate(schema, parse |
| 79 | (error) => |
| 80 | ), |
| 81 | command, |
| 82 | ).toEqual([]); |
| 83 | } |
| 84 | }); |
| 85 | |
| 86 | it('calls only REST routes that Ghost serves', |
| 87 | for (const { |
| 88 | (request) => |
| 89 | )) { |
| 90 | const call = |
| 91 | // Express answers HEAD with the GET route, which OpenAPI lists alone. |
| 92 | const served = |
| 93 | expect( |
| 94 | call in UNSERVED |
| 95 | routes.some( |
| 96 | (candidate) => |
| 97 | candidate.method === |
| 98 | ), |
| 99 | `${command}: ${call |
| 100 | ).toBe(true); |
| 101 | } |
| 102 | }); |
| 103 | }); |
| 104 | }); |