|
| 1 | +import { expect } from 'chai'; |
| 2 | +import * as alerts from '../../../../src/v2/providers/alerts'; |
| 3 | +import * as performance from '../../../../src/v2/providers/alerts/performance'; |
| 4 | +import { FULL_ENDPOINT, FULL_OPTIONS } from '../fixtures'; |
| 5 | + |
| 6 | +const APPID = '123456789'; |
| 7 | +const myHandler = () => 42; |
| 8 | + |
| 9 | +const APP_EVENT_FILTER = { |
| 10 | + appid: APPID, |
| 11 | +}; |
| 12 | + |
| 13 | +describe('performance', () => { |
| 14 | + describe('onThresholdAlertPublished', () => { |
| 15 | + it('should create a function with alertType & appId', () => { |
| 16 | + const func = performance.onThresholdAlertPublished(APPID, myHandler); |
| 17 | + |
| 18 | + expect(func.__endpoint).to.deep.equal({ |
| 19 | + platform: 'gcfv2', |
| 20 | + labels: {}, |
| 21 | + eventTrigger: { |
| 22 | + eventType: alerts.eventType, |
| 23 | + eventFilters: { |
| 24 | + ...APP_EVENT_FILTER, |
| 25 | + alerttype: performance.thresholdAlert, |
| 26 | + }, |
| 27 | + retry: false, |
| 28 | + }, |
| 29 | + }); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should create a function with opts', () => { |
| 33 | + const func = performance.onThresholdAlertPublished( |
| 34 | + { ...FULL_OPTIONS }, |
| 35 | + myHandler |
| 36 | + ); |
| 37 | + |
| 38 | + expect(func.__endpoint).to.deep.equal({ |
| 39 | + ...FULL_ENDPOINT, |
| 40 | + eventTrigger: { |
| 41 | + eventType: alerts.eventType, |
| 42 | + eventFilters: { |
| 43 | + alerttype: performance.thresholdAlert, |
| 44 | + }, |
| 45 | + retry: false, |
| 46 | + }, |
| 47 | + }); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should create a function with appid in opts', () => { |
| 51 | + const func = performance.onThresholdAlertPublished( |
| 52 | + { ...FULL_OPTIONS, appId: APPID }, |
| 53 | + myHandler |
| 54 | + ); |
| 55 | + |
| 56 | + expect(func.__endpoint).to.deep.equal({ |
| 57 | + ...FULL_ENDPOINT, |
| 58 | + eventTrigger: { |
| 59 | + eventType: alerts.eventType, |
| 60 | + eventFilters: { |
| 61 | + ...APP_EVENT_FILTER, |
| 62 | + alerttype: performance.thresholdAlert, |
| 63 | + }, |
| 64 | + retry: false, |
| 65 | + }, |
| 66 | + }); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should create a function without opts or appId', () => { |
| 70 | + const func = performance.onThresholdAlertPublished(myHandler); |
| 71 | + |
| 72 | + expect(func.__endpoint).to.deep.equal({ |
| 73 | + platform: 'gcfv2', |
| 74 | + labels: {}, |
| 75 | + eventTrigger: { |
| 76 | + eventType: alerts.eventType, |
| 77 | + eventFilters: { |
| 78 | + alerttype: performance.thresholdAlert, |
| 79 | + }, |
| 80 | + retry: false, |
| 81 | + }, |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should create a function with a run method', () => { |
| 86 | + const func = performance.onThresholdAlertPublished( |
| 87 | + APPID, |
| 88 | + (event) => event |
| 89 | + ); |
| 90 | + |
| 91 | + const res = func.run('input' as any); |
| 92 | + |
| 93 | + expect(res).to.equal('input'); |
| 94 | + }); |
| 95 | + }); |
| 96 | + |
| 97 | + describe('getOptsAndApp', () => { |
| 98 | + it('should parse a string', () => { |
| 99 | + const [opts, appId] = performance.getOptsAndApp(APPID); |
| 100 | + |
| 101 | + expect(opts).to.deep.equal({}); |
| 102 | + expect(appId).to.equal(APPID); |
| 103 | + }); |
| 104 | + |
| 105 | + it('should parse an options object without appId', () => { |
| 106 | + const myOpts: performance.PerformanceOptions = { |
| 107 | + region: 'us-west1', |
| 108 | + }; |
| 109 | + |
| 110 | + const [opts, appId] = performance.getOptsAndApp(myOpts); |
| 111 | + |
| 112 | + expect(opts).to.deep.equal({ region: 'us-west1' }); |
| 113 | + expect(appId).to.be.undefined; |
| 114 | + }); |
| 115 | + |
| 116 | + it('should parse an options object with appId', () => { |
| 117 | + const myOpts: performance.PerformanceOptions = { |
| 118 | + appId: APPID, |
| 119 | + region: 'us-west1', |
| 120 | + }; |
| 121 | + |
| 122 | + const [opts, appId] = performance.getOptsAndApp(myOpts); |
| 123 | + |
| 124 | + expect(opts).to.deep.equal({ region: 'us-west1' }); |
| 125 | + expect(appId).to.equal(APPID); |
| 126 | + }); |
| 127 | + }); |
| 128 | + |
| 129 | + describe('convertPayload', () => { |
| 130 | + it('should return the same payload', () => { |
| 131 | + const payload = { |
| 132 | + a: 'b', |
| 133 | + conditionPercentile: 23, |
| 134 | + appVersion: '3', |
| 135 | + }; |
| 136 | + |
| 137 | + const convertedPayload = performance.convertPayload(payload as any); |
| 138 | + |
| 139 | + expect(convertedPayload).to.deep.eq(payload); |
| 140 | + }); |
| 141 | + |
| 142 | + it('should return the same payload if the fields are undefined', () => { |
| 143 | + const payload = { |
| 144 | + a: 'b', |
| 145 | + }; |
| 146 | + |
| 147 | + const convertedPayload = performance.convertPayload(payload as any); |
| 148 | + |
| 149 | + expect(convertedPayload).to.deep.eq({ |
| 150 | + a: 'b', |
| 151 | + }); |
| 152 | + }); |
| 153 | + |
| 154 | + it('should remove fields', () => { |
| 155 | + const payload = { |
| 156 | + a: 'b', |
| 157 | + conditionPercentile: 0, |
| 158 | + appVersion: '', |
| 159 | + }; |
| 160 | + |
| 161 | + const convertedPayload = performance.convertPayload(payload as any); |
| 162 | + |
| 163 | + expect(convertedPayload).to.deep.eq({ |
| 164 | + a: 'b', |
| 165 | + }); |
| 166 | + }); |
| 167 | + }); |
| 168 | +}); |
0 commit comments