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