|
| 1 | +import supertest from 'supertest'; |
| 2 | + |
| 3 | +import { getConnections } from '@constructive-io/graphql-test'; |
| 4 | + |
| 5 | +jest.setTimeout(120000); |
| 6 | + |
| 7 | +const delay = (ms: number) => |
| 8 | + new Promise<void>((resolve) => setTimeout(resolve, ms)); |
| 9 | + |
| 10 | +type GraphqlClient = { |
| 11 | + http: ReturnType<typeof supertest>; |
| 12 | + path: string; |
| 13 | + host?: string; |
| 14 | +}; |
| 15 | + |
| 16 | +const getGraphqlClient = (): GraphqlClient => { |
| 17 | + const rawUrl = |
| 18 | + process.env.TEST_GRAPHQL_URL || |
| 19 | + process.env.GRAPHQL_URL || |
| 20 | + 'http://localhost:3000/graphql'; |
| 21 | + const parsed = new URL(rawUrl); |
| 22 | + const origin = `${parsed.protocol}//${parsed.host}`; |
| 23 | + const path = |
| 24 | + parsed.pathname === '/' ? '/graphql' : `${parsed.pathname}${parsed.search}`; |
| 25 | + const host = process.env.TEST_GRAPHQL_HOST || process.env.GRAPHQL_HOST; |
| 26 | + |
| 27 | + return { |
| 28 | + http: supertest(origin), |
| 29 | + path, |
| 30 | + host |
| 31 | + }; |
| 32 | +}; |
| 33 | + |
| 34 | +const sendGraphql = async ( |
| 35 | + client: GraphqlClient, |
| 36 | + query: string, |
| 37 | + variables?: Record<string, unknown> |
| 38 | +) => { |
| 39 | + let req = client.http |
| 40 | + .post(client.path) |
| 41 | + .set('Content-Type', 'application/json'); |
| 42 | + if (client.host) { |
| 43 | + req = req.set('Host', client.host); |
| 44 | + } |
| 45 | + return req.send({ query, variables }); |
| 46 | +}; |
| 47 | + |
| 48 | +const addJobMutation = ` |
| 49 | + mutation AddJob($input: AddJobInput!) { |
| 50 | + addJob(input: $input) { |
| 51 | + job { |
| 52 | + id |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | +`; |
| 57 | + |
| 58 | +const jobByIdQuery = ` |
| 59 | + query JobById($id: BigInt!) { |
| 60 | + job(id: $id) { |
| 61 | + id |
| 62 | + lastError |
| 63 | + attempts |
| 64 | + } |
| 65 | + } |
| 66 | +`; |
| 67 | + |
| 68 | +const unwrapGraphqlData = <T>( |
| 69 | + response: supertest.Response, |
| 70 | + label: string |
| 71 | +): T => { |
| 72 | + if (response.status !== 200) { |
| 73 | + throw new Error(`${label} failed: HTTP ${response.status}`); |
| 74 | + } |
| 75 | + if (response.body?.errors?.length) { |
| 76 | + throw new Error( |
| 77 | + `${label} failed: ${response.body.errors |
| 78 | + .map((err: { message: string }) => err.message) |
| 79 | + .join('; ')}` |
| 80 | + ); |
| 81 | + } |
| 82 | + if (!response.body?.data) { |
| 83 | + throw new Error(`${label} returned no data`); |
| 84 | + } |
| 85 | + return response.body.data as T; |
| 86 | +}; |
| 87 | + |
| 88 | +const getJobById = async ( |
| 89 | + client: GraphqlClient, |
| 90 | + jobId: string | number |
| 91 | +) => { |
| 92 | + const response = await sendGraphql(client, jobByIdQuery, { |
| 93 | + id: String(jobId) |
| 94 | + }); |
| 95 | + const data = unwrapGraphqlData<{ job: { lastError?: string | null; attempts?: number } | null }>( |
| 96 | + response, |
| 97 | + 'Job query' |
| 98 | + ); |
| 99 | + return data.job; |
| 100 | +}; |
| 101 | + |
| 102 | +const waitForJobCompletion = async ( |
| 103 | + client: GraphqlClient, |
| 104 | + jobId: string | number |
| 105 | +) => { |
| 106 | + const timeoutMs = 30000; |
| 107 | + const started = Date.now(); |
| 108 | + |
| 109 | + while (Date.now() - started < timeoutMs) { |
| 110 | + const job = await getJobById(client, jobId); |
| 111 | + |
| 112 | + if (!job) return; |
| 113 | + |
| 114 | + if (job.lastError) { |
| 115 | + const attempts = job.attempts ?? 0; |
| 116 | + throw new Error(`Job ${jobId} failed after ${attempts} attempt(s): ${job.lastError}`); |
| 117 | + } |
| 118 | + |
| 119 | + await delay(250); |
| 120 | + } |
| 121 | + |
| 122 | + throw new Error(`Job ${jobId} did not complete within ${timeoutMs}ms`); |
| 123 | +}; |
| 124 | + |
| 125 | +describe('jobs e2e', () => { |
| 126 | + let teardown: () => Promise<void>; |
| 127 | + let graphqlClient: GraphqlClient; |
| 128 | + let databaseId = ''; |
| 129 | + let pg: { oneOrNone?: <T>(query: string, values?: unknown[]) => Promise<T | null> } | undefined; |
| 130 | + |
| 131 | + beforeAll(async () => { |
| 132 | + const targetDb = process.env.TEST_DB || process.env.PGDATABASE; |
| 133 | + if (!targetDb) { |
| 134 | + throw new Error('TEST_DB or PGDATABASE must point at the jobs database'); |
| 135 | + } |
| 136 | + process.env.TEST_DB = targetDb; |
| 137 | + |
| 138 | + ({ teardown, pg } = await getConnections( |
| 139 | + { |
| 140 | + schemas: ['app_jobs'], |
| 141 | + authRole: 'administrator' |
| 142 | + } |
| 143 | + )); |
| 144 | + |
| 145 | + graphqlClient = getGraphqlClient(); |
| 146 | + databaseId = process.env.TEST_DATABASE_ID ?? ''; |
| 147 | + if (!databaseId && pg?.oneOrNone) { |
| 148 | + const row = await pg.oneOrNone<{ id: string }>( |
| 149 | + 'SELECT id FROM metaschema_public.database ORDER BY created_at LIMIT 1' |
| 150 | + ); |
| 151 | + databaseId = row?.id ?? ''; |
| 152 | + } |
| 153 | + if (!databaseId) { |
| 154 | + throw new Error('TEST_DATABASE_ID is required or metaschema_public.database must contain a row'); |
| 155 | + } |
| 156 | + process.env.TEST_DATABASE_ID = databaseId; |
| 157 | + }); |
| 158 | + |
| 159 | + afterAll(async () => { |
| 160 | + if (teardown) { |
| 161 | + await teardown(); |
| 162 | + } |
| 163 | + }); |
| 164 | + |
| 165 | + it('creates and processes a simple-email job', async () => { |
| 166 | + const jobInput = { |
| 167 | + dbId: databaseId, |
| 168 | + identifier: 'simple-email', |
| 169 | + payload: { |
| 170 | + to: 'user@example.com', |
| 171 | + subject: 'Jobs e2e', |
| 172 | + html: '<p>jobs test</p>' |
| 173 | + } |
| 174 | + }; |
| 175 | + |
| 176 | + const response = await sendGraphql(graphqlClient, addJobMutation, { |
| 177 | + input: jobInput |
| 178 | + }); |
| 179 | + |
| 180 | + expect(response.status).toBe(200); |
| 181 | + expect(response.body?.errors).toBeUndefined(); |
| 182 | + |
| 183 | + const jobId = response.body?.data?.addJob?.job?.id; |
| 184 | + |
| 185 | + expect(jobId).toBeTruthy(); |
| 186 | + |
| 187 | + await waitForJobCompletion(graphqlClient, jobId); |
| 188 | + }); |
| 189 | + |
| 190 | + it('creates and processes a send-email-link job', async () => { |
| 191 | + const jobInput = { |
| 192 | + dbId: databaseId, |
| 193 | + identifier: 'send-email-link', |
| 194 | + payload: { |
| 195 | + email_type: 'invite_email', |
| 196 | + email: 'user@example.com', |
| 197 | + invite_token: 'invite123', |
| 198 | + sender_id: '00000000-0000-0000-0000-000000000000' |
| 199 | + } |
| 200 | + }; |
| 201 | + |
| 202 | + const response = await sendGraphql(graphqlClient, addJobMutation, { |
| 203 | + input: jobInput |
| 204 | + }); |
| 205 | + |
| 206 | + expect(response.status).toBe(200); |
| 207 | + expect(response.body?.errors).toBeUndefined(); |
| 208 | + |
| 209 | + const jobId = response.body?.data?.addJob?.job?.id; |
| 210 | + |
| 211 | + expect(jobId).toBeTruthy(); |
| 212 | + |
| 213 | + await waitForJobCompletion(graphqlClient, jobId); |
| 214 | + }); |
| 215 | +}); |
0 commit comments