67 lines
3.1 KiB
TypeScript
67 lines
3.1 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
import { extractBusyIntervals, mergeBusyIntervals, overlapsBusy, type BusyCandidate, type BusyInterval } from "./availability";
|
|
|
|
const d = (iso: string): Date => new Date(iso);
|
|
|
|
describe("extractBusyIntervals (leak-protection, DoD 3)", () => {
|
|
const base: Omit<BusyCandidate, "startsAt" | "endsAt"> = { isBusyForAvailability: true, archivedAt: null };
|
|
|
|
it("returns only anonymous {from,to} — never any content field", () => {
|
|
const events: BusyCandidate[] = [{ ...base, startsAt: d("2026-07-01T09:00:00Z"), endsAt: d("2026-07-01T10:00:00Z") }];
|
|
const result = extractBusyIntervals(events);
|
|
expect(result).toEqual([{ from: d("2026-07-01T09:00:00Z"), to: d("2026-07-01T10:00:00Z") }]);
|
|
// Structural leak check: the only keys are from/to.
|
|
expect(Object.keys(result[0]).sort()).toEqual(["from", "to"]);
|
|
});
|
|
|
|
it("drops non-busy (Free), archived, and zero/negative-length events", () => {
|
|
const events: BusyCandidate[] = [
|
|
{ ...base, isBusyForAvailability: false, startsAt: d("2026-07-01T09:00:00Z"), endsAt: d("2026-07-01T10:00:00Z") },
|
|
{ ...base, archivedAt: d("2026-07-01T08:00:00Z"), startsAt: d("2026-07-01T11:00:00Z"), endsAt: d("2026-07-01T12:00:00Z") },
|
|
{ ...base, startsAt: d("2026-07-01T13:00:00Z"), endsAt: d("2026-07-01T13:00:00Z") },
|
|
];
|
|
expect(extractBusyIntervals(events)).toEqual([]);
|
|
});
|
|
|
|
it("sorts ascending by start", () => {
|
|
const events: BusyCandidate[] = [
|
|
{ ...base, startsAt: d("2026-07-01T15:00:00Z"), endsAt: d("2026-07-01T16:00:00Z") },
|
|
{ ...base, startsAt: d("2026-07-01T09:00:00Z"), endsAt: d("2026-07-01T10:00:00Z") },
|
|
];
|
|
expect(extractBusyIntervals(events).map((i) => i.from.toISOString())).toEqual(["2026-07-01T09:00:00.000Z", "2026-07-01T15:00:00.000Z"]);
|
|
});
|
|
});
|
|
|
|
describe("mergeBusyIntervals", () => {
|
|
it("merges overlapping and touching intervals", () => {
|
|
const intervals: BusyInterval[] = [
|
|
{ from: d("2026-07-01T09:00:00Z"), to: d("2026-07-01T10:00:00Z") },
|
|
{ from: d("2026-07-01T10:00:00Z"), to: d("2026-07-01T11:00:00Z") },
|
|
{ from: d("2026-07-01T10:30:00Z"), to: d("2026-07-01T12:00:00Z") },
|
|
];
|
|
expect(mergeBusyIntervals(intervals)).toEqual([{ from: d("2026-07-01T09:00:00Z"), to: d("2026-07-01T12:00:00Z") }]);
|
|
});
|
|
|
|
it("keeps disjoint intervals separate", () => {
|
|
const intervals: BusyInterval[] = [
|
|
{ from: d("2026-07-01T09:00:00Z"), to: d("2026-07-01T10:00:00Z") },
|
|
{ from: d("2026-07-01T11:00:00Z"), to: d("2026-07-01T12:00:00Z") },
|
|
];
|
|
expect(mergeBusyIntervals(intervals)).toHaveLength(2);
|
|
});
|
|
|
|
it("returns [] for empty input", () => {
|
|
expect(mergeBusyIntervals([])).toEqual([]);
|
|
});
|
|
});
|
|
|
|
describe("overlapsBusy", () => {
|
|
const intervals: BusyInterval[] = [{ from: d("2026-07-01T09:00:00Z"), to: d("2026-07-01T10:00:00Z") }];
|
|
it("detects an overlapping slot", () => {
|
|
expect(overlapsBusy(d("2026-07-01T09:30:00Z"), d("2026-07-01T10:30:00Z"), intervals)).toBe(true);
|
|
});
|
|
it("treats back-to-back as free (half-open)", () => {
|
|
expect(overlapsBusy(d("2026-07-01T10:00:00Z"), d("2026-07-01T11:00:00Z"), intervals)).toBe(false);
|
|
});
|
|
});
|