|
| 1 | +import * as chai from "chai"; |
| 2 | +import * as chaiAsPromised from "chai-as-promised"; |
| 3 | + |
| 4 | +import { validateEventFilters, EventFilter } from "./filters"; |
| 5 | +import { FirebaseError } from "../error"; |
| 6 | + |
| 7 | +chai.use(chaiAsPromised); |
| 8 | +const expect = chai.expect; |
| 9 | + |
| 10 | +describe("filters", () => { |
| 11 | + describe("validateEventFilters", () => { |
| 12 | + it("should not throw for undefined filter", () => { |
| 13 | + expect(() => validateEventFilters(undefined)).to.not.throw(); |
| 14 | + }); |
| 15 | + |
| 16 | + it("should not throw for empty filter", () => { |
| 17 | + expect(() => validateEventFilters({} as EventFilter)).to.not.throw(); |
| 18 | + }); |
| 19 | + |
| 20 | + describe("deviceDisplayNames validation", () => { |
| 21 | + it("should not throw for valid device display name format", () => { |
| 22 | + const filter: EventFilter = { |
| 23 | + deviceDisplayNames: ["Samsung (Galaxy S21)", "Google (Pixel 6)", "Apple (iPhone 13)"], |
| 24 | + }; |
| 25 | + expect(() => validateEventFilters(filter)).to.not.throw(); |
| 26 | + }); |
| 27 | + |
| 28 | + it("should throw for invalid device display name without parentheses", () => { |
| 29 | + const filter: EventFilter = { |
| 30 | + deviceDisplayNames: ["Samsung Galaxy S21"], |
| 31 | + }; |
| 32 | + expect(() => validateEventFilters(filter)).to.throw( |
| 33 | + FirebaseError, |
| 34 | + "deviceDisplayNames must match pattern 'manufacturer (device)'", |
| 35 | + ); |
| 36 | + }); |
| 37 | + |
| 38 | + it("should throw for invalid device display name with missing manufacturer", () => { |
| 39 | + const filter: EventFilter = { |
| 40 | + deviceDisplayNames: ["(Galaxy S21)"], |
| 41 | + }; |
| 42 | + expect(() => validateEventFilters(filter)).to.throw( |
| 43 | + FirebaseError, |
| 44 | + "deviceDisplayNames must match pattern 'manufacturer (device)'", |
| 45 | + ); |
| 46 | + }); |
| 47 | + |
| 48 | + it("should throw for invalid device display name with empty parentheses", () => { |
| 49 | + const filter: EventFilter = { |
| 50 | + deviceDisplayNames: ["Samsung ()"], |
| 51 | + }; |
| 52 | + expect(() => validateEventFilters(filter)).to.throw( |
| 53 | + FirebaseError, |
| 54 | + "deviceDisplayNames must match pattern 'manufacturer (device)'", |
| 55 | + ); |
| 56 | + }); |
| 57 | + |
| 58 | + it("should throw when any device display name is invalid", () => { |
| 59 | + const filter: EventFilter = { |
| 60 | + deviceDisplayNames: ["Samsung (Galaxy S21)", "InvalidFormat", "Google (Pixel 6)"], |
| 61 | + }; |
| 62 | + expect(() => validateEventFilters(filter)).to.throw( |
| 63 | + FirebaseError, |
| 64 | + "deviceDisplayNames must match pattern 'manufacturer (device)'", |
| 65 | + ); |
| 66 | + }); |
| 67 | + }); |
| 68 | + |
| 69 | + describe("operatingSystemDisplayNames validation", () => { |
| 70 | + it("should not throw for valid OS display name format", () => { |
| 71 | + const filter: EventFilter = { |
| 72 | + operatingSystemDisplayNames: ["iOS (15.0)", "Android (12)", "Windows (11)"], |
| 73 | + }; |
| 74 | + expect(() => validateEventFilters(filter)).to.not.throw(); |
| 75 | + }); |
| 76 | + |
| 77 | + it("should throw for invalid OS display name without parentheses", () => { |
| 78 | + const filter: EventFilter = { |
| 79 | + operatingSystemDisplayNames: ["iOS 15.0"], |
| 80 | + }; |
| 81 | + expect(() => validateEventFilters(filter)).to.throw( |
| 82 | + FirebaseError, |
| 83 | + "operatingSystemDisplayNames must match pattern 'os (version)'", |
| 84 | + ); |
| 85 | + }); |
| 86 | + |
| 87 | + it("should throw for invalid OS display name with empty parentheses", () => { |
| 88 | + const filter: EventFilter = { |
| 89 | + operatingSystemDisplayNames: ["iOS ()"], |
| 90 | + }; |
| 91 | + expect(() => validateEventFilters(filter)).to.throw( |
| 92 | + FirebaseError, |
| 93 | + "operatingSystemDisplayNames must match pattern 'os (version)'", |
| 94 | + ); |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + describe("versionDisplayNames validation", () => { |
| 99 | + it("should not throw for valid version display name format", () => { |
| 100 | + const filter: EventFilter = { |
| 101 | + versionDisplayNames: ["1.0.0 (100)", "2.1.3 (213)", "3.0.0-beta (300)"], |
| 102 | + }; |
| 103 | + expect(() => validateEventFilters(filter)).to.not.throw(); |
| 104 | + }); |
| 105 | + |
| 106 | + it("should throw for invalid version display name without parentheses", () => { |
| 107 | + const filter: EventFilter = { |
| 108 | + versionDisplayNames: ["1.0.0 build 100"], |
| 109 | + }; |
| 110 | + expect(() => validateEventFilters(filter)).to.throw( |
| 111 | + FirebaseError, |
| 112 | + "versionDisplayNames must match pattern 'version (build)'", |
| 113 | + ); |
| 114 | + }); |
| 115 | + |
| 116 | + it("should throw for invalid version display name with missing version", () => { |
| 117 | + const filter: EventFilter = { |
| 118 | + versionDisplayNames: ["(100)"], |
| 119 | + }; |
| 120 | + expect(() => validateEventFilters(filter)).to.throw( |
| 121 | + FirebaseError, |
| 122 | + "versionDisplayNames must match pattern 'version (build)'", |
| 123 | + ); |
| 124 | + }); |
| 125 | + |
| 126 | + it("should throw when any version display name is invalid", () => { |
| 127 | + const filter: EventFilter = { |
| 128 | + versionDisplayNames: ["1.0.0 (100)", "InvalidFormat", "2.0.0 (200)"], |
| 129 | + }; |
| 130 | + expect(() => validateEventFilters(filter)).to.throw( |
| 131 | + FirebaseError, |
| 132 | + "versionDisplayNames must match pattern 'version (build)'", |
| 133 | + ); |
| 134 | + }); |
| 135 | + }); |
| 136 | + |
| 137 | + describe("intervalStartTime validation", () => { |
| 138 | + it("should not throw for intervalStartTime within 90 days", () => { |
| 139 | + const thirtyDaysAgo = new Date(Date.now() - 30 * 24 * 60 * 60 * 1000).toISOString(); |
| 140 | + const filter: EventFilter = { |
| 141 | + intervalStartTime: thirtyDaysAgo, |
| 142 | + }; |
| 143 | + expect(() => validateEventFilters(filter)).to.not.throw(); |
| 144 | + }); |
| 145 | + |
| 146 | + it("should not throw for intervalStartTime exactly 89 days ago", () => { |
| 147 | + const eightyNineDaysAgo = new Date(Date.now() - 89 * 24 * 60 * 60 * 1000).toISOString(); |
| 148 | + const filter: EventFilter = { |
| 149 | + intervalStartTime: eightyNineDaysAgo, |
| 150 | + }; |
| 151 | + expect(() => validateEventFilters(filter)).to.not.throw(); |
| 152 | + }); |
| 153 | + |
| 154 | + it("should throw for intervalStartTime more than 90 days in the past", () => { |
| 155 | + const ninetyOneDaysAgo = new Date(Date.now() - 91 * 24 * 60 * 60 * 1000).toISOString(); |
| 156 | + const filter: EventFilter = { |
| 157 | + intervalStartTime: ninetyOneDaysAgo, |
| 158 | + }; |
| 159 | + expect(() => validateEventFilters(filter)).to.throw( |
| 160 | + FirebaseError, |
| 161 | + "intervalStartTime must be less than 90 days in the past", |
| 162 | + ); |
| 163 | + }); |
| 164 | + |
| 165 | + it("should throw for intervalStartTime 100 days in the past", () => { |
| 166 | + const hundredDaysAgo = new Date(Date.now() - 100 * 24 * 60 * 60 * 1000).toISOString(); |
| 167 | + const filter: EventFilter = { |
| 168 | + intervalStartTime: hundredDaysAgo, |
| 169 | + }; |
| 170 | + expect(() => validateEventFilters(filter)).to.throw( |
| 171 | + FirebaseError, |
| 172 | + "intervalStartTime must be less than 90 days in the past", |
| 173 | + ); |
| 174 | + }); |
| 175 | + }); |
| 176 | + }); |
| 177 | +}); |
0 commit comments