From f32f9d4326fd214e3916337d0cc007d367eaaee2 Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Thu, 26 Jun 2025 18:34:00 +0300 Subject: [PATCH] test(server/script): dayjs is available --- apps/server/src/services/script.spec.ts | 76 +++++++++++++++++++++++++ apps/server/src/services/script.ts | 4 +- 2 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 apps/server/src/services/script.spec.ts diff --git a/apps/server/src/services/script.spec.ts b/apps/server/src/services/script.spec.ts new file mode 100644 index 000000000..9059bd463 --- /dev/null +++ b/apps/server/src/services/script.spec.ts @@ -0,0 +1,76 @@ +import becca from "../becca/becca.js"; +import { note, NoteBuilder } from "../test/becca_mocking.js"; +import cls from "./cls.js"; +import { executeBundle, getScriptBundle } from "./script.js"; +import BBranch from "../becca/entities/bbranch.js"; +import BNote from "../becca/entities/bnote.js"; + + +describe("Script", () => { + let rootNote!: NoteBuilder; + + beforeEach(() => { + + becca.reset(); + + rootNote = new NoteBuilder( + new BNote({ + noteId: "root", + title: "root", + type: "text" + }) + ); + new BBranch({ + branchId: "none_root", + noteId: "root", + parentNoteId: "none", + notePosition: 10 + }); + + vi.mock("./sql.js", () => { + return { + default: { + transactional: (cb: Function) => { + cb(); + }, + execute: () => {}, + replace: () => {}, + getMap: () => {} + } + }; + }); + + vi.mock("./sql_init.js", () => { + return { + dbReady: () => { + console.log("Hello world"); + } + }; + }); + }); + + it("returns result from script", () => { + cls.init(() => { + const result = executeBundle({ + script: `return "world";`, + html: "", + }); + expect(result).toBe("world"); + }); + }); + + describe("dayjs", () => { + 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}$/); + }); + }); + }); +}); diff --git a/apps/server/src/services/script.ts b/apps/server/src/services/script.ts index 1afc3b763..3e4adab2b 100644 --- a/apps/server/src/services/script.ts +++ b/apps/server/src/services/script.ts @@ -39,7 +39,7 @@ function executeNoteNoException(note: BNote, apiParams: ApiParams) { } } -function executeBundle(bundle: Bundle, apiParams: ApiParams = {}) { +export function executeBundle(bundle: Bundle, apiParams: ApiParams = {}) { if (!apiParams.startNote) { // this is the default case, the only exception is when we want to preserve frontend startNote apiParams.startNote = bundle.note; @@ -140,7 +140,7 @@ function getScriptBundleForFrontend(note: BNote, script?: string, params?: Scrip return bundle; } -function getScriptBundle(note: BNote, root: boolean = true, scriptEnv: string | null = null, includedNoteIds: string[] = [], overrideContent: string | null = null): Bundle | undefined { +export function getScriptBundle(note: BNote, root: boolean = true, scriptEnv: string | null = null, includedNoteIds: string[] = [], overrideContent: string | null = null): Bundle | undefined { if (!note.isContentAvailable()) { return; }