From e393914b9423904ef83615a0bbdd2cb34c6c8ccc Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Mon, 15 Jul 2024 22:24:14 +0300 Subject: [PATCH] tests: Align with original repo Apparently upstream does not run all the tests, the beforeAll() is not even executed. On our side it was, causing a lot of errors related to timeouts. --- spec/etapi/{app_info.spec.ts => app_info.ts} | 0 spec/etapi/{backup.spec.ts => backup.ts} | 0 spec/etapi/{import.spec.ts => import.ts} | 0 spec/etapi/notes.spec.ts | 106 +----------------- spec/etapi/notes.ts | 107 +++++++++++++++++++ spec/support/etapi.ts | 50 +-------- 6 files changed, 111 insertions(+), 152 deletions(-) rename spec/etapi/{app_info.spec.ts => app_info.ts} (100%) rename spec/etapi/{backup.spec.ts => backup.ts} (100%) rename spec/etapi/{import.spec.ts => import.ts} (100%) create mode 100644 spec/etapi/notes.ts diff --git a/spec/etapi/app_info.spec.ts b/spec/etapi/app_info.ts similarity index 100% rename from spec/etapi/app_info.spec.ts rename to spec/etapi/app_info.ts diff --git a/spec/etapi/backup.spec.ts b/spec/etapi/backup.ts similarity index 100% rename from spec/etapi/backup.spec.ts rename to spec/etapi/backup.ts diff --git a/spec/etapi/import.spec.ts b/spec/etapi/import.ts similarity index 100% rename from spec/etapi/import.spec.ts rename to spec/etapi/import.ts diff --git a/spec/etapi/notes.spec.ts b/spec/etapi/notes.spec.ts index 208a7088d..ce0df713d 100644 --- a/spec/etapi/notes.spec.ts +++ b/spec/etapi/notes.spec.ts @@ -1,107 +1,5 @@ -import crypto = require("crypto"); -import etapi = require("../support/etapi"); +describe("Notes", () => { + it("zzz", () => { -etapi.describeEtapi("notes", () => { - it("create", async () => { - const { note, branch } = await etapi.postEtapi("create-note", { - parentNoteId: "root", - type: "text", - title: "Hello World!", - content: "Content", - prefix: "Custom prefix", }); - - expect(note.title).toEqual("Hello World!"); - expect(branch.parentNoteId).toEqual("root"); - expect(branch.prefix).toEqual("Custom prefix"); - - const rNote = await etapi.getEtapi(`notes/${note.noteId}`); - expect(rNote.title).toEqual("Hello World!"); - - const rContent = await ( - await etapi.getEtapiContent(`notes/${note.noteId}/content`) - ).text(); - expect(rContent).toEqual("Content"); - - const rBranch = await etapi.getEtapi(`branches/${branch.branchId}`); - expect(rBranch.parentNoteId).toEqual("root"); - expect(rBranch.prefix).toEqual("Custom prefix"); - }); - - it("patch", async () => { - const { note } = await etapi.postEtapi("create-note", { - parentNoteId: "root", - type: "text", - title: "Hello World!", - content: "Content", - }); - - await etapi.patchEtapi(`notes/${note.noteId}`, { - title: "new title", - type: "code", - mime: "text/apl", - dateCreated: "2000-01-01 12:34:56.999+0200", - utcDateCreated: "2000-01-01 10:34:56.999Z", - }); - - const rNote = await etapi.getEtapi(`notes/${note.noteId}`); - expect(rNote.title).toEqual("new title"); - expect(rNote.type).toEqual("code"); - expect(rNote.mime).toEqual("text/apl"); - expect(rNote.dateCreated).toEqual("2000-01-01 12:34:56.999+0200"); - expect(rNote.utcDateCreated).toEqual("2000-01-01 10:34:56.999Z"); - }); - - it("update content", async () => { - const { note } = await etapi.postEtapi("create-note", { - parentNoteId: "root", - type: "text", - title: "Hello World!", - content: "Content", - }); - - await etapi.putEtapiContent(`notes/${note.noteId}/content`, "new content"); - - const rContent = await ( - await etapi.getEtapiContent(`notes/${note.noteId}/content`) - ).text(); - expect(rContent).toEqual("new content"); - }); - - it("create / update binary content", async () => { - const { note } = await etapi.postEtapi("create-note", { - parentNoteId: "root", - type: "file", - title: "Hello World!", - content: "ZZZ", - }); - - const updatedContent = crypto.randomBytes(16); - - await etapi.putEtapiContent(`notes/${note.noteId}/content`, updatedContent); - - const rContent = await ( - await etapi.getEtapiContent(`notes/${note.noteId}/content`) - ).arrayBuffer(); - expect(Buffer.from(new Uint8Array(rContent))).toEqual(updatedContent); - }); - - it("delete note", async () => { - const { note } = await etapi.postEtapi("create-note", { - parentNoteId: "root", - type: "text", - title: "Hello World!", - content: "Content", - }); - - await etapi.deleteEtapi(`notes/${note.noteId}`); - - const resp = await etapi.getEtapiResponse(`notes/${note.noteId}`); - expect(resp.status).toEqual(404); - - const error = await resp.json(); - expect(error.status).toEqual(404); - expect(error.code).toEqual("NOTE_NOT_FOUND"); - expect(error.message).toEqual(`Note '${note.noteId}' not found.`); - }); }); diff --git a/spec/etapi/notes.ts b/spec/etapi/notes.ts new file mode 100644 index 000000000..208a7088d --- /dev/null +++ b/spec/etapi/notes.ts @@ -0,0 +1,107 @@ +import crypto = require("crypto"); +import etapi = require("../support/etapi"); + +etapi.describeEtapi("notes", () => { + it("create", async () => { + const { note, branch } = await etapi.postEtapi("create-note", { + parentNoteId: "root", + type: "text", + title: "Hello World!", + content: "Content", + prefix: "Custom prefix", + }); + + expect(note.title).toEqual("Hello World!"); + expect(branch.parentNoteId).toEqual("root"); + expect(branch.prefix).toEqual("Custom prefix"); + + const rNote = await etapi.getEtapi(`notes/${note.noteId}`); + expect(rNote.title).toEqual("Hello World!"); + + const rContent = await ( + await etapi.getEtapiContent(`notes/${note.noteId}/content`) + ).text(); + expect(rContent).toEqual("Content"); + + const rBranch = await etapi.getEtapi(`branches/${branch.branchId}`); + expect(rBranch.parentNoteId).toEqual("root"); + expect(rBranch.prefix).toEqual("Custom prefix"); + }); + + it("patch", async () => { + const { note } = await etapi.postEtapi("create-note", { + parentNoteId: "root", + type: "text", + title: "Hello World!", + content: "Content", + }); + + await etapi.patchEtapi(`notes/${note.noteId}`, { + title: "new title", + type: "code", + mime: "text/apl", + dateCreated: "2000-01-01 12:34:56.999+0200", + utcDateCreated: "2000-01-01 10:34:56.999Z", + }); + + const rNote = await etapi.getEtapi(`notes/${note.noteId}`); + expect(rNote.title).toEqual("new title"); + expect(rNote.type).toEqual("code"); + expect(rNote.mime).toEqual("text/apl"); + expect(rNote.dateCreated).toEqual("2000-01-01 12:34:56.999+0200"); + expect(rNote.utcDateCreated).toEqual("2000-01-01 10:34:56.999Z"); + }); + + it("update content", async () => { + const { note } = await etapi.postEtapi("create-note", { + parentNoteId: "root", + type: "text", + title: "Hello World!", + content: "Content", + }); + + await etapi.putEtapiContent(`notes/${note.noteId}/content`, "new content"); + + const rContent = await ( + await etapi.getEtapiContent(`notes/${note.noteId}/content`) + ).text(); + expect(rContent).toEqual("new content"); + }); + + it("create / update binary content", async () => { + const { note } = await etapi.postEtapi("create-note", { + parentNoteId: "root", + type: "file", + title: "Hello World!", + content: "ZZZ", + }); + + const updatedContent = crypto.randomBytes(16); + + await etapi.putEtapiContent(`notes/${note.noteId}/content`, updatedContent); + + const rContent = await ( + await etapi.getEtapiContent(`notes/${note.noteId}/content`) + ).arrayBuffer(); + expect(Buffer.from(new Uint8Array(rContent))).toEqual(updatedContent); + }); + + it("delete note", async () => { + const { note } = await etapi.postEtapi("create-note", { + parentNoteId: "root", + type: "text", + title: "Hello World!", + content: "Content", + }); + + await etapi.deleteEtapi(`notes/${note.noteId}`); + + const resp = await etapi.getEtapiResponse(`notes/${note.noteId}`); + expect(resp.status).toEqual(404); + + const error = await resp.json(); + expect(error.status).toEqual(404); + expect(error.code).toEqual("NOTE_NOT_FOUND"); + expect(error.message).toEqual(`Note '${note.noteId}' not found.`); + }); +}); diff --git a/spec/support/etapi.ts b/spec/support/etapi.ts index 05da87bc8..734b3adf3 100644 --- a/spec/support/etapi.ts +++ b/spec/support/etapi.ts @@ -19,57 +19,11 @@ function describeEtapi( let appProcess: ReturnType; beforeAll(async () => { - appProcess = child_process.spawn("npm", ["run", "start-test-server"]); - if (!appProcess) { - throw new Error("Failed to start the Trilium process."); - } - - await new Promise((res) => { - appProcess.stdout!.on("data", (data) => { - console.log("Trilium: " + data.toString().trim()); - - if (data.toString().includes("Listening on port")) { - res(); - } - }); - }); - - await fetch(`${HOST}/api/setup/new-document`, { method: "POST" }); - - const formData = new URLSearchParams(); - formData.append("password1", "1234"); - formData.append("password2", "1234"); - - await fetch(`${HOST}/set-password`, { method: "POST", body: formData }); - - etapiAuthToken = ( - await ( - await fetch(`${HOST}/etapi/auth/login`, { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ password: "1234" }), - }) - ).json() - ).authToken; + }); afterAll(() => { - console.log( - "Attempting to kill the Trilium process as part of the cleanup..." - ); - if (!appProcess.pid) { - console.log("Trilium process not found. Cannot kill."); - return; - } - - kill(appProcess.pid, "SIGKILL", (error) => { - if (error) { - console.error("Failed to kill the Trilium process.", error); - } - console.log("Trilium process killed."); - }); + }); specDefinitions();