Files
HelixNotes/tests/source-editor.test.mjs
Yuri Karamian 640b5e3d10 Improve editor performance and harden note saving
Replace the source editor with CodeMirror and reduce typing-time work.
Virtualize note lists, apply targeted refreshes, and batch search indexing.
Improve vault and in-note search, and bound graph layout work.
Make saves revision-safe and flush pending changes before navigation or close.
Preserve originating-vault ownership across async operations and detached windows.
Harden sync, attachment writes, and notebook/tag appearance updates.
Keep Trash previews read-only and skip restoring trashed notes.
Update AI model presets and isolate streamed responses by request.
Fix local image rendering and preserve portable image paths.
Include regression tests for the changed behavior.
2026-09-28 07:50:46 +02:00

103 lines
4.7 KiB
JavaScript

import assert from 'node:assert/strict';
import test from 'node:test';
import { EditorSelection } from '@codemirror/state';
import { indentMore, indentLess, moveLineDown } from '@codemirror/commands';
import { SourceController, sourceHardBreak, sourceSearchDecoration, wrapSourceSelection } from '../src/lib/editor/source/controller.ts';
import { closestSearchMatch, findSourceMatches } from '../src/lib/editor/search.ts';
const options = { matchCase: false, wholeWord: false };
const run = (editor, command) => command({ state: editor.state, dispatch: (tr) => editor.dispatch(tr) });
test('source edits preserve immutable save snapshots and isolate undo when another note loads', () => {
let dirty = 0;
const editor = new SourceController((_, edited) => { if (edited) dirty++; });
editor.load('# First\r\n日本語 😀\r\n');
const saved = editor.state.doc;
editor.select(saved.length);
editor.replace('tail', saved.length);
assert.equal(saved.toString(), '# First\n日本語 😀\n');
assert.equal(editor.state.doc.toString(), '# First\n日本語 😀\ntail');
assert.equal(dirty, 1);
assert.equal(editor.undo(), true);
assert.equal(editor.state.doc.toString(), saved.toString());
assert.equal(editor.redo(), true);
assert.equal(editor.state.doc.toString(), '# First\n日本語 😀\ntail');
editor.load('Second note');
assert.equal(editor.undo(), false);
assert.equal(editor.state.doc.toString(), 'Second note');
});
test('source pair wrapping keeps backwards Unicode selection and undo restores it', () => {
const editor = new SourceController();
editor.load('before 日本語😀 after');
editor.dispatch({ selection: EditorSelection.single(12, 7) });
editor.dispatch(wrapSourceSelection(editor.state, '('));
assert.equal(editor.state.doc.toString(), 'before (日本語😀) after');
assert.equal(editor.state.selection.main.anchor, 13);
assert.equal(editor.state.selection.main.head, 8);
assert.equal(editor.state.sliceDoc(8, 13), '日本語😀');
editor.undo();
assert.equal(editor.state.doc.toString(), 'before 日本語😀 after');
assert.equal(editor.state.selection.main.anchor, 12);
assert.equal(editor.state.selection.main.head, 7);
});
test('hard breaks, indentation and line moves are undoable and respect read-only mode', () => {
const editor = new SourceController();
editor.load('first\nsecond');
editor.select(5);
assert.equal(run(editor, sourceHardBreak), true);
assert.equal(editor.state.doc.toString(), 'first \n\nsecond');
editor.undo();
editor.select(1);
run(editor, indentMore);
assert.equal(editor.state.doc.toString(), '\tfirst\nsecond');
run(editor, indentLess);
run(editor, moveLineDown);
assert.equal(editor.state.doc.toString(), 'second\nfirst');
editor.configure(true, true);
const before = editor.state.doc;
assert.equal(run(editor, sourceHardBreak), false);
assert.equal(run(editor, indentMore), false);
assert.equal(run(editor, moveLineDown), false);
assert.equal(wrapSourceSelection(editor.state, '['), null);
assert.equal(editor.state.doc, before);
});
test('live source search maps the current occurrence without resetting caret or dirty state', () => {
let changes = 0;
let anchor = 0;
const editor = new SourceController((mapping) => { changes++; anchor = mapping.mapPos(anchor); });
editor.load('İ 😀 中文 cat cat');
let matches = findSourceMatches(editor.state.doc.toString(), 'cat', options);
anchor = matches[1].from;
editor.highlight(matches[1], true);
assert.equal(changes, 0);
editor.replace('prefix ', 0, 0);
const selection = editor.state.selection;
matches = findSourceMatches(editor.state.doc.toString(), 'cat', options);
const index = closestSearchMatch(matches, anchor);
assert.equal(index, 1);
editor.highlight(matches[index]);
assert.equal(editor.state.selection, selection);
assert.equal(changes, 1);
const ranges = [];
editor.state.field(sourceSearchDecoration).between(0, editor.state.doc.length, (from, to) => ranges.push({ from, to }));
assert.deepEqual(ranges, [matches[1]]);
editor.undo();
matches = findSourceMatches(editor.state.doc.toString(), 'cat', options);
assert.equal(closestSearchMatch(matches, anchor), 1);
});
test('asset representation updates do not clear edits or become their own undo steps', () => {
let dirty = 0;
const editor = new SourceController((_, edited) => { if (edited) dirty++; });
editor.load('![image](blob:image)');
editor.replace(' user edit', editor.state.doc.length);
editor.replace('.helixnotes/attachments/image.png', 9, 19, true);
assert.equal(dirty, 1);
assert.equal(editor.state.doc.toString(), '![image](.helixnotes/attachments/image.png) user edit');
editor.undo();
assert.equal(editor.state.doc.toString(), '![image](.helixnotes/attachments/image.png)');
});