Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 1 | import {assert} from 'chai'; |
| 2 | import sinon from 'sinon'; |
| 3 | |
| 4 | import MonorailTSMon, {PAGE_TYPES} from './monorail-ts-mon.js'; |
| 5 | |
| 6 | describe('MonorailTSMon', () => { |
| 7 | let mts; |
| 8 | |
| 9 | beforeEach(() => { |
| 10 | window.CS_env = { |
| 11 | token: 'rutabaga-token', |
| 12 | tokenExpiresSec: 1234, |
| 13 | app_version: 'rutabaga-version', |
| 14 | }; |
| 15 | window.chops = {rpc: {PrpcClient: sinon.spy()}}; |
| 16 | MonorailTSMon.prototype.disableAfterNextFlush = sinon.spy(); |
| 17 | mts = new MonorailTSMon(); |
| 18 | }); |
| 19 | |
| 20 | afterEach(() => { |
| 21 | delete window.CS_env; |
| 22 | }); |
| 23 | |
| 24 | describe('constructor', () => { |
| 25 | it('initializes a prpcClient', () => { |
| 26 | assert.equal(mts.prpcClient.constructor.name, 'AutoRefreshPrpcClient'); |
| 27 | }); |
| 28 | |
| 29 | it('sets a client ID', () => { |
| 30 | assert.isNotNull(mts.clientId); |
| 31 | }); |
| 32 | |
| 33 | it('disables sending after next flush', () => { |
| 34 | sinon.assert.calledOnce(mts.disableAfterNextFlush); |
| 35 | }); |
| 36 | }); |
| 37 | |
| 38 | it('generateClientId', () => { |
| 39 | const clientID = MonorailTSMon.generateClientId(); |
| 40 | assert.isNotNumber(clientID); |
| 41 | const clientIDNum = parseInt(clientID, 32); |
| 42 | assert.isNumber(clientIDNum); |
| 43 | assert.isAtLeast(clientIDNum, 0); |
| 44 | assert.isAtMost(clientIDNum, Math.pow(2, 32)); |
| 45 | }); |
| 46 | |
| 47 | describe('recordUserTiming', () => { |
| 48 | it('records a timing metric only if matches', () => { |
| 49 | const metric = {add: sinon.spy()}; |
| 50 | mts._userTimingMetrics = [{ |
| 51 | category: 'rutabaga', |
| 52 | eventName: 'rutabaga-name', |
| 53 | eventLabel: 'rutabaga-label', |
| 54 | metric: metric, |
| 55 | }]; |
| 56 | |
| 57 | mts.recordUserTiming('kohlrabi', 'rutabaga-name', 'rutabaga-label', 1); |
| 58 | sinon.assert.notCalled(metric.add); |
| 59 | metric.add.resetHistory(); |
| 60 | |
| 61 | mts.recordUserTiming('rutabaga', 'is-a-tuber', 'rutabaga-label', 1); |
| 62 | sinon.assert.notCalled(metric.add); |
| 63 | metric.add.resetHistory(); |
| 64 | |
| 65 | mts.recordUserTiming('rutabaga', 'rutabaga-name', 'went bad', 1); |
| 66 | sinon.assert.notCalled(metric.add); |
| 67 | metric.add.resetHistory(); |
| 68 | |
| 69 | mts.recordUserTiming('rutabaga', 'rutabaga-name', 'rutabaga-label', 1); |
| 70 | sinon.assert.calledOnce(metric.add); |
| 71 | assert.equal(metric.add.args[0][0], 1); |
| 72 | const argsKeys = Array.from(metric.add.args[0][1].keys()); |
| 73 | assert.deepEqual(argsKeys, ['client_id', 'host_name', 'document_visible']); |
| 74 | }); |
| 75 | }); |
| 76 | |
| 77 | describe('recordPageLoadTiming', () => { |
| 78 | beforeEach(() => { |
| 79 | mts.pageLoadMetric = {add: sinon.spy()}; |
| 80 | sinon.stub(MonorailTSMon, 'isPageVisible').callsFake(() => (true)); |
| 81 | }); |
| 82 | |
| 83 | afterEach(() => { |
| 84 | MonorailTSMon.isPageVisible.restore(); |
| 85 | }); |
| 86 | |
| 87 | it('records page load on issue entry page', () => { |
| 88 | mts.recordIssueEntryTiming(); |
| 89 | sinon.assert.calledOnce(mts.pageLoadMetric.add); |
| 90 | assert.isNumber(mts.pageLoadMetric.add.getCall(0).args[0]); |
| 91 | assert.isString(mts.pageLoadMetric.add.getCall(0).args[1].get( |
| 92 | 'client_id')); |
| 93 | assert.equal(mts.pageLoadMetric.add.getCall(0).args[1].get( |
| 94 | 'host_name'), 'rutabaga-version'); |
| 95 | assert.equal(mts.pageLoadMetric.add.getCall(0).args[1].get( |
| 96 | 'template_name'), 'issue_entry'); |
| 97 | assert.equal(mts.pageLoadMetric.add.getCall(0).args[1].get( |
| 98 | 'document_visible'), true); |
| 99 | }); |
| 100 | |
| 101 | it('does not record page load timing on other pages', () => { |
| 102 | mts.recordPageLoadTiming(); |
| 103 | sinon.assert.notCalled(mts.pageLoadMetric.add); |
| 104 | }); |
| 105 | |
| 106 | it('does not record page load timing if over max threshold', () => { |
| 107 | window.performance = { |
| 108 | timing: { |
| 109 | navigationStart: 1000, |
| 110 | domContentLoadedEventEnd: 2001, |
| 111 | }, |
| 112 | }; |
| 113 | mts.recordIssueEntryTiming(1000); |
| 114 | sinon.assert.notCalled(mts.pageLoadMetric.add); |
| 115 | }); |
| 116 | |
| 117 | it('records page load on issue entry page if under threshold', () => { |
| 118 | MonorailTSMon.isPageVisible.restore(); |
| 119 | sinon.stub(MonorailTSMon, 'isPageVisible').callsFake(() => (false)); |
| 120 | window.performance = { |
| 121 | timing: { |
| 122 | navigationStart: 1000, |
| 123 | domContentLoadedEventEnd: 1999, |
| 124 | }, |
| 125 | }; |
| 126 | mts.recordIssueEntryTiming(1000); |
| 127 | sinon.assert.calledOnce(mts.pageLoadMetric.add); |
| 128 | assert.isNumber(mts.pageLoadMetric.add.getCall(0).args[0]); |
| 129 | assert.equal(mts.pageLoadMetric.add.getCall(0).args[0], 999); |
| 130 | assert.isString(mts.pageLoadMetric.add.getCall(0).args[1].get( |
| 131 | 'client_id')); |
| 132 | assert.equal(mts.pageLoadMetric.add.getCall(0).args[1].get( |
| 133 | 'host_name'), 'rutabaga-version'); |
| 134 | assert.equal(mts.pageLoadMetric.add.getCall(0).args[1].get( |
| 135 | 'template_name'), 'issue_entry'); |
| 136 | assert.equal(mts.pageLoadMetric.add.getCall(0).args[1].get( |
| 137 | 'document_visible'), false); |
| 138 | }); |
| 139 | }); |
| 140 | |
| 141 | describe('getGlobalClient', () => { |
| 142 | it('only creates one global client', () => { |
| 143 | delete window.__tsMonClient; |
| 144 | const client1 = MonorailTSMon.getGlobalClient(); |
| 145 | assert.equal(client1, window.__tsMonClient); |
| 146 | |
| 147 | const client2 = MonorailTSMon.getGlobalClient(); |
| 148 | assert.equal(client2, window.__tsMonClient); |
| 149 | assert.equal(client2, client1); |
| 150 | }); |
| 151 | }); |
| 152 | }); |