From f195c7d1b63693d713c1c6c6a6b5499c47cbbf75 Mon Sep 17 00:00:00 2001 From: zadam Date: Mon, 27 Nov 2023 00:41:46 +0100 Subject: [PATCH] wip --- spec/etapi/{app_info.js => app_info.spec.js} | 0 spec/etapi/{backup.js => backup.spec.js} | 0 spec/etapi/{import.js => import.spec.js} | 0 spec/etapi/notes.js | 109 ------------------- spec/etapi/notes.spec.js | 107 +++++++++++++++++- spec/etapi/search.spec.js | 43 ++++++++ spec/support/etapi.js | 33 ++++++ 7 files changed, 181 insertions(+), 111 deletions(-) rename spec/etapi/{app_info.js => app_info.spec.js} (100%) rename spec/etapi/{backup.js => backup.spec.js} (100%) rename spec/etapi/{import.js => import.spec.js} (100%) delete mode 100644 spec/etapi/notes.js create mode 100644 spec/etapi/search.spec.js diff --git a/spec/etapi/app_info.js b/spec/etapi/app_info.spec.js similarity index 100% rename from spec/etapi/app_info.js rename to spec/etapi/app_info.spec.js diff --git a/spec/etapi/backup.js b/spec/etapi/backup.spec.js similarity index 100% rename from spec/etapi/backup.js rename to spec/etapi/backup.spec.js diff --git a/spec/etapi/import.js b/spec/etapi/import.spec.js similarity index 100% rename from spec/etapi/import.js rename to spec/etapi/import.spec.js diff --git a/spec/etapi/notes.js b/spec/etapi/notes.js deleted file mode 100644 index f63acee0a..000000000 --- a/spec/etapi/notes.js +++ /dev/null @@ -1,109 +0,0 @@ -const crypto = require('crypto'); -const { - deleteEtapi, - getEtapiResponse, - describeEtapi, postEtapi, - getEtapi, - getEtapiContent, - patchEtapi, putEtapi, - putEtapiContent -} = require("../support/etapi"); - -describeEtapi("notes", () => { - it("create", async () => { - const {note, branch} = await 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 getEtapi(`notes/${note.noteId}`); - expect(rNote.title).toEqual("Hello World!"); - - const rContent = await (await getEtapiContent(`notes/${note.noteId}/content`)).text(); - expect(rContent).toEqual("Content"); - - const rBranch = await getEtapi(`branches/${branch.branchId}`); - expect(rBranch.parentNoteId).toEqual("root"); - expect(rBranch.prefix).toEqual("Custom prefix"); - }); - - it("patch", async () => { - const {note} = await postEtapi('create-note', { - parentNoteId: 'root', - type: 'text', - title: 'Hello World!', - content: 'Content' - }); - - await 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 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 postEtapi('create-note', { - parentNoteId: 'root', - type: 'text', - title: 'Hello World!', - content: 'Content' - }); - - await putEtapiContent(`notes/${note.noteId}/content`, "new content"); - - const rContent = await (await getEtapiContent(`notes/${note.noteId}/content`)).text(); - expect(rContent).toEqual("new content"); - }); - - it("create / update binary content", async () => { - const {note} = await postEtapi('create-note', { - parentNoteId: 'root', - type: 'file', - title: 'Hello World!', - content: 'ZZZ' - }); - - const updatedContent = crypto.randomBytes(16); - - await putEtapiContent(`notes/${note.noteId}/content`, updatedContent); - - const rContent = await (await getEtapiContent(`notes/${note.noteId}/content`)).arrayBuffer(); - expect(Buffer.from(new Uint8Array(rContent))).toEqual(updatedContent); - }); - - it("delete note", async () => { - const {note} = await postEtapi('create-note', { - parentNoteId: 'root', - type: 'text', - title: 'Hello World!', - content: 'Content' - }); - - await deleteEtapi(`notes/${note.noteId}`); - - const resp = await 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.spec.js b/spec/etapi/notes.spec.js index ce0df713d..ad6bbeec0 100644 --- a/spec/etapi/notes.spec.js +++ b/spec/etapi/notes.spec.js @@ -1,5 +1,108 @@ -describe("Notes", () => { - it("zzz", () => { +const crypto = require('crypto'); +const { + deleteEtapi, + getEtapiResponse, + describeEtapi, postEtapi, + getEtapi, + getEtapiContent, + patchEtapi, putEtapi, + putEtapiContent +} = require("../support/etapi"); +describeEtapi("notes", () => { + it("create", async () => { + const {note, branch} = await 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"); + + const rNote = await getEtapi(`notes/${note.noteId}`); + expect(rNote.title).toEqual("Hello World!"); + + const rContent = await (await getEtapiContent(`notes/${note.noteId}/content`)).text(); + expect(rContent).toEqual("Content"); + + const rBranch = await getEtapi(`branches/${branch.branchId}`); + expect(rBranch.parentNoteId).toEqual("root"); + expect(rBranch.prefix).toEqual("Custom prefix"); + }); + + it("patch", async () => { + const {note} = await postEtapi('create-note', { + parentNoteId: 'root', + type: 'text', + title: 'Hello World!', + content: 'Content' + }); + + await 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 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 postEtapi('create-note', { + parentNoteId: 'root', + type: 'text', + title: 'Hello World!', + content: 'Content' + }); + + await putEtapiContent(`notes/${note.noteId}/content`, "new content"); + + const rContent = await (await getEtapiContent(`notes/${note.noteId}/content`)).text(); + expect(rContent).toEqual("new content"); + }); + + it("create / update binary content", async () => { + const {note} = await postEtapi('create-note', { + parentNoteId: 'root', + type: 'file', + title: 'Hello World!', + content: 'ZZZ' + }); + + const updatedContent = crypto.randomBytes(16); + + await putEtapiContent(`notes/${note.noteId}/content`, updatedContent); + + const rContent = await (await getEtapiContent(`notes/${note.noteId}/content`)).arrayBuffer(); + expect(Buffer.from(new Uint8Array(rContent))).toEqual(updatedContent); + }); + + it("delete note", async () => { + const {note} = await postEtapi('create-note', { + parentNoteId: 'root', + type: 'text', + title: 'Hello World!', + content: 'Content' + }); + + await deleteEtapi(`notes/${note.noteId}`); + + const resp = await 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/search.spec.js b/spec/etapi/search.spec.js new file mode 100644 index 000000000..4356b5a7d --- /dev/null +++ b/spec/etapi/search.spec.js @@ -0,0 +1,43 @@ +const crypto = require('crypto'); +const { + deleteEtapi, + getEtapiResponse, + describeEtapi, postEtapi, + getEtapi, + getEtapiContent, + patchEtapi, putEtapi, + putEtapiContent +} = require("../support/etapi"); +const {createTextNote} = require("../support/etapi.js"); + +describeEtapi("search", () => { + describe('search', () => { + let europe, america; + let austria, czechia; + let usa, canada; + + beforeAll(async () => { + europe = await createTextNote(null, 'Europe'); + austria = await createTextNote(europe.noteId, 'Austria'); + czechia = await createTextNote(europe.noteId, 'Czechia'); + + america = await createTextNote(null, 'America'); + usa = await createTextNote(null, 'USA'); + canada = await createTextNote(null, 'Canada'); + }); + + async function search(searchString, params) { + const keyToValues = Object.keys(params).map(key => `${key}=${params[key]}`); + + const {results} = await getEtapi(`notes?search=${searchString}&${keyToValues.join('&')}`); + + return results; + } + + it("search", async () => { + const results = await search('Austria'); + + expect(results.length).toEqual(0); + }); + }); +}); diff --git a/spec/support/etapi.js b/spec/support/etapi.js index 97bf5aee1..0fb3e2167 100644 --- a/spec/support/etapi.js +++ b/spec/support/etapi.js @@ -8,6 +8,8 @@ const getEtapiAuthorizationHeader = () => "Basic " + Buffer.from(`etapi:${etapiA const PORT = '9999'; const HOST = 'http://localhost:' + PORT; +let currentTestRootNote = null; + function describeEtapi(description, specDefinitions) { describe(description, () => { let appProcess; @@ -42,6 +44,10 @@ function describeEtapi(description, specDefinitions) { })).json()).authToken; }); + beforeEach(async () => { + currentTestRootNote = await createTextNote('root', "test root"); + }); + afterAll(() => { console.log("Attempting to kill the Trilium process as part of the cleanup..."); kill(appProcess.pid, 'SIGKILL', () => { console.log("Trilium process killed.") }); @@ -51,6 +57,30 @@ function describeEtapi(description, specDefinitions) { }); } +async function createTextNote(parentNoteId = null, title = 'new note', content = '') { + if (!parentNoteId) { + parentNoteId = currentTestRootNote.noteId; + } + + const {note} = await postEtapi('create-note', { + parentNoteId, + type: 'text', + title, + content + }); + + return note; +} + +async function createLabel(noteId, name, value = '', isInheritable = false) { + return await postEtapi('attributes', { + type: 'label', + name, + value, + isInheritable + }); +} + async function getEtapiResponse(url) { return await fetch(`${HOST}/etapi/${url}`, { method: 'GET', @@ -172,6 +202,9 @@ function checkStatus(response) { module.exports = { describeEtapi, + createTextNote, + createLabel, + getCurrentTestRootNote: () => currentTestRootNote, getEtapi, getEtapiResponse, getEtapiContent,