Skip to content

Open-source status page, incident response and uptime monitoring

32.1 KB
Raw Blame
import { db, eq } from "@openstatus/db";
import {
  monitor,
  page as pageTable,
  pageComponent,
  selectWorkspaceSchema,
  statusReport,
  workspace,
} from "@openstatus/db/src/schema";
import { getLimits } from "@openstatus/db/src/schema/plan/utils";
import { afterAll, beforeAll, describe, expect, test } from "vitest";

import {
  createWorkspaceFixture,
  expectAuditRow,
  makeApiKeyCtx,
  makeUserCtx,
  withTestTransaction,
} from "../../../test/helpers";
import type { DrizzleTx, ServiceContext } from "../../context";
import {
  ConflictError,
  ForbiddenError,
  LimitExceededError,
  NotFoundError,
} from "../../errors";
import { createPage, newPage } from "../create";
import { deletePage } from "../delete";
import { getPage, getPageBySlug, getSlugAvailable, listPages } from "../list";
import { type CreatePageInput, UpdatePageConfigurationInput } from "../schemas";
import {
  updatePageConfiguration,
  updatePageCustomDomain,
  updatePageCustomTheme,
  updatePageGeneral,
  updatePageLocales,
} from "../update";

const TEST_PREFIX = "svc-page-test";

let teamCtx: ServiceContext;
let freeCtx: ServiceContext;
let teamMonitorId: number;

// Dedicated, freshly-inserted free-plan workspace for the quota-sensitive
// negative-path tests (status-pages limit = 1). They assume exclusive control
// of the workspace's page count, but the shared seeded free workspace (#2) is
// written concurrently by the apps/server RPC suites under parallel test
// execution — which intermittently exhausts the quota and fails the wrong
// assertion. An isolated workspace removes that cross-suite race; because every
// test writes inside a rolled-back transaction, its committed page count stays
// at zero for the whole suite.
const FREE_WS_SLUG = `${TEST_PREFIX}-free-ws`;

beforeAll(async () => {
  const team = (await createWorkspaceFixture("team")).workspace;
  teamCtx = makeUserCtx(team, { userId: 1 });

  await db
    .delete(workspace)
    .where(eq(workspace.slug, FREE_WS_SLUG))
    .catch(() => undefined);
  const freeRow = await db
    .insert(workspace)
    .values({
      slug: FREE_WS_SLUG,
      name: `${TEST_PREFIX}-free`,
      plan: "free",
      limits: JSON.stringify(getLimits("free")),
    })
    .returning()
    .get();
  freeCtx = makeUserCtx(selectWorkspaceSchema.parse(freeRow), { userId: 2 });

  const teamMonitor = await db
    .insert(monitor)
    .values({
      workspaceId: team.id,
      active: true,
      url: "https://example.com",
      name: `${TEST_PREFIX}-monitor`,
      method: "GET",
      periodicity: "10m",
      regions: "ams",
    })
    .returning()
    .get();
  teamMonitorId = teamMonitor.id;
});

afterAll(async () => {
  await db
    .delete(monitor)
    .where(eq(monitor.id, teamMonitorId))
    .catch(() => undefined);
  await db
    .delete(workspace)
    .where(eq(workspace.slug, FREE_WS_SLUG))
    .catch(() => undefined);
});

let slugCounter = 0;
const uniqueSlug = (tag: string) => `${TEST_PREFIX}-${tag}-${++slugCounter}`;

describe("newPage", () => {
  test("happy path + audit", async () => {
    await withTestTransaction(async (tx) => {
      const ctx = { ...teamCtx, db: tx };
      const slug = uniqueSlug("new");
      const row = await newPage({
        ctx,
        input: { title: "Test", slug },
      });
      expect(row.slug).toBe(slug);
      await expectAuditRow({
        workspaceId: teamCtx.workspace.id,
        action: "page.create",
        entityType: "page",
        entityId: row.id,
        db: tx,
      });
    });
  });

  test("rejects reserved subdomain", async () => {
    await withTestTransaction(async (tx) => {
      await expect(
        newPage({
          ctx: { ...teamCtx, db: tx },
          input: { title: "Test", slug: "api" },
        }),
      ).rejects.toBeInstanceOf(ConflictError);
    });
  });

  test("rejects duplicate slug", async () => {
    await withTestTransaction(async (tx) => {
      const ctx = { ...teamCtx, db: tx };
      const slug = uniqueSlug("dup");
      await newPage({
        ctx,
        input: { title: "First", slug },
      });
      await expect(
        newPage({ ctx, input: { title: "Second", slug } }),
      ).rejects.toBeInstanceOf(ConflictError);
    });
  });
});

