Skip to content

Open-source status page, incident response and uptime monitoring

5.0 KB
Raw Blame
/** @jsxRuntime automatic @jsxImportSource react */

import "./test-preload.ts";
import { render } from "react-email";
import { describe, expect, test } from "vitest";

import PageUpdateEmail from "../emails/page-update";
import { pageUpdateSubject } from "./client";

describe("Status Report Email - Unsubscribe Link in Body", () => {
  const unsubscribeUrl =
    "https://openstatus.openstatus.dev/unsubscribe/test-token";
  const manageUrl = "https://openstatus.openstatus.dev/manage/test-token";

  test("should include unsubscribe link in email body when URL is provided", async () => {
    const html = await render(
      <PageUpdateEmail
        pageTitle="Test Page"
        reportTitle="Test Report"
        status="investigating"
        date={new Date().toISOString()}
        message="Test message"
        pageComponents={["Monitor 1"]}
        unsubscribeUrl={unsubscribeUrl}
        manageUrl={manageUrl}
      />,
    );

    expect(html).toContain(unsubscribeUrl);
    expect(html).toContain("Unsubscribe");
  });

  test("should not include unsubscribe section when URL is not provided", async () => {
    const html = await render(
      <PageUpdateEmail
        pageTitle="Test Page"
        reportTitle="Test Report"
        status="investigating"
        date={new Date().toISOString()}
        message="Test message"
        pageComponents={["Monitor 1"]}
        manageUrl={manageUrl}
        unsubscribeUrl=""
      />,
    );

    // Should not contain the unsubscribe text when no URL provided
    expect(html).not.toContain("Unsubscribe");
  });

  test("should render unsubscribe link as clickable", async () => {
    const html = await render(
      <PageUpdateEmail
        pageTitle="Test Page"
        reportTitle="Test Report"
        status="investigating"
        date={new Date().toISOString()}
        message="Test message"
        pageComponents={["Monitor 1"]}
        unsubscribeUrl={unsubscribeUrl}
        manageUrl={manageUrl}
      />,
    );

    // Check that the URL is in an href attribute
    expect(html).toContain(`href="${unsubscribeUrl}"`);
  });

  test("should render the unsubscribe link in the footer, outside the card", async () => {
    const html = await render(
      <PageUpdateEmail
        pageTitle="Test Page"
        reportTitle="Test Report"
        status="investigating"
        date={new Date().toISOString()}
        message="Test message"
        pageComponents={["Monitor 1"]}
        unsubscribeUrl={unsubscribeUrl}
        manageUrl={manageUrl}
      />,
    );

    expect(html.indexOf("Test message")).toBeLessThan(
      html.indexOf(unsubscribeUrl),
    );
    expect(html).toContain("Manage notifications");
  });
});

describe("Status Report Email - Subject Line", () => {
  const reportTitle = "API Outage";

  test('prepends "RESOLVED:" only when status is "resolved"', () => {
    expect(pageUpdateSubject({ status: "resolved", reportTitle })).toBe(
      `RESOLVED: ${reportTitle}`,
    );
  });

  test("uses the bare report title for ongoing report statuses", () => {
    const ongoing = ["investigating", "identified", "monitoring"] as const;

    for (const status of ongoing) {
      const subject = pageUpdateSubject({ status, reportTitle });
      expect(subject).toBe(reportTitle);
      expect(subject).not.toContain("RESOLVED:");
    }
  });

  test('prepends "Planned Maintenance:" when status is "maintenance"', () => {
    expect(pageUpdateSubject({ status: "maintenance", reportTitle })).toBe(
      `Planned Maintenance: ${reportTitle}`,
    );
  });
});

describe("Status Report Email - Email Content Validation", () => {
  test("should include all required email fields", async () => {
    const props = {
      pageTitle: "openstatus",
      reportTitle: "API Outage",
      status: "investigating" as const,
      date: "2024-01-15T10:00:00.000Z",
      message: "We are investigating the issue",
      pageComponents: ["API", "Web"],
      unsubscribeUrl: "https://openstatus.openstatus.dev/unsubscribe/test",
      manageUrl: "https://openstatus.openstatus.dev/manage/test",
    };

    const html = await render(<PageUpdateEmail {...props} />);

    expect(html).toContain(props.pageTitle);
    expect(html).toContain(props.reportTitle);
    expect(html).toContain(props.message);
    expect(html).toContain("API");
    expect(html).toContain("Web");
    expect(html).toContain(props.unsubscribeUrl);
  });

  test("should handle all status types correctly", async () => {
    const statuses = [
      "investigating",
      "identified",
      "monitoring",
      "resolved",
      "maintenance",
    ] as const;

    for (const status of statuses) {
      const html = await render(
        <PageUpdateEmail
          pageTitle="Test"
          reportTitle="Test Report"
          status={status}
          date={new Date().toISOString()}
          message="Test"
          pageComponents={[]}
          unsubscribeUrl="https://example.com/unsubscribe"
          manageUrl="https://example.com/manage"
        />,
      );

      expect(html).toContain(status.toUpperCase());
    }
  });
});