Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

6.3 KB
Raw Blame
import { describe, it, expect } from "vitest";

import { type MonitorAlert } from "../types";
import { buildMonitorAlertSlackMessage } from "./buildMonitorAlertSlackMessage";

/** mockMonitorAlert is the ALERT each case overrides. */
const mockMonitorAlert: MonitorAlert = {
  monitorId: "mon_01",
  projectId: "proj_01",
  permalink: "https://cloud.langfuse.com/project/proj_01/monitors/mon_01",
  message: {
    title: "High error rate",
    body: "**count(observations.value)** is **above** `100`",
  },
  severity: "ALERT",
  timestamp: new Date("2026-05-18T12:01:00.000Z"),
  fromTimestamp: new Date("2026-05-18T11:55:30.000Z"),
  toTimestamp: new Date("2026-05-18T12:00:30.000Z"),
  view: "observations",
  filters: [],
  window: "5m",
};

describe("buildMonitorAlertSlackMessage", () => {
  it("ALERT: red attachment; linked title; body mrkdwn; timestamp; view link", () => {
    const { blocks, attachments } =
      buildMonitorAlertSlackMessage(mockMonitorAlert);
    expect(blocks).toEqual([]);
    expect(attachments).toHaveLength(1);
    expect(attachments![0].color).toBe("#dc3545");
    expect(attachments![0].fallback).toBe("High error rate");
    const inner = attachments![0].blocks!;
    expect(inner[0]).toMatchObject({
      type: "section",
      text: {
        type: "mrkdwn",
        text: "*<https://cloud.langfuse.com/project/proj_01/monitors/mon_01|High error rate>*",
      },
    });
    expect(inner[1]).toMatchObject({
      type: "section",
      text: { type: "mrkdwn" },
    });
    expect(inner[1].text.text).toContain("*count(observations.value)*");
    expect(inner[2]).toMatchObject({ type: "context" });
    expect(inner[2].elements[0].text).toContain("2026-05-18T12:01:00.000Z");
    expect(inner[3]).toMatchObject({ type: "section" });
    expect(inner[3].text.text).toContain(
      "<https://cloud.langfuse.com/project/proj_01/monitors/mon_01|View in Langfuse>",
    );
  });

  it.each([
    ["WARNING", "#ffc107"],
    ["OK", "#28a745"],
    ["NO_DATA", "#6c757d"],
    ["UNKNOWN", "#6c757d"],
    ["PAUSED", "#6c757d"],
  ] as const)("%s: linked title, attachment color %s", (severity, color) => {
    const { attachments } = buildMonitorAlertSlackMessage({
      ...mockMonitorAlert,
      severity,
    });
    expect(attachments![0].color).toBe(color);
    expect(attachments![0].blocks![0].text.text).toContain("High error rate");
  });

  it("title links to the permalink", () => {
    const { attachments } = buildMonitorAlertSlackMessage(mockMonitorAlert);
    expect(attachments![0].blocks![0].text.text).toBe(
      "*<https://cloud.langfuse.com/project/proj_01/monitors/mon_01|High error rate>*",
    );
  });

  it("renders a plain bold title and omits the link when permalink is absent", () => {
    const { attachments } = buildMonitorAlertSlackMessage({
      ...mockMonitorAlert,
      permalink: undefined,
    });
    const inner = attachments![0].blocks!;
    expect(inner[0].text.text).toBe("*High error rate*");
    expect(JSON.stringify(inner)).not.toContain("View in Langfuse");
    expect(inner.some((b: any) => b.type === "context")).toBe(true);
  });

  it("adds a 'View observations' link to the windowed data table when dataPermalink is set (observations view)", () => {
    const dataPermalink =
      "https://cloud.langfuse.com/project/proj_01/observations?dateRange=1779450930000-1779451230000";
    const { attachments } = buildMonitorAlertSlackMessage({
      ...mockMonitorAlert,
      dataPermalink,
    });
    const links = linkText(attachments);
    expect(links).toContain(`<${mockMonitorAlert.permalink}|View in Langfuse>`);
    expect(links).toContain(`<${dataPermalink}|View observations>`);
  });

  it("labels the data link 'View scores' for a scores view", () => {
    const dataPermalink =
      "https://cloud.langfuse.com/project/proj_01/scores?dateRange=1779450930000-1779451230000";
    const { attachments } = buildMonitorAlertSlackMessage({
      ...mockMonitorAlert,
      view: "scores-numeric",
      dataPermalink,
    });
    expect(linkText(attachments)).toContain(`<${dataPermalink}|View scores>`);
  });

  it("keeps only the config link when dataPermalink is absent", () => {
    const { attachments } = buildMonitorAlertSlackMessage(mockMonitorAlert);
    expect(linkText(attachments)).toBe(
      `:arrow_right: *<${mockMonitorAlert.permalink}|View in Langfuse>*`,
    );
  });

  it("explains an unavailable score link while retaining the alert link", () => {
    const { attachments } = buildMonitorAlertSlackMessage({
      ...mockMonitorAlert,
      view: "scores-numeric",
    });
    const blocks = attachments![0].blocks!;
    expect(blocks[1].text.text).toContain(
      "A filtered scores link is unavailable",
    );
    expect(linkText(attachments)).toBe(
      `:arrow_right: *<${mockMonitorAlert.permalink}|View in Langfuse>*`,
    );
  });

  it("explains a score link that does not fit in the link section", () => {
    const dataPermalink = `https://cloud.langfuse.com/project/proj_01/scores?filter=${"a".repeat(2900)}`;
    const { attachments } = buildMonitorAlertSlackMessage({
      ...mockMonitorAlert,
      view: "scores-numeric",
      dataPermalink,
    });
    expect(attachments![0].blocks![1].text.text).toContain(
      "A filtered scores link is unavailable",
    );
    expect(linkText(attachments)).toBe(
      `:arrow_right: *<${mockMonitorAlert.permalink}|View in Langfuse>*`,
    );
  });

  // buildAlert only sets dataPermalink for breaching severities, so recovery
  // (OK) and NO_DATA notifications arrive here with dataPermalink undefined and
  // must render no secondary data link — recovery messages stay unchanged.
  it.each(["OK", "NO_DATA"] as const)(
    "renders no secondary data link for a %s (non-breach) alert",
    (severity) => {
      const { attachments } = buildMonitorAlertSlackMessage({
        ...mockMonitorAlert,
        view: "scores-numeric",
        severity,
        dataPermalink: undefined,
      });
      expect(linkText(attachments)).toBe(
        `:arrow_right: *<${mockMonitorAlert.permalink}|View in Langfuse>*`,
      );
      expect(attachments![0].blocks![1].text.text).not.toContain(
        "scores link is unavailable",
      );
    },
  );
});

function linkText(
  attachments: ReturnType<typeof buildMonitorAlertSlackMessage>["attachments"],
): string {
  return attachments![0].blocks!.at(-1).text.text;
}