aboutsummaryrefslogtreecommitdiffstats
path: root/desktop/modules/notes/test_notes.js
diff options
context:
space:
mode:
Diffstat (limited to 'desktop/modules/notes/test_notes.js')
-rw-r--r--desktop/modules/notes/test_notes.js14
1 files changed, 13 insertions, 1 deletions
diff --git a/desktop/modules/notes/test_notes.js b/desktop/modules/notes/test_notes.js
index 955369c..6a316ce 100644
--- a/desktop/modules/notes/test_notes.js
+++ b/desktop/modules/notes/test_notes.js
@@ -18,7 +18,7 @@ const assert = require("assert");
const src = fs.readFileSync(__dirname + "/Notes.js", "utf8")
.replace(/^\.pragma library$/m, "");
-const N = new Function(src + "\nreturn { parse, stringify, sorted, formatDate, nextId,"
+const N = new Function(src + "\nreturn { parse, valid, stringify, sorted, formatDate, nextId,"
+ " filter, addNote, updateNote, removeNote, setLastpull };")();
const db = {
@@ -51,6 +51,18 @@ assert.deepStrictEqual(N.parse(N.stringify(db)), db);
assert.deepStrictEqual(N.parse("not json"), { notes: [] });
assert.deepStrictEqual(N.parse('{"a":1}'), { notes: [] });
+// valid(): the object, or null when the file cannot be read. A write must
+// refuse on null rather than replace an unreadable index, which is what a
+// conflicted db.json is: merge markers from an autostash pop over a remote
+// change. An empty or missing file is an empty store, not an error.
+assert.deepStrictEqual(N.valid(N.stringify(db)), db);
+assert.deepStrictEqual(N.valid(""), { notes: [] });
+assert.deepStrictEqual(N.valid(" \n"), { notes: [] });
+assert.strictEqual(N.valid("not json"), null);
+assert.strictEqual(N.valid('{"a":1}'), null);
+assert.strictEqual(N.valid("<<<<<<< HEAD\n{}\n=======\n{}\n>>>>>>> other\n"), null);
+assert.deepStrictEqual(N.parse("not json"), { notes: [] }); // display stays tolerant
+
// pure transforms preserve everything else and do not mutate
const added = N.addNote(db, "T", "1750");
assert.strictEqual(added.notes.length, 4);