Skip to article
Documentation

src/lib/db/inbox-query.test.ts

Linked repository file from this build.

import { expect, test } from "bun:test";
import { awaitingStates, inboxQuerySchema } from "./inbox-query";

test("inbox pagination bounds each independent page and accepts browser query strings", () => {
  const query = inboxQuerySchema.parse({
    pageSize: "8",
    newPage: "2",
    touchedPage: "3",
    queuePage: "4",
    eventsPage: "5",
    section: "touched",
  });
  expect(query).toMatchObject({
    pageSize: 8,
    newPage: 2,
    touchedPage: 3,
    queuePage: 4,
    eventsPage: 5,
    section: "touched",
  });
  expect(inboxQuerySchema.parse({})).toMatchObject({
    pageSize: 20,
    newPage: 0,
    touchedPage: 0,
    queuePage: 0,
    eventsPage: 0,
    section: "all",
  });
  for (const input of [
    { pageSize: 0 },
    { pageSize: 101 },
    { newPage: -1 },
    { touchedPage: 1.5 },
    { eventsPage: "NaN" },
    { queuePage: 1_000_001 },
    { section: "everything" },
  ]) {
    expect(inboxQuerySchema.safeParse(input).success).toBe(false);
  }
});

test("news windows normalize offsets and reject invalid or reversed timestamps", () => {
  expect(inboxQuerySchema.parse({ since: "2026-09-08T08:00:00-04:00" }).since).toBe(
    "2026-09-08T12:00:00.000Z",
  );
  expect(inboxQuerySchema.safeParse({ since: "yesterday" }).success).toBe(false);
  expect(
    inboxQuerySchema.safeParse({ since: "2026-09-09T00:00:00Z", asOf: "2026-09-08T00:00:00Z" })
      .success,
  ).toBe(false);
});

test("each role gets its correct fourth task list", () => {
  for (const role of ["examiner", "admin", "statistics"] as const)
    expect(awaitingStates(role)).toEqual([
      "awaiting_review",
      "awaiting_pec_approval",
      "awaiting_manager",
      "awaiting_cao_response",
    ]);
  expect(awaitingStates("supervisor")).toEqual(["awaiting_review"]);
  expect(awaitingStates("pec")).toEqual(["awaiting_pec_approval"]);
  expect(awaitingStates("program_manager")).toEqual([
    "awaiting_review",
    "awaiting_manager",
    "awaiting_cao_response",
  ]);
});