|
| 1 | +import { equal, throws } from "node:assert"; |
| 2 | +import test from "node:test"; |
| 3 | +import type { ABTest } from "../../types.ts"; |
| 4 | +import { allExpirationsValid } from "./validExpiration.ts"; |
| 5 | + |
| 6 | +function createTestABTest( |
| 7 | + name: ABTest["name"], |
| 8 | + expirationDate: ABTest["expirationDate"], |
| 9 | +): ABTest { |
| 10 | + return { |
| 11 | + name, |
| 12 | + description: "A test description", |
| 13 | + |
| 14 | + status: "ON", |
| 15 | + expirationDate, |
| 16 | + type: "client", |
| 17 | + audienceSize: 10 / 100, |
| 18 | + groups: ["control", "variant"], |
| 19 | + }; |
| 20 | +} |
| 21 | + |
| 22 | +function getOffsetDate(days: number): ABTest["expirationDate"] { |
| 23 | + const today = new Date(); |
| 24 | + today.setDate(today.getDate() + days); |
| 25 | + return today.toISOString().split("T")[0] as ABTest["expirationDate"]; // Format as YYYY-MM-DD |
| 26 | +} |
| 27 | + |
| 28 | +test("allExpirationsValid - passes when all tests have valid expiration dates", () => { |
| 29 | + const nextYear = new Date().getFullYear() + 1; |
| 30 | + const futureDate = `${nextYear}-01-01` as ABTest["expirationDate"]; |
| 31 | + const tests: ABTest[] = [ |
| 32 | + createTestABTest("commercial-test-one", futureDate), |
| 33 | + createTestABTest("commercial-test-two", futureDate), |
| 34 | + createTestABTest("webex-test-three", futureDate), |
| 35 | + ]; |
| 36 | + |
| 37 | + equal(allExpirationsValid(tests), true); |
| 38 | +}); |
| 39 | + |
| 40 | +test("allExpirationsValid - fails when one of the tests has an invalid expiration date", () => { |
| 41 | + const nextYear = new Date().getFullYear() + 1; |
| 42 | + const feb31stNextYear = `${nextYear}-02-31` as ABTest["expirationDate"]; |
| 43 | + const tests: ABTest[] = [ |
| 44 | + createTestABTest("commercial-test-one", getOffsetDate(5)), |
| 45 | + createTestABTest("commercial-test-two", getOffsetDate(10)), |
| 46 | + createTestABTest("webex-invalid-date", feb31stNextYear), |
| 47 | + ]; |
| 48 | + |
| 49 | + throws( |
| 50 | + () => allExpirationsValid(tests), |
| 51 | + Error, |
| 52 | + `Invalid expiration date provided on test webex-invalid-date: ${feb31stNextYear}`, |
| 53 | + ); |
| 54 | +}); |
0 commit comments