|
| 1 | +import * as core from '@actions/core' |
| 2 | +import Axios from 'axios' |
| 3 | +import fs from 'fs' |
| 4 | +import { postBody, run } from '../src/main' |
| 5 | + |
| 6 | +jest.mock('@actions/core') |
| 7 | +jest.mock('axios') |
| 8 | +jest.mock('fs', () => ({ |
| 9 | + ...jest.requireActual('fs'), |
| 10 | + readFileSync: jest.fn() |
| 11 | +})) |
| 12 | +jest.mock('form-data', () => |
| 13 | + jest.fn().mockImplementation(() => ({ |
| 14 | + append: jest.fn(), |
| 15 | + getHeaders: jest.fn().mockReturnValue({}) |
| 16 | + })) |
| 17 | +) |
| 18 | + |
| 19 | +const uploadResult = { |
| 20 | + url: 'https://example.com/uploads/file.yaml', |
| 21 | + short_url: 'upload://abc123.yaml', |
| 22 | + short_path: 'uploads/short/abc123.yaml', |
| 23 | + original_filename: 'petstore.yaml' |
| 24 | +} |
| 25 | + |
| 26 | +describe('postBody', () => { |
| 27 | + it('generates a post body with the expected content', () => { |
| 28 | + const fixedDate = '2024-01-01T00:00:00.000Z' |
| 29 | + jest |
| 30 | + .spyOn(global, 'Date') |
| 31 | + .mockImplementation(() => ({ toISOString: () => fixedDate }) as any) |
| 32 | + |
| 33 | + const result = postBody('discourse.example.com', uploadResult, 'abc1234 ') |
| 34 | + |
| 35 | + expect(result).toContain('petstore.yaml') |
| 36 | + expect(result).toContain( |
| 37 | + 'https://discourse.example.com/uploads/short/abc123.yaml' |
| 38 | + ) |
| 39 | + expect(result).toContain('upload://abc123.yaml') |
| 40 | + expect(result).toContain('sha abc1234') // commit is trimmed |
| 41 | + expect(result).toContain(fixedDate) |
| 42 | + |
| 43 | + jest.restoreAllMocks() |
| 44 | + }) |
| 45 | + |
| 46 | + it('matches the exact full content', () => { |
| 47 | + const fixedDate = '2024-01-01T00:00:00.000Z' |
| 48 | + jest |
| 49 | + .spyOn(global, 'Date') |
| 50 | + .mockImplementation(() => ({ toISOString: () => fixedDate }) as any) |
| 51 | + |
| 52 | + const result = postBody('discourse.example.com', uploadResult, 'abc1234 ') |
| 53 | + |
| 54 | + expect(result).toBe( |
| 55 | + 'API Documentation/Specification `petstore.yaml`\n' + |
| 56 | + '\n' + |
| 57 | + '```apidoc\n' + |
| 58 | + 'https://discourse.example.com/uploads/short/abc123.yaml\n' + |
| 59 | + '```\n' + |
| 60 | + '\n' + |
| 61 | + '[petstore.yaml|attachment](upload://abc123.yaml)\n' + |
| 62 | + '\n' + |
| 63 | + '*last updated*: 2024-01-01T00:00:00.000Z (sha abc1234)\n' |
| 64 | + ) |
| 65 | + |
| 66 | + jest.restoreAllMocks() |
| 67 | + }) |
| 68 | + |
| 69 | + it('uses a custom body template when provided', () => { |
| 70 | + const customTemplate = 'Custom: {ORIGINAL_FILENAME} at {DISCOURSE_URL}' |
| 71 | + const result = postBody( |
| 72 | + 'discourse.example.com', |
| 73 | + uploadResult, |
| 74 | + 'abc1234', |
| 75 | + customTemplate |
| 76 | + ) |
| 77 | + expect(result).toBe('Custom: petstore.yaml at discourse.example.com') |
| 78 | + }) |
| 79 | +}) |
| 80 | + |
| 81 | +const mockPost = jest.fn() |
| 82 | +const mockHttp = { |
| 83 | + post: mockPost, |
| 84 | + interceptors: { request: { use: jest.fn() } } |
| 85 | +} |
| 86 | + |
| 87 | +const uploadApiResponse = { |
| 88 | + url: 'https://discourse.example.com/uploads/file.yaml', |
| 89 | + short_url: 'upload://abc123.yaml', |
| 90 | + short_path: 'uploads/short/abc123.yaml', |
| 91 | + original_filename: 'petstore.yaml' |
| 92 | +} |
| 93 | + |
| 94 | +const runArgs = [ |
| 95 | + 'discourse.example.com', |
| 96 | + '42', |
| 97 | + 'api-key', |
| 98 | + 'api-user', |
| 99 | + 'abc1234', |
| 100 | + 'petstore.yaml' |
| 101 | +] as const |
| 102 | + |
| 103 | +describe('run', () => { |
| 104 | + beforeEach(() => { |
| 105 | + ;(Axios.create as jest.Mock).mockReturnValue(mockHttp) |
| 106 | + ;(fs.readFileSync as jest.Mock).mockReturnValue(Buffer.from('spec content')) |
| 107 | + }) |
| 108 | + |
| 109 | + describe('success', () => { |
| 110 | + beforeEach(() => { |
| 111 | + mockPost.mockResolvedValue({ data: uploadApiResponse }) |
| 112 | + global.fetch = jest.fn().mockResolvedValue({ statusText: 'OK' }) |
| 113 | + }) |
| 114 | + |
| 115 | + it('creates the Axios instance with the correct base URL and auth headers', async () => { |
| 116 | + await run(...runArgs) |
| 117 | + |
| 118 | + expect(Axios.create).toHaveBeenCalledWith( |
| 119 | + expect.objectContaining({ |
| 120 | + baseURL: 'https://discourse.example.com', |
| 121 | + headers: expect.objectContaining({ |
| 122 | + 'Api-Key': 'api-key', |
| 123 | + 'Api-Username': 'api-user' |
| 124 | + }) |
| 125 | + }) |
| 126 | + ) |
| 127 | + }) |
| 128 | + |
| 129 | + it('reads the spec file and posts it to /uploads.json', async () => { |
| 130 | + await run(...runArgs) |
| 131 | + |
| 132 | + expect(fs.readFileSync).toHaveBeenCalledWith('petstore.yaml') |
| 133 | + expect(mockPost).toHaveBeenCalledWith( |
| 134 | + '/uploads.json', |
| 135 | + expect.anything(), |
| 136 | + { |
| 137 | + params: { type: 'composer', synchronous: true } |
| 138 | + } |
| 139 | + ) |
| 140 | + }) |
| 141 | + |
| 142 | + it('sends a PUT request to the post URL with auth headers and post body', async () => { |
| 143 | + await run(...runArgs) |
| 144 | + |
| 145 | + expect(global.fetch).toHaveBeenCalledWith( |
| 146 | + 'https://discourse.example.com/posts/42.json', |
| 147 | + expect.objectContaining({ |
| 148 | + method: 'PUT', |
| 149 | + headers: expect.objectContaining({ |
| 150 | + 'Content-Type': 'application/json', |
| 151 | + 'Api-Key': 'api-key', |
| 152 | + 'Api-Username': 'api-user' |
| 153 | + }) |
| 154 | + }) |
| 155 | + ) |
| 156 | + const body = JSON.parse((global.fetch as jest.Mock).mock.calls[0][1].body) |
| 157 | + expect(body.post.edit_reason).toBe('Uploaded spec at abc1234') |
| 158 | + expect(body.post.raw).toContain('petstore.yaml') |
| 159 | + }) |
| 160 | + }) |
| 161 | + |
| 162 | + it('calls core.setFailed when the upload request fails', async () => { |
| 163 | + mockPost.mockRejectedValue(new Error('Network error')) |
| 164 | + jest.spyOn(console, 'error').mockImplementation(() => {}) |
| 165 | + |
| 166 | + await run(...runArgs) |
| 167 | + |
| 168 | + expect(core.setFailed).toHaveBeenCalledWith('Network error') |
| 169 | + }) |
| 170 | + |
| 171 | + it('calls core.error and process.exit(1) when updating the post fails', async () => { |
| 172 | + mockPost.mockResolvedValue({ data: uploadApiResponse }) |
| 173 | + global.fetch = jest.fn().mockRejectedValue(new Error('Fetch error')) |
| 174 | + const mockExit = jest |
| 175 | + .spyOn(process, 'exit') |
| 176 | + .mockImplementation(() => undefined as never) |
| 177 | + |
| 178 | + await run(...runArgs) |
| 179 | + |
| 180 | + expect(core.error).toHaveBeenCalledWith(new Error('Fetch error')) |
| 181 | + expect(mockExit).toHaveBeenCalledWith(1) |
| 182 | + mockExit.mockRestore() |
| 183 | + }) |
| 184 | +}) |
0 commit comments