0ea94811777bde0f620d92e4465ad8a3f1cd1f80
[WebKit.git] / Websites / perf.webkit.org / server-tests / api-commits.js
1 'use strict';
2
3 const assert = require('assert');
4
5 const TestServer = require('./resources/test-server.js');
6 const addSlaveForReport = require('./resources/common-operations.js').addSlaveForReport;
7 const prepareServerTest = require('./resources/common-operations.js').prepareServerTest;
8
9 describe("/api/commits/", function () {
10     prepareServerTest(this);
11
12     const subversionCommits = {
13         "slaveName": "someSlave",
14         "slavePassword": "somePassword",
15         "commits": [
16             {
17                 "repository": "WebKit",
18                 "revision": "210948",
19                 "time": "2017-01-20T02:52:34.577Z",
20                 "author": {"name": "Zalan Bujtas", "account": "zalan@apple.com"},
21                 "message": "a message",
22             },
23             {
24                 "repository": "WebKit",
25                 "revision": "210949",
26                 "time": "2017-01-20T03:23:50.645Z",
27                 "author": {"name": "Chris Dumez", "account": "cdumez@apple.com"},
28                 "message": "some message",
29             },
30             {
31                 "repository": "WebKit",
32                 "previousCommit": "210949",
33                 "revision": "210950",
34                 "time": "2017-01-20T03:49:37.887Z",
35                 "author": {"name": "Commit Queue", "account": "commit-queue@webkit.org"},
36                 "message": "another message",
37             }
38         ]
39     }
40
41     const systemVersionCommits = {
42         "slaveName": "someSlave",
43         "slavePassword": "somePassword",
44         "commits": [
45             {
46                 "repository": "OSX",
47                 "revision": "16D32",
48                 "order": 6
49             },
50             {
51                 "repository": "OSX",
52                 "revision": "16C68",
53                 "order": 5
54             },
55             {
56                 "repository": "OSX",
57                 "revision": "16C67",
58                 "order": 4
59             },
60             {
61                 "repository": "OSX",
62                 "revision": "16B2657",
63                 "order": 3
64             },
65             {
66                 "repository": "OSX",
67                 "revision": "16B2555",
68                 "order": 2
69             },
70             {
71                 "repository": "OSX",
72                 "revision": "16A323",
73                 "order": 1
74             }
75         ]
76     }
77
78     const notYetReportedCommit = {
79         revision: '210951',
80         time: '2017-01-20T03:56:20.045Z'
81     }
82
83     function assertCommitIsSameAsOneSubmitted(commit, submitted)
84     {
85         assert.equal(commit['revision'], submitted['revision']);
86         assert.equal(new Date(commit['time']).toISOString(), submitted['time']);
87         assert.equal(commit['message'], submitted['message']);
88         assert.equal(commit['authorName'], submitted['author']['name']);
89         assert.equal(commit['authorEmail'], submitted['author']['account']);
90         if(submitted['previousCommit']) {
91             assert.ok(commit['previousCommit']);
92         } else {
93             assert.equal(commit['previousCommit'], null);
94         }
95     }
96
97     describe('/api/commits/<repository>/', () => {
98         it("should return RepositoryNotFound when there are no matching repository", () => {
99             return TestServer.remoteAPI().getJSON('/api/commits/WebKit').then((response) => {
100                 assert.equal(response['status'], 'RepositoryNotFound');
101             });
102         });
103
104         it("should return an empty result when there are no reported commits", () => {
105             const db = TestServer.database();
106             return Promise.all([
107                 db.insert('repositories', {'id': 1, 'name': 'WebKit'}),
108                 db.insert('commits', {'repository': 1, 'revision': '210950', 'time': '2017-01-20T03:49:37.887Z'})
109             ]).then(() => {
110                 return TestServer.remoteAPI().getJSON('/api/commits/WebKit');
111             }).then((response) => {
112                 assert.equal(response['status'], 'OK');
113                 assert.deepEqual(response['commits'], []);
114             });
115         });
116
117         it("should return the list of all commits for a given repository", () => {
118             return addSlaveForReport(subversionCommits).then(() => {
119                 return TestServer.remoteAPI().postJSON('/api/report-commits/', subversionCommits);
120             }).then(function (response) {
121                 assert.equal(response['status'], 'OK');
122                 return TestServer.remoteAPI().getJSON('/api/commits/WebKit/');
123             }).then(function (result) {
124                 assert.equal(result['status'], 'OK');
125
126                 const commits = result['commits'];
127                 assert.equal(commits.length, 3);
128                 const submittedCommits = subversionCommits['commits'];
129                 assertCommitIsSameAsOneSubmitted(commits[0], submittedCommits[0]);
130                 assertCommitIsSameAsOneSubmitted(commits[1], submittedCommits[1]);
131                 assertCommitIsSameAsOneSubmitted(commits[2], submittedCommits[2]);
132                 assert.equal(commits[2]['previousCommit'], commits[1]['id']);
133             });
134         });
135
136         it("should return the list of ordered commits for a given repository", () => {
137             return addSlaveForReport(subversionCommits).then(() => {
138                 return TestServer.remoteAPI().postJSON('/api/report-commits/', systemVersionCommits);
139             }).then(function (response) {
140                 assert.equal(response['status'], 'OK');
141                 return TestServer.remoteAPI().getJSON('/api/commits/OSX/');
142             }).then(function (result) {
143                 assert.equal(result['status'], 'OK');
144                 const commits = result['commits'];
145                 const submittedCommits = systemVersionCommits['commits'];
146                 assert.equal(commits.length, submittedCommits.length);
147                 assert.equal(commits[0]['revision'], submittedCommits[5]['revision']);
148                 assert.equal(commits[1]['revision'], submittedCommits[4]['revision']);
149                 assert.equal(commits[2]['revision'], submittedCommits[3]['revision']);
150                 assert.equal(commits[3]['revision'], submittedCommits[2]['revision']);
151                 assert.equal(commits[4]['revision'], submittedCommits[1]['revision']);
152                 assert.equal(commits[5]['revision'], submittedCommits[0]['revision']);
153             });
154         });
155     });
156
157     describe('/api/commits/<repository>/oldest', () => {
158         it("should return RepositoryNotFound when there are no matching repository", () => {
159             return TestServer.remoteAPI().getJSON('/api/commits/WebKit/oldest').then((response) => {
160                 assert.equal(response['status'], 'RepositoryNotFound');
161             });
162         });
163
164         it("should return an empty results when there are no commits", () => {
165             return TestServer.database().insert('repositories', {'id': 1, 'name': 'WebKit'}).then(() => {
166                 return TestServer.remoteAPI().getJSON('/api/commits/WebKit/oldest');
167             }).then((response) => {
168                 assert.equal(response['status'], 'OK');
169                 assert.deepEqual(response['commits'], []);
170             });
171         });
172
173         it("should return the oldest commit", () => {
174             const remote = TestServer.remoteAPI();
175             return addSlaveForReport(subversionCommits).then(() => {
176                 return remote.postJSONWithStatus('/api/report-commits/', subversionCommits);
177             }).then(() => {
178                 return remote.getJSON('/api/commits/WebKit/oldest');
179             }).then(function (result) {
180                 assert.equal(result['status'], 'OK');
181                 assert.equal(result['commits'].length, 1);
182                 assertCommitIsSameAsOneSubmitted(result['commits'][0], subversionCommits['commits'][0]);
183             });
184         });
185
186         it("should return the oldest commit based on 'commit_order' when 'commit_time' is missing", () => {
187             const remote = TestServer.remoteAPI();
188             return addSlaveForReport(systemVersionCommits).then(() => {
189                 return remote.postJSONWithStatus('/api/report-commits/', systemVersionCommits);
190             }).then(() => {
191                 return remote.getJSON('/api/commits/OSX/oldest');
192             }).then(function (result) {
193                 assert.equal(result['status'], 'OK');
194                 assert.equal(result['commits'].length, 1);
195                 assert.equal(result['commits'][0]['revision'], systemVersionCommits['commits'][5]['revision']);
196             });
197         });
198     });
199
200     describe('/api/commits/<repository>/latest', () => {
201         it("should return RepositoryNotFound when there are no matching repository", () => {
202             return TestServer.remoteAPI().getJSON('/api/commits/WebKit/latest').then((response) => {
203                 assert.equal(response['status'], 'RepositoryNotFound');
204             });
205         });
206
207         it("should return an empty results when there are no commits", () => {
208             return TestServer.database().insert('repositories', {'id': 1, 'name': 'WebKit'}).then(() => {
209                 return TestServer.remoteAPI().getJSON('/api/commits/WebKit/latest');
210             }).then((response) => {
211                 assert.equal(response['status'], 'OK');
212                 assert.deepEqual(response['commits'], []);
213             });
214         });
215
216         it("should return the oldest commit", () => {
217             const remote = TestServer.remoteAPI();
218             return addSlaveForReport(subversionCommits).then(() => {
219                 return remote.postJSONWithStatus('/api/report-commits/', subversionCommits);
220             }).then(() => {
221                 return remote.getJSON('/api/commits/WebKit/latest');
222             }).then(function (result) {
223                 assert.equal(result['status'], 'OK');
224                 assert.equal(result['commits'].length, 1);
225                 assertCommitIsSameAsOneSubmitted(result['commits'][0], subversionCommits['commits'].slice().pop());
226             });
227         });
228
229         it("should return the latest commit based on 'commit_order' when 'commit_time' is missing", () => {
230             const remote = TestServer.remoteAPI();
231             return addSlaveForReport(systemVersionCommits).then(() => {
232                 return remote.postJSONWithStatus('/api/report-commits/', systemVersionCommits);
233             }).then(() => {
234                 return remote.getJSON('/api/commits/OSX/latest');
235             }).then(function (result) {
236                 assert.equal(result['status'], 'OK');
237                 assert.equal(result['commits'].length, 1);
238                 assert.equal(result['commits'][0]['revision'], systemVersionCommits['commits'][0]['revision']);
239             });
240         });
241     });
242
243     describe('/api/commits/<repository>/last-reported', () => {
244         it("should return RepositoryNotFound when there are no matching repository", () => {
245             return TestServer.remoteAPI().getJSON('/api/commits/WebKit/last-reported').then((response) => {
246                 assert.equal(response['status'], 'RepositoryNotFound');
247             });
248         });
249
250         it("should return an empty result when there are no reported commits", () => {
251             const db = TestServer.database();
252             return Promise.all([
253                 db.insert('repositories', {'id': 1, 'name': 'WebKit'}),
254                 db.insert('commits', {'repository': 1, 'revision': '210950', 'time': '2017-01-20T03:49:37.887Z'})
255             ]).then(() => {
256                 return TestServer.remoteAPI().getJSON('/api/commits/WebKit/last-reported');
257             }).then((response) => {
258                 assert.equal(response['status'], 'OK');
259                 assert.deepEqual(response['commits'], []);
260             });
261         });
262
263         it("should return an empty results when there are no reported commits", () => {
264             return TestServer.database().insert('repositories', {'id': 1, 'name': 'WebKit'}).then(() => {
265                 return TestServer.remoteAPI().getJSON('/api/commits/WebKit/last-reported');
266             }).then((response) => {
267                 assert.equal(response['status'], 'OK');
268                 assert.deepEqual(response['commits'], []);
269             });
270         });
271
272         it("should return the oldest reported commit", () => {
273             const db = TestServer.database();
274             const remote = TestServer.remoteAPI();
275             return Promise.all([
276                 addSlaveForReport(subversionCommits),
277                 db.insert('repositories', {'id': 1, 'name': 'WebKit'}),
278                 db.insert('commits', {'repository': 1, 'revision': notYetReportedCommit.revision, 'time': notYetReportedCommit.time}),
279             ]).then(() => {
280                 return remote.postJSONWithStatus('/api/report-commits/', subversionCommits);
281             }).then(() => {
282                 return remote.getJSON('/api/commits/WebKit/last-reported');
283             }).then(function (result) {
284                 assert.equal(result['status'], 'OK');
285                 assert.equal(result['commits'].length, 1);
286                 assertCommitIsSameAsOneSubmitted(result['commits'][0], subversionCommits['commits'].slice().pop());
287             });
288         });
289
290         it("should return the last reported commit based on 'commit_order' when 'commit_time' is missing", () => {
291             const remote = TestServer.remoteAPI();
292             return addSlaveForReport(systemVersionCommits).then(() => {
293                 return remote.postJSONWithStatus('/api/report-commits/', systemVersionCommits);
294             }).then(() => {
295                 return remote.getJSON('/api/commits/OSX/last-reported');
296             }).then(function (result) {
297                 assert.equal(result['status'], 'OK');
298                 assert.equal(result['commits'].length, 1);
299                 assert.equal(result['commits'][0]['revision'], systemVersionCommits['commits'][0]['revision']);
300             });
301         });
302     });
303
304     describe('/api/commits/<repository>/<commit>', () => {
305         it("should return RepositoryNotFound when there are no matching repository", () => {
306             return TestServer.remoteAPI().getJSON('/api/commits/WebKit/210949').then((response) => {
307                 assert.equal(response['status'], 'RepositoryNotFound');
308             });
309         });
310
311         it("should return UnknownCommit when one of the specified commit does not exist in the database", () => {
312             const db = TestServer.database();
313             return Promise.all([
314                 db.insert('repositories', {'id': 1, 'name': 'WebKit'}),
315                 db.insert('commits', {'repository': 1, 'revision': '210950', 'time': '2017-01-20T03:49:37.887Z'})
316             ]).then(() => {
317                 return TestServer.remoteAPI().getJSON('/api/commits/WebKit/210949');
318             }).then((response) => {
319                 assert.equal(response['status'], 'UnknownCommit');
320             });
321         });
322
323         it("should return the commit even if it had not been reported", () => {
324             const db = TestServer.database();
325             return Promise.all([
326                 db.insert('repositories', {'id': 1, 'name': 'WebKit'}),
327                 db.insert('commits', {'repository': 1, 'revision': '210950', 'time': '2017-01-20T03:49:37.887Z'})
328             ]).then(() => {
329                 return TestServer.remoteAPI().getJSON('/api/commits/WebKit/210950');
330             }).then((result) => {
331                 assert.equal(result['status'], 'OK');
332                 assert.equal(result['commits'].length, 1);
333                 assertCommitIsSameAsOneSubmitted(result['commits'][0], {
334                     previousCommit: null,
335                     revision: '210950',
336                     time: '2017-01-20T03:49:37.887Z',
337                     author: {name: null, account: null},
338                     message: null,
339                 });
340             });
341         });
342
343         it("should return the full result for a reported commit", () => {
344             const remote = TestServer.remoteAPI();
345             return addSlaveForReport(subversionCommits).then(() => {
346                 return remote.postJSONWithStatus('/api/report-commits/', subversionCommits);
347             }).then(() => {
348                 return remote.getJSON('/api/commits/WebKit/210949');
349             }).then((result) => {
350                 assert.equal(result['status'], 'OK');
351                 assert.deepEqual(result['commits'].length, 1);
352                 assertCommitIsSameAsOneSubmitted(result['commits'][0], subversionCommits['commits'][1]);
353             });
354         });
355
356     });
357
358     describe('/api/commits/<repository>/?from=<commit-1>&to=<commit-2>', () => {
359         it("should return RepositoryNotFound when there are no matching repository", () => {
360             return TestServer.remoteAPI().getJSON('/api/commits/WebKit/?from=210900&to=211000').then((response) => {
361                 assert.equal(response['status'], 'RepositoryNotFound');
362             });
363         });
364
365         it("should return UnknownCommit when one of the specified commit does not exist in the database", () => {
366             const db = TestServer.database();
367             return Promise.all([
368                 db.insert('repositories', {'id': 1, 'name': 'WebKit'}),
369                 db.insert('commits', {'repository': 1, 'revision': '210950', 'time': '2017-01-20T03:49:37.887Z'})
370             ]).then(() => {
371                 return TestServer.remoteAPI().getJSON('/api/commits/WebKit/?from=210900&to=211000');
372             }).then((response) => {
373                 assert.equal(response['status'], 'UnknownCommit');
374             });
375         });
376
377         it("should return an empty result when commits in the specified range have not reported", () => {
378             const db = TestServer.database();
379             return Promise.all([
380                 db.insert('repositories', {'id': 1, 'name': 'WebKit'}),
381                 db.insert('commits', {'repository': 1, 'revision': '210949', 'time': '2017-01-20T03:23:50.645Z'}),
382                 db.insert('commits', {'repository': 1, 'revision': '210950', 'time': '2017-01-20T03:49:37.887Z'}),
383             ]).then(() => {
384                 return TestServer.remoteAPI().getJSON('/api/commits/WebKit/?from=210949&to=210950');
385             }).then((response) => {
386                 assert.equal(response['status'], 'OK');
387                 assert.deepEqual(response['commits'], []);
388             });
389         });
390
391         it("should return reported commits in the specified range", () => {
392             const db = TestServer.database();
393             return Promise.all([
394                 db.insert('repositories', {'id': 1, 'name': 'WebKit'}),
395                 db.insert('commits', {'repository': 1, 'revision': '210949', 'time': '2017-01-20T03:23:50.645Z', 'reported': true}),
396                 db.insert('commits', {'repository': 1, 'revision': '210950', 'time': '2017-01-20T03:49:37.887Z', 'reported': true}),
397             ]).then(() => {
398                 return TestServer.remoteAPI().getJSON('/api/commits/WebKit/?from=210949&to=210950');
399             }).then((result) => {
400                 assert.equal(result['status'], 'OK');
401                 assert.deepEqual(result['commits'].length, 2);
402                 assertCommitIsSameAsOneSubmitted(result['commits'][0], {
403                     previousCommit: null,
404                     revision: '210949',
405                     time: '2017-01-20T03:23:50.645Z',
406                     author: {name: null, account: null},
407                     message: null,
408                 });
409                 assertCommitIsSameAsOneSubmitted(result['commits'][1], {
410                     previousCommit: null,
411                     revision: '210950',
412                     time: '2017-01-20T03:49:37.887Z',
413                     author: {name: null, account: null},
414                     message: null,
415                 });
416             });
417         });
418
419         it("should not include a revision not within the specified range", () => {
420             const remote = TestServer.remoteAPI();
421             return addSlaveForReport(subversionCommits).then(() => {
422                 return remote.postJSONWithStatus('/api/report-commits/', subversionCommits);
423             }).then(() => {
424                 return remote.getJSON('/api/commits/WebKit/?from=210948&to=210949');
425             }).then((result) => {
426                 assert.equal(result['status'], 'OK');
427                 assert.deepEqual(result['commits'].length, 2);
428                 assertCommitIsSameAsOneSubmitted(result['commits'][0], subversionCommits['commits'][0]);
429                 assertCommitIsSameAsOneSubmitted(result['commits'][1], subversionCommits['commits'][1]);
430             });
431         });
432
433     });
434
435 });