|
1 |
| -import {wait} from '../src/wait' |
2 |
| -import * as process from 'process' |
3 |
| -import * as cp from 'child_process' |
4 |
| -import * as path from 'path' |
5 |
| -import {expect, test} from '@jest/globals' |
6 |
| - |
7 |
| -test('throws invalid number', async () => { |
8 |
| - const input = parseInt('foo', 10) |
9 |
| - await expect(wait(input)).rejects.toThrow('milliseconds not a number') |
10 |
| -}) |
| 1 | +import {run, request} from '../src/main' |
| 2 | +import * as core from '@actions/core' |
| 3 | +import nock from 'nock' |
| 4 | + |
| 5 | +const currentsApiUrl = 'http://localhost:4000/v1' |
| 6 | +const currentsApiCancelationPath = '/runs/cancel-by-github-ci' |
| 7 | +const githubRunId = '45166321' |
| 8 | +const githubRunAttempt = '1' |
| 9 | + |
| 10 | +describe('input validation', () => { |
| 11 | + test('currents-api-url is required', async () => { |
| 12 | + const spy = jest.spyOn(core, 'setFailed') |
| 13 | + |
| 14 | + await run() |
| 15 | + |
| 16 | + expect(spy).toHaveBeenCalledWith( |
| 17 | + 'Input required and not supplied: currents-api-url' |
| 18 | + ) |
| 19 | + }) |
| 20 | + |
| 21 | + test('bearer-token is required', async () => { |
| 22 | + process.env['INPUT_CURRENTS-API-URL'] = currentsApiUrl |
| 23 | + |
| 24 | + const spy = jest.spyOn(core, 'setFailed') |
| 25 | + |
| 26 | + await run() |
| 27 | + |
| 28 | + expect(spy).toHaveBeenCalledWith( |
| 29 | + 'Input required and not supplied: bearer-token' |
| 30 | + ) |
| 31 | + }) |
11 | 32 |
|
12 |
| -test('wait 500 ms', async () => { |
13 |
| - const start = new Date() |
14 |
| - await wait(500) |
15 |
| - const end = new Date() |
16 |
| - var delta = Math.abs(end.getTime() - start.getTime()) |
17 |
| - expect(delta).toBeGreaterThan(450) |
| 33 | + test('github-run-id is required', async () => { |
| 34 | + process.env['INPUT_CURRENTS-API-URL'] = currentsApiUrl |
| 35 | + process.env['INPUT_BEARER-TOKEN'] = 'bearer-token' |
| 36 | + |
| 37 | + const spy = jest.spyOn(core, 'setFailed') |
| 38 | + |
| 39 | + await run() |
| 40 | + |
| 41 | + expect(spy).toHaveBeenCalledWith( |
| 42 | + 'Input required and not supplied: github-run-id' |
| 43 | + ) |
| 44 | + }) |
| 45 | + |
| 46 | + test('github-run-attempt is required', async () => { |
| 47 | + process.env['INPUT_CURRENTS-API-URL'] = currentsApiUrl |
| 48 | + process.env['INPUT_BEARER-TOKEN'] = 'bearer-token' |
| 49 | + process.env['INPUT_GITHUB-RUN-ID'] = githubRunId |
| 50 | + |
| 51 | + const spy = jest.spyOn(core, 'setFailed') |
| 52 | + |
| 53 | + await run() |
| 54 | + |
| 55 | + expect(spy).toHaveBeenCalledWith( |
| 56 | + 'Input required and not supplied: github-run-attempt' |
| 57 | + ) |
| 58 | + }) |
18 | 59 | })
|
19 | 60 |
|
20 |
| -// shows how the runner will run a javascript action with env / stdout protocol |
21 |
| -test('test runs', () => { |
22 |
| - process.env['INPUT_MILLISECONDS'] = '500' |
23 |
| - const np = process.execPath |
24 |
| - const ip = path.join(__dirname, '..', 'lib', 'main.js') |
25 |
| - const options: cp.ExecFileSyncOptions = { |
26 |
| - env: process.env |
27 |
| - } |
28 |
| - console.log(cp.execFileSync(np, [ip], options).toString()) |
| 61 | +describe('api request', () => { |
| 62 | + beforeEach(() => { |
| 63 | + process.env['INPUT_CURRENTS-API-URL'] = currentsApiUrl |
| 64 | + process.env['INPUT_BEARER-TOKEN'] = 'bearer-token' |
| 65 | + process.env['INPUT_GITHUB-RUN-ID'] = githubRunId |
| 66 | + process.env['INPUT_GITHUB-RUN-ATTEMPT'] = githubRunAttempt |
| 67 | + }) |
| 68 | + |
| 69 | + afterEach(() => { |
| 70 | + delete process.env['INPUT_CURRENTS-API-URL'] |
| 71 | + delete process.env['INPUT_BEARER-TOKEN'] |
| 72 | + delete process.env['INPUT_GITHUB-RUN-ID'] |
| 73 | + delete process.env['INPUT_GITHUB-RUN-ATTEMPT'] |
| 74 | + }) |
| 75 | + |
| 76 | + test('should resolve when status code is not 200', () => { |
| 77 | + const error = JSON.stringify({ |
| 78 | + error: 'Invalid params' |
| 79 | + }) |
| 80 | + nock(currentsApiUrl).put(currentsApiCancelationPath).reply(400, error) |
| 81 | + expect( |
| 82 | + request({ |
| 83 | + url: `${currentsApiUrl}${currentsApiCancelationPath}`, |
| 84 | + body: { |
| 85 | + githubRunId, |
| 86 | + githubRunAttempt |
| 87 | + }, |
| 88 | + bearerToken: 'token' |
| 89 | + }) |
| 90 | + ).rejects.toThrowError(error) |
| 91 | + }) |
| 92 | + |
| 93 | + test('should fail when status code is 404', async () => { |
| 94 | + nock(currentsApiUrl).put(currentsApiCancelationPath).reply(404, {}) |
| 95 | + const spy = jest.spyOn(core, 'setFailed') |
| 96 | + |
| 97 | + await run() |
| 98 | + expect(spy).toHaveBeenCalledWith('Resource not found') |
| 99 | + }) |
| 100 | + |
| 101 | + test('should retry when status code is 500', async () => { |
| 102 | + nock(currentsApiUrl).put(currentsApiCancelationPath).reply(500) |
| 103 | + const spy = jest.spyOn(core, 'setFailed') |
| 104 | + |
| 105 | + await run() |
| 106 | + expect(spy).toBeCalled() |
| 107 | + }, 15000) |
| 108 | + |
| 109 | + test('should fail when the input is invalid', async () => { |
| 110 | + process.env['INPUT_CURRENTS-API-URL'] = 'bad url' |
| 111 | + const spy = jest.spyOn(core, 'setFailed') |
| 112 | + |
| 113 | + await run() |
| 114 | + expect(spy).toHaveBeenCalledWith(expect.any(String)) |
| 115 | + }) |
| 116 | + |
| 117 | + test('should return the result when status code is 200', () => { |
| 118 | + const result = { |
| 119 | + githubRunId, |
| 120 | + githubRunAttempt, |
| 121 | + status: 'OK', |
| 122 | + actor: 'api', |
| 123 | + canceledAt: new Date().toDateString(), |
| 124 | + reason: 'api call' |
| 125 | + } |
| 126 | + |
| 127 | + nock(currentsApiUrl).put(currentsApiCancelationPath).reply(200, result) |
| 128 | + |
| 129 | + expect( |
| 130 | + request({ |
| 131 | + url: `${currentsApiUrl}${currentsApiCancelationPath}`, |
| 132 | + body: { |
| 133 | + githubRunId, |
| 134 | + githubRunAttempt |
| 135 | + }, |
| 136 | + bearerToken: 'token' |
| 137 | + }) |
| 138 | + ).resolves.toEqual({ |
| 139 | + headers: { |
| 140 | + 'content-type': 'application/json' |
| 141 | + }, |
| 142 | + result, |
| 143 | + statusCode: 200 |
| 144 | + }) |
| 145 | + }) |
| 146 | + |
| 147 | + test('should show the result when debug is enabled', async () => { |
| 148 | + const result = { |
| 149 | + githubRunId, |
| 150 | + githubRunAttempt, |
| 151 | + status: 'OK', |
| 152 | + actor: 'api', |
| 153 | + canceledAt: new Date().toDateString(), |
| 154 | + reason: 'api call' |
| 155 | + } |
| 156 | + |
| 157 | + const spy = jest.spyOn(core, 'debug') |
| 158 | + |
| 159 | + // enable debug |
| 160 | + process.env['RUNNER_DEBUG'] = '1' |
| 161 | + |
| 162 | + nock(currentsApiUrl).put(currentsApiCancelationPath).reply(200, result) |
| 163 | + |
| 164 | + await run() |
| 165 | + |
| 166 | + expect(spy).toHaveBeenCalled() |
| 167 | + }) |
29 | 168 | })
|
0 commit comments