d696f68ff53aa422574f1ac264b5d955df6731c0
[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                             null,
76                             1454481204649
77                         ],
78                         [
79                             105975,
80                             11,
81                             '196051',
82                             null,
83                             1454481246108
84                         ],
85                         [
86                             105502,
87                             9,
88                             '10.11 15D21',
89                             1504021,
90                             0
91                         ]
92                     ],
93                     1454481246108,
94                     227020,
95                     1454492139496,
96                     '3151',
97                     179
98                 ],
99             ],
100         },
101         'endTime': 1454716800000,
102         'formatMap': [
103             'id',
104             'mean',
105             'iterationCount',
106             'sum',
107             'squareSum',
108             'markedOutlier',
109             'revisions',
110             'commitTime',
111             'build',
112             'buildTime',
113             'buildNumber',
114             'builder'
115         ],
116         'lastModified': 1455236216153,
117         'startTime': 1449532800000,
118         'status': 'OK'
119     };
120 }
121
122 describe('AnalysisTask', () => {
123     MockModels.inject();
124     let requests = MockRemoteAPI.inject();
125
126     describe('fetchAll', () => {
127         it('should request all analysis tasks', () => {
128             let callCount = 0;
129             AnalysisTask.fetchAll().then(() => { callCount++; });
130             assert.equal(callCount, 0);
131             assert.equal(requests.length, 1);
132             assert.equal(requests[0].url, '/api/analysis-tasks');
133         });
134
135         it('should not request all analysis tasks multiple times', () => {
136             let callCount = 0;
137             AnalysisTask.fetchAll().then(() => { callCount++; });
138             assert.equal(callCount, 0);
139             assert.equal(requests.length, 1);
140             assert.equal(requests[0].url, '/api/analysis-tasks');
141
142             AnalysisTask.fetchAll().then(() => { callCount++; });
143             assert.equal(callCount, 0);
144             assert.equal(requests.length, 1);
145         });
146
147         it('should resolve the promise when the request is fullfilled', () => {
148             let callCount = 0;
149             const promise = AnalysisTask.fetchAll().then(() => { callCount++; });
150             assert.equal(callCount, 0);
151             assert.equal(requests.length, 1);
152             assert.equal(requests[0].url, '/api/analysis-tasks');
153
154             requests[0].resolve(sampleAnalysisTask());
155
156             let anotherCallCount = 0;
157             return promise.then(() => {
158                 assert.equal(callCount, 1);
159                 AnalysisTask.fetchAll().then(() => { anotherCallCount++; });
160             }).then(() => {
161                 assert.equal(callCount, 1);
162                 assert.equal(anotherCallCount, 1);
163                 assert.equal(requests.length, 1);
164             });
165         });
166
167         it('should create AnalysisTask objects', () => {
168             const promise = AnalysisTask.fetchAll();
169             requests[0].resolve(sampleAnalysisTask());
170
171             return promise.then(() => {
172                 assert.equal(AnalysisTask.all().length, 1);
173                 var task = AnalysisTask.all()[0];
174                 assert.equal(task.id(), 1082);
175                 assert.equal(task.metric(), MockModels.someMetric);
176                 assert.equal(task.platform(), MockModels.somePlatform);
177                 assert.ok(task.hasResults());
178                 assert.ok(task.hasPendingRequests());
179                 assert.equal(task.requestLabel(), '6 of 14');
180                 assert.equal(task.category(), 'investigated');
181                 assert.equal(task.changeType(), 'regression');
182                 assert.equal(task.startMeasurementId(), 37117949);
183                 assert.equal(task.startTime(), 1454444458791);
184                 assert.equal(task.endMeasurementId(), 37253448);
185                 assert.equal(task.endTime(), 1454515020303);
186             });
187         });
188
189         it('should create CommitLog objects for `causes`', () => {
190             const promise = AnalysisTask.fetchAll();
191             requests[0].resolve(sampleAnalysisTask());
192
193             return promise.then(() => {
194                 assert.equal(AnalysisTask.all().length, 1);
195                 var task = AnalysisTask.all()[0];
196
197                 assert.equal(task.causes().length, 1);
198                 var commit = task.causes()[0];
199
200                 assert.equal(commit.revision(), '196051');
201                 assert.equal(commit.repository(), MockModels.webkit);
202                 assert.equal(+commit.time(), 1454481246108);
203             });
204         });
205
206         it('should find CommitLog objects for `causes` when MeasurementAdaptor created matching objects', () => {
207             const adaptor = new MeasurementAdaptor(measurementCluster().formatMap);
208             const adaptedMeasurement = adaptor.applyTo(measurementCluster().configurations.current[0]);
209             assert.equal(adaptedMeasurement.id, 37188161);
210             assert.equal(adaptedMeasurement.commitSet().commitForRepository(MockModels.webkit).revision(), '196051');
211
212             const promise = AnalysisTask.fetchAll();
213             requests[0].resolve(sampleAnalysisTask());
214
215             return promise.then(() => {
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             });
225         });
226     });
227 });