|
| 1 | +// @vitest-environment node |
| 2 | +// |
| 3 | +// /exporter/event/{id}.biopax (spec 015 T070): node writes research R6's |
| 4 | +// answer around the library service's BioPAX file (T069). Java has no BioPAX |
| 5 | +// URL, so R6 chose the answers: application/rdf+xml, a {stId}.biopax download, |
| 6 | +// and SBML's 404 and 400 in the envelope. |
| 7 | +import { createServer, type Server, type ServerResponse } from 'node:http'; |
| 8 | +import type { AddressInfo } from 'node:net'; |
| 9 | +import { describe, expect, it, beforeAll, afterAll, afterEach } from 'vitest'; |
| 10 | +import { |
| 11 | + biopaxAnswer, |
| 12 | + eventExportAnswer, |
| 13 | + exportersEndpoints, |
| 14 | + sbmlAnswer, |
| 15 | + type ExportEvent, |
| 16 | +} from './exporters-endpoints.ts'; |
| 17 | +import type { Answer, Request } from './endpoint.ts'; |
| 18 | + |
| 19 | +/** The graph as these cases need it: one pathway, one object that is no event. */ |
| 20 | +const find = async (raw: string): Promise<ExportEvent | null> => { |
| 21 | + const id = raw.split('.')[0]; |
| 22 | + if (id === 'R-HSA-1' || id === '1') return { stId: 'R-HSA-1', isEvent: true }; |
| 23 | + if (id === '9626065') return { stId: null, isEvent: false }; |
| 24 | + return null; |
| 25 | +}; |
| 26 | + |
| 27 | +const request = (path: string): Request => |
| 28 | + ({ |
| 29 | + originalUrl: `/ContentService/exporter/${path}`, |
| 30 | + protocol: 'http', |
| 31 | + get: () => 'localhost', |
| 32 | + }) as unknown as Request; |
| 33 | + |
| 34 | +const envelope = (answer: Answer) => { |
| 35 | + const body = JSON.parse(String(answer.body)) as { reason: string; messages: string[] }; |
| 36 | + return [answer.status, body.reason, body.messages]; |
| 37 | +}; |
| 38 | + |
| 39 | +// The library service, as each case needs it to behave. |
| 40 | +let behave: (path: string, response: ServerResponse) => void; |
| 41 | +let asked: string[] = []; |
| 42 | +let service: Server; |
| 43 | +let serviceUrl: string; |
| 44 | + |
| 45 | +beforeAll(async () => { |
| 46 | + service = createServer((req, res) => { |
| 47 | + asked.push(String(req.url)); |
| 48 | + behave(String(req.url), res); |
| 49 | + }); |
| 50 | + await new Promise<void>((resolve) => service.listen(0, '127.0.0.1', () => resolve())); |
| 51 | + serviceUrl = `http://127.0.0.1:${(service.address() as AddressInfo).port}`; |
| 52 | +}); |
| 53 | +afterAll(() => { |
| 54 | + service.closeAllConnections(); |
| 55 | + return new Promise<void>((resolve) => service.close(() => resolve())); |
| 56 | +}); |
| 57 | +afterEach(() => { |
| 58 | + delete process.env['SBML_SERVICE']; |
| 59 | + asked = []; |
| 60 | +}); |
| 61 | + |
| 62 | +const withService = () => (process.env['SBML_SERVICE'] = serviceUrl); |
| 63 | +const answers = (path: string, headersMs?: number) => biopaxAnswer(request(path), find, headersMs); |
| 64 | + |
| 65 | +async function text(answer: Answer) { |
| 66 | + const chunks: Buffer[] = []; |
| 67 | + for await (const chunk of answer.body as AsyncIterable<Buffer>) chunks.push(chunk); |
| 68 | + return Buffer.concat(chunks).toString('utf8'); |
| 69 | +} |
| 70 | + |
| 71 | +describe('before any file is made', () => { |
| 72 | + it('names an id nothing has, as given, in a 404', async () => { |
| 73 | + withService(); |
| 74 | + expect(envelope(await answers('event/R-HSA-0.3.biopax'))).toEqual([ |
| 75 | + 404, |
| 76 | + 'NOT_FOUND', |
| 77 | + ["Identifier 'R-HSA-0.3' not found"], |
| 78 | + ]); |
| 79 | + expect(asked).toEqual([]); |
| 80 | + }); |
| 81 | + |
| 82 | + it('refuses an object that is no pathway or reaction with a 400', async () => { |
| 83 | + withService(); |
| 84 | + expect(envelope(await answers('event/9626065.biopax'))).toEqual([ |
| 85 | + 400, |
| 86 | + 'BAD_REQUEST', |
| 87 | + ["The identifier '9626065' does not correspond to a pathway or reaction"], |
| 88 | + ]); |
| 89 | + expect(asked).toEqual([]); |
| 90 | + }); |
| 91 | + |
| 92 | + it('answers 503 without the service, never another implementation', async () => { |
| 93 | + expect(envelope(await answers('event/R-HSA-1.biopax'))).toEqual([ |
| 94 | + 503, |
| 95 | + 'SERVICE_UNAVAILABLE', |
| 96 | + ['The BioPAX service is not configured'], |
| 97 | + ]); |
| 98 | + }); |
| 99 | +}); |
| 100 | + |
| 101 | +describe('the file', () => { |
| 102 | + it('passes the service’s BioPAX on, as RDF named {stId}.biopax, from any form of the id', async () => { |
| 103 | + withService(); |
| 104 | + behave = (_path, res) => { |
| 105 | + res.writeHead(200, { 'Content-Type': 'application/rdf+xml;charset=UTF-8' }); |
| 106 | + res.end('<rdf:RDF/>'); |
| 107 | + }; |
| 108 | + for (const form of ['R-HSA-1', 'R-HSA-1.4', '1']) { |
| 109 | + const answer = await answers(`event/${form}.biopax`); |
| 110 | + expect(answer.status).toBe(200); |
| 111 | + expect(answer.type).toBe('application/rdf+xml;charset=UTF-8'); |
| 112 | + expect(answer.headers?.['Content-Disposition']).toBe('attachment; filename="R-HSA-1.biopax"'); |
| 113 | + expect(await text(answer)).toBe('<rdf:RDF/>'); |
| 114 | + } |
| 115 | + // The service is asked for BioPAX, by the bare stable id |
| 116 | + expect(asked).toEqual(['/biopax/R-HSA-1', '/biopax/R-HSA-1', '/biopax/R-HSA-1']); |
| 117 | + }); |
| 118 | + |
| 119 | + it('the service’s 404 is a 404, and its failure a 500 without its own reason', async () => { |
| 120 | + withService(); |
| 121 | + behave = (_path, res) => { |
| 122 | + res.writeHead(404, { 'Content-Type': 'application/json' }); |
| 123 | + res.end('{"error":"No event"}'); |
| 124 | + }; |
| 125 | + expect((await answers('event/R-HSA-1.biopax')).status).toBe(404); |
| 126 | + behave = (_path, res) => { |
| 127 | + res.writeHead(500, { 'Content-Type': 'application/json' }); |
| 128 | + res.end('{"error":"/cache/tmp/biopax/x cannot be written"}'); |
| 129 | + }; |
| 130 | + expect(envelope(await answers('event/R-HSA-1.biopax'))).toEqual([ |
| 131 | + 500, |
| 132 | + 'INTERNAL_SERVER_ERROR', |
| 133 | + ['The BioPAX of R-HSA-1 cannot be made'], |
| 134 | + ]); |
| 135 | + }); |
| 136 | + |
| 137 | + it('a service that sends no headers in time is a 503', async () => { |
| 138 | + withService(); |
| 139 | + behave = () => undefined; |
| 140 | + expect(envelope(await answers('event/R-HSA-1.biopax', 50))).toEqual([ |
| 141 | + 503, |
| 142 | + 'SERVICE_UNAVAILABLE', |
| 143 | + ['The BioPAX file took too long to make'], |
| 144 | + ]); |
| 145 | + service.closeAllConnections(); |
| 146 | + }); |
| 147 | +}); |
| 148 | + |
| 149 | +describe('BioPAX’s own share of two', () => { |
| 150 | + it('refuses a third file, and is not taken by SBML’s', async () => { |
| 151 | + withService(); |
| 152 | + const held: ServerResponse[] = []; |
| 153 | + behave = (_path, res) => { |
| 154 | + held.push(res); |
| 155 | + }; |
| 156 | + const biopax = [answers('event/R-HSA-1.biopax'), answers('event/R-HSA-1.biopax')]; |
| 157 | + const sbml = [ |
| 158 | + sbmlAnswer(request('event/R-HSA-1.sbml'), 'sbml', find), |
| 159 | + sbmlAnswer(request('event/R-HSA-1.sbml'), 'sbml', find), |
| 160 | + ]; |
| 161 | + await expect.poll(() => held.length).toBe(4); |
| 162 | + // SBML's two do not count against BioPAX's share, and BioPAX's own are full |
| 163 | + const third = await answers('event/R-HSA-1.biopax'); |
| 164 | + expect(envelope(third)).toEqual([ |
| 165 | + 503, |
| 166 | + 'SERVICE_UNAVAILABLE', |
| 167 | + ['Too many BioPAX files are being made; try again shortly'], |
| 168 | + ]); |
| 169 | + expect(third.headers?.['Retry-After']).toBe('5'); |
| 170 | + for (const res of held) { |
| 171 | + res.writeHead(200); |
| 172 | + res.end('x'); |
| 173 | + } |
| 174 | + for (const answer of [...(await Promise.all(biopax)), ...(await Promise.all(sbml))]) |
| 175 | + await text(answer); |
| 176 | + // A place is given back once a file has been passed on |
| 177 | + behave = (_path, res) => { |
| 178 | + res.writeHead(200); |
| 179 | + res.end('<rdf:RDF/>'); |
| 180 | + }; |
| 181 | + await expect.poll(async () => (await answers('event/R-HSA-1.biopax')).status).toBe(200); |
| 182 | + }); |
| 183 | +}); |
| 184 | + |
| 185 | +describe('/exporter/event/{id}', () => { |
| 186 | + it('answers .biopax with BioPAX and .sbml with SBML, and leaves .sbgn to Java', async () => { |
| 187 | + withService(); |
| 188 | + behave = (_path, res) => { |
| 189 | + res.writeHead(200); |
| 190 | + res.end('x'); |
| 191 | + }; |
| 192 | + const biopax = await eventExportAnswer(request('event/R-HSA-1.biopax'), find); |
| 193 | + expect(biopax.headers?.['Content-Disposition']).toBe('attachment; filename="R-HSA-1.biopax"'); |
| 194 | + await text(biopax); |
| 195 | + const sbml = await eventExportAnswer(request('event/R-HSA-1.sbml'), find); |
| 196 | + expect(sbml.headers?.['Content-Disposition']).toBe('attachment; filename="R-HSA-1.sbml"'); |
| 197 | + await text(sbml); |
| 198 | + expect((await eventExportAnswer(request('event/R-HSA-1.sbgn'), find)).status).toBe(404); |
| 199 | + expect(asked).toEqual(['/biopax/R-HSA-1', '/sbml/R-HSA-1']); |
| 200 | + }); |
| 201 | + |
| 202 | + it('is the endpoint’s handler', () => { |
| 203 | + const endpoint = exportersEndpoints.find( |
| 204 | + (candidate) => candidate.path === '/ContentService/exporter/event/{id}' |
| 205 | + ); |
| 206 | + expect(String(endpoint?.handler)).toContain('eventExportAnswer'); |
| 207 | + }); |
| 208 | +}); |
| 209 | + |
| 210 | +describe('standard form, which decides what is cached per release (spec 011)', () => { |
| 211 | + it('takes .biopax and .sbml on /exporter/event/, and nothing else', async () => { |
| 212 | + const { standardForm } = await import('./parameters.ts'); |
| 213 | + const form = (path: string) => |
| 214 | + standardForm('/ContentService/exporter/event/{id}', `/ContentService/exporter/event/${path}`); |
| 215 | + expect(form('R-HSA-1.biopax')).toEqual({ ok: true }); |
| 216 | + expect(form('R-HSA-1.sbml')).toEqual({ ok: true }); |
| 217 | + expect(form('R-HSA-1.owl')).toEqual({ ok: false, rule: 'path:id' }); |
| 218 | + expect(form('R-HSA-1.biopax.sbml')).toEqual({ ok: false, rule: 'path:id' }); |
| 219 | + }); |
| 220 | +}); |
0 commit comments