describe("createPage (full form)", () => {
  for (const { name, input } of [
    {
      name: "custom theme",
      input: { customTheme: { dark: { "--primary": "pink" } } },
    },
    {
      name: "nondefault locale",
      input: { defaultLocale: "fr" },
    },
    {
      name: "additional locale",
      input: { locales: ["en", "fr"] },
    },
    {
      name: "custom domain",
      input: { customDomain: `${TEST_PREFIX}-denied.example.com` },
    },
  ] satisfies {
    name: string;
    input: Partial<CreatePageInput>;
  }[]) {
    test(`rejects ${name} without the feature entitlement`, async () => {
      await withTestTransaction(async (tx) => {
        const slug = uniqueSlug("feature-denied");
        await expect(
          createPage({
            ctx: { ...freeCtx, db: tx },
            input: {
              workspaceId: freeCtx.workspace.id,
              title: "Restricted feature",
              description: "",
              slug,
              ...input,
            },
          }),
        ).rejects.toMatchObject({ code: "LIMIT_EXCEEDED", max: 0 });
        expect(
          await tx
            .select()
            .from(pageTable)
            .where(eq(pageTable.slug, slug))
            .get(),
        ).toBeUndefined();
      });
    });
  }

  for (const [index, input] of (
    [
      {},
      { customTheme: null, locales: null },
      {
        customTheme: { light: {}, dark: {} },
        defaultLocale: "en",
        locales: ["en"],
      },
    ] satisfies Partial<CreatePageInput>[]
  ).entries()) {
    test(`allows default features without entitlements (${index})`, async () => {
      await withTestTransaction(async (tx) => {
        const row = await createPage({
          ctx: { ...freeCtx, db: tx },
          input: {
            workspaceId: freeCtx.workspace.id,
            title: "Default features",
            description: "",
            slug: uniqueSlug("feature-defaults"),
            ...input,
          },
        });
        expect(row.customTheme).toBeNull();
        expect(row.defaultLocale).toBe("en");
        expect(row.locales).toEqual(input.locales ?? null);
      });
    });
  }

  test("stores custom theme and locales with feature entitlements", async () => {
    await withTestTransaction(async (tx) => {
      const row = await createPage({
        ctx: { ...teamCtx, db: tx },
        input: {
          workspaceId: teamCtx.workspace.id,
          title: "Paid features",
          description: "",
          slug: uniqueSlug("feature-allowed"),
          customTheme: { light: { "--primary": " red " } },
          defaultLocale: "fr",
          locales: ["fr"],
        },
      });
      expect(row.customTheme).toEqual({
        light: { "--primary": "red" },
        dark: {},
      });
      expect(row.defaultLocale).toBe("fr");
      expect(row.locales).toEqual(["fr"]);
      await expectAuditRow({
        workspaceId: teamCtx.workspace.id,
        action: "page.create",
        entityType: "page",
        entityId: row.id,
        db: tx,
      });
    });
  });

  test("attaches monitors as pageComponents", async () => {
    await withTestTransaction(async (tx) => {
      const slug = uniqueSlug("full");
      const row = await createPage({
        ctx: { ...teamCtx, db: tx },
        input: {
          title: "Full Create",
          slug,
          description: "desc",
          customDomain: "",
          workspaceId: teamCtx.workspace.id,
          monitors: [{ monitorId: teamMonitorId }],
        },
      });
      const components = await tx
        .select()
        .from(pageComponent)
        .where(eq(pageComponent.pageId, row.id))
        .all();
      expect(components.map((c) => c.monitorId)).toEqual([teamMonitorId]);
    });
  });

  test("counts components on other pages and the full monitor batch", async () => {
    await withTestTransaction(async (tx) => {
      const ctx = { ...teamCtx, db: tx };
      await tx
        .update(workspace)
        .set({
          limits: JSON.stringify({
            ...teamCtx.workspace.limits,
            "status-pages": 10,
            "page-components": 2,
          }),
        })
        .where(eq(workspace.id, ctx.workspace.id));
      const existing = await newPage({
        ctx,
        input: { title: "Existing", slug: uniqueSlug("component-existing") },
      });
      await tx.insert(pageComponent).values({
        workspaceId: ctx.workspace.id,
        pageId: existing.id,
        type: "static",
        name: "Existing component",
      });
      const secondMonitor = await tx
        .insert(monitor)
        .values({
          workspaceId: ctx.workspace.id,
          active: true,
          url: "https://example.com",
          name: "Second monitor",
          method: "GET",
          periodicity: "10m",
          regions: "ams",
        })
        .returning()
        .get();
      await expect(
        createPage({
          ctx,
          input: {
            workspaceId: ctx.workspace.id,
            description: "",
            title: "Over limit",
            slug: uniqueSlug("component-batch"),
            monitors: [
              { monitorId: teamMonitorId },
              { monitorId: secondMonitor.id },
            ],
          },
        }),
      ).rejects.toMatchObject({
        code: "LIMIT_EXCEEDED",
        max: 2,
        current: 1,
      });
      const allowed = await createPage({
        ctx,
        input: {
          workspaceId: ctx.workspace.id,
          description: "",
          title: "At limit",
          slug: uniqueSlug("component-allowed"),
          monitors: [{ monitorId: teamMonitorId }],
        },
      });
      const components = await tx
        .select()
        .from(pageComponent)
        .where(eq(pageComponent.workspaceId, ctx.workspace.id))
        .all();
      expect(components.map((c) => c.pageId).sort()).toEqual(
        [existing.id, allowed.id].sort(),
      );
      await expect(
        createPage({
          ctx,
          input: {
            workspaceId: ctx.workspace.id,
            description: "",
            title: "Cap spent",
            slug: uniqueSlug("component-spent"),
            monitors: [{ monitorId: secondMonitor.id }],
          },
        }),
      ).rejects.toMatchObject({
        code: "LIMIT_EXCEEDED",
        max: 2,
        current: 2,
      });
      const empty = await createPage({
        ctx,
        input: {
          workspaceId: ctx.workspace.id,
          description: "",
          title: "No components",
          slug: uniqueSlug("component-empty"),
          monitors: [],
        },
      });
      expect(
        await tx
          .select()
          .from(pageComponent)
          .where(eq(pageComponent.pageId, empty.id))
          .all(),
      ).toEqual([]);
    });
  });

  test("rejects cross-workspace monitor", async () => {
    await withTestTransaction(async (tx) => {
      const slug = uniqueSlug("cross-ws");
      await expect(
        createPage({
          ctx: { ...freeCtx, db: tx },
          input: {
            title: "Cross",
            slug,
            description: "",
            customDomain: "",
            workspaceId: freeCtx.workspace.id,
            monitors: [{ monitorId: teamMonitorId }],
          },
        }),
      ).rejects.toBeInstanceOf(ForbiddenError);
    });
  });
});

