Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

core: make sure all tests are passing

Abdullah Attaa700787cf3ef
diff --git a/packages/core/__tests__/content.test.js b/packages/core/__tests__/content.test.js
index 9f5e81480..fcfb9c7d5 100644
--- a/packages/core/__tests__/content.test.js
+++ b/packages/core/__tests__/content.test.js
@@ -24,7 +24,6 @@ test("adding a deleted content should not throw", () =>
databaseTest().then(async (db) => {
await expect(
db.content.add({
- remote: true,
deleted: true,
dateEdited: new Date(),
id: "hello",
diff --git a/packages/core/__tests__/notebooks.test.js b/packages/core/__tests__/notebooks.test.js
index 8ae6eaf6a..d48cec1c0 100644
--- a/packages/core/__tests__/notebooks.test.js
+++ b/packages/core/__tests__/notebooks.test.js
@@ -20,6 +20,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
import { notebookTest, TEST_NOTEBOOK, TEST_NOTE, delay } from "./utils";
import { makeTopic } from "../src/collections/topics";
import { test, expect } from "vitest";
+import qclone from "qclone";
test("add a notebook", () =>
notebookTest().then(({ db, id }) => {
@@ -62,47 +63,61 @@ test("merge notebook with new topics", () =>
notebookTest().then(async ({ db, id }) => {
let notebook = db.notebooks.notebook(id);
- const newNotebook = { ...notebook.data, remote: true };
- newNotebook.topics.push(makeTopic("Home", id));
-
- await expect(db.notebooks.merge(newNotebook)).resolves.not.toThrow();
+ const newNotebook = db.notebooks.merge(notebook.data, {
+ ...notebook.data,
+ topics: [...notebook.data.topics, makeTopic("Home", id)],
+ remote: true
+ });
- expect(notebook.topics.has("Home")).toBe(true);
- expect(notebook.topics.has("hello")).toBe(true);
+ expect(
+ newNotebook.topics.findIndex((t) => t.title === "Home")
+ ).toBeGreaterThanOrEqual(0);
+ expect(
+ newNotebook.topics.findIndex((t) => t.title === "hello")
+ ).toBeGreaterThanOrEqual(0);
}));
test("merge notebook with topics removed", () =>
notebookTest().then(async ({ db, id }) => {
let notebook = db.notebooks.notebook(id);
- const newNotebook = { ...notebook.data, remote: true };
- newNotebook.topics.splice(0, 1); // remove hello topic
- newNotebook.topics.push(makeTopic("Home", id));
-
- await expect(db.notebooks.merge(newNotebook)).resolves.not.toThrow();
+ const newNotebook = db.notebooks.merge(notebook.data, {
+ ...notebook.data,
+ topics: [makeTopic("Home", id)],
+ remote: true
+ });
- expect(notebook.topics.has("Home")).toBe(true);
- expect(notebook.topics.has("hello")).toBe(false);
+ expect(
+ newNotebook.topics.findIndex((t) => t.title === "Home")
+ ).toBeGreaterThanOrEqual(0);
+ expect(
+ newNotebook.topics.findIndex((t) => t.title === "hello")
+ ).toBeLessThan(0);
}));
test("merge notebook with topic edited", () =>
notebookTest().then(async ({ db, id }) => {
let notebook = db.notebooks.notebook(id);
- const newNotebook = { ...notebook.data, remote: true };
- newNotebook.topics[0].title = "hello (edited)";
-
- await expect(db.notebooks.merge(newNotebook)).resolves.not.toThrow();
+ const newNotebook = db.notebooks.merge(notebook.data, {
+ ...notebook.data,
+ topics: [{ ...notebook.data.topics[0], title: "hello (edited)" }],
+ remote: true
+ });
- expect(notebook.topics.has("hello (edited)")).toBe(true);
- expect(notebook.topics.has("hello")).toBe(false);
+ expect(
+ newNotebook.topics.findIndex((t) => t.title === "hello (edited)")
+ ).toBeGreaterThanOrEqual(0);
+ expect(
+ newNotebook.topics.findIndex((t) => t.title === "hello")
+ ).toBeLessThan(0);
}));
test("merge notebook when local notebook is also edited", () =>
notebookTest().then(async ({ db, id }) => {
let notebook = db.notebooks.notebook(id);
- const newNotebook = { ...notebook.data, remote: true };
+ let newNotebook = { ...qclone(notebook.data), remote: true };
newNotebook.topics[0].title = "hello (edited)";
await delay(500);
@@ -112,11 +127,20 @@ test("merge notebook when local notebook is also edited", () =>
title: "hello (edited too)"
});
- await expect(db.notebooks.merge(newNotebook)).resolves.not.toThrow();
-
- expect(notebook.topics.has("hello (edited too)")).toBe(true);
- expect(notebook.topics.has("hello (edited)")).toBe(false);
- expect(notebook.topics.has("hello")).toBe(false);
+ newNotebook = db.notebooks.merge(
+ db.notebooks.notebook(id).data,
+ newNotebook,
+ 0
+ );
+ expect(
+ newNotebook.topics.findIndex((t) => t.title === "hello (edited too)")
+ ).toBeGreaterThanOrEqual(0);
+ expect(
+ newNotebook.topics.findIndex((t) => t.title === "hello (edited)")
+ ).toBeLessThan(0);
+ expect(
+ newNotebook.topics.findIndex((t) => t.title === "hello")
+ ).toBeLessThan(0);
}));
test("merging notebook when local notebook is not edited should not update remote notebook dateEdited", () =>
@@ -129,9 +153,10 @@ test("merging notebook when local notebook is not edited should not update remot
note
);
- const newNotebook = { ...notebook.data, remote: true };
-
- await expect(db.notebooks.merge(newNotebook)).resolves.not.toThrow();
+ const newNotebook = db.notebooks.merge(notebook.data, {
+ ...notebook.data,
+ remote: true
+ });
expect(db.notebooks.notebook(id).dateEdited).toBe(newNotebook.dateEdited);
}));
@@ -140,10 +165,10 @@ test("merge notebook with topic removed that is edited in the local notebook", (
notebookTest().then(async ({ db, id }) => {
let notebook = db.notebooks.notebook(id);
- const newNotebook = { ...notebook.data, remote: true };
+ let newNotebook = { ...qclone(notebook.data), remote: true };
newNotebook.topics.splice(0, 1); // remove hello topic
- await db.storage.write("lastSynced", Date.now());
+ const lastSynced = Date.now();
await delay(500);
@@ -152,8 +177,16 @@ test("merge notebook with topic removed that is edited in the local notebook", (
title: "hello (i exist)"
});
- await expect(db.notebooks.merge(newNotebook)).resolves.not.toThrow();
+ newNotebook = db.notebooks.merge(
+ db.notebooks.notebook(id).data,
+ newNotebook,
+ lastSynced
+ );
- expect(notebook.topics.has("hello (i exist)")).toBe(true);
- expect(notebook.topics.has("hello")).toBe(false);
+ expect(
+ newNotebook.topics.findIndex((t) => t.title === "hello (i exist)")
+ ).toBeGreaterThanOrEqual(0);
+ expect(
+ newNotebook.topics.findIndex((t) => t.title === "hello")
+ ).toBeLessThan(0);
}));
diff --git a/packages/core/__tests__/settings.test.js b/packages/core/__tests__/settings.test.js
index fbb22fae1..66c8af085 100644
--- a/packages/core/__tests__/settings.test.js
+++ b/packages/core/__tests__/settings.test.js
@@ -30,10 +30,10 @@ test("settings' dateModified should not update on init", () =>
test("settings' dateModified should update after merge conflict resolve", () =>
databaseTest().then(async (db) => {
- await db.storage.write("lastSynced", 0);
- const beforeDateModified = (db.settings._settings.dateModified = 1);
- await db.settings.merge({ groupOptions: {}, aliases: {} });
- const afterDateModified = db.settings._settings.dateModified;
+ // await db.storage.write("lastSynced", 0);
+ const beforeDateModified = (db.settings.raw.dateModified = 1);
+ await db.settings.merge({ groupOptions: {}, aliases: {} }, 0);
+ const afterDateModified = db.settings.raw.dateModified;
expect(afterDateModified).toBeGreaterThan(beforeDateModified);
}));
diff --git a/packages/core/src/collections/content.js b/packages/core/src/collections/content.js
index e94530d3c..699c216d6 100644
--- a/packages/core/src/collections/content.js
+++ b/packages/core/src/collections/content.js
@@ -26,11 +26,14 @@ export default class Content extends Collection {
async add(content) {
if (!content) return;
- if (
- !!content.data &&
- (!!content.data.data || (!content.data.iv && !content.data.cipher))
- ) {
- if (content.data.data && content.data.data.length)
+ if (content.remote)
+ throw new Error(
+ "Please do not use this method for merging. Instead add the item directly to database."
+ );
+ if (content.deleted) return await this._collection.addItem(content);
+
+ if (typeof content.data === "object") {
+ if (typeof content.data.data === "string")
content.data = content.data.data;
else if (!content.data.iv && !content.data.cipher)
content.data = `<p>Content is invalid: ${JSON.stringify(
diff --git a/packages/core/src/database/migrator.js b/packages/core/src/database/migrator.js
index 4cbab4038..f44196f68 100644
--- a/packages/core/src/database/migrator.js
+++ b/packages/core/src/database/migrator.js
@@ -66,10 +66,14 @@ class Migrator {
);
if (migrated || restore) {
- if (collection.dbCollection.merge) {
+ if (collection.type === "settings") {
await collection.dbCollection.merge(item);
- } else if (collection.dbCollection.add) {
- await collection.dbCollection.add(item);
+ } else if (collection.dbCollection._collection) {
+ await collection.dbCollection._collection?.addItem(item);
+ } else {
+ throw new Error(
+ `No idea how to handle this kind of item: ${item.type}.`
+ );
}
// if id changed after migration, we need to delete the old one.