blob: 0d926892e8c3d9daa8279445fc4b2e1cd10f59ef [file]
<!DOCTYPE html>
<!--
Copyright 2021 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
-->
<meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1.0, user-scalable=yes">
<title>resultdb-client tests</title>
<script src="../node_modules/@webcomponents/webcomponentsjs/webcomponents-loader.js"></script>
<script src="../node_modules/web-component-tester/browser.js"></script>
<script type="module">
import {
ResultDbV1Client,
} from '../src/main/resources/static/resultdb-client.js';
suite('ResultDbV1Client tests', () => {
let sandbox;
let client;
setup(() => {
sandbox = sinon.sandbox.create();
client = new ResultDbV1Client('resultdb.example.com');
client._getAccessToken = async () => 'accessToken';
});
teardown(() => {
sandbox.restore();
});
test('fetchTestVariants caches both additional and non-additional variants',
async () => {
const build = {
id: '123',
endTime: '2017-12-15T01:30:15.05Z',
infra: {
resultdb: {
invocation: 'foo'
}
},
};
const fetch = sandbox.stub(window, 'fetch');
fetch.onCall(0).returns(Promise.resolve({
ok: true,
text: async () => `)]}'${JSON.stringify({testVariants: []})}`,
}));
fetch.onCall(1).returns(Promise.resolve({
ok: true,
text: async () => `)]}'${JSON.stringify({testVariants: [{status: 'FLAKY'}]})}`,
}));
// inclAdditional is false.
assert.deepEqual(
await client.fetchTestVariants(build, 1, false),
[],
);
// inclAdditional is true.
assert.deepEqual(
await client.fetchTestVariants(build, 1, true),
[{status: 'FLAKY'}],
);
sinon.assert.calledTwice(fetch);
// Results should be cached.
assert.deepEqual(
await client.fetchTestVariants(build, 1, false),
[],
);
assert.deepEqual(
await client.fetchTestVariants(build, 1, true),
[{status: 'FLAKY'}],
);
sinon.assert.calledTwice(fetch);
});
test('fetchArtifacts fetches and sorts artifacts', async () => {
const firstPage = {
artifacts: Array.from({length: 10},
() => ({
name: 'invocations/123/result/artifacts/foo',
fetchUrlExpiration: '2017-12-15T01:30:15.05Z',
})),
};
const secondPage = {
artifacts: [{
name: 'invocations/123/artifacts/foo',
fetchUrlExpiration: '2017-12-15T01:30:15.05Z',
}],
};
const fetch = sandbox.stub(window, 'fetch');
fetch.onCall(0).returns(Promise.resolve({
ok: true,
text: async () => `)]}'${JSON.stringify(firstPage)}`,
}));
fetch.onCall(1).returns(Promise.resolve({
ok: true,
text: async () => `)]}'${JSON.stringify(secondPage)}`,
}));
assert.deepEqual(
await client.fetchArtifacts('invocations/123/result', true),
{
fromCache: false,
resArtifacts: firstPage.artifacts,
invArtifacts: secondPage.artifacts,
}
);
sinon.assert.calledTwice(fetch);
});
test('fetchArtifacts caches artifacts', async () => {
const fooArtifacts = [{
name: 'invocations/123/artifacts/foo',
fetchUrlExpiration: '2999-12-15T01:30:15.05Z',
}];
const barArtifacts = [{
name: 'invocations/123/artifacts/bar',
fetchUrlExpiration: '2999-12-15T01:30:15.05Z',
}];
const fetch = sandbox.stub(window, 'fetch');
fetch.onCall(0).returns(Promise.resolve({
ok: true,
text: async () => `)]}'${JSON.stringify({artifacts: fooArtifacts})}`,
}));
fetch.onCall(1).returns(Promise.resolve({
ok: true,
text: async () => `)]}'${JSON.stringify([])}`,
}));
fetch.onCall(2).returns(Promise.resolve({
ok: true,
text: async () => `)]}'${JSON.stringify({artifacts: barArtifacts})}`,
}));
fetch.onCall(3).returns(Promise.resolve({
ok: true,
text: async () => `)]}'${JSON.stringify([])}`,
}));
assert.deepEqual(await client.fetchArtifacts('invocations/123/foo', true),
{
fromCache: false,
resArtifacts: fooArtifacts,
invArtifacts: [],
}
);
assert.deepEqual(await client.fetchArtifacts('invocations/123/bar', true),
{
fromCache: false,
resArtifacts: barArtifacts,
invArtifacts: [],
}
);
});
});
</script>