Rename 'commit_parent' in 'commits' table to 'commit_previous_commit'.
[WebKit-https.git] / Websites / perf.webkit.org / unit-tests / analysis-task-tests.js
1 'use strict';
2
3 var assert = require('assert');
4
5 require('../tools/js/v3-models.js');
6 let MockModels = require('./resources/mock-v3-models.js').MockModels;
7 let MockRemoteAPI = require('./resources/mock-remote-api.js').MockRemoteAPI;
8
9 function sampleAnalysisTask()
10 {
11     return {
12         'analysisTasks': [
13             {
14                 'author': null,
15                 'bugs': [],
16                 'buildRequestCount': '14',
17                 'finishedBuildRequestCount': '6',
18                 'category': 'identified',
19                 'causes': [
20                     '105975'
21                 ],
22                 'createdAt': 1454594330000,
23                 'endRun': '37253448',
24                 'endRunTime': 1454515020303,
25                 'fixes': [],
26                 'id': '1082',
27                 'metric': '2884',
28                 'name': 'Potential 1.2% regression between 2016-02-02 20:20 and 02-03 15:57',
29                 'needed': null,
30                 'platform': '65',
31                 'result': 'regression',
32                 'segmentationStrategy': '1',
33                 'startRun': '37117949',
34                 'startRunTime': 1454444458791,
35                 'testRangeStragegy': '2'
36             }
37         ],
38         'bugs': [],
39         'commits': [
40             {
41                 'authorEmail': 'commit-queue@webkit.org',
42                 'authorName': 'Commit Queue',
43                 'id': '105975',
44                 'message': 'Commit message',
45                 'order': null,
46                 'previousCommit': null,
47                 'repository': '11',
48                 'revision': '196051',
49                 'time': 1454481246108
50             }
51         ],
52         'status': 'OK'
53     };
54 }
55
56 function measurementCluster()
57 {
58     return {
59         'clusterSize': 5184000000,
60         'clusterStart': 946684800000,
61         'configurations': {
62             'current': [
63                 [
64                     37188161,
65                     124.15015662116,
66                     25,
67                     3103.7539155291,
68                     385398.06003414,
69                     false,
70                     [
71                         [
72                             105978,
73                             10,
74                             '791451',
75                             1454481204649
76                         ],
77                         [
78                             105975,
79                             11,
80                             '196051',
81                             1454481246108
82                         ],
83                         [
84                             105502,
85                             9,
86                             '10.11 15D21',
87                             0
88                         ]
89                     ],
90                     1454481246108,
91                     227020,
92                     1454492139496,
93                     '3151',
94                     179
95                 ],
96             ],
97         },
98         'endTime': 1454716800000,
99         'formatMap': [
100             'id',
101             'mean',
102             'iterationCount',
103             'sum',
104             'squareSum',
105             'markedOutlier',
106             'revisions',
107             'commitTime',
108             'build',
109             'buildTime',
110             'buildNumber',
111             'builder'
112         ],
113         'lastModified': 1455236216153,
114         'startTime': 1449532800000,
115         'status': 'OK'
116     };
117 }
118
119 describe('AnalysisTask', function () {
120     MockModels.inject();
121     let requests = MockRemoteAPI.inject();
122
123     describe('fetchAll', function () {
124         it('should request all analysis tasks', function () {
125             var callCount = 0;
126             AnalysisTask.fetchAll().then(function () { callCount++; });
127             assert.equal(callCount, 0);
128             assert.equal(requests.length, 1);
129             assert.equal(requests[0].url, '../api/analysis-tasks');
130         });
131
132         it('should not request all analysis tasks multiple times', function () {
133             var callCount = 0;
134             AnalysisTask.fetchAll().then(function () { callCount++; });
135             assert.equal(callCount, 0);
136             assert.equal(requests.length, 1);
137             assert.equal(requests[0].url, '../api/analysis-tasks');
138
139             AnalysisTask.fetchAll().then(function () { callCount++; });
140             assert.equal(callCount, 0);
141             assert.equal(requests.length, 1);
142         });
143
144         it('should resolve the promise when the request is fullfilled', function (done) {
145             var callCount = 0;
146             var promise = AnalysisTask.fetchAll().then(function () { callCount++; });
147             assert.equal(callCount, 0);
148             assert.equal(requests.length, 1);
149             assert.equal(requests[0].url, '../api/analysis-tasks');
150
151             requests[0].resolve(sampleAnalysisTask());
152
153             var anotherCallCount = 0;
154             promise.then(function () {
155                 assert.equal(callCount, 1);
156                 AnalysisTask.fetchAll().then(function () { anotherCallCount++; });
157             }).then(function () {
158                 assert.equal(callCount, 1);
159                 assert.equal(anotherCallCount, 1);
160                 assert.equal(requests.length, 1);
161                 done();
162             }).catch(function (error) { done(error); });
163         });
164
165         it('should create AnalysisTask objects', function (done) {
166             var promise = AnalysisTask.fetchAll();
167             requests[0].resolve(sampleAnalysisTask());
168
169             promise.then(function () {
170                 assert.equal(AnalysisTask.all().length, 1);
171                 var task = AnalysisTask.all()[0];
172                 assert.equal(task.id(), 1082);
173                 assert.equal(task.metric(), MockModels.someMetric);
174                 assert.equal(task.platform(), MockModels.somePlatform);
175                 assert.ok(task.hasResults());
176                 assert.ok(task.hasPendingRequests());
177                 assert.equal(task.requestLabel(), '6 of 14');
178                 assert.equal(task.category(), 'investigated');
179                 assert.equal(task.changeType(), 'regression');
180                 assert.equal(task.startMeasurementId(), 37117949);
181                 assert.equal(task.startTime(), 1454444458791);
182                 assert.equal(task.endMeasurementId(), 37253448);
183                 assert.equal(task.endTime(), 1454515020303);
184                 done();
185             }).catch(function (error) { done(error); });
186         });
187
188         it('should create CommitLog objects for `causes`', function (done) {
189             var promise = AnalysisTask.fetchAll();
190             requests[0].resolve(sampleAnalysisTask());
191
192             promise.then(function () {
193                 assert.equal(AnalysisTask.all().length, 1);
194                 var task = AnalysisTask.all()[0];
195
196                 assert.equal(task.causes().length, 1);
197                 var commit = task.causes()[0];
198
199                 assert.equal(commit.revision(), '196051');
200                 assert.equal(commit.repository(), MockModels.webkit);
201                 assert.equal(+commit.time(), 1454481246108);
202                 done();
203             }).catch(function (error) { done(error); });
204         });
205
206         it('should find CommitLog objects for `causes` when MeasurementAdaptor created matching objects', function (done) {
207             var adoptor = new MeasurementAdaptor(measurementCluster().formatMap);
208             var adoptedMeasurement = adoptor.applyTo(measurementCluster().configurations.current[0]);
209             assert.equal(adoptedMeasurement.id, 37188161);
210             assert.equal(adoptedMeasurement.rootSet().commitForRepository(MockModels.webkit).revision(), '196051');
211
212             var promise = AnalysisTask.fetchAll();
213             requests[0].resolve(sampleAnalysisTask());
214
215             promise.then(function () {
216                 assert.equal(AnalysisTask.all().length, 1);
217                 var task = AnalysisTask.all()[0];
218
219                 assert.equal(task.causes().length, 1);
220                 var commit = task.causes()[0];
221                 assert.equal(commit.revision(), '196051');
222                 assert.equal(commit.repository(), MockModels.webkit);
223                 assert.equal(+commit.time(), 1454481246108);
224                 done();
225             }).catch(function (error) { done(error); });
226         });
227     });
228 });