|
| 1 | +import { expect } from 'chai'; |
| 2 | +import f from 'fastify'; |
| 3 | +import ky from 'ky'; |
| 4 | +import { afterEach, beforeEach, describe, it } from 'mocha'; |
| 5 | +import sinon from 'sinon'; |
| 6 | + |
| 7 | +import { issuesRoutes } from '../src/routes/issues.routes.js'; |
| 8 | +import { loadModels } from '../src/schemas/loadModels.js'; |
| 9 | +import { cleanup, setupGitHubEnv } from './test.utils.js'; |
| 10 | + |
| 11 | +/** |
| 12 | + * Labels exactly as the GitHub REST API returns them: an array of objects. |
| 13 | + * See https://docs.github.com/en/rest/issues/issues#get-an-issue |
| 14 | + */ |
| 15 | +const GITHUB_LABELS = [ |
| 16 | + { |
| 17 | + color: 'ededed', |
| 18 | + default: false, |
| 19 | + description: null, |
| 20 | + id: 6450392658, |
| 21 | + name: 'REPORTED-BY-USER', |
| 22 | + node_id: 'LA_kwDOJMP9Ms8AAAABgHkuUg', |
| 23 | + url: 'https://api.github.com/repos/hasadna/open-bus-map-search/labels/REPORTED-BY-USER', |
| 24 | + }, |
| 25 | +]; |
| 26 | + |
| 27 | +const VALID_BODY = { |
| 28 | + actualBehavior: 'Does not work', |
| 29 | + contactEmail: 'john@example.com', |
| 30 | + contactName: 'John Doe', |
| 31 | + description: 'Test description long enough', |
| 32 | + environment: 'Test environment', |
| 33 | + expectedBehavior: 'Should work', |
| 34 | + reproducibility: 'always', |
| 35 | + title: 'Test Issue', |
| 36 | + type: 'bug', |
| 37 | +}; |
| 38 | + |
| 39 | +/** |
| 40 | + * These tests drive the route through `fastify.inject`, so the response passes |
| 41 | + * through Fastify's schema serializer. The unit tests in `issues.test.js` call the |
| 42 | + * controller with a plain mock `reply`, which never serializes and therefore cannot |
| 43 | + * observe what the client actually receives. |
| 44 | + */ |
| 45 | +describe('createIssue response serialization', () => { |
| 46 | + let app; |
| 47 | + let post; |
| 48 | + |
| 49 | + beforeEach(async () => { |
| 50 | + setupGitHubEnv(); |
| 51 | + post = sinon.stub(ky, 'post'); |
| 52 | + |
| 53 | + app = f(); |
| 54 | + loadModels(app); |
| 55 | + app.register(issuesRoutes, { prefix: 'issues' }); |
| 56 | + await app.ready(); |
| 57 | + }); |
| 58 | + |
| 59 | + afterEach(async () => { |
| 60 | + cleanup(); |
| 61 | + await app.close(); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should preserve GitHub label objects in the serialized response', async () => { |
| 65 | + const githubIssue = { |
| 66 | + created_at: new Date().toISOString(), |
| 67 | + html_url: 'https://github.com/test/repo/issues/123', |
| 68 | + id: 123, |
| 69 | + labels: GITHUB_LABELS, |
| 70 | + number: 123, |
| 71 | + state: 'open', |
| 72 | + title: 'Test Issue', |
| 73 | + url: 'https://api.github.com/repos/test/repo/issues/123', |
| 74 | + }; |
| 75 | + |
| 76 | + post.resolves({ json: () => Promise.resolve(githubIssue) }); |
| 77 | + |
| 78 | + const response = await app.inject({ method: 'POST', payload: VALID_BODY, url: '/issues/create' }); |
| 79 | + |
| 80 | + expect(response.statusCode).to.equal(200); |
| 81 | + expect(response.json().data.labels).to.deep.equal(GITHUB_LABELS); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should still accept labels sent as plain strings', async () => { |
| 85 | + const githubIssue = { |
| 86 | + created_at: new Date().toISOString(), |
| 87 | + html_url: 'https://github.com/test/repo/issues/124', |
| 88 | + id: 124, |
| 89 | + labels: ['REPORTED-BY-USER'], |
| 90 | + number: 124, |
| 91 | + state: 'open', |
| 92 | + title: 'Test Issue', |
| 93 | + url: 'https://api.github.com/repos/test/repo/issues/124', |
| 94 | + }; |
| 95 | + |
| 96 | + post.resolves({ json: () => Promise.resolve(githubIssue) }); |
| 97 | + |
| 98 | + const response = await app.inject({ method: 'POST', payload: VALID_BODY, url: '/issues/create' }); |
| 99 | + |
| 100 | + expect(response.statusCode).to.equal(200); |
| 101 | + expect(response.json().data.labels).to.deep.equal(['REPORTED-BY-USER']); |
| 102 | + }); |
| 103 | +}); |
0 commit comments