Skip to content

A local-first multi-agent app development platform, built on PenguinHarness

10.8 KB
Raw Blame
/**
 * A company's finance page, as pure data (features/company/finance-tree.ts).
 *
 * - The spend tree walks the reporting line depth first; an unknown manager starts a tree of
 *   its own. The ticket ledger nests children under parents the same way.
 * - The ledger shows the roots alone until a parent is expanded, one level per expanded
 *   parent, never below a folded ancestor, and a childless row has nothing to fold.
 * - Periods move by whole months across years; other shapes are refused.
 * - A budget reads success under 80%, attention from 80%, danger from 100%, muted without one.
 * - Each day becomes a trend point on its date carrying its cost; the axis breaks after a
 *   skipped calendar day (across months too) and never around an unparsable date.
 * - The KPI row measures the total against the root's budget (leaving budget and ratio out
 *   without one) and counts budgets, warnings and pauses.
 * - Alerts group into paused and warned, a paused alert under paused only, newest first.
 * - The row tooltips name every figure the tables dropped a column for, with stand-ins for a
 *   missing budget or owner.
 * - The state column leads with the live state and adds what the budget did, says paused
 *   once however it is spelled, and stands on the budget alone without a live state.
 */
import { describe, expect, it } from "vitest";
import type { OrgFinanceEmployee, OrgFinanceTicket } from "@adelie/server/api";
import {
  budgetTone,
  dailyBreaks,
  financeKpis,
  financeSeries,
  groupAlerts,
  shiftPeriod,
  spendRowTooltip,
  spendStateMarks,
  spendTreeRows,
  ticketRowTooltip,
  ticketTreeRows,
  visibleLedgerRows,
} from "../src/features/company/finance-tree";

const employee = (agentId: string, reportsTo: string | null): OrgFinanceEmployee => ({
  agentId,
  name: agentId,
  title: "",
  reportsTo,
  own: 1,
  cumulative: 1,
  warned: false,
  paused: false,
});

const ticket = (ticketId: string, parent?: string): OrgFinanceTicket => ({
  ticketId,
  title: ticketId,
  status: "done",
  ...(parent !== undefined ? { parent } : {}),
  cost: 1,
  rolledUp: 1,
});

describe("spendTreeRows", () => {
  it("walks the reporting line depth-first with depths, an unknown manager starting a tree of its own", () => {
    const rows = spendTreeRows([
      employee("dev", "cto"),
      employee("ceo", null),
      employee("cto", "ceo"),
      employee("lost", "gone"),
    ]);
    expect(rows.map((r) => [r.employee.agentId, r.depth])).toEqual([
      ["ceo", 0],
      ["cto", 1],
      ["dev", 2],
      ["lost", 0],
    ]);
  });
});

describe("ticketTreeRows", () => {
  it("nests children under their parent and starts at the top for an unknown parent", () => {
    const rows = ticketTreeRows([ticket("b", "a"), ticket("a"), ticket("c", "zzz")]);
    expect(rows.map((r) => [r.ticket.ticketId, r.depth])).toEqual([
      ["a", 0],
      ["b", 1],
      ["c", 0],
    ]);
  });
});

describe("visibleLedgerRows", () => {
  /** a > b > c, a > d, and the root e with nothing under it. */
  const tree = () =>
    ticketTreeRows([
      ticket("a"),
      ticket("b", "a"),
      ticket("c", "b"),
      ticket("d", "a"),
      ticket("e"),
    ]);

  it("shows the roots alone when nothing is expanded, each counting only its direct children", () => {
    expect(
      visibleLedgerRows(tree(), new Set()).map((r) => [r.ticket.ticketId, r.children]),
    ).toEqual([
      ["a", 2],
      ["e", 0],
    ]);
  });

  it("opens one level per expanded parent", () => {
    expect(visibleLedgerRows(tree(), new Set(["a"])).map((r) => r.ticket.ticketId)).toEqual([
      "a",
      "b",
      "d",
      "e",
    ]);
    expect(
      visibleLedgerRows(tree(), new Set(["a", "b"])).map((r) => [r.ticket.ticketId, r.depth]),
    ).toEqual([
      ["a", 0],
      ["b", 1],
      ["c", 2],
      ["d", 1],
      ["e", 0],
    ]);
  });

  it("keeps a subtree hidden while an ancestor above the expanded parent is folded", () => {
    expect(visibleLedgerRows(tree(), new Set(["b"])).map((r) => r.ticket.ticketId)).toEqual([
      "a",
      "e",
    ]);
  });

  it("leaves a childless row with no children to count and nothing to fold", () => {
    const rows = visibleLedgerRows(ticketTreeRows([ticket("only")]), new Set());
    expect(rows).toEqual([{ ticket: ticket("only"), depth: 0, children: 0 }]);
  });
});