describe("updatePageGeneral", () => {
  test("updates title + slug; rejects duplicate slug", async () => {
    await withTestTransaction(async (tx) => {
      const ctx = { ...teamCtx, db: tx };
      const a = await newPage({
        ctx,
        input: { title: "A", slug: uniqueSlug("a") },
      });
      const b = await newPage({
        ctx,
        input: { title: "B", slug: uniqueSlug("b") },
      });

      // Rename A to a new slug — fine.
      const newSlug = uniqueSlug("a-renamed");
      await updatePageGeneral({
        ctx,
        input: { id: a.id, title: "A Renamed", slug: newSlug },
      });

      // Rename A to B's slug — conflict.
      await expect(
        updatePageGeneral({
          ctx,
          input: { id: a.id, title: "A", slug: b.slug },
        }),
      ).rejects.toBeInstanceOf(ConflictError);
    });
  });

  test("cross-workspace → NotFoundError", async () => {
    await withTestTransaction(async (tx) => {
      const p = await newPage({
        ctx: { ...teamCtx, db: tx },
        input: { title: "Team", slug: uniqueSlug("ws-iso") },
      });
      await expect(
        updatePageGeneral({
          ctx: { ...freeCtx, db: tx },
          input: { id: p.id, title: "Hacked", slug: uniqueSlug("hack") },
        }),
      ).rejects.toBeInstanceOf(NotFoundError);
    });
  });

  test("rejects read-only actor", async () => {
    await withTestTransaction(async (tx) => {
      const p = await newPage({
        ctx: { ...teamCtx, db: tx },
        input: { title: "Team", slug: uniqueSlug("read-only") },
      });
      const readOnlyCtx = {
        ...makeApiKeyCtx(teamCtx.workspace, {
          keyId: "k-read",
          userId: 1,
          scopes: ["read"],
        }),
        db: tx,
      };
      await expect(
        updatePageGeneral({
          ctx: readOnlyCtx,
          input: {
            id: p.id,
            title: "Read-only Update",
            slug: uniqueSlug("read-only-update"),
          },
        }),
      ).rejects.toBeInstanceOf(ForbiddenError);
    });
  });
});

