|
| 1 | +import assert from "node:assert"; |
| 2 | +import test from "node:test"; |
| 3 | +import type { ABTest } from "../../types.ts"; |
| 4 | +import { checkExpiry } from "./checkExpiry.ts"; |
| 5 | + |
| 6 | +function getOffsetDate(days: number): ABTest["expirationDate"] { |
| 7 | + const today = new Date(); |
| 8 | + today.setDate(today.getDate() + days); |
| 9 | + return today.toISOString().split("T")[0] as ABTest["expirationDate"]; // Format as YYYY-MM-DD |
| 10 | +} |
| 11 | + |
| 12 | +test("checkExpiry - does not flag when the expiration is far in the future", () => { |
| 13 | + const futureDayTest: ABTest = { |
| 14 | + name: "commercial-future", |
| 15 | + description: "End on a weekday", |
| 16 | + |
| 17 | + status: "ON", |
| 18 | + expirationDate: getOffsetDate(10), |
| 19 | + type: "client", |
| 20 | + audienceSize: 10 / 100, |
| 21 | + groups: ["control", "variant"], |
| 22 | + }; |
| 23 | + |
| 24 | + assert.deepStrictEqual(checkExpiry([futureDayTest]), { |
| 25 | + within2Days: [], |
| 26 | + within1Day: [], |
| 27 | + expired: [], |
| 28 | + }); |
| 29 | +}); |
| 30 | + |
| 31 | +test("checkExpiry - flags when the expiration is within two days", () => { |
| 32 | + const dayAfterTomorrowTest: ABTest = { |
| 33 | + name: "commercial-future", |
| 34 | + description: "End on a weekday", |
| 35 | + |
| 36 | + status: "ON", |
| 37 | + expirationDate: getOffsetDate(2), |
| 38 | + type: "client", |
| 39 | + audienceSize: 10 / 100, |
| 40 | + groups: ["control", "variant"], |
| 41 | + }; |
| 42 | + |
| 43 | + assert.deepStrictEqual(checkExpiry([dayAfterTomorrowTest]), { |
| 44 | + within2Days: [dayAfterTomorrowTest], |
| 45 | + within1Day: [], |
| 46 | + expired: [], |
| 47 | + }); |
| 48 | +}); |
| 49 | + |
| 50 | +test("checkExpiry - flags when the expiration is within one day", () => { |
| 51 | + const tomorrowTest: ABTest = { |
| 52 | + name: "commercial-future", |
| 53 | + description: "End on a weekday", |
| 54 | + |
| 55 | + status: "ON", |
| 56 | + expirationDate: getOffsetDate(1), |
| 57 | + type: "client", |
| 58 | + audienceSize: 10 / 100, |
| 59 | + groups: ["control", "variant"], |
| 60 | + }; |
| 61 | + |
| 62 | + assert.deepStrictEqual(checkExpiry([tomorrowTest]), { |
| 63 | + within2Days: [], |
| 64 | + within1Day: [tomorrowTest], |
| 65 | + expired: [], |
| 66 | + }); |
| 67 | +}); |
| 68 | + |
| 69 | +test("checkExpiry - marks test as expired with a date of today", () => { |
| 70 | + const todayTest: ABTest = { |
| 71 | + name: "commercial-future", |
| 72 | + description: "End on a weekday", |
| 73 | + |
| 74 | + status: "ON", |
| 75 | + expirationDate: getOffsetDate(0), |
| 76 | + type: "client", |
| 77 | + audienceSize: 10 / 100, |
| 78 | + groups: ["control", "variant"], |
| 79 | + }; |
| 80 | + |
| 81 | + assert.deepStrictEqual(checkExpiry([todayTest]), { |
| 82 | + within2Days: [], |
| 83 | + within1Day: [], |
| 84 | + expired: [todayTest], |
| 85 | + }); |
| 86 | +}); |
| 87 | + |
| 88 | +test("checkExpiry - flags when the expiration is in the past", () => { |
| 89 | + const pastTest: ABTest = { |
| 90 | + name: "commercial-future", |
| 91 | + description: "End on a weekday", |
| 92 | + |
| 93 | + status: "ON", |
| 94 | + expirationDate: getOffsetDate(-2), |
| 95 | + type: "client", |
| 96 | + audienceSize: 10 / 100, |
| 97 | + groups: ["control", "variant"], |
| 98 | + }; |
| 99 | + |
| 100 | + assert.deepStrictEqual(checkExpiry([pastTest]), { |
| 101 | + within2Days: [], |
| 102 | + within1Day: [], |
| 103 | + expired: [pastTest], |
| 104 | + }); |
| 105 | +}); |
0 commit comments