Add a model for parsing buildbot JSON with unit tests
[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 require('./resources/mock-v3-models.js');
7 require('./resources/mock-remote-api.js');
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                 'parent': 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     describe('fetchAll', function () {
121         it('should request all analysis tasks', function () {
122             var callCount = 0;
123             AnalysisTask.fetchAll().then(function () { callCount++; });
124             assert.equal(callCount, 0);
125             assert.equal(requests.length, 1);
126             assert.equal(requests[0].url, '../api/analysis-tasks');
127         });
128
129         it('should not request all analysis tasks multiple times', function () {
130             var callCount = 0;
131             AnalysisTask.fetchAll().then(function () { callCount++; });
132             assert.equal(callCount, 0);
133             assert.equal(requests.length, 1);
134             assert.equal(requests[0].url, '../api/analysis-tasks');
135
136             AnalysisTask.fetchAll().then(function () { callCount++; });
137             assert.equal(callCount, 0);
138             assert.equal(requests.length, 1);
139         });
140
141         it('should resolve the promise when the request is fullfilled', function (done) {
142             var callCount = 0;
143             var promise = AnalysisTask.fetchAll().then(function () { callCount++; });
144             assert.equal(callCount, 0);
145             assert.equal(requests.length, 1);
146             assert.equal(requests[0].url, '../api/analysis-tasks');
147
148             requests[0].resolve(sampleAnalysisTask());
149
150             var anotherCallCount = 0;
151             return promise.then(function () {
152                 assert.equal(callCount, 1);
153                 AnalysisTask.fetchAll().then(function () { anotherCallCount++; });
154             }).then(function () {
155                 assert.equal(callCount, 1);
156                 assert.equal(anotherCallCount, 1);
157                 assert.equal(requests.length, 1);
158                 done();
159             }).catch(function (error) { done(error); });
160         });
161
162         it('should create AnalysisTask objects', function (done) {
163             var promise = AnalysisTask.fetchAll();
164             requests[0].resolve(sampleAnalysisTask());
165
166             return promise.then(function () {
167                 assert.equal(AnalysisTask.all().length, 1);
168                 var task = AnalysisTask.all()[0];
169                 assert.equal(task.id(), 1082);
170                 assert.equal(task.metric(), someMetric);
171                 assert.equal(task.platform(), somePlatform);
172                 assert.ok(task.hasResults());
173                 assert.ok(task.hasPendingRequests());
174                 assert.equal(task.requestLabel(), '6 of 14');
175                 assert.equal(task.category(), 'identified');
176                 assert.equal(task.changeType(), 'regression');
177                 assert.equal(task.startMeasurementId(), 37117949);
178                 assert.equal(task.startTime(), 1454444458791);
179                 assert.equal(task.endMeasurementId(), 37253448);
180                 assert.equal(task.endTime(), 1454515020303);
181                 done();
182             }).catch(function (error) { done(error); });
183         });
184
185         it('should create CommitLog objects for `causes`', function (done) {
186             var promise = AnalysisTask.fetchAll();
187             requests[0].resolve(sampleAnalysisTask());
188
189             return promise.then(function () {
190                 assert.equal(AnalysisTask.all().length, 1);
191                 var task = AnalysisTask.all()[0];
192
193                 assert.equal(task.causes().length, 1);
194                 var commit = task.causes()[0];
195
196                 assert.equal(commit.revision(), '196051');
197                 assert.equal(commit.repository(), webkit);
198                 assert.equal(+commit.time(), 1454481246108);
199                 done();
200             }).catch(function (error) { done(error); });
201         });
202
203         it('should find CommitLog objects for `causes` when MeasurementAdaptor created matching objects', function (done) {
204             var adoptor = new MeasurementAdaptor(measurementCluster().formatMap);
205             var adoptedMeasurement = adoptor.applyTo(measurementCluster().configurations.current[0]);
206             assert.equal(adoptedMeasurement.id, 37188161);
207             assert.equal(adoptedMeasurement.rootSet().commitForRepository(webkit).revision(), '196051');
208
209             var promise = AnalysisTask.fetchAll();
210             requests[0].resolve(sampleAnalysisTask());
211
212             return promise.then(function () {
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(), webkit);
220                 assert.equal(+commit.time(), 1454481246108);
221                 done();
222             }).catch(function (error) { done(error); });
223         });
224     });
225 });