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
test(client): running script bundle with dayjs
Elian Doran8c324cd185f2
diff --git a/apps/client/src/services/bundle.spec.ts b/apps/client/src/services/bundle.spec.tsnew file mode 100644index 0000000000..70f574b017--- /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.tsindex e7b88a3433..def3156abe 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<HTMLElement>) {+export async function executeBundle(bundle: Bundle, originEntity?: Entity | null, $container?: JQuery<HTMLElement>) {const apiContext = await ScriptContext(bundle.noteId, bundle.allNoteIds, originEntity, $container);try {