Make unit tests return a promise instead of manually calling done
[WebKit-https.git] / Websites / perf.webkit.org / unit-tests / analysis-task-tests.js
1 'use strict';
2
3 const 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', () => {
120     MockModels.inject();
121     let requests = MockRemoteAPI.inject();
122
123     describe('fetchAll', () => {
124         it('should request all analysis tasks', () => {
125             let callCount = 0;
126             AnalysisTask.fetchAll().then(() => { 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', () => {
133             let callCount = 0;
134             AnalysisTask.fetchAll().then(() => { 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(() => { 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', () => {
145             let callCount = 0;
146             const promise = AnalysisTask.fetchAll().then(() => { 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             let anotherCallCount = 0;
154             return promise.then(() => {
155                 assert.equal(callCount, 1);
156                 AnalysisTask.fetchAll().then(() => { anotherCallCount++; });
157             }).then(() => {
158                 assert.equal(callCount, 1);
159                 assert.equal(anotherCallCount, 1);
160                 assert.equal(requests.length, 1);
161             });
162         });
163
164         it('should create AnalysisTask objects', () => {
165             const promise = AnalysisTask.fetchAll();
166             requests[0].resolve(sampleAnalysisTask());
167
168             return promise.then(() => {
169                 assert.equal(AnalysisTask.all().length, 1);
170                 var task = AnalysisTask.all()[0];
171                 assert.equal(task.id(), 1082);
172                 assert.equal(task.metric(), MockModels.someMetric);
173                 assert.equal(task.platform(), MockModels.somePlatform);
174                 assert.ok(task.hasResults());
175                 assert.ok(task.hasPendingRequests());
176                 assert.equal(task.requestLabel(), '6 of 14');
177                 assert.equal(task.category(), 'investigated');
178                 assert.equal(task.changeType(), 'regression');
179                 assert.equal(task.startMeasurementId(), 37117949);
180                 assert.equal(task.startTime(), 1454444458791);
181                 assert.equal(task.endMeasurementId(), 37253448);
182                 assert.equal(task.endTime(), 1454515020303);
183             });
184         });
185
186         it('should create CommitLog objects for `causes`', () => {
187             const promise = AnalysisTask.fetchAll();
188             requests[0].resolve(sampleAnalysisTask());
189
190             return promise.then(() => {
191                 assert.equal(AnalysisTask.all().length, 1);
192                 var task = AnalysisTask.all()[0];
193
194                 assert.equal(task.causes().length, 1);
195                 var commit = task.causes()[0];
196
197                 assert.equal(commit.revision(), '196051');
198                 assert.equal(commit.repository(), MockModels.webkit);
199                 assert.equal(+commit.time(), 1454481246108);
200             });
201         });
202
203         it('should find CommitLog objects for `causes` when MeasurementAdaptor created matching objects', () => {
204             const adaptor = new MeasurementAdaptor(measurementCluster().formatMap);
205             const adaptedMeasurement = adaptor.applyTo(measurementCluster().configurations.current[0]);
206             assert.equal(adaptedMeasurement.id, 37188161);
207             assert.equal(adaptedMeasurement.commitSet().commitForRepository(MockModels.webkit).revision(), '196051');
208
209             const promise = AnalysisTask.fetchAll();
210             requests[0].resolve(sampleAnalysisTask());
211
212             return promise.then(() => {
213                 assert.equal(AnalysisTask.all().length, 1);
214                 var task = AnalysisTask.all()[0];
215
216                 assert.equal(task.causes().length, 1);
217                 var commit = task.causes()[0];
218                 assert.equal(commit.revision(), '196051');
219                 assert.equal(commit.repository(), MockModels.webkit);
220                 assert.equal(+commit.time(), 1454481246108);
221             });
222         });
223     });
224 });