describe("shiftPeriod", () => {
  it("moves whole months across year boundaries and rejects other shapes", () => {
    expect(shiftPeriod("2026-09", -1)).toBe("2026-08");
    expect(shiftPeriod("2026-01", -1)).toBe("2025-12");
    expect(shiftPeriod("2026-12", 1)).toBe("2027-01");
    expect(shiftPeriod("2026-13", 1)).toBeNull();
    expect(shiftPeriod("nope", 1)).toBeNull();
  });
});

describe("budgetTone", () => {
  it("is success under 80%, attention from 80%, danger from 100%, muted without a budget", () => {
    expect(budgetTone(0.5)).toBe("success");
    expect(budgetTone(0.8)).toBe("attention");
    expect(budgetTone(1)).toBe("danger");
    expect(budgetTone(undefined)).toBe("muted");
    expect(budgetTone(Number.NaN)).toBe("muted");
  });
});

describe("financeSeries", () => {
  it("makes each day a trend point on its date carrying its cost", () => {
    const points = financeSeries([
      { date: "2026-09-01", cost: 2.5 },
      { date: "2026-09-02", cost: 0 },
    ]);
    expect(points.map((p) => [p.bucket, p.cost])).toEqual([
      ["2026-09-01", 2.5],
      ["2026-09-02", 0],
    ]);
  });
});

describe("dailyBreaks", () => {
  it("marks the point after which a calendar day was skipped, across a month boundary too", () => {
    const daily = [
      { date: "2026-08-30" },
      { date: "2026-08-31" },
      { date: "2026-09-01" },
      { date: "2026-09-03" },
      { date: "2026-09-10" },
    ];
    expect(dailyBreaks(daily)).toEqual([2, 3]);
    expect(dailyBreaks([{ date: "2026-09-02" }])).toEqual([]);
    expect(dailyBreaks([])).toEqual([]);
  });

  it("ignores an unparsable date rather than breaking around it", () => {
    expect(dailyBreaks([{ date: "2026-09-01" }, { date: "nope" }, { date: "2026-09-05" }])).toEqual(
      [],
    );
  });
});

describe("financeKpis", () => {
  const withBudget = (
    agentId: string,
    reportsTo: string | null,
    extra: Partial<OrgFinanceEmployee>,
  ): OrgFinanceEmployee => ({ ...employee(agentId, reportsTo), ...extra });

  it("measures the total against the root's budget and counts budgets, warnings and pauses", () => {
    const kpis = financeKpis({
      total: 21,
      employees: [
        withBudget("cto", "ceo", { budget: 4, warned: true, paused: true }),
        withBudget("ceo", null, { budget: 10, warned: true }),
        withBudget("dev", "cto", {}),
        withBudget("growth", "ceo", { budget: 3, warned: true }),
      ],
    });
    expect(kpis).toEqual({
      total: 21,
      budget: 10,
      ratio: 2.1,
      employees: 4,
      budgeted: 3,
      warned: 2,
      paused: 1,
    });
  });

  it("leaves budget and ratio out when the root has no budget", () => {
    const kpis = financeKpis({ total: 5, employees: [employee("ceo", null)] });
    expect(kpis).toEqual({ total: 5, employees: 1, budgeted: 0, warned: 0, paused: 0 });
    expect("budget" in kpis).toBe(false);
  });
});

describe("groupAlerts", () => {
  it("lists a paused alert under paused only, newest first in each group", () => {
    const groups = groupAlerts([
      { agentId: "a", period: "2026-09", warnedAt: "2026-09-01T00:00:00Z" },
      {
        agentId: "b",
        period: "2026-09",
        warnedAt: "2026-09-01T00:00:00Z",
        pausedAt: "2026-09-02T00:00:00Z",
      },
      { agentId: "c", period: "2026-09", warnedAt: "2026-09-03T00:00:00Z" },
      { agentId: "d", period: "2026-09", pausedAt: "2026-09-04T00:00:00Z" },
      { agentId: "e", period: "2026-09" },
    ]);
    expect(groups.paused.map((a) => a.agentId)).toEqual(["d", "b"]);
    expect(groups.warned.map((a) => a.agentId)).toEqual(["c", "a"]);
  });
});

describe("spendRowTooltip", () => {
  const labels = {
    own: "Own spend",
    cumulative: "Cumulative",
    budget: "Budget",
    noBudget: "Unbounded",
  };
  const money = (value: number) => `$${value.toFixed(2)}`;

  it("names all three figures, the budget among them", () => {
    expect(spendRowTooltip({ own: 12, cumulative: 41, budget: 100 }, labels, money)).toBe(
      "Own spend $12.00 · Cumulative $41.00 · Budget $100.00",
    );
  });

  it("says a missing budget is unbounded rather than dropping the field", () => {
    expect(spendRowTooltip({ own: 12, cumulative: 41 }, labels, money)).toBe(
      "Own spend $12.00 · Cumulative $41.00 · Budget Unbounded",
    );
  });
});

describe("ticketRowTooltip", () => {
  const labels = {
    owner: "Owner",
    noOwner: "Unassigned",
    cost: "Cost",
    rolledUp: "Rolled-up cost",
  };
  const money = (value: number) => `$${value.toFixed(2)}`;

  it("leads with the id and carries the owner and both costs", () => {
    expect(
      ticketRowTooltip({ ticketId: "TCK-1", cost: 3.2, rolledUp: 8.7 }, "Ada", labels, money),
    ).toBe("TCK-1 · Owner Ada · Cost $3.20 · Rolled-up cost $8.70");
  });

  it("keeps the owner field with a stand-in when nobody owns the ticket", () => {
    expect(
      ticketRowTooltip({ ticketId: "TCK-2", cost: 0, rolledUp: 0 }, undefined, labels, money),
    ).toBe("TCK-2 · Owner Unassigned · Cost $0.00 · Rolled-up cost $0.00");
  });
});

describe("spendStateMarks", () => {
  const calm = { warned: false, paused: false };

  it("leads with the live state and adds what the budget did", () => {
    expect(spendStateMarks(calm, "running")).toEqual([{ tone: "busy", key: "running" }]);
    expect(spendStateMarks(calm, "idle")).toEqual([{ tone: "success", key: "idle" }]);
    expect(spendStateMarks({ warned: true, paused: false }, "running")).toEqual([
      { tone: "busy", key: "running" },
      { tone: "attention", key: "warned" },
    ]);
  });

  it("says paused once, however the two sources spell it", () => {
    // A budget pause is the reason the live state reads `paused`: one mark, not two.
    const paused = [{ tone: "danger", key: "paused" }];
    expect(spendStateMarks({ warned: true, paused: true }, "paused")).toEqual(paused);
    expect(spendStateMarks({ warned: false, paused: true }, "idle")).toEqual([
      { tone: "success", key: "idle" },
      { tone: "danger", key: "paused" },
    ]);
    // The chart knows before the period's figures do, and the row still says why.
    expect(spendStateMarks(calm, "paused")).toEqual(paused);
  });

  it("stands on the budget alone while the live state is unknown", () => {
    // The chart is a best-effort join: a row must still read without it.
    expect(spendStateMarks(calm, undefined)).toEqual([]);
    expect(spendStateMarks({ warned: true, paused: false }, undefined)).toEqual([
      { tone: "attention", key: "warned" },
    ]);
    expect(spendStateMarks({ warned: false, paused: true }, undefined)).toEqual([
      { tone: "danger", key: "paused" },
    ]);
  });
});