From fe3350f39ffc3e90640e848d79827e0e6bcc6e1a Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Thu, 26 Jun 2025 20:13:01 +0300 Subject: [PATCH] feat(server/script): enable a few dayjs plugins (closes #6080) --- .../server/src/services/backend_script_api.ts | 11 +++++ apps/server/src/services/script.spec.ts | 46 +++++++++++++++++-- 2 files changed, 53 insertions(+), 4 deletions(-) diff --git a/apps/server/src/services/backend_script_api.ts b/apps/server/src/services/backend_script_api.ts index 2d69ee608..b3796637c 100644 --- a/apps/server/src/services/backend_script_api.ts +++ b/apps/server/src/services/backend_script_api.ts @@ -36,6 +36,17 @@ import type Becca from "../becca/becca-interface.js"; import type { NoteParams } from "./note-interface.js"; import type { ApiParams } from "./backend_script_api_interface.js"; +// Dayjs plugins +import isSameOrBefore from "dayjs/plugin/isSameOrBefore"; +import isSameOrAfter from "dayjs/plugin/isSameOrAfter"; +import isBetween from "dayjs/plugin/isBetween"; +import advancedFormat from "dayjs/plugin/advancedFormat.js"; + +dayjs.extend(isSameOrBefore); +dayjs.extend(isSameOrAfter); +dayjs.extend(isBetween); +dayjs.extend(advancedFormat); + /** * A whole number * @typedef {number} int diff --git a/apps/server/src/services/script.spec.ts b/apps/server/src/services/script.spec.ts index 9059bd463..f94c93f26 100644 --- a/apps/server/src/services/script.spec.ts +++ b/apps/server/src/services/script.spec.ts @@ -60,17 +60,55 @@ describe("Script", () => { }); describe("dayjs", () => { + const scriptNote = note("dayjs", { + type: "code", + mime: "application/javascript;env=backend", + }); + it("dayjs is available", () => { cls.init(() => { - const scriptNote = note("dayjs", { - type: "code", - mime: "application/javascript;env=backend", - }); const bundle = getScriptBundle(scriptNote.note, true, "backend", [], `return api.dayjs().format("YYYY-MM-DD");`); expect(bundle).toBeDefined(); const result = executeBundle(bundle!); expect(result).toMatch(/^\d{4}-\d{2}-\d{2}$/); }); }); + + it("dayjs is-same-or-before", () => { + cls.init(() => { + const bundle = getScriptBundle(scriptNote.note, true, "backend", [], `return api.dayjs("2023-10-01").isSameOrBefore(api.dayjs("2023-10-02"));`); + expect(bundle).toBeDefined(); + const result = executeBundle(bundle!); + expect(result).toBe(true); + }); + }); + + it("dayjs is-same-or-after", () => { + cls.init(() => { + const bundle = getScriptBundle(scriptNote.note, true, "backend", [], `return api.dayjs("2023-10-02").isSameOrAfter(api.dayjs("2023-10-01"));`); + expect(bundle).toBeDefined(); + const result = executeBundle(bundle!); + expect(result).toBe(true); + }); + }); + + it("dayjs is-between", () => { + cls.init(() => { + const bundle = getScriptBundle(scriptNote.note, true, "backend", [], `return api.dayjs("2023-10-02").isBetween(api.dayjs("2023-10-01"), api.dayjs("2023-10-03"));`); + expect(bundle).toBeDefined(); + const result = executeBundle(bundle!); + expect(result).toBe(true); + }); + }); + + // advanced format + it("dayjs advanced format", () => { + cls.init(() => { + const bundle = getScriptBundle(scriptNote.note, true, "backend", [], `return api.dayjs("2023-10-01").format("Q");`); + expect(bundle).toBeDefined(); + const result = executeBundle(bundle!); + expect(result).not.toBe("Q"); + }); + }); }); });