Skip to content

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

test: fix all broken tests

thecodrr31c1e8900c17
diff --git a/packages/core/__mocks__/fs.mock.js b/packages/core/__mocks__/fs.mock.js
new file mode 100644
index 000000000..146894b8a
--- /dev/null
+++ b/packages/core/__mocks__/fs.mock.js
@@ -0,0 +1,94 @@
+const fs = {};
+
+function hasItem(key) {
+ return !!fs[key];
+}
+
+/**
+ * We perform 4 steps here:
+ * 1. We convert base64 to Uint8Array (if we get base64, that is)
+ * 2. We hash the Uint8Array.
+ * 3. We encrypt the Uint8Array
+ * 4. We save the encrypted Uint8Array
+ */
+async function writeEncrypted(filename, { data, type, key }) {
+ const { hash, type: hashType } = hashBuffer(data);
+ if (!filename) filename = hash;
+ if (hasItem(filename)) return { hash, hashType };
+ fs[filename] = data;
+ return {
+ hash,
+ hashType,
+ iv: "some iv",
+ cipher: data,
+ salt: "i am some salt",
+ length: data.length,
+ };
+}
+
+function hashBuffer(data) {
+ return {
+ hash: hashCode(data).toString(16),
+ type: "xxh3",
+ };
+}
+
+async function readEncrypted(filename, key, cipherData) {
+ const cipher = fs[filename];
+ if (!cipher) {
+ console.error(`File not found. Filename: ${filename}`);
+ return null;
+ }
+ return cipher.data;
+}
+
+async function uploadFile(filename, requestOptions) {
+ let cipher = fs[filename];
+ if (!cipher) throw new Error(`File not found. Filename: ${filename}`);
+ return true;
+}
+
+async function downloadFile(filename, requestOptions) {
+ return hasItem(filename);
+}
+
+async function deleteFile(filename, requestOptions) {
+ if (!hasItem(filename)) return true;
+ delete fs[filename];
+ return true;
+}
+
+function exists(filename) {
+ return hasItem(filename);
+}
+
+module.exports = {
+ writeEncrypted,
+ readEncrypted,
+ uploadFile: cancellable(uploadFile),
+ downloadFile: cancellable(downloadFile),
+ deleteFile,
+ exists,
+};
+
+function cancellable(operation) {
+ return function (filename, requestOptions) {
+ return {
+ execute: () => operation(filename, requestOptions),
+ cancel: () => {},
+ };
+ };
+}
+
+function hashCode(str) {
+ var hash = 0,
+ i,
+ chr;
+ if (str.length === 0) return hash;
+ for (i = 0; i < str.length; i++) {
+ chr = str.charCodeAt(i);
+ hash = (hash << 5) - hash + chr;
+ hash |= 0; // Convert to 32bit integer
+ }
+ return hash;
+}
diff --git a/packages/core/__tests__/notes.test.js b/packages/core/__tests__/notes.test.js
index 031d33c9c..10e204e5c 100644
--- a/packages/core/__tests__/notes.test.js
+++ b/packages/core/__tests__/notes.test.js
@@ -243,11 +243,7 @@ test("moving note to same notebook and topic should do nothing", () =>
test("export note to html", () =>
noteTest().then(async ({ db, id }) => {
const html = await db.notes.note(id).export("html");
- expect(
- html.includes(
- `<p>Hello<br/><span style='color:#f00'>This is colorful</span></p>`
- )
- ).toBeTruthy();
+ expect(html.includes(TEST_NOTE.content.data)).toBeTruthy();
}));
test("export note to md", () =>
diff --git a/packages/core/__tests__/utils/index.js b/packages/core/__tests__/utils/index.js
index 64f5d9c6d..92e8516ac 100644
--- a/packages/core/__tests__/utils/index.js
+++ b/packages/core/__tests__/utils/index.js
@@ -29,7 +29,7 @@ const notebookTest = (notebook = TEST_NOTEBOOK) =>
var TEST_NOTE = {
content: {
type: "tiny",
- data: `<p>Hello<br/><span style='color:#f00'>This is colorful</span></p>`,
+ data: `<p>Hello<br><span style="color:#f00">This is colorful</span></p>`,
},
};
diff --git a/packages/core/__tests__/vault.test.js b/packages/core/__tests__/vault.test.js
index b4e74ad14..20002c6eb 100644
--- a/packages/core/__tests__/vault.test.js
+++ b/packages/core/__tests__/vault.test.js
@@ -48,7 +48,7 @@ test("lock a note", () =>
expect(note.headline).toBe("");
- const content = await db.content.raw(note.data.contentId);
+ const content = await db.content.raw(note.data.contentId, false);
expect(content.noteId).toBeDefined();
expect(content.data.iv).toBeDefined();
expect(content.data.cipher).toBeDefined();
diff --git a/packages/core/api/vault.js b/packages/core/api/vault.js
index 6e4da674b..3b52e70c0 100644
--- a/packages/core/api/vault.js
+++ b/packages/core/api/vault.js
@@ -223,7 +223,6 @@ export default class Vault {
data = content.data;
type = content.type;
}
- console.log("encrypting data:", data);
await this._encryptContent(contentId, data, type, password);
diff --git a/packages/core/collections/content.js b/packages/core/collections/content.js
index 8532501ca..4474661e7 100644
--- a/packages/core/collections/content.js
+++ b/packages/core/collections/content.js
@@ -42,7 +42,7 @@ export default class Content extends Collection {
return content.data;
}
- async raw(id, withAttachments = true) {
+ async raw(id, withAttachments = false) {
const content = await this._collection.getItem(id);
if (!content) return;
return withAttachments ? await this.insertAttachments(content) : content;
diff --git a/packages/core/jest.setup.js b/packages/core/jest.setup.js
index ea61d2759..8f81eded3 100644
--- a/packages/core/jest.setup.js
+++ b/packages/core/jest.setup.js
@@ -8,3 +8,8 @@ const dom = new jsdom.JSDOM("", {});
global.window = dom.window;
global.document = dom.window.document;
global.crypto = nodeCrypto;
+
+global.HTMLParser = new dom.window.DOMParser().parseFromString(
+ "<body></body>",
+ "text/html"
+);
diff --git a/packages/core/models/note.js b/packages/core/models/note.js
index 277dfcb19..4316150c1 100644
--- a/packages/core/models/note.js
+++ b/packages/core/models/note.js
@@ -86,8 +86,12 @@ export default class Note {
}
}
- content() {
- return this._db.content.get(this._note.contentId);
+ async content(withAttachments = false) {
+ const content = await this._db.content.raw(
+ this._note.contentId,
+ withAttachments
+ );
+ return content.data;
}
async color(color) {