3b31e5c071d1cc05cc8a8c0d46bf3d5a367ef309
[WebKit-https.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 fetchingPromise = ownerCommit().fetchOwnedCommits();
195             const requests = MockRemoteAPI.requests;
196             assert.equal(requests.length, 1);
197             assert.equal(requests[0].url, '../api/commits/111/owned-commits?owner-revision=10.11.4%2015E65');
198             assert.equal(requests[0].method, 'GET');
199
200             requests[0].resolve({commits: [{
201                 id: 233,
202                 repository: MockModels.ownedRepository.id(),
203                 revision: '6f8b0dbbda95a440503b88db1dd03dad3a7b07fb',
204                 time: +(new Date('2016-05-13T00:55:57.841344Z')),
205             }]});
206             return fetchingPromise.then((ownedCommits) => {
207                 assert.equal(ownedCommits.length, 1);
208                 assert.equal(ownedCommits[0].repository(), MockModels.ownedRepository);
209                 assert.equal(ownedCommits[0].revision(), '6f8b0dbbda95a440503b88db1dd03dad3a7b07fb');
210                 assert.equal(ownedCommits[0].id(), 233);
211             });
212         });
213
214         it('should only fetch owned-commits exactly once', () => {
215             const commit = ownerCommit();
216             const fetchingPromise = commit.fetchOwnedCommits();
217             const requests = MockRemoteAPI.requests;
218             let existingOwnedCommits = null;
219             assert.equal(requests.length, 1);
220             assert.equal(requests[0].url, '../api/commits/111/owned-commits?owner-revision=10.11.4%2015E65');
221             assert.equal(requests[0].method, 'GET');
222
223             MockRemoteAPI.requests[0].resolve({commits: [{
224                 id: 233,
225                 repository: MockModels.ownedRepository.id(),
226                 revision: '6f8b0dbbda95a440503b88db1dd03dad3a7b07fb',
227                 time: +(new Date('2016-05-13T00:55:57.841344Z')),
228             }]});
229
230             return fetchingPromise.then((ownedCommits) => {
231                 existingOwnedCommits = ownedCommits;
232                 assert.equal(ownedCommits.length, 1);
233                 assert.equal(ownedCommits[0].repository(), MockModels.ownedRepository);
234                 assert.equal(ownedCommits[0].revision(), '6f8b0dbbda95a440503b88db1dd03dad3a7b07fb');
235                 assert.equal(ownedCommits[0].id(), 233);
236                 return commit.fetchOwnedCommits();
237             }).then((ownedCommits) => {
238                 assert.equal(requests.length, 1);
239                 assert.equal(existingOwnedCommits, ownedCommits);
240             });
241         });
242     });
243
244     describe('diffOwnedCommits', () => {
245         beforeEach(() => {
246             MockRemoteAPI.reset();
247         });
248
249         it('should return difference between 2 owned-commits', () => {
250             const oneCommit = ownerCommit();
251             const otherCommit = otherOwnerCommit();
252             const fetchingPromise = oneCommit.fetchOwnedCommits();
253             const requests = MockRemoteAPI.requests;
254             assert.equal(requests.length, 1);
255             assert.equal(requests[0].url, '../api/commits/111/owned-commits?owner-revision=10.11.4%2015E65');
256             assert.equal(requests[0].method, 'GET');
257
258             requests[0].resolve({commits: [{
259                 id: 233,
260                 repository: MockModels.ownedRepository.id(),
261                 revision: '6f8b0dbbda95a440503b88db1dd03dad3a7b07fb',
262                 time: +(new Date('2016-05-13T00:55:57.841344Z')),
263             }, {
264                 id: 299,
265                 repository: MockModels.webkitGit.id(),
266                 revision: '04a6c72038f0b771a19248ca2549e1258617b5fc',
267                 time: +(new Date('2016-05-13T00:55:57.841344Z')),
268             }]});
269
270             return fetchingPromise.then(() => {
271                 const otherFetchingPromise = otherCommit.fetchOwnedCommits();
272                 assert.equal(requests.length, 2);
273                 assert.equal(requests[1].url, '../api/commits/111/owned-commits?owner-revision=10.11.4%2015E66');
274                 assert.equal(requests[1].method, 'GET');
275
276                 requests[1].resolve({commits: [{
277                     id: 234,
278                     repository: MockModels.ownedRepository.id(),
279                     revision: 'd5099e03b482abdd77f6c4dcb875afd05bda5ab8',
280                     time: +(new Date('2016-05-13T00:55:57.841344Z')),
281                 }, {
282                     id: 299,
283                     repository: MockModels.webkitGit.id(),
284                     revision: '04a6c72038f0b771a19248ca2549e1258617b5fc',
285                     time: +(new Date('2016-05-13T00:55:57.841344Z')),
286                 }]});
287
288                 return otherFetchingPromise;
289             }).then(() => {
290                 const difference = CommitLog.diffOwnedCommits(oneCommit, otherCommit);
291                 assert.equal(difference.size, 1);
292                 assert.equal(difference.keys().next().value, MockModels.ownedRepository);
293             });
294
295         });
296     });
297 });