trilium/spec/search/note_cache_mocking.js

71 lines
1.7 KiB
JavaScript
Raw Normal View History

2020-06-03 22:24:41 +08:00
const Note = require('../../src/services/note_cache/entities/note.js');
const Branch = require('../../src/services/note_cache/entities/branch.js');
const Attribute = require('../../src/services/note_cache/entities/attribute.js');
const noteCache = require('../../src/services/note_cache/note_cache.js');
2020-05-25 06:25:47 +08:00
const randtoken = require('rand-token').generator({source: 'crypto'});
/** @return {Note} */
function findNoteByTitle(searchResults, title) {
return searchResults
.map(sr => noteCache.notes[sr.noteId])
.find(note => note.title === title);
}
class NoteBuilder {
constructor(note) {
this.note = note;
}
label(name, value = '', isInheritable = false) {
new Attribute(noteCache, {
attributeId: id(),
noteId: this.note.noteId,
type: 'label',
isInheritable,
name,
value
});
return this;
}
relation(name, targetNote) {
new Attribute(noteCache, {
attributeId: id(),
noteId: this.note.noteId,
type: 'relation',
name,
value: targetNote.noteId
});
return this;
}
child(childNoteBuilder, prefix = "") {
new Branch(noteCache, {
branchId: id(),
noteId: childNoteBuilder.note.noteId,
parentNoteId: this.note.noteId,
prefix
});
return this;
}
}
function id() {
return randtoken.generate(10);
}
2020-08-31 05:42:24 +08:00
function note(title, type = 'text', mime = 'text/html') {
const note = new Note(noteCache, {noteId: id(), title, type, mime});
2020-05-25 06:25:47 +08:00
return new NoteBuilder(note);
}
module.exports = {
NoteBuilder,
findNoteByTitle,
note
};