describe("updatePageCustomDomain", () => {
  test("rejects a domain without the feature and preserves the stored domain", async () => {
    await withTestTransaction(async (tx) => {
      const ctx = { ...freeCtx, db: tx };
      const p = await newPage({
        ctx,
        input: { title: "Domain", slug: uniqueSlug("domain-denied") },
      });
      await expect(
        updatePageCustomDomain({
          ctx,
          input: { id: p.id, customDomain: "status.example.com" },
        }),
      ).rejects.toMatchObject({
        code: "LIMIT_EXCEEDED",
        max: 0,
      });
      const row = await tx
        .select()
        .from(pageTable)
        .where(eq(pageTable.id, p.id))
        .get();
      expect(row?.customDomain).toBe(p.customDomain);
    });
  });

  test("allows an enabled domain and clearing it after the feature is disabled", async () => {
    await withTestTransaction(async (tx) => {
      const ctx = { ...teamCtx, db: tx };
      const p = await newPage({
        ctx,
        input: { title: "Domain", slug: uniqueSlug("domain-clear") },
      });
      await updatePageCustomDomain({
        ctx,
        input: { id: p.id, customDomain: "status.example.com" },
      });
      const row = await tx
        .select()
        .from(pageTable)
        .where(eq(pageTable.id, p.id))
        .get();
      expect(row?.customDomain).toBe("status.example.com");
      await expectAuditRow({
        workspaceId: ctx.workspace.id,
        action: "page.update",
        entityType: "page",
        entityId: p.id,
        db: tx,
      });
      await updatePageCustomDomain({
        ctx: {
          ...ctx,
          workspace: {
            ...ctx.workspace,
            limits: { ...ctx.workspace.limits, "custom-domain": false },
          },
        },
        input: { id: p.id, customDomain: "" },
      });
      const cleared = await tx
        .select()
        .from(pageTable)
        .where(eq(pageTable.id, p.id))
        .get();
      expect(cleared?.customDomain).toBe("");
    });
  });
});

