|
| 1 | +// The MIT License (MIT) |
| 2 | +// |
| 3 | +// Copyright (c) 2022 Firebase |
| 4 | +// |
| 5 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +// of this software and associated documentation files (the "Software"), to deal |
| 7 | +// in the Software without restriction, including without limitation the rights |
| 8 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +// copies of the Software, and to permit persons to whom the Software is |
| 10 | +// furnished to do so, subject to the following conditions: |
| 11 | +// |
| 12 | +// The above copyright notice and this permission notice shall be included in all |
| 13 | +// copies or substantial portions of the Software. |
| 14 | +// |
| 15 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | +// SOFTWARE. |
| 22 | + |
| 23 | +import { expect } from 'chai'; |
| 24 | +import * as schedule from '../../../src/v2/providers/scheduler'; |
| 25 | + |
| 26 | +describe('schedule', () => { |
| 27 | + describe('getOpts', () => { |
| 28 | + it('should handle a schedule', () => { |
| 29 | + expect(schedule.getOpts('* * * * *')).to.deep.eq({ |
| 30 | + schedule: '* * * * *', |
| 31 | + opts: {}, |
| 32 | + }); |
| 33 | + }); |
| 34 | + |
| 35 | + it('should handle full options', () => { |
| 36 | + const options: schedule.ScheduleOptions = { |
| 37 | + schedule: '* * * * *', |
| 38 | + timeZone: 'utc', |
| 39 | + retryCount: 3, |
| 40 | + maxRetrySeconds: 1, |
| 41 | + minBackoffSeconds: 2, |
| 42 | + maxBackoffSeconds: 3, |
| 43 | + maxDoublings: 4, |
| 44 | + memory: '128MiB', |
| 45 | + region: 'us-central1', |
| 46 | + }; |
| 47 | + |
| 48 | + expect(schedule.getOpts(options)).to.deep.eq({ |
| 49 | + schedule: '* * * * *', |
| 50 | + timeZone: 'utc', |
| 51 | + retryCount: 3, |
| 52 | + maxRetrySeconds: 1, |
| 53 | + minBackoffSeconds: 2, |
| 54 | + maxBackoffSeconds: 3, |
| 55 | + maxDoublings: 4, |
| 56 | + opts: { |
| 57 | + ...options, |
| 58 | + memory: '128MiB', |
| 59 | + region: 'us-central1', |
| 60 | + }, |
| 61 | + }); |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + describe('onSchedule', () => { |
| 66 | + it('should create a schedule function given a schedule', () => { |
| 67 | + const schfn = schedule.onSchedule('* * * * *', (event) => console.log(1)); |
| 68 | + |
| 69 | + expect(schfn.__endpoint).to.deep.eq({ |
| 70 | + platform: 'gcfv2', |
| 71 | + labels: {}, |
| 72 | + scheduleTrigger: { |
| 73 | + schedule: '* * * * *', |
| 74 | + retryConfig: {}, |
| 75 | + }, |
| 76 | + }); |
| 77 | + expect(schfn.__requiredAPIs).to.deep.eq([ |
| 78 | + { |
| 79 | + api: 'cloudscheduler.googleapis.com', |
| 80 | + reason: 'Needed for scheduled functions.', |
| 81 | + }, |
| 82 | + ]); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should create a schedule function given options', () => { |
| 86 | + const schfn = schedule.onSchedule( |
| 87 | + { |
| 88 | + schedule: '* * * * *', |
| 89 | + timeZone: 'utc', |
| 90 | + retryCount: 3, |
| 91 | + maxRetrySeconds: 10, |
| 92 | + minBackoffSeconds: 11, |
| 93 | + maxBackoffSeconds: 12, |
| 94 | + maxDoublings: 2, |
| 95 | + region: 'us-central1', |
| 96 | + labels: { key: 'val' }, |
| 97 | + }, |
| 98 | + (event) => {} |
| 99 | + ); |
| 100 | + |
| 101 | + expect(schfn.__endpoint).to.deep.eq({ |
| 102 | + platform: 'gcfv2', |
| 103 | + labels: { key: 'val' }, |
| 104 | + region: ['us-central1'], |
| 105 | + scheduleTrigger: { |
| 106 | + schedule: '* * * * *', |
| 107 | + timeZone: 'utc', |
| 108 | + retryConfig: { |
| 109 | + retryCount: 3, |
| 110 | + maxRetrySeconds: 10, |
| 111 | + minBackoffSeconds: 11, |
| 112 | + maxBackoffSeconds: 12, |
| 113 | + maxDoublings: 2, |
| 114 | + }, |
| 115 | + }, |
| 116 | + }); |
| 117 | + expect(schfn.__requiredAPIs).to.deep.eq([ |
| 118 | + { |
| 119 | + api: 'cloudscheduler.googleapis.com', |
| 120 | + reason: 'Needed for scheduled functions.', |
| 121 | + }, |
| 122 | + ]); |
| 123 | + }); |
| 124 | + |
| 125 | + it('should have a .run method', () => { |
| 126 | + const testObj = { |
| 127 | + foo: 'bar', |
| 128 | + }; |
| 129 | + const schfn = schedule.onSchedule('* * * * *', (event) => { |
| 130 | + testObj.foo = 'newBar'; |
| 131 | + }); |
| 132 | + |
| 133 | + schfn.run('input' as any); |
| 134 | + |
| 135 | + expect(testObj).to.deep.eq({ |
| 136 | + foo: 'newBar', |
| 137 | + }); |
| 138 | + }); |
| 139 | + }); |
| 140 | +}); |
0 commit comments