Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
import { describe, expect, it } from "vitest";
import { AttributeRow, BranchRow } from "../../../services/load_results";
import { buildNote } from "../../../test/easy-froca";
import { formatDateToLocalISO, formatTimeToLocalISO, getMonthsInDateRange, getTimedDurationMinutes, isAttributeChangeAffecting, isBranchChangeAffecting, offsetDate, parseStartEndDateFromEvent, parseStartEndTimeFromEvent } from "./utils";
/**
* A moment built in the machine's own zone, which is what the calendar hands these functions: a
* click on the grid names a wall-clock time, not an instant in UTC. Built this way the expectations
* below hold in any zone the suite happens to run in — the functions answer with the local reading,
* and that is the reading these arguments were written in.
*/
function localDate(year: number, month: number, day: number, hours = 0, minutes = 0) {
return new Date(year, month - 1, day, hours, minutes);
}
/** What the calendar hands over for a selection or an event: the two ends and whether it is all-day. */
function span(start: Date | null, end: Date | null, allDay: boolean) {
return { start, end, allDay } as unknown as Parameters<typeof parseStartEndDateFromEvent>[0];
}
describe("formatDateToLocalISO", () => {
it("says the day as it reads on the local clock, whatever the time of day", () => {
expect(formatDateToLocalISO(localDate(2026, 6, 5))).toBe("2026-06-05");
// Either side of midnight, where a UTC reading would slip to a neighbouring day in most zones.
expect(formatDateToLocalISO(localDate(2026, 6, 5, 23, 59))).toBe("2026-06-05");
expect(formatDateToLocalISO(localDate(2026, 1, 1, 0, 30))).toBe("2026-01-01");
});
it("has nothing to say for a moment that was never given", () => {
expect(formatDateToLocalISO(null)).toBeUndefined();
expect(formatDateToLocalISO(undefined)).toBeUndefined();
});
});
describe("formatTimeToLocalISO", () => {
it("says the hour and minute on the local clock, dropping the seconds", () => {
expect(formatTimeToLocalISO(localDate(2026, 6, 5, 13, 30))).toBe("13:30");
expect(formatTimeToLocalISO(localDate(2026, 6, 5, 0, 0))).toBe("00:00");
expect(formatTimeToLocalISO(localDate(2026, 6, 5, 23, 59))).toBe("23:59");
});
it("has nothing to say for a moment that was never given", () => {
expect(formatTimeToLocalISO(null)).toBeUndefined();
expect(formatTimeToLocalISO(undefined)).toBeUndefined();
});
});
describe("offsetDate", () => {
it("walks a date by whole days, over the end of a month and of a year", () => {
expect(formatDateToLocalISO(offsetDate(localDate(2026, 6, 5), 1))).toBe("2026-06-06");
expect(formatDateToLocalISO(offsetDate(localDate(2026, 6, 5), -1))).toBe("2026-06-04");
expect(formatDateToLocalISO(offsetDate(localDate(2026, 6, 30), 1))).toBe("2026-07-01");
expect(formatDateToLocalISO(offsetDate(localDate(2026, 1, 1), -1))).toBe("2025-12-31");
});
it("takes a date written out as well as one already built", () => {
expect(formatDateToLocalISO(offsetDate("2026-06-05T00:00:00", 2))).toBe("2026-06-07");
});
it("has nothing to walk where no date was given", () => {
expect(offsetDate(null, 1)).toBeUndefined();
expect(offsetDate(undefined, 1)).toBeUndefined();
// The empty string is no date either, and is what an unfilled label reads as.
expect(offsetDate("", 1)).toBeUndefined();
});
});
describe("parseStartEndDateFromEvent", () => {
it("takes the end of an all-day span back a day, the calendar's own end being exclusive", () => {
// FullCalendar says a span covering the 5th and the 6th as ending on the 7th.
expect(parseStartEndDateFromEvent(span(localDate(2026, 6, 5), localDate(2026, 6, 7), true)))
.toEqual({ startDate: "2026-06-05", endDate: "2026-06-06" });
});
it("keeps the end of a timed span as it stands, that one naming the day it falls on", () => {
expect(parseStartEndDateFromEvent(span(localDate(2026, 6, 5, 13, 0), localDate(2026, 6, 5, 14, 0), false)))
.toEqual({ startDate: "2026-06-05", endDate: "2026-06-05" });
});
it("answers with neither date where there is no start to read", () => {
expect(parseStartEndDateFromEvent(span(null, localDate(2026, 6, 7), true)))
.toEqual({ startDate: null, endDate: null });
});
it("leaves the end unsaid where the calendar names none", () => {
expect(parseStartEndDateFromEvent(span(localDate(2026, 6, 5), null, true)))
.toEqual({ startDate: "2026-06-05", endDate: undefined });
expect(parseStartEndDateFromEvent(span(localDate(2026, 6, 5, 13, 0), null, false)))
.toEqual({ startDate: "2026-06-05", endDate: undefined });
});
});
describe("parseStartEndTimeFromEvent", () => {
it("reads the hours of a timed span", () => {
expect(parseStartEndTimeFromEvent(span(localDate(2026, 6, 5, 13, 0), localDate(2026, 6, 5, 14, 30), false)))
.toEqual({ startTime: "13:00", endTime: "14:30" });
});
it("says no hours at all for an all-day span, which has none to give", () => {
expect(parseStartEndTimeFromEvent(span(localDate(2026, 6, 5), localDate(2026, 6, 7), true)))
.toEqual({ startTime: null, endTime: null });
});
});
describe("getMonthsInDateRange", () => {
it("names every month a range touches within one year", () => {
expect(getMonthsInDateRange("2026-03-14", "2026-06-02"))
.toEqual([ "2026-03", "2026-04", "2026-05", "2026-06" ]);
});
it("names a single month where both ends fall in it", () => {
expect(getMonthsInDateRange("2026-06-01", "2026-06-30")).toEqual([ "2026-06" ]);
});
it("runs a range spanning years out to the end of each one and in from the start of the next", () => {
expect(getMonthsInDateRange("2025-11-20", "2026-02-03"))
.toEqual([ "2025-11", "2025-12", "2026-01", "2026-02" ]);
});
it("pads the single-digit months, the names being what a lookup is keyed by", () => {
const months = getMonthsInDateRange("2026-01-01", "2026-12-31");
expect(months).toHaveLength(12);
expect(months[0]).toBe("2026-01");
expect(months[8]).toBe("2026-09");
expect(months[9]).toBe("2026-10");
});
});
describe("isAttributeChangeAffecting", () => {
/** What a reload hands over for one changed attribute; only the owner and the inheritance flag are read. */
function changed(noteId: string, name: string, isInheritable = false): AttributeRow {
return { attributeId: `${noteId}_${name}`, componentId: "some-component", noteId, type: "label", name, isInheritable };
}
it("answers for a label written on a drawn note itself", () => {
const note = buildNote({ id: "own", title: "Event", "#color": "red" });
expect(isAttributeChangeAffecting([ changed(note.noteId, "color") ], [ note.noteId ])).toBe(true);
});
it("answers for an inheritable label on an ancestor, which is drawn on the chip all the same", () => {
const parent = buildNote({
id: "inh-parent",
title: "Collection",
"#color(inheritable)": "red",
children: [ { id: "inh-child", title: "Event", "#startDate": "2026-06-05" } ]
});
// The collection note is nowhere in the drawn notes — only its child is.
expect(isAttributeChangeAffecting([ changed(parent.noteId, "color", true) ], [ "inh-child" ])).toBe(true);
});
it("answers for a label on a template the drawn note points at", () => {
buildNote({ id: "tpl", title: "Template", "#color": "blue" });
buildNote({ id: "tpl-instance", title: "Event", "~template": "tpl" });
expect(isAttributeChangeAffecting([ changed("tpl", "color") ], [ "tpl-instance" ])).toBe(true);
});
it("keeps quiet for an ancestor's label that no child inherits", () => {
buildNote({
id: "own-parent",
title: "Collection",
"#color": "red",
children: [ { id: "own-child", title: "Event" } ]
});
expect(isAttributeChangeAffecting([ changed("own-parent", "color") ], [ "own-child" ])).toBe(false);
});
it("keeps quiet for a change with nothing to do with the calendar, and for no change at all", () => {
buildNote({ id: "elsewhere", title: "Somewhere else", "#color": "green" });
buildNote({ id: "drawn", title: "Event" });
expect(isAttributeChangeAffecting([ changed("elsewhere", "color", true) ], [ "drawn" ])).toBe(false);
expect(isAttributeChangeAffecting([], [ "drawn" ])).toBe(false);
});
});
describe("isBranchChangeAffecting", () => {
/** What a reload hands over for one branch; only where it puts the note is read. */
function filed(parentNoteId: string): BranchRow {
return { branchId: `${parentNoteId}_child`, componentId: "some-component", parentNoteId };
}
it("answers for a note filed under the calendar itself, which is where a new year lands", () => {
expect(isBranchChangeAffecting([ filed("journal") ], "journal", [])).toBe(true);
});
it("answers for a note filed under one the calendar already knows of", () => {
// The day note a click on an empty day makes: filed under a month the calendar knows, and
// itself unknown until the events are built again.
expect(isBranchChangeAffecting([ filed("month") ], "journal", [ "year", "month" ])).toBe(true);
});
it("answers where only the outermost of several new notes hangs off something known", () => {
// A click on a day of a month that has never been opened makes all three at once, and the
// day's own parent is as new as the day is.
const rows = [ filed("journal"), filed("year"), filed("month") ];
expect(isBranchChangeAffecting(rows, "journal", [])).toBe(true);
});
it("keeps quiet for a note filed somewhere else entirely, and for no filing at all", () => {
expect(isBranchChangeAffecting([ filed("elsewhere") ], "journal", [ "year", "month" ])).toBe(false);
expect(isBranchChangeAffecting([], "journal", [ "year" ])).toBe(false);
});
});
describe("getTimedDurationMinutes", () => {
it("measures a timed event within its day", () => {
expect(getTimedDurationMinutes({ startDate: "2026-06-05", startTime: "09:00", endTime: "10:30" })).toBe(90);
expect(getTimedDurationMinutes({ startDate: "2026-06-05", startTime: "09:00", endTime: "09:00" })).toBe(0);
});
it("runs an end before the start into the next day, as the calendar draws it", () => {
expect(getTimedDurationMinutes({ startDate: "2026-06-05", startTime: "11:00", endTime: "10:00" })).toBe(23 * 60);
});
it("counts the days of a timed event that spans several", () => {
expect(getTimedDurationMinutes({ startDate: "2026-03-28", endDate: "2026-03-30", startTime: "22:00", endTime: "01:00" }))
.toBe(27 * 60);
});
it("has no duration without both times", () => {
expect(getTimedDurationMinutes({ startDate: "2026-06-05" })).toBeNull();
expect(getTimedDurationMinutes({ startDate: "2026-06-05", startTime: "09:00" })).toBeNull();
});
});