Skip to content

Commit fa95aa8

Browse files
Merge branch 'main' into cc/aag-read-more-tracking
2 parents 2e87ccd + f08df84 commit fa95aa8

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
owners: ["[email protected]"],
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+
});

ab-testing/config/scripts/validation/validExpiration.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,23 @@ export function allExpirationsValid(tests: ABTest[]): boolean {
1212
} has an expiration date in the past: ${expires.toISOString()}, has it expired?`,
1313
);
1414
}
15+
16+
/**
17+
* Converts the expiration date to a `Date` object and then back into a string
18+
* This is to confirm Javascript interprets the input the same way humans do
19+
* If there's a difference, we throw an error to highlight the discrepancy
20+
* @example `2026-02-30` is interpreted as `2026-03-02` so this fails
21+
* @example `2026-01-30` is interpreted as `2026-01-30` so this passes
22+
*/
23+
const interpretedExpirationDate = new Date(test.expirationDate)
24+
.toISOString()
25+
.split("T")[0];
26+
27+
if (test.expirationDate !== interpretedExpirationDate) {
28+
throw new Error(
29+
`Invalid expiration date provided on test ${test.name}: ${test.expirationDate}`,
30+
);
31+
}
1532
});
1633
return true;
1734
}

0 commit comments

Comments
 (0)