Add UI for A/B testing on owned commits.
[WebKit.git] / Websites / perf.webkit.org / unit-tests / commit-log-tests.js
1 'use strict';
2
3 const assert = require('assert');
4
5 require('../tools/js/v3-models.js');
6 const MockModels = require('./resources/mock-v3-models.js').MockModels;
7 const MockRemoteAPI = require('../unit-tests/resources/mock-remote-api.js').MockRemoteAPI;
8
9 function webkitCommit()
10 {
11     return new CommitLog(1, {
12         repository: MockModels.webkit,
13         revision: '200805',
14         time: +(new Date('2016-05-13T00:55:57.841344Z')),
15     });
16 }
17
18 function oldWebKitCommit()
19 {
20     return new CommitLog(2, {
21         repository: MockModels.webkit,
22         revision: '200574',
23         time: +(new Date('2016-05-09T14:59:23.553767Z')),
24     });
25 }
26
27 function gitWebKitCommit()
28 {
29     return new CommitLog(3, {
30         repository: MockModels.webkit,
31         revision: '6f8b0dbbda95a440503b88db1dd03dad3a7b07fb',
32         time: +(new Date('2016-05-13T00:55:57.841344Z')),
33     });
34 }
35
36 function oldGitWebKitCommit()
37 {
38     return new CommitLog(4, {
39         repository: MockModels.webkit,
40         revision: 'ffda14e6db0746d10d0f050907e4a7325851e502',
41         time: +(new Date('2016-05-09T14:59:23.553767Z')),
42     });
43 }
44
45 function osxCommit()
46 {
47     return new CommitLog(5, {
48         repository: MockModels.osx,
49         revision: '10.11.4 15E65',
50         time: null,
51     });
52 }
53
54 function oldOSXCommit()
55 {
56     return new CommitLog(6, {
57         repository: MockModels.osx,
58         revision: '10.11.3 15D21',
59         time: null,
60     });
61 }
62
63 function commitWithoutOwnedCommits()
64 {
65     return new CommitLog(6, {
66         repository: MockModels.ownerRepository,
67         revision: '10.11.4 15E66',
68         ownsCommits: false,
69         time: null,
70     });
71 }
72
73 function ownerCommit()
74 {
75     return new CommitLog(5, {
76         repository: MockModels.ownerRepository,
77         revision: '10.11.4 15E65',
78         ownsCommits: true,
79         time: null,
80     });
81 }
82
83 function otherOwnerCommit()
84 {
85     return new CommitLog(5, {
86         repository: MockModels.ownerRepository,
87         revision: '10.11.4 15E66',
88         ownsCommits: true,
89         time: null,
90     });
91 }
92
93 describe('CommitLog', function () {
94     MockModels.inject();
95
96     describe('label', function () {
97         it('should prefix SVN revision with "r"', function () {
98             assert.equal(webkitCommit().label(), 'r200805');
99         });
100
101         it('should truncate a Git hash at 8th character', function () {
102             assert.equal(gitWebKitCommit().label(), '6f8b0dbb');
103         });
104
105         it('should not modify OS X version', function () {
106             assert.equal(osxCommit().label(), '10.11.4 15E65');
107         });
108     });
109
110     describe('title', function () {
111         it('should prefix SVN revision with "r"', function () {
112             assert.equal(webkitCommit().title(), 'WebKit at r200805');
113         });
114
115         it('should truncate a Git hash at 8th character', function () {
116             assert.equal(gitWebKitCommit().title(), 'WebKit at 6f8b0dbb');
117         });
118
119         it('should not modify OS X version', function () {
120             assert.equal(osxCommit().title(), 'OS X at 10.11.4 15E65');
121         });
122     });
123
124     describe('diff', function () {
125         it('should use label() as the label the previous commit is missing', function () {
126             assert.deepEqual(webkitCommit().diff(), {
127                 label: 'r200805',
128                 url: 'http://trac.webkit.org/changeset/200805',
129                 repository: MockModels.webkit
130             });
131
132             assert.deepEqual(gitWebKitCommit().diff(), {
133                 label: '6f8b0dbb',
134                 url: 'http://trac.webkit.org/changeset/6f8b0dbbda95a440503b88db1dd03dad3a7b07fb',
135                 repository: MockModels.webkit,
136             });
137
138             assert.deepEqual(osxCommit().diff(), {
139                 label: '10.11.4 15E65',
140                 url: '',
141                 repository: MockModels.osx,
142             });
143         });
144
145         it('should use increment the old SVN revision by 1', function () {
146             assert.deepEqual(webkitCommit().diff(oldWebKitCommit()), {
147                 label: 'r200574-r200805',
148                 url: '',
149                 repository: MockModels.webkit
150             });
151         });
152
153         it('should truncate a Git hash at 8th character', function () {
154             assert.deepEqual(gitWebKitCommit().diff(oldGitWebKitCommit()), {
155                 label: 'ffda14e6..6f8b0dbb',
156                 url: '',
157                 repository: MockModels.webkit
158             });
159         });
160
161         it('should surround "-" with spaces', function () {
162             assert.deepEqual(osxCommit().diff(oldOSXCommit()), {
163                 label: '10.11.3 15D21 - 10.11.4 15E65',
164                 url: '',
165                 repository: MockModels.osx
166             });
167         });
168     });
169
170     describe('fetchOwnedCommits', () => {
171         beforeEach(() => {
172             MockRemoteAPI.inject();
173         });
174
175         it('should reject if repository of the commit does not own other repositories', () => {
176             const commit = osxCommit();
177             return commit.fetchOwnedCommits().then(() => {
178                assert(false, 'Should not execute this line.');
179             }, (error) => {
180                 assert.equal(error, undefined);
181             });
182         });
183
184         it('should reject if commit does not own other owned-commits', () => {
185             const commit = commitWithoutOwnedCommits();
186             return commit.fetchOwnedCommits().then(() => {
187                 assert(false, 'Should not execute this line.');
188             }, (error) => {
189                 assert.equal(error, undefined);
190             });
191         });
192
193         it('should return owned-commit for a valid commit revision', () => {
194             const commit = ownerCommit();
195             const fetchingPromise = commit.fetchOwnedCommits();
196             const requests = MockRemoteAPI.requests;
197             assert.equal(requests.length, 1);
198             assert.equal(requests[0].url, '../api/commits/111/owned-commits?owner-revision=10.11.4%2015E65');
199             assert.equal(requests[0].method, 'GET');
200
201             requests[0].resolve({commits: [{
202                 id: 233,
203                 repository: MockModels.ownedRepository.id(),
204                 revision: '6f8b0dbbda95a440503b88db1dd03dad3a7b07fb',
205                 time: +(new Date('2016-05-13T00:55:57.841344Z')),
206             }]});
207             return fetchingPromise.then((ownedCommits) => {
208                 assert.equal(ownedCommits.length, 1);
209                 assert.equal(ownedCommits[0].repository(), MockModels.ownedRepository);
210                 assert.equal(ownedCommits[0].revision(), '6f8b0dbbda95a440503b88db1dd03dad3a7b07fb');
211                 assert.equal(ownedCommits[0].id(), 233);
212                 assert.equal(ownedCommits[0].ownerCommit(), commit);
213             });
214         });
215
216         it('should only fetch owned-commits exactly once', () => {
217             const commit = ownerCommit();
218             const fetchingPromise = commit.fetchOwnedCommits();
219             const requests = MockRemoteAPI.requests;
220             let existingOwnedCommits = null;
221             assert.equal(requests.length, 1);
222             assert.equal(requests[0].url, '../api/commits/111/owned-commits?owner-revision=10.11.4%2015E65');
223             assert.equal(requests[0].method, 'GET');
224
225             MockRemoteAPI.requests[0].resolve({commits: [{
226                 id: 233,
227                 repository: MockModels.ownedRepository.id(),
228                 revision: '6f8b0dbbda95a440503b88db1dd03dad3a7b07fb',
229                 time: +(new Date('2016-05-13T00:55:57.841344Z')),
230             }]});
231
232             return fetchingPromise.then((ownedCommits) => {
233                 existingOwnedCommits = ownedCommits;
234                 assert.equal(ownedCommits.length, 1);
235                 assert.equal(ownedCommits[0].repository(), MockModels.ownedRepository);
236                 assert.equal(ownedCommits[0].revision(), '6f8b0dbbda95a440503b88db1dd03dad3a7b07fb');
237                 assert.equal(ownedCommits[0].id(), 233);
238                 assert.equal(ownedCommits[0].ownerCommit(), commit);
239                 return commit.fetchOwnedCommits();
240             }).then((ownedCommits) => {
241                 assert.equal(requests.length, 1);
242                 assert.equal(existingOwnedCommits, ownedCommits);
243             });
244         });
245     });
246
247     describe('ownedCommitDifferenceForOwnerCommits', () => {
248         beforeEach(() => {
249             MockRemoteAPI.reset();
250         });
251
252         it('should return difference between owned-commits of 2 owner commits', () => {
253             const oneCommit = ownerCommit();
254             const otherCommit = otherOwnerCommit();
255             const fetchingPromise = oneCommit.fetchOwnedCommits();
256             const requests = MockRemoteAPI.requests;
257             assert.equal(requests.length, 1);
258             assert.equal(requests[0].url, '../api/commits/111/owned-commits?owner-revision=10.11.4%2015E65');
259             assert.equal(requests[0].method, 'GET');
260
261             requests[0].resolve({commits: [{
262                 id: 233,
263                 repository: MockModels.ownedRepository.id(),
264                 revision: '6f8b0dbbda95a440503b88db1dd03dad3a7b07fb',
265                 time: +(new Date('2016-05-13T00:55:57.841344Z')),
266             }, {
267                 id: 299,
268                 repository: MockModels.webkitGit.id(),
269                 revision: '04a6c72038f0b771a19248ca2549e1258617b5fc',
270                 time: +(new Date('2016-05-13T00:55:57.841344Z')),
271             }]});
272
273             return fetchingPromise.then((ownedCommits) => {
274                 assert.equal(ownedCommits.length, 2);
275                 assert.equal(ownedCommits[0].repository(), MockModels.ownedRepository);
276                 assert.equal(ownedCommits[0].revision(), '6f8b0dbbda95a440503b88db1dd03dad3a7b07fb');
277                 assert.equal(ownedCommits[0].id(), 233);
278                 assert.equal(ownedCommits[0].ownerCommit(), oneCommit);
279                 assert.equal(ownedCommits[1].repository(), MockModels.webkitGit);
280                 assert.equal(ownedCommits[1].revision(), '04a6c72038f0b771a19248ca2549e1258617b5fc');
281                 assert.equal(ownedCommits[1].id(), 299);
282                 assert.equal(ownedCommits[1].ownerCommit(), oneCommit);
283
284                 const otherFetchingPromise = otherCommit.fetchOwnedCommits();
285                 assert.equal(requests.length, 2);
286                 assert.equal(requests[1].url, '../api/commits/111/owned-commits?owner-revision=10.11.4%2015E66');
287                 assert.equal(requests[1].method, 'GET');
288
289                 requests[1].resolve({commits: [{
290                     id: 234,
291                     repository: MockModels.ownedRepository.id(),
292                     revision: 'd5099e03b482abdd77f6c4dcb875afd05bda5ab8',
293                     time: +(new Date('2016-05-13T00:55:57.841344Z')),
294                 }, {
295                     id: 299,
296                     repository: MockModels.webkitGit.id(),
297                     revision: '04a6c72038f0b771a19248ca2549e1258617b5fc',
298                     time: +(new Date('2016-05-13T00:55:57.841344Z')),
299                 }]});
300
301                 return otherFetchingPromise;
302             }).then((ownedCommits) => {
303                 assert.equal(ownedCommits.length, 2);
304                 assert.equal(ownedCommits[0].repository(), MockModels.ownedRepository);
305                 assert.equal(ownedCommits[0].revision(), 'd5099e03b482abdd77f6c4dcb875afd05bda5ab8');
306                 assert.equal(ownedCommits[0].id(), 234);
307                 assert.equal(ownedCommits[0].ownerCommit(), otherCommit);
308                 assert.equal(ownedCommits[1].repository(), MockModels.webkitGit);
309                 assert.equal(ownedCommits[1].revision(), '04a6c72038f0b771a19248ca2549e1258617b5fc');
310                 assert.equal(ownedCommits[1].id(), 299);
311                 assert.equal(ownedCommits[1].ownerCommit(), otherCommit);
312                 const difference = CommitLog.ownedCommitDifferenceForOwnerCommits(oneCommit, otherCommit);
313                 assert.equal(difference.size, 1);
314                 assert.equal(difference.keys().next().value, MockModels.ownedRepository);
315             });
316
317         });
318     });
319 });