|
21 | 21 | // SOFTWARE. |
22 | 22 |
|
23 | 23 | import { expect } from "chai"; |
24 | | -import { defineJsonSecret, defineSecret } from "../../src/params"; |
25 | | -import { GlobalOptions, optionsToEndpoint, RESET_VALUE } from "../../src/v2/options"; |
| 24 | +import { defineInt, defineJsonSecret, defineSecret } from "../../src/params"; |
| 25 | +import { |
| 26 | + assertTimeoutSecondsValid, |
| 27 | + GlobalOptions, |
| 28 | + optionsToEndpoint, |
| 29 | + optionsToTriggerAnnotations, |
| 30 | + RESET_VALUE, |
| 31 | + setGlobalOptions, |
| 32 | +} from "../../src/v2/options"; |
26 | 33 |
|
27 | 34 | describe("GlobalOptions", () => { |
28 | 35 | it("should accept all valid secret types in secrets array (type test)", () => { |
@@ -92,3 +99,172 @@ describe("optionsToEndpoint", () => { |
92 | 99 | expect(endpoint.vpc).to.equal(RESET_VALUE); |
93 | 100 | }); |
94 | 101 | }); |
| 102 | + |
| 103 | +describe("assertTimeoutSecondsValid", () => { |
| 104 | + afterEach(() => { |
| 105 | + setGlobalOptions({}); |
| 106 | + }); |
| 107 | + |
| 108 | + it("is a no-op when timeoutSeconds is undefined", () => { |
| 109 | + expect(() => assertTimeoutSecondsValid({}, "event")).to.not.throw(); |
| 110 | + expect(() => assertTimeoutSecondsValid({}, "https")).to.not.throw(); |
| 111 | + expect(() => assertTimeoutSecondsValid({}, "task")).to.not.throw(); |
| 112 | + expect(() => assertTimeoutSecondsValid({}, "identity")).to.not.throw(); |
| 113 | + }); |
| 114 | + |
| 115 | + it("accepts values within each kind's limit", () => { |
| 116 | + expect(() => assertTimeoutSecondsValid({ timeoutSeconds: 540 }, "event")).to.not.throw(); |
| 117 | + expect(() => assertTimeoutSecondsValid({ timeoutSeconds: 3600 }, "https")).to.not.throw(); |
| 118 | + expect(() => assertTimeoutSecondsValid({ timeoutSeconds: 1800 }, "task")).to.not.throw(); |
| 119 | + expect(() => assertTimeoutSecondsValid({ timeoutSeconds: 0 }, "event")).to.not.throw(); |
| 120 | + expect(() => assertTimeoutSecondsValid({ timeoutSeconds: 1 }, "event")).to.not.throw(); |
| 121 | + expect(() => assertTimeoutSecondsValid({ timeoutSeconds: 7 }, "identity")).to.not.throw(); |
| 122 | + }); |
| 123 | + |
| 124 | + it("throws when timeoutSeconds exceeds the event-handler limit", () => { |
| 125 | + expect(() => assertTimeoutSecondsValid({ timeoutSeconds: 3600 }, "event")).to.throw( |
| 126 | + /between 0 and 540 for event-handling functions/ |
| 127 | + ); |
| 128 | + }); |
| 129 | + |
| 130 | + it("throws when timeoutSeconds exceeds the HTTPS limit", () => { |
| 131 | + expect(() => assertTimeoutSecondsValid({ timeoutSeconds: 3601 }, "https")).to.throw( |
| 132 | + /between 0 and 3600 for HTTPS and callable functions/ |
| 133 | + ); |
| 134 | + }); |
| 135 | + |
| 136 | + it("throws when timeoutSeconds exceeds the task-queue limit", () => { |
| 137 | + expect(() => assertTimeoutSecondsValid({ timeoutSeconds: 1801 }, "task")).to.throw( |
| 138 | + /between 0 and 1800 for task queue functions/ |
| 139 | + ); |
| 140 | + }); |
| 141 | + |
| 142 | + it("throws when timeoutSeconds exceeds the identity limit", () => { |
| 143 | + expect(() => assertTimeoutSecondsValid({ timeoutSeconds: 8 }, "identity")).to.throw( |
| 144 | + /between 0 and 7 for blocking functions/ |
| 145 | + ); |
| 146 | + }); |
| 147 | + |
| 148 | + it("throws when timeoutSeconds is negative", () => { |
| 149 | + expect(() => assertTimeoutSecondsValid({ timeoutSeconds: -1 }, "event")).to.throw( |
| 150 | + /between 0 and 540/ |
| 151 | + ); |
| 152 | + }); |
| 153 | + |
| 154 | + it("skips validation for Expression timeouts", () => { |
| 155 | + const expr = { timeoutSeconds: defineInt("TIMEOUT") }; |
| 156 | + expect(() => assertTimeoutSecondsValid(expr, "event")).to.not.throw(); |
| 157 | + }); |
| 158 | + |
| 159 | + it("skips validation for RESET_VALUE timeouts", () => { |
| 160 | + const opts = { timeoutSeconds: RESET_VALUE as unknown as number }; |
| 161 | + expect(() => assertTimeoutSecondsValid(opts, "event")).to.not.throw(); |
| 162 | + }); |
| 163 | + |
| 164 | + it("throws when timeoutSeconds has an invalid non-number type", () => { |
| 165 | + const opts = { timeoutSeconds: "30" as unknown as number }; |
| 166 | + expect(() => assertTimeoutSecondsValid(opts, "event")).to.throw( |
| 167 | + /must be a number, Expression, or RESET_VALUE/ |
| 168 | + ); |
| 169 | + }); |
| 170 | + |
| 171 | + it("throws when timeoutSeconds is null", () => { |
| 172 | + const opts = { timeoutSeconds: null as unknown as number }; |
| 173 | + expect(() => assertTimeoutSecondsValid(opts, "event")).to.throw( |
| 174 | + /must be a number, Expression, or RESET_VALUE/ |
| 175 | + ); |
| 176 | + }); |
| 177 | + |
| 178 | + it("throws when global timeoutSeconds has an invalid non-number type", () => { |
| 179 | + setGlobalOptions({ timeoutSeconds: true as unknown as number }); |
| 180 | + expect(() => assertTimeoutSecondsValid({}, "event")).to.throw( |
| 181 | + /must be a number, Expression, or RESET_VALUE/ |
| 182 | + ); |
| 183 | + }); |
| 184 | + |
| 185 | + it("falls back to the global timeoutSeconds when the function-level option is absent", () => { |
| 186 | + setGlobalOptions({ timeoutSeconds: 3600 }); |
| 187 | + expect(() => assertTimeoutSecondsValid({}, "event")).to.throw( |
| 188 | + /between 0 and 540 for event-handling functions/ |
| 189 | + ); |
| 190 | + expect(() => assertTimeoutSecondsValid({}, "https")).to.not.throw(); |
| 191 | + expect(() => assertTimeoutSecondsValid({}, "task")).to.throw(); |
| 192 | + expect(() => assertTimeoutSecondsValid({}, "identity")).to.throw(); |
| 193 | + }); |
| 194 | + |
| 195 | + it("prefers the function-level timeoutSeconds over the global one", () => { |
| 196 | + setGlobalOptions({ timeoutSeconds: 60 }); |
| 197 | + expect(() => assertTimeoutSecondsValid({ timeoutSeconds: 1000 }, "event")).to.throw( |
| 198 | + /between 0 and 540/ |
| 199 | + ); |
| 200 | + }); |
| 201 | + |
| 202 | + it("treats a function-level RESET_VALUE as a clear of an out-of-range global", () => { |
| 203 | + setGlobalOptions({ timeoutSeconds: 3600 }); |
| 204 | + expect(() => |
| 205 | + assertTimeoutSecondsValid({ timeoutSeconds: RESET_VALUE as unknown as number }, "event") |
| 206 | + ).to.not.throw(); |
| 207 | + }); |
| 208 | +}); |
| 209 | + |
| 210 | +describe("optionsToEndpoint timeout validation", () => { |
| 211 | + afterEach(() => { |
| 212 | + setGlobalOptions({}); |
| 213 | + }); |
| 214 | + |
| 215 | + it("does not validate when kind is omitted (backwards compatibility)", () => { |
| 216 | + expect(() => optionsToEndpoint({ timeoutSeconds: 9999 })).to.not.throw(); |
| 217 | + }); |
| 218 | + |
| 219 | + it("throws when kind is provided and timeoutSeconds exceeds the limit", () => { |
| 220 | + expect(() => optionsToEndpoint({ timeoutSeconds: 3600 }, "event")).to.throw( |
| 221 | + /between 0 and 540/ |
| 222 | + ); |
| 223 | + expect(() => optionsToEndpoint({ timeoutSeconds: 3601 }, "https")).to.throw( |
| 224 | + /between 0 and 3600/ |
| 225 | + ); |
| 226 | + expect(() => optionsToEndpoint({ timeoutSeconds: 1801 }, "task")).to.throw( |
| 227 | + /between 0 and 1800/ |
| 228 | + ); |
| 229 | + expect(() => optionsToEndpoint({ timeoutSeconds: 8 }, "identity")).to.throw(/between 0 and 7/); |
| 230 | + }); |
| 231 | + |
| 232 | + it("is a no-op for in-range timeouts when kind is provided", () => { |
| 233 | + expect(() => optionsToEndpoint({ timeoutSeconds: 540 }, "event")).to.not.throw(); |
| 234 | + expect(() => optionsToEndpoint({ timeoutSeconds: 3600 }, "https")).to.not.throw(); |
| 235 | + expect(() => optionsToEndpoint({ timeoutSeconds: 1800 }, "task")).to.not.throw(); |
| 236 | + expect(() => optionsToEndpoint({ timeoutSeconds: 7 }, "identity")).to.not.throw(); |
| 237 | + }); |
| 238 | +}); |
| 239 | + |
| 240 | +describe("optionsToTriggerAnnotations timeout validation", () => { |
| 241 | + afterEach(() => { |
| 242 | + setGlobalOptions({}); |
| 243 | + }); |
| 244 | + |
| 245 | + it("does not validate when kind is omitted (backwards compatibility)", () => { |
| 246 | + expect(() => optionsToTriggerAnnotations({ timeoutSeconds: 9999 })).to.not.throw(); |
| 247 | + }); |
| 248 | + |
| 249 | + it("throws when kind is provided and timeoutSeconds exceeds the limit", () => { |
| 250 | + expect(() => optionsToTriggerAnnotations({ timeoutSeconds: 3600 }, "event")).to.throw( |
| 251 | + /between 0 and 540/ |
| 252 | + ); |
| 253 | + expect(() => optionsToTriggerAnnotations({ timeoutSeconds: 3601 }, "https")).to.throw( |
| 254 | + /between 0 and 3600/ |
| 255 | + ); |
| 256 | + expect(() => optionsToTriggerAnnotations({ timeoutSeconds: 1801 }, "task")).to.throw( |
| 257 | + /between 0 and 1800/ |
| 258 | + ); |
| 259 | + expect(() => optionsToTriggerAnnotations({ timeoutSeconds: 8 }, "identity")).to.throw( |
| 260 | + /between 0 and 7/ |
| 261 | + ); |
| 262 | + }); |
| 263 | + |
| 264 | + it("is a no-op for in-range timeouts when kind is provided", () => { |
| 265 | + expect(() => optionsToTriggerAnnotations({ timeoutSeconds: 540 }, "event")).to.not.throw(); |
| 266 | + expect(() => optionsToTriggerAnnotations({ timeoutSeconds: 3600 }, "https")).to.not.throw(); |
| 267 | + expect(() => optionsToTriggerAnnotations({ timeoutSeconds: 1800 }, "task")).to.not.throw(); |
| 268 | + expect(() => optionsToTriggerAnnotations({ timeoutSeconds: 7 }, "identity")).to.not.throw(); |
| 269 | + }); |
| 270 | +}); |
0 commit comments