|
| 1 | +import { assertEquals } from "jsr:@std/assert"; |
| 2 | +import type { Anomaly } from "./anomaly.ts"; |
| 3 | +import { |
| 4 | + anomaliesHtml, |
| 5 | + anomaliesText, |
| 6 | + batchSubject, |
| 7 | + formatBucket, |
| 8 | +} from "./email.ts"; |
| 9 | + |
| 10 | +const singleAnomaly: Anomaly = { |
| 11 | + projectId: "proj1", |
| 12 | + eventName: "login", |
| 13 | + bucket: "2026-04-06T23", |
| 14 | + expected: 10, |
| 15 | + actual: 30, |
| 16 | + zScore: 3.5, |
| 17 | + metric: "totalCount", |
| 18 | + detectedAt: "2026-04-06T23:30:00Z", |
| 19 | +}; |
| 20 | + |
| 21 | +const twoAnomalies: Anomaly[] = [ |
| 22 | + singleAnomaly, |
| 23 | + { |
| 24 | + projectId: "proj1", |
| 25 | + eventName: "signup", |
| 26 | + bucket: "2026-04-07T09", |
| 27 | + expected: 5, |
| 28 | + actual: 20, |
| 29 | + zScore: 4.1, |
| 30 | + metric: "percentageSpike", |
| 31 | + detectedAt: "2026-04-07T09:15:00Z", |
| 32 | + userId: "user123", |
| 33 | + }, |
| 34 | +]; |
| 35 | + |
| 36 | +Deno.test("batchSubject uses specific subject for single anomaly", () => { |
| 37 | + assertEquals( |
| 38 | + batchSubject("myapp", [singleAnomaly]), |
| 39 | + "[myapp] Total Count: login", |
| 40 | + ); |
| 41 | +}); |
| 42 | + |
| 43 | +Deno.test("batchSubject shows count for multiple anomalies", () => { |
| 44 | + assertEquals( |
| 45 | + batchSubject("myapp", twoAnomalies), |
| 46 | + "[myapp] 2 anomalies detected", |
| 47 | + ); |
| 48 | +}); |
| 49 | + |
| 50 | +Deno.test("anomaliesHtml says 'Anomaly Detected' without count for single anomaly", () => { |
| 51 | + const html = anomaliesHtml("myapp", [singleAnomaly]); |
| 52 | + assertEquals(html.includes("1 Anomaly"), false); |
| 53 | + assertEquals(html.includes("Anomaly Detected"), true); |
| 54 | +}); |
| 55 | + |
| 56 | +Deno.test("anomaliesHtml shows count for multiple anomalies", () => { |
| 57 | + const html = anomaliesHtml("myapp", twoAnomalies); |
| 58 | + assertEquals(html.includes("2 Anomalies Detected"), true); |
| 59 | +}); |
| 60 | + |
| 61 | +Deno.test("formatBucket renders human-readable date", () => { |
| 62 | + assertEquals(formatBucket("2026-04-06T23"), "Mon Apr 6, 11pm"); |
| 63 | +}); |
| 64 | + |
| 65 | +Deno.test("formatBucket renders midnight as 12am", () => { |
| 66 | + assertEquals(formatBucket("2026-04-07T00"), "Tue Apr 7, 12am"); |
| 67 | +}); |
| 68 | + |
| 69 | +Deno.test("formatBucket renders noon as 12pm", () => { |
| 70 | + assertEquals(formatBucket("2026-04-07T12"), "Tue Apr 7, 12pm"); |
| 71 | +}); |
| 72 | + |
| 73 | +Deno.test("formatBucket renders morning hour", () => { |
| 74 | + assertEquals(formatBucket("2026-04-07T09"), "Tue Apr 7, 9am"); |
| 75 | +}); |
| 76 | + |
| 77 | +Deno.test("anomaliesHtml uses formatted bucket not raw ISO", () => { |
| 78 | + const html = anomaliesHtml("myapp", [singleAnomaly]); |
| 79 | + assertEquals(html.includes("2026-04-06T23"), false); |
| 80 | + assertEquals(html.includes("Mon Apr 6, 11pm"), true); |
| 81 | +}); |
| 82 | + |
| 83 | +Deno.test("anomaliesText uses formatted bucket not raw ISO", () => { |
| 84 | + const text = anomaliesText([singleAnomaly]); |
| 85 | + assertEquals(text.includes("2026-04-06T23"), false); |
| 86 | + assertEquals(text.includes("Mon Apr 6, 11pm"), true); |
| 87 | +}); |
0 commit comments