import { expect, test } from "bun:test";
import { cardCalendar, cardStats } from "./progress-card";
import type { ShareData } from "../sharing/contracts";
const habit = (days: ShareData["habits"][number]["days"]): ShareData["habits"][number] => ({ name: "Reading", color: "#90647e", completed: days.filter(d => d.complete).length, scheduled: days.filter(d => d.due).length, days, legend: { empty: "#eee", partial: ["#ccc"], complete: "#90647e" } });
const days = [
  { date: "2024-02-28", due: true, complete: true, ratio: 1, color: "#90647e" },
  { date: "2024-02-29", due: true, complete: false, ratio: .5, color: "#ccc" },
  { date: "2024-03-01", due: false, complete: false, ratio: null, color: "#fff" },
  { date: "2024-03-02", due: true, complete: false, ratio: 0, color: "#eee" },
  { date: "2024-03-03", due: true, complete: true, ratio: 1, color: "#90647e" },
];
test("share calendar preserves dashboard week positions, leap days, shades and days off", () => {
  const chart = cardCalendar(habit(days));
  expect(chart.entries.map(d => [d.column, d.row])).toEqual([[1, 4], [1, 5], [1, 6], [1, 7], [2, 1]]);
  expect(chart.entries.map(d => d.day.color)).toEqual(days.map(d => d.color));
  expect(chart.entries[2]!.day.state).toBe("not-due");
  expect(chart.entries[1]!.day.value).toBe(.5);
});
test("period summary counts the whole selected range, not the last day's completion", () => {
  const data: ShareData = { from: days[0]!.date, to: days.at(-1)!.date, timezone: "UTC", habits: [habit(days)] };
  expect(cardStats(data)).toEqual({ completed: 2, scheduled: 4, percent: 50 });
  expect(cardStats({ ...data, from: data.to, habits: [habit(days.slice(-1))] })).toEqual({ completed: 1, scheduled: 1, percent: 100 });
  expect(cardStats({ ...data, habits: [habit([days[2]!])] }).percent).toBeNull();
});
