diff --git a/apps/client/src/services/bundle.spec.ts b/apps/client/src/services/bundle.spec.ts new file mode 100644 index 000000000..70f574b01 --- /dev/null +++ b/apps/client/src/services/bundle.spec.ts @@ -0,0 +1,32 @@ +import { describe, expect, it } from "vitest"; +import { Bundle, executeBundle } from "./bundle"; +import { buildNote } from "../test/easy-froca"; + +describe("Script bundle", () => { + it("dayjs is available", async () => { + const script = /* js */`return api.dayjs().format("YYYY-MM-DD");`; + const bundle = getBundle(script); + const result = await executeBundle(bundle, null, $()); + expect(result).toMatch(/^\d{4}-\d{2}-\d{2}$/); + }); + + it("dayjs is-same-or-before plugin exists", async () => { + const script = /* js */`return api.dayjs("2023-10-01").isSameOrBefore(api.dayjs("2023-10-02"));`; + const bundle = getBundle(script); + const result = await executeBundle(bundle, null, $()); + expect(result).toBe(true); + }); +}); + +function getBundle(script: string) { + const id = buildNote({ + title: "Script note" + }).noteId; + const bundle: Bundle = { + script: `\napiContext.modules['${id}'] = { exports: {} };\nreturn await ((async function(exports, module, require, api) {\ntry {\n${script}\n;\n} catch (e) { throw new Error(\"Load of script note \\\"Client\\\" (${id}) failed with: \" + e.message); }\nfor (const exportKey in exports) module.exports[exportKey] = exports[exportKey];\nreturn module.exports;\n}).call({}, {}, apiContext.modules['${id}'], apiContext.require([]), apiContext.apis['${id}']));\n`, + html: "", + noteId: id, + allNoteIds: [ id ] + }; + return bundle; +} diff --git a/apps/client/src/services/bundle.ts b/apps/client/src/services/bundle.ts index e7b88a343..def3156ab 100644 --- a/apps/client/src/services/bundle.ts +++ b/apps/client/src/services/bundle.ts @@ -27,7 +27,7 @@ async function getAndExecuteBundle(noteId: string, originEntity = null, script = return await executeBundle(bundle, originEntity); } -async function executeBundle(bundle: Bundle, originEntity?: Entity | null, $container?: JQuery) { +export async function executeBundle(bundle: Bundle, originEntity?: Entity | null, $container?: JQuery) { const apiContext = await ScriptContext(bundle.noteId, bundle.allNoteIds, originEntity, $container); try {