core: make sure all tests are passing

This commit is contained in:
Abdullah Atta
2023-08-30 10:42:24 +05:00
committed by Abdullah Atta
parent fb26229715
commit a700787cf3
5 changed files with 85 additions and 46 deletions

View File

@@ -24,7 +24,6 @@ test("adding a deleted content should not throw", () =>
databaseTest().then(async (db) => { databaseTest().then(async (db) => {
await expect( await expect(
db.content.add({ db.content.add({
remote: true,
deleted: true, deleted: true,
dateEdited: new Date(), dateEdited: new Date(),
id: "hello", id: "hello",

View File

@@ -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 { notebookTest, TEST_NOTEBOOK, TEST_NOTE, delay } from "./utils";
import { makeTopic } from "../src/collections/topics"; import { makeTopic } from "../src/collections/topics";
import { test, expect } from "vitest"; import { test, expect } from "vitest";
import qclone from "qclone";
test("add a notebook", () => test("add a notebook", () =>
notebookTest().then(({ db, id }) => { notebookTest().then(({ db, id }) => {
@@ -62,47 +63,61 @@ test("merge notebook with new topics", () =>
notebookTest().then(async ({ db, id }) => { notebookTest().then(async ({ db, id }) => {
let notebook = db.notebooks.notebook(id); let notebook = db.notebooks.notebook(id);
const newNotebook = { ...notebook.data, remote: true }; const newNotebook = db.notebooks.merge(notebook.data, {
newNotebook.topics.push(makeTopic("Home", id)); ...notebook.data,
topics: [...notebook.data.topics, makeTopic("Home", id)],
remote: true
});
await expect(db.notebooks.merge(newNotebook)).resolves.not.toThrow(); expect(
newNotebook.topics.findIndex((t) => t.title === "Home")
expect(notebook.topics.has("Home")).toBe(true); ).toBeGreaterThanOrEqual(0);
expect(notebook.topics.has("hello")).toBe(true); expect(
newNotebook.topics.findIndex((t) => t.title === "hello")
).toBeGreaterThanOrEqual(0);
})); }));
test("merge notebook with topics removed", () => test("merge notebook with topics removed", () =>
notebookTest().then(async ({ db, id }) => { notebookTest().then(async ({ db, id }) => {
let notebook = db.notebooks.notebook(id); let notebook = db.notebooks.notebook(id);
const newNotebook = { ...notebook.data, remote: true }; const newNotebook = db.notebooks.merge(notebook.data, {
newNotebook.topics.splice(0, 1); // remove hello topic ...notebook.data,
newNotebook.topics.push(makeTopic("Home", id)); topics: [makeTopic("Home", id)],
remote: true
});
await expect(db.notebooks.merge(newNotebook)).resolves.not.toThrow(); expect(
newNotebook.topics.findIndex((t) => t.title === "Home")
expect(notebook.topics.has("Home")).toBe(true); ).toBeGreaterThanOrEqual(0);
expect(notebook.topics.has("hello")).toBe(false); expect(
newNotebook.topics.findIndex((t) => t.title === "hello")
).toBeLessThan(0);
})); }));
test("merge notebook with topic edited", () => test("merge notebook with topic edited", () =>
notebookTest().then(async ({ db, id }) => { notebookTest().then(async ({ db, id }) => {
let notebook = db.notebooks.notebook(id); let notebook = db.notebooks.notebook(id);
const newNotebook = { ...notebook.data, remote: true }; const newNotebook = db.notebooks.merge(notebook.data, {
newNotebook.topics[0].title = "hello (edited)"; ...notebook.data,
topics: [{ ...notebook.data.topics[0], title: "hello (edited)" }],
remote: true
});
await expect(db.notebooks.merge(newNotebook)).resolves.not.toThrow(); expect(
newNotebook.topics.findIndex((t) => t.title === "hello (edited)")
expect(notebook.topics.has("hello (edited)")).toBe(true); ).toBeGreaterThanOrEqual(0);
expect(notebook.topics.has("hello")).toBe(false); expect(
newNotebook.topics.findIndex((t) => t.title === "hello")
).toBeLessThan(0);
})); }));
test("merge notebook when local notebook is also edited", () => test("merge notebook when local notebook is also edited", () =>
notebookTest().then(async ({ db, id }) => { notebookTest().then(async ({ db, id }) => {
let notebook = db.notebooks.notebook(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)"; newNotebook.topics[0].title = "hello (edited)";
await delay(500); await delay(500);
@@ -112,11 +127,20 @@ test("merge notebook when local notebook is also edited", () =>
title: "hello (edited too)" title: "hello (edited too)"
}); });
await expect(db.notebooks.merge(newNotebook)).resolves.not.toThrow(); newNotebook = db.notebooks.merge(
db.notebooks.notebook(id).data,
expect(notebook.topics.has("hello (edited too)")).toBe(true); newNotebook,
expect(notebook.topics.has("hello (edited)")).toBe(false); 0
expect(notebook.topics.has("hello")).toBe(false); );
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", () => 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 note
); );
const newNotebook = { ...notebook.data, remote: true }; const newNotebook = db.notebooks.merge(notebook.data, {
...notebook.data,
await expect(db.notebooks.merge(newNotebook)).resolves.not.toThrow(); remote: true
});
expect(db.notebooks.notebook(id).dateEdited).toBe(newNotebook.dateEdited); 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 }) => { notebookTest().then(async ({ db, id }) => {
let notebook = db.notebooks.notebook(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 newNotebook.topics.splice(0, 1); // remove hello topic
await db.storage.write("lastSynced", Date.now()); const lastSynced = Date.now();
await delay(500); await delay(500);
@@ -152,8 +177,16 @@ test("merge notebook with topic removed that is edited in the local notebook", (
title: "hello (i exist)" 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(
expect(notebook.topics.has("hello")).toBe(false); newNotebook.topics.findIndex((t) => t.title === "hello (i exist)")
).toBeGreaterThanOrEqual(0);
expect(
newNotebook.topics.findIndex((t) => t.title === "hello")
).toBeLessThan(0);
})); }));

View File

@@ -30,10 +30,10 @@ test("settings' dateModified should not update on init", () =>
test("settings' dateModified should update after merge conflict resolve", () => test("settings' dateModified should update after merge conflict resolve", () =>
databaseTest().then(async (db) => { databaseTest().then(async (db) => {
await db.storage.write("lastSynced", 0); // await db.storage.write("lastSynced", 0);
const beforeDateModified = (db.settings._settings.dateModified = 1); const beforeDateModified = (db.settings.raw.dateModified = 1);
await db.settings.merge({ groupOptions: {}, aliases: {} }); await db.settings.merge({ groupOptions: {}, aliases: {} }, 0);
const afterDateModified = db.settings._settings.dateModified; const afterDateModified = db.settings.raw.dateModified;
expect(afterDateModified).toBeGreaterThan(beforeDateModified); expect(afterDateModified).toBeGreaterThan(beforeDateModified);
})); }));

View File

@@ -26,11 +26,14 @@ export default class Content extends Collection {
async add(content) { async add(content) {
if (!content) return; if (!content) return;
if ( if (content.remote)
!!content.data && throw new Error(
(!!content.data.data || (!content.data.iv && !content.data.cipher)) "Please do not use this method for merging. Instead add the item directly to database."
) { );
if (content.data.data && content.data.data.length) 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; content.data = content.data.data;
else if (!content.data.iv && !content.data.cipher) else if (!content.data.iv && !content.data.cipher)
content.data = `<p>Content is invalid: ${JSON.stringify( content.data = `<p>Content is invalid: ${JSON.stringify(

View File

@@ -66,10 +66,14 @@ class Migrator {
); );
if (migrated || restore) { if (migrated || restore) {
if (collection.dbCollection.merge) { if (collection.type === "settings") {
await collection.dbCollection.merge(item); await collection.dbCollection.merge(item);
} else if (collection.dbCollection.add) { } else if (collection.dbCollection._collection) {
await collection.dbCollection.add(item); 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. // if id changed after migration, we need to delete the old one.