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) => {
await expect(
db.content.add({
remote: true,
deleted: true,
dateEdited: new Date(),
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 { 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));
const newNotebook = db.notebooks.merge(notebook.data, {
...notebook.data,
topics: [...notebook.data.topics, makeTopic("Home", id)],
remote: true
});
await expect(db.notebooks.merge(newNotebook)).resolves.not.toThrow();
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));
const newNotebook = db.notebooks.merge(notebook.data, {
...notebook.data,
topics: [makeTopic("Home", id)],
remote: true
});
await expect(db.notebooks.merge(newNotebook)).resolves.not.toThrow();
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)";
const newNotebook = db.notebooks.merge(notebook.data, {
...notebook.data,
topics: [{ ...notebook.data.topics[0], title: "hello (edited)" }],
remote: true
});
await expect(db.notebooks.merge(newNotebook)).resolves.not.toThrow();
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);
}));

View File

@@ -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);
}));

View File

@@ -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(

View File

@@ -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.