| import {assert} from '@open-wc/testing'; |
| |
| import './test-setup'; |
| import '../cache-object'; |
| import {BuildbucketV2Client, BuildRequests} from '../buildbucket-client'; |
| |
| suite('BuildbucketV2Client tests', () => { |
| let sandbox: sinon.SinonSandbox; |
| let client: BuildbucketV2Client; |
| |
| setup(() => { |
| sandbox = sinon.createSandbox(); |
| client = new BuildbucketV2Client('buildbucket.example.com', '123456'); |
| client.getAuthorizationHeader = async () => { |
| return {authorization: 'Bearer accessToken'}; |
| }; |
| }); |
| |
| teardown(() => { |
| sandbox.restore(); |
| }); |
| |
| test('batch makes POST request to buildbucket v2 API', async () => { |
| const expected = { |
| responses: [ |
| { |
| searchBuilds: { |
| builds: [], |
| }, |
| }, |
| ], |
| }; |
| sandbox.stub(window, 'fetch').returns( |
| Promise.resolve({ |
| ok: true, |
| text: async () => `)]}'${JSON.stringify(expected)}`, |
| } as Response) |
| ); |
| |
| const req: BuildRequests = { |
| requests: [ |
| { |
| searchBuilds: { |
| pageSize: 100, |
| predicate: { |
| includeExperimental: false, |
| gerritChanges: [], |
| builder: { |
| project: 'chromium', |
| }, |
| }, |
| }, |
| }, |
| ], |
| }; |
| const actual = await client.batch(req); |
| assert.deepEqual(actual, expected); |
| assert.isTrue( |
| (fetch as any).alwaysCalledWithExactly( |
| 'https://buildbucket.example.com/prpc/buildbucket.v2.Builds/Batch', |
| { |
| method: 'POST', |
| headers: { |
| accept: 'application/json', |
| 'content-type': 'application/json', |
| authorization: 'Bearer accessToken', |
| }, |
| body: JSON.stringify(req), |
| signal: undefined, |
| } |
| ) |
| ); |
| }); |
| }); |