A fully open source & end-to-end encrypted note taking alternative to Evernote.
TypeScript asp-net-coredotnet-coreelectronfosshacktoberfestnote-managmentnote-takingnote-taking-appnotebooknotesnotes-appopen-sourceopensourceprivacyproductivityreactreact-nativereactjsself-hosted
100% test coverage
thecodrr4c665e6aeb61
diff --git a/packages/core/__tests__/database.test.js b/packages/core/__tests__/database.test.jsindex dba92226e..d49b5b65b 100644--- a/packages/core/__tests__/database.test.js+++ b/packages/core/__tests__/database.test.js@@ -4,14 +4,19 @@ import StorageInterface from "../../notes-web/src/interfaces/storage";const db = new Database(StorageInterface);const TEST_NOTE = {- title: "I am a title",content: { delta: "I am a delta", text: "I am a text" }};const TEST_NOTEBOOK = {title: "Test Notebook",description: "Test Description",- topics: ["hello"]+ topics: ["hello", "hello"]+};++const TEST_NOTEBOOK2 = {+ title: "Test Notebook 2",+ description: "Test Description 2",+ topics: ["Home2"]};test("storage is defined", () => {@@ -43,23 +48,43 @@ test("search all notes", () => {test("get a note", () => {let note = db.getNote(TEST_NOTE.dateCreated);- expect(note.title).toBe("I am a title");+ expect(note.dateCreated).toBe(TEST_NOTE.dateCreated);+});++test("note without a title should get title from content", () => {+ let note = db.getNote(TEST_NOTE.dateCreated);+ expect(note.title).toBe("I am a");});test("update note", async () => {let updateTimestamp = await db.addNote({...TEST_NOTE,- title: "I am a new title"+ title: "I am a new title",+ content: {+ text:+ "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.",+ delta: []+ }});expect(updateTimestamp).toBe(TEST_NOTE.dateCreated);});+test("note with text longer than 150 characters should have ... in the headline", () => {+ let note = db.getNote(TEST_NOTE.dateCreated);+ expect(note.headline.includes("...")).toBe(true);+});+test("add a notebook", async () => {let id = await db.addNotebook(TEST_NOTEBOOK);TEST_NOTEBOOK["dateCreated"] = id;expect(id).toBeGreaterThan(0);});+test("duplicate notebook should not be added", async () => {+ let id = await db.addNotebook(TEST_NOTEBOOK);+ expect(id).toBeUndefined();+});+test("get all notebooks", () => {let notebooks = db.getNotebooks();expect(notebooks.length).toBeGreaterThan(0);@@ -77,6 +102,11 @@ test("add topic to notebook", async () => {expect(topicIndex).toBeGreaterThan(-1);});+test("duplicate topic to notebook should not be added", async () => {+ let res = await db.addTopicToNotebook(TEST_NOTEBOOK.dateCreated, "Home");+ expect(res).toBe(false);+});+test("add note to topic", async () => {await db.addNoteToTopic(TEST_NOTEBOOK.dateCreated,@@ -88,16 +118,56 @@ test("add note to topic", async () => {expect(notebook.topics[topicIndex].notes[0]).toBeGreaterThan(0);});+test("duplicate note to topic should not be added", async () => {+ let res = await db.addNoteToTopic(+ TEST_NOTEBOOK.dateCreated,+ "Home",+ TEST_NOTE.dateCreated+ );+ expect(res).toBe(false);+});+test("note has notebook", () => {let note = db.getNote(TEST_NOTE.dateCreated);expect(note.notebooks.hasOwnProperty(TEST_NOTEBOOK.dateCreated)).toBe(true);});+test("get empty topic", () => {+ let notes = db.getTopic(TEST_NOTEBOOK.dateCreated, "General");+ expect(notes.length).toBe(0);+});+test("get topic", () => {let notes = db.getTopic(TEST_NOTEBOOK.dateCreated, "Home");expect(notes.length).toBeGreaterThan(0);});+test("getting invalid topic should return undefined", async () => {+ let res = await db.getTopic(TEST_NOTEBOOK.dateCreated, "invalid_topic");+ expect(res).toBeUndefined();+});++test("move note", async () => {+ let id = await db.addNotebook(TEST_NOTEBOOK2);+ TEST_NOTEBOOK2["dateCreated"] = id;+ expect(id).toBeGreaterThan(0);+ let res = await db.moveNote(+ TEST_NOTE.dateCreated,+ { notebook: TEST_NOTEBOOK.dateCreated, topic: "Home" },+ { notebook: id, topic: "Home2" }+ );+ expect(res).toBe(true);+});++test("deletion of note from invalid topic should return false", async () => {+ let res = await db.deleteNoteFromTopic(+ TEST_NOTEBOOK.dateCreated,+ "invalid_topic",+ TEST_NOTE.dateCreated+ );+ expect(res).toBe(false);+});+test("delete note from topic", async () => {await db.deleteNoteFromTopic(TEST_NOTEBOOK.dateCreated,@@ -132,3 +202,78 @@ test("delete notebook", async () => {let notebook = db.getNotebook(TEST_NOTEBOOK.dateCreated);expect(notebook).toBeUndefined();});++//null & undefined checks++test("empty note should not be added", async () => {+ let res = await db.addNote({});+ expect(res).toBeUndefined();+});++test("note with empty text should not be added", async () => {+ let res = await db.addNote({ content: { text: "", delta: [] } });+ expect(res).toBeUndefined();+});++test("note with invalid delta should not be added", async () => {+ let res = await db.addNote({ content: { text: "sacsa", delta: undefined } });+ expect(res).toBeUndefined();+});++test("empty notebook should not be added", async () => {+ let res = await db.addNotebook({});+ expect(res).toBeUndefined();+});++test("deletion of unknown item should return false", async () => {+ let res = await db.deleteNotes(undefined);+ expect(res).toBe(false);+});++test("deletion of invalid items should cause continue and return true", async () => {+ let res = await db.deleteNotes([null, null, "2021"]);+ expect(res).toBe(true);+});++test("deletion of invalid topic from notebook should return false", async () => {+ await db.addNotebook(TEST_NOTEBOOK2);+ let res = await db.deleteTopicFromNotebook(+ TEST_NOTEBOOK2.dateCreated,+ "invalid_topic"+ );+ expect(res).toBe(false);+ await db.deleteNotebooks([TEST_NOTEBOOK2]);+});++test("moving note with wrong id should return false", async () => {+ let res = await db.moveNote(0, undefined, undefined);+ expect(res).toBe(false);+});++test("moving note to non-existent notebook should return false", async () => {+ let res = await db.moveNote(+ 29,+ { notebook: 2, topic: "2" },+ { notebook: 5, topic: "123" }+ );+ expect(res).toBe(false);+});++test("moving note to same notebook should return false", async () => {+ let res = await db.moveNote(+ 29,+ { notebook: 2, topic: "2" },+ { notebook: 2, topic: "2" }+ );+ expect(res).toBe(false);+});++test("search with empty query should return []", () => {+ let filtered = db.searchNotes("");+ expect(filtered).toStrictEqual([]);+});++test("getting invalid topic from invalid notebook should return undefined", async () => {+ let res = await db.getTopic(1213, "invalid_topic");+ expect(res).toBeUndefined();+});diff --git a/packages/core/__tests__/storage.test.js b/packages/core/__tests__/storage.test.jsnew file mode 100644index 000000000..3659b8b51--- /dev/null+++ b/packages/core/__tests__/storage.test.js@@ -0,0 +1,21 @@+import StorageInterface from "../../notes-web/src/interfaces/storage";+import Storage from "../helpers/storage";+const storage = new Storage(StorageInterface);+test("add a value", async () => {+ await storage.write("hello", "world");+ let value = await storage.read("hello");+ expect(value).toBe("world");+});++test("remove", async () => {+ await storage.remove("hello");+ let value = await storage.read("hello");+ expect(value).toBeUndefined();+});++test("clear", async () => {+ await storage.write("hello", "world");+ await storage.clear();+ let value = await storage.read("hello");+ expect(value).toBeUndefined();+});diff --git a/packages/core/api/database.js b/packages/core/api/database.jsindex b36de53f3..7f377c8c2 100644--- a/packages/core/api/database.js+++ b/packages/core/api/database.js@@ -32,7 +32,12 @@ class Database {* @param {object} note The note to add or update*/async addNote(note) {- if (!note || (!note.title && !note.content)) return undefined;+ if (+ !note ||+ (!note.title &&+ (!note.content || !note.content.text || !note.content.delta))+ )+ return;let timestamp = note.dateCreated || Date.now();//add or update a note into the database@@ -51,7 +56,9 @@ class Database {notebooks: note.notebooks || {},colors: note.colors || [],favorite: note.favorite || false,- headline: note.content.text.substring(0, 150) + "...",+ headline:+ note.content.text.substring(0, 150) ++ (note.content.text.length > 150 ? "..." : ""),length: note.content.text.length,dateEditted: Date.now(),dateCreated: timestamp@@ -65,7 +72,7 @@ class Database {* @param {array} notes the notes to be deleted*/async deleteNotes(notes) {- await deleteItems.call(this, notes, KEYS.notes);+ return await deleteItems.call(this, notes, KEYS.notes);}/**@@ -104,15 +111,29 @@ class Database {* @returns The ID of the added notebook*/async addNotebook(notebook) {- if (!notebook || !notebook.title) return;+ if (!notebook || !notebook.title) {+ return;+ }+ if (+ extractValues(this.notebooks).findIndex(+ nb => nb.title === notebook.title+ ) > -1+ ) {+ return;+ }+const id = notebook.dateCreated || Date.now();let topics = [makeTopic("General")];- if (notebook.topics) {- for (let topic of notebook.topics) {- if (!topic || topic.trim().length <= 0) continue;- topics[topics.length] = makeTopic(topic);- }+ for (let topic of notebook.topics) {+ if (+ !topic ||+ topic.trim().length <= 0 ||+ topics.findIndex(t => t.title === topic) > -1 //check for duplicate+ )+ continue;+ topics[topics.length] = makeTopic(topic);}+this.notebooks[id] = {type: "notebook",title: notebook.title,@@ -135,14 +156,11 @@ class Database {* @param {string} topic The topic to add*/addTopicToNotebook(notebookId, topic) {- return notebookTopicFn.call(- this,- notebookId,- topic,- notebook => (- (notebook.topics[notebook.topics.length] = makeTopic(topic)), true- )- );+ return notebookTopicFn.call(this, notebookId, topic, notebook => {+ if (notebook.topics.findIndex(t => t.title === topic) > -1) return false; //check for duplicates+ notebook.topics[notebook.topics.length] = makeTopic(topic);+ return true;+ });}/**@@ -171,7 +189,9 @@ class Database {notebookId,topic,noteId,- async (notebook, topicIndex) => {+ (notebook, topicIndex) => {+ if (notebook.topics[topicIndex].notes.indexOf(noteId) > -1)+ return false; //duplicate checknotebook.topics[topicIndex].notes.push(noteId);//increment totalNotes countnotebook.topics[topicIndex].totalNotes++;@@ -239,42 +259,61 @@ class Database {* @param {array} notebooks The notebooks to delete*/async deleteNotebooks(notebooks) {- await deleteItems.call(this, notebooks, KEYS.notebooks);+ return await deleteItems.call(this, notebooks, KEYS.notebooks);+ }++ async moveNote(noteId, from, to) {+ if (+ !noteId ||+ !from ||+ !to ||+ !from.notebook ||+ !to.notebook ||+ !from.topic ||+ !to.topic ||+ from.notebook === to.notebook //moving to same notebook shouldn't be possible+ )+ return false;+ if (await this.deleteNoteFromTopic(from.notebook, from.topic, noteId)) {+ return await this.addNoteToTopic(to.notebook, to.topic, noteId);+ }+ return false;}}export default Database;-async function deleteItems(items, key) {+function deleteItems(items, key) {if (!items || items.length <= 0 || !this[key] || this[key].length <= 0)- return; //TODO add test+ return false; //TODO add testfor (let item of items) {+ if (!item) continue;if (this[key].hasOwnProperty(item.dateCreated)) {delete this[key][item.dateCreated];}}- await this.storage.write(key, this[key]);+ return this.storage.write(key, this[key]).then(s => true);}function notebookTopicFn(notebookId, topic, fn) {- if (!notebookId || !topic || !this.notebooks[notebookId]) return;- let notebook = this.notebooks[notebookId];- if (fn(notebook)) {+ if (!notebookId || !topic || !this.notebooks[notebookId]) return false;+ const notebook = this.notebooks[notebookId];+ const result = fn(notebook);+ if (result === true) {this.notes[notebookId] = notebook;- return this.storage.write(KEYS.notebooks, this.notebooks);+ return this.storage.write(KEYS.notebooks, this.notebooks).then(s => true);}//TODO add test- return Promise.resolve();+ return result;}function topicNoteFn(notebookId, topic, noteId, fn) {- return notebookTopicFn.call(this, notebookId, topic, async notebook => {+ return notebookTopicFn.call(this, notebookId, topic, notebook => {let topicIndex = notebook.topics.findIndex(t => t.title === topic);if (topicIndex === -1 || !this.notes.hasOwnProperty(noteId)) return false;if (fn(notebook, topicIndex)) {- await this.storage.write(KEYS.notes, this.notes);- return true;+ return this.storage.write(KEYS.notes, this.notes).then(s => true);}return false;});