// SPDX-License-Identifier: AGPL-3.0-or-later import { Editor, type JSONContent } from "@tiptap/core"; import { describe, expect, it } from "vitest"; import { dateChoices, formatDate, insertDate, parseIso, spacedDate } from "./dates"; import { docwellExtensions } from "./extensions"; import { englishLabels } from "./labels"; // Wednesday, 4 November 2026, in local time. const now = new Date(2026, 10, 4, 15, 30); const dates = (q: string, canRemind = true) => dateChoices(q, englishLabels, canRemind, now); describe("dateChoices", () => { it("offers today and a reminder tomorrow for an empty query", () => { expect(dates("")).toEqual([ { date: "2026-11-04", remind: false }, { date: "2026-11-05", remind: true }, ]); expect(dates("", false)).toEqual([{ date: "2026-11-04", remind: false }]); }); it("understands date words and weekdays", () => { expect(dates("tom")).toEqual([{ date: "2026-11-05", remind: false }]); expect(dates("next")).toEqual([{ date: "2026-11-11", remind: false }]); // The next Friday, and a Wednesday a week ahead rather than today. expect(dates("fri")).toEqual([{ date: "2026-11-06", remind: false }]); expect(dates("wed")).toEqual([{ date: "2026-11-11", remind: false }]); expect(dates("2026-12-24")).toEqual([{ date: "2026-12-24", remind: false }]); expect(dates("2026-02-30")).toEqual([]); expect(dates("ann")).toEqual([]); }); it("makes reminders after remind", () => { expect(dates("remind friday")).toEqual([{ date: "2026-11-06", remind: true }]); expect(dates("rem").map((c) => c.date)).toEqual(["2026-11-05", "2026-11-04", "2026-11-11"]); expect(dates("remind fri", false)).toEqual([]); }); it("uses the labels' language too", () => { const pl = { ...englishLabels, locale: "pl", dates: { ...englishLabels.dates, tomorrow: "Jutro", remindWord: "przypomnij" }, }; expect(dateChoices("jut", pl, true, now)).toEqual([{ date: "2026-11-05", remind: false }]); expect(dateChoices("przypomnij piÄ…tek", pl, true, now)).toEqual([{ date: "2026-11-06", remind: true }]); }); }); describe("spacedDate", () => { it("keeps spaces only in reminders and next week", () => { expect(spacedDate("remind fri", englishLabels)).toBe(true); expect(spacedDate("next week", englishLabels)).toBe(true); expect(spacedDate("ann about it", englishLabels)).toBe(false); expect(spacedDate("remind", englishLabels)).toBe(false); expect(spacedDate("remind me on friday please", englishLabels)).toBe(false); }); }); describe("formatDate", () => { it("names nearby days and leaves out this year", () => { expect(formatDate("2026-11-04", englishLabels, now)).toBe("Today"); expect(formatDate("2026-11-05", englishLabels, now)).toBe("Tomorrow"); expect(formatDate("2026-11-03", englishLabels, now)).toBe("Yesterday"); expect(formatDate("2026-12-24", { ...englishLabels, locale: "en-US" }, now)).toBe("Dec 24"); expect(formatDate("2027-01-02", { ...englishLabels, locale: "en-US" }, now)).toBe("Jan 2, 2027"); expect(formatDate("soon", englishLabels, now)).toBe("soon"); expect(parseIso("2026-11-31")).toBeNull(); }); }); describe("insertDate", () => { it("inserts a date, with a reminder only for a signed-in person", () => { const editor = new Editor({ extensions: docwellExtensions(), content: "

Due @fri

" }); insertDate(editor, { from: 5, to: 9 }, { date: "2026-11-06", remind: true }, "aaaaaaaaaaaaaaa"); insertDate(editor, { from: 1, to: 1 }, { date: "2026-11-07", remind: true }); const [none, , mine] = editor.getJSON().content![0]!.content! as JSONContent[]; expect(none).toEqual({ type: "dateMention", attrs: { date: "2026-11-07", remind: "", tz: "" } }); expect(mine!.type).toBe("dateMention"); expect(mine!.attrs).toMatchObject({ date: "2026-11-06", remind: "aaaaaaaaaaaaaaa" }); expect(mine!.attrs!.tz).not.toBe(""); expect(editor.getHTML()).toContain('data-type="date"'); editor.destroy(); }); });