describe("custom domain uniqueness", () => {
  const uniqueDomain = (tag: string) =>
    `${uniqueSlug(tag)}.example.com`.toLowerCase();

  test("createPage rejects a domain another workspace already uses", async () => {
    await withTestTransaction(async (tx) => {
      const domain = uniqueDomain("taken-create");
      await tx
        .insert(pageTable)
        .values({
          workspaceId: freeCtx.workspace.id,
          title: "Owner",
          description: "",
          slug: uniqueSlug("domain-owner"),
          customDomain: domain,
        })
        .run();

      const slug = uniqueSlug("domain-thief");
      await expect(
        createPage({
          ctx: { ...teamCtx, db: tx },
          input: {
            workspaceId: teamCtx.workspace.id,
            title: "Thief",
            description: "",
            slug,
            customDomain: domain.toUpperCase(),
          },
        }),
      ).rejects.toBeInstanceOf(ConflictError);
      expect(
        await tx.select().from(pageTable).where(eq(pageTable.slug, slug)).get(),
      ).toBeUndefined();
    });
  });

  test("updatePageCustomDomain rejects a domain used by another page", async () => {
    await withTestTransaction(async (tx) => {
      const ctx = { ...teamCtx, db: tx };
      const domain = uniqueDomain("taken-update");
      const owner = await newPage({
        ctx,
        input: { title: "Owner", slug: uniqueSlug("domain-owner") },
      });
      await updatePageCustomDomain({
        ctx,
        input: { id: owner.id, customDomain: domain },
      });

      const other = await newPage({
        ctx,
        input: { title: "Other", slug: uniqueSlug("domain-other") },
      });
      await expect(
        updatePageCustomDomain({
          ctx,
          input: { id: other.id, customDomain: domain },
        }),
      ).rejects.toBeInstanceOf(ConflictError);
      const row = await tx
        .select()
        .from(pageTable)
        .where(eq(pageTable.id, other.id))
        .get();
      expect(row?.customDomain).toBe("");
    });
  });

  test("updatePageCustomDomain lets a page re-save its own domain", async () => {
    await withTestTransaction(async (tx) => {
      const ctx = { ...teamCtx, db: tx };
      const domain = uniqueDomain("own");
      const p = await newPage({
        ctx,
        input: { title: "Own", slug: uniqueSlug("domain-own") },
      });
      await updatePageCustomDomain({
        ctx,
        input: { id: p.id, customDomain: domain },
      });
      await updatePageCustomDomain({
        ctx,
        input: { id: p.id, customDomain: domain },
      });
      const row = await tx
        .select()
        .from(pageTable)
        .where(eq(pageTable.id, p.id))
        .get();
      expect(row?.customDomain).toBe(domain);
    });
  });
});

describe("updatePageConfiguration", () => {
  test("preserves omitted settings in partial updates", async () => {
    await withTestTransaction(async (tx) => {
      const ctx = { ...teamCtx, db: tx };
      const p = await createPage({
        ctx,
        input: {
          workspaceId: ctx.workspace.id,
          title: "Configuration",
          slug: uniqueSlug("configuration"),
          description: "",
          customDomain: "",
          configuration: {
            type: "manual",
            value: "manual",
            uptime: false,
            theme: "default-rounded",
            days: 30,
          },
        },
      });

      for (const configuration of [
        { days: 45 },
        {},
        { theme: undefined },
        null,
        undefined,
      ]) {
        await updatePageConfiguration({
          ctx,
          input: UpdatePageConfigurationInput.parse({
            id: p.id,
            configuration,
          }),
        });
        const row = await tx
          .select()
          .from(pageTable)
          .where(eq(pageTable.id, p.id))
          .get();
        expect(row?.configuration).toEqual({
          type: "manual",
          value: "manual",
          uptime: false,
          theme: "default-rounded",
          days: 45,
        });
      }
      await expectAuditRow({
        workspaceId: ctx.workspace.id,
        action: "page.update",
        entityType: "page",
        entityId: p.id,
        db: tx,
      });
    });
  });
});

describe("updatePageLocales", () => {
  test("rejects when plan lacks i18n", async () => {
    await withTestTransaction(async (tx) => {
      const ctx = { ...freeCtx, db: tx };
      // free plan has i18n: false
      const p = await newPage({
        ctx,
        input: { title: "Free", slug: uniqueSlug("free") },
      });
      await expect(
        updatePageLocales({
          ctx,
          input: { id: p.id, defaultLocale: "en", locales: ["en"] },
        }),
      ).rejects.toBeInstanceOf(LimitExceededError);
    });
  });
});

describe("updatePageCustomTheme", () => {
  test("happy path stores sanitized vars + audit", async () => {
    await withTestTransaction(async (tx) => {
      const ctx = { ...teamCtx, db: tx };
      const p = await newPage({
        ctx,
        input: { title: "Theme", slug: uniqueSlug("theme") },
      });
      await updatePageCustomTheme({
        ctx,
        input: {
          id: p.id,
          customTheme: {
            light: { "--primary": " red " },
            dark: { "--primary": "pink" },
          },
        },
      });
      const row = await tx
        .select()
        .from(pageTable)
        .where(eq(pageTable.id, p.id))
        .get();
      expect(row?.customTheme).toEqual({
        light: { "--primary": "red" },
        dark: { "--primary": "pink" },
      });
      await expectAuditRow({
        workspaceId: teamCtx.workspace.id,
        action: "page.update",
        entityType: "page",
        entityId: p.id,
        db: tx,
      });
    });
  });

  test("rejects unknown css variables", async () => {
    await withTestTransaction(async (tx) => {
      const ctx = { ...teamCtx, db: tx };
      await expect(
        updatePageCustomTheme({
          ctx,
          input: { id: 1, customTheme: { light: { "--nope": "red" } } },
        }),
      ).rejects.toThrow("Unknown CSS variable");
    });
  });

  test("rejects values with style-tag breakout characters", async () => {
    await withTestTransaction(async (tx) => {
      const ctx = { ...teamCtx, db: tx };
      await expect(
        updatePageCustomTheme({
          ctx,
          input: {
            id: 1,
            customTheme: { light: { "--primary": "red;} </style>" } },
          },
        }),
      ).rejects.toThrow("unsupported characters");
    });
  });

  test("empty custom theme clears the column", async () => {
    await withTestTransaction(async (tx) => {
      const ctx = { ...teamCtx, db: tx };
      const p = await newPage({
        ctx,
        input: { title: "Theme Clear", slug: uniqueSlug("theme-clear") },
      });
      await updatePageCustomTheme({
        ctx,
        input: { id: p.id, customTheme: { dark: { "--primary": "pink" } } },
      });
      await updatePageCustomTheme({
        ctx,
        input: { id: p.id, customTheme: { light: {}, dark: {} } },
      });
      const row = await tx
        .select()
        .from(pageTable)
        .where(eq(pageTable.id, p.id))
        .get();
      expect(row?.customTheme).toBeNull();
    });
  });

  test("rejects when plan lacks custom-theme", async () => {
    await withTestTransaction(async (tx) => {
      const ctx = { ...freeCtx, db: tx };
      // No page needed: the `custom-theme` limit check fires before the
      // page lookup (mirrors the read-only-actor case below). Creating a
      // page here would depend on the shared free workspace's status-pages
      // quota, which a parallel suite can exhaust → flaky LimitExceededError
      // on the wrong assertion.
      await expect(
        updatePageCustomTheme({
          ctx,
          input: { id: 1, customTheme: { light: { "--primary": "red" } } },
        }),
      ).rejects.toBeInstanceOf(LimitExceededError);
    });
  });

  test("rejects read-only actor", async () => {
    await withTestTransaction(async (tx) => {
      const readOnlyCtx = {
        ...makeApiKeyCtx(teamCtx.workspace, {
          keyId: "k-read",
          userId: 1,
          scopes: ["read"],
        }),
        db: tx,
      };
      await expect(
        updatePageCustomTheme({
          ctx: readOnlyCtx,
          input: { id: 1, customTheme: { light: { "--primary": "red" } } },
        }),
      ).rejects.toBeInstanceOf(ForbiddenError);
    });
  });
});

describe("list / get / getSlugAvailable", () => {
  test("list respects workspace isolation", async () => {
    await withTestTransaction(async (tx) => {
      const teamCtxTx = { ...teamCtx, db: tx };
      const freeCtxTx = { ...freeCtx, db: tx };
      const p = await newPage({
        ctx: teamCtxTx,
        input: { title: "List Test", slug: uniqueSlug("list") },
      });

      const teamItems = await listPages({
        ctx: teamCtxTx,
        input: { order: "desc" },
      });
      expect(teamItems.find((x) => x.id === p.id)).toBeDefined();

      const freeItems = await listPages({
        ctx: freeCtxTx,
        input: { order: "desc" },
      });
      expect(freeItems.find((x) => x.id === p.id)).toBeUndefined();
    });
  });

  test("get cross-workspace → NotFoundError", async () => {
    await withTestTransaction(async (tx) => {
      const p = await newPage({
        ctx: { ...teamCtx, db: tx },
        input: { title: "Get", slug: uniqueSlug("get") },
      });
      await expect(
        getPage({ ctx: { ...freeCtx, db: tx }, input: { id: p.id } }),
      ).rejects.toBeInstanceOf(NotFoundError);
    });
  });

  test("getSlugAvailable handles reserved + taken", async () => {
    await withTestTransaction(async (tx) => {
      const ctx = { ...teamCtx, db: tx };
      const p = await newPage({
        ctx,
        input: { title: "Slug", slug: uniqueSlug("slug") },
      });

      expect(await getSlugAvailable({ ctx, input: { slug: p.slug } })).toBe(
        false,
      );
      expect(await getSlugAvailable({ ctx, input: { slug: "api" } })).toBe(
        false,
      );
      expect(
        await getSlugAvailable({
          ctx,
          input: { slug: uniqueSlug("free-slug") },
        }),
      ).toBe(true);
    });
  });
});

describe("listPages hasActiveStatusReport", () => {
  const addReport = async (
    tx: DrizzleTx,
    pageId: number,
    workspaceId: number,
    status: "investigating" | "resolved",
  ) =>
    tx.insert(statusReport).values({
      workspaceId,
      pageId,
      status,
      title: `${TEST_PREFIX}-report-${status}`,
    });

  const flagFor = async (ctx: ServiceContext, pageId: number) => {
    const items = await listPages({ ctx, input: { order: "desc" } });
    return items.find((p) => p.id === pageId)?.hasActiveStatusReport;
  };

  test("false when the page has no status reports", async () => {
    await withTestTransaction(async (tx) => {
      const ctx = { ...teamCtx, db: tx };
      const p = await newPage({
        ctx,
        input: { title: "No reports", slug: uniqueSlug("flag-none") },
      });
      expect(await flagFor(ctx, p.id)).toBe(false);
    });
  });

  test("true when the page has a non-resolved report", async () => {
    await withTestTransaction(async (tx) => {
      const ctx = { ...teamCtx, db: tx };
      const p = await newPage({
        ctx,
        input: { title: "Active", slug: uniqueSlug("flag-active") },
      });
      await addReport(tx, p.id, teamCtx.workspace.id, "investigating");
      expect(await flagFor(ctx, p.id)).toBe(true);
    });
  });

  test("false once every report on the page is resolved", async () => {
    await withTestTransaction(async (tx) => {
      const ctx = { ...teamCtx, db: tx };
      const p = await newPage({
        ctx,
        input: { title: "Resolved", slug: uniqueSlug("flag-resolved") },
      });
      await addReport(tx, p.id, teamCtx.workspace.id, "resolved");
      await addReport(tx, p.id, teamCtx.workspace.id, "resolved");
      expect(await flagFor(ctx, p.id)).toBe(false);
    });
  });

  test("an active report on one page does not flag its siblings", async () => {
    await withTestTransaction(async (tx) => {
      const ctx = { ...teamCtx, db: tx };
      const noisy = await newPage({
        ctx,
        input: { title: "Noisy", slug: uniqueSlug("flag-noisy") },
      });
      const quiet = await newPage({
        ctx,
        input: { title: "Quiet", slug: uniqueSlug("flag-quiet") },
      });
      await addReport(tx, noisy.id, teamCtx.workspace.id, "investigating");

      expect(await flagFor(ctx, noisy.id)).toBe(true);
      expect(await flagFor(ctx, quiet.id)).toBe(false);
    });
  });

  test("the probe is workspace-scoped", async () => {
    await withTestTransaction(async (tx) => {
      const teamCtxTx = { ...teamCtx, db: tx };
      const freeCtxTx = { ...freeCtx, db: tx };
      const foreign = await newPage({
        ctx: freeCtxTx,
        input: { title: "Foreign", slug: uniqueSlug("flag-foreign") },
      });
      // Active report owned by the *other* workspace, pointing at its page.
      await addReport(tx, foreign.id, freeCtx.workspace.id, "investigating");

      const teamItems = await listPages({
        ctx: teamCtxTx,
        input: { order: "desc" },
      });
      expect(teamItems.some((p) => p.hasActiveStatusReport)).toBe(false);
      expect(await flagFor(freeCtxTx, foreign.id)).toBe(true);
    });
  });
});

describe("getPageBySlug", () => {
  test("returns the row when slug exists", async () => {
    await withTestTransaction(async (tx) => {
      const p = await newPage({
        ctx: { ...teamCtx, db: tx },
        input: { title: "BySlug", slug: uniqueSlug("by-slug") },
      });

      const row = await getPageBySlug({ input: { slug: p.slug } }, { db: tx });
      expect(row?.id).toBe(p.id);
      expect(row?.slug).toBe(p.slug);
    });
  });

  test("normalizes slug casing", async () => {
    await withTestTransaction(async (tx) => {
      const slug = uniqueSlug("case");
      const p = await newPage({
        ctx: { ...teamCtx, db: tx },
        input: { title: "Case", slug },
      });

      const upper = await getPageBySlug(
        { input: { slug: slug.toUpperCase() } },
        { db: tx },
      );
      expect(upper?.id).toBe(p.id);
    });
  });

  test("returns undefined when slug is missing", async () => {
    await withTestTransaction(async (tx) => {
      const row = await getPageBySlug(
        { input: { slug: `${TEST_PREFIX}-missing-${Date.now()}` } },
        { db: tx },
      );
      expect(row).toBeUndefined();
    });
  });

  test("ignores workspace scope (cross-workspace lookup)", async () => {
    await withTestTransaction(async (tx) => {
      // Page lives in the team workspace; querying without any ctx still
      // resolves it — this is the contract that lets the public status-page
      // render path resolve a slug for unauthenticated visitors.
      const p = await newPage({
        ctx: { ...teamCtx, db: tx },
        input: { title: "Cross", slug: uniqueSlug("cross") },
      });

      const row = await getPageBySlug({ input: { slug: p.slug } }, { db: tx });
      expect(row?.workspaceId).toBe(teamCtx.workspace.id);
      // (No `ctx` passed → confirms the helper does not rely on
      // workspace scoping; `db: tx` only routes the read through our
      // wrapping test tx so it sees the row created above.)
    });
  });
});

describe("deletePage", () => {
  test("cross-workspace → NotFoundError", async () => {
    await withTestTransaction(async (tx) => {
      const p = await newPage({
        ctx: { ...teamCtx, db: tx },
        input: { title: "Delete", slug: uniqueSlug("del") },
      });
      await expect(
        deletePage({ ctx: { ...freeCtx, db: tx }, input: { id: p.id } }),
      ).rejects.toBeInstanceOf(NotFoundError);
    });
  });
});

describe("status-page count quota", () => {
  test("createPage rejects once the plan's page cap is spent", async () => {
    await withTestTransaction(async (tx) => {
      // Free plan caps status pages at 1.
      const ctx = { ...freeCtx, db: tx };
      await newPage({
        ctx,
        input: { title: "Quota 1", slug: uniqueSlug("quota-1") },
      });

      await expect(
        createPage({
          ctx,
          input: {
            title: "Quota 2",
            description: "",
            slug: uniqueSlug("quota-2"),
            customDomain: "",
            workspaceId: ctx.workspace.id,
            monitors: [],
          },
        }),
      ).rejects.toBeInstanceOf(LimitExceededError);
    });
  });

  test("newPage rejects once the cap is spent", async () => {
    await withTestTransaction(async (tx) => {
      const ctx = { ...freeCtx, db: tx };
      await newPage({
        ctx,
        input: { title: "Quota 3", slug: uniqueSlug("quota-3") },
      });

      await expect(
        newPage({
          ctx,
          input: { title: "Quota 4", slug: uniqueSlug("quota-4") },
        }),
      ).rejects.toBeInstanceOf(LimitExceededError);
    });
  });
});