mirror of
https://github.com/Dasemu/Synapse.git
synced 2026-11-26 15:26:17 +00:00
feat(app): Flutter outliner with journals, page view and timeline
Logseq-like outliner (render/edit Markdown per block, Enter/Tab/Shift-Tab, backspace merge, arrow navigation, drag to reorder, collapse, [[ # (( autocomplete, TODO/DONE), journals feed with prev/next links, page view with linked references and related panel, virtualised CustomPainter timeline, Ctrl+K quick switcher, sidebar with recents, light/dark theme with bundled Inter, Noto Serif and Source Code Pro. Edits are optimistic and serialised to the core through one write queue. Cargokit builds synapse-ffi; its build tool is patched to use the library name (synapse_ffi) of hyphenated crates. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
c3d6636606
commit
df42d4010f
135 files changed
+17689
No files matched your search
@@ -0,0 +1,347 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:synapse/src/editor/outline_view.dart';
|
||||
import 'package:synapse/src/state/app_state.dart';
|
||||
import 'package:synapse/src/state/outline_controller.dart';
|
||||
import 'package:synapse/src/theme/theme.dart';
|
||||
|
||||
import 'support/memory_backend.dart';
|
||||
|
||||
class Harness {
|
||||
Harness(this.backend, this.app, this.controller, this.pageId);
|
||||
final MemoryBackend backend;
|
||||
final AppState app;
|
||||
final OutlineController controller;
|
||||
final String pageId;
|
||||
|
||||
List<(int, String)> get local => [
|
||||
for (final n in controller.nodes) (n.depth, n.content),
|
||||
];
|
||||
|
||||
List<(int, String)> get stored => [
|
||||
for (final n in backend.outlines[pageId]!) (n.depth, n.content),
|
||||
];
|
||||
}
|
||||
|
||||
Future<Harness> pumpOutline(
|
||||
WidgetTester tester,
|
||||
List<(int, String)> rows, {
|
||||
List<String> otherPages = const [],
|
||||
}) async {
|
||||
final backend = MemoryBackend();
|
||||
final page = backend.createPage('Test');
|
||||
for (final t in otherPages) {
|
||||
backend.createPage(t);
|
||||
}
|
||||
backend.seed(page.id, rows);
|
||||
final app = AppState(backend);
|
||||
final view = await backend.page(page.id);
|
||||
final controller = OutlineController(
|
||||
backend: backend,
|
||||
queue: app.queue,
|
||||
pageId: page.id,
|
||||
blocks: view!.blocks,
|
||||
saveDelay: const Duration(milliseconds: 50),
|
||||
);
|
||||
addTearDown(controller.dispose);
|
||||
await tester.pumpWidget(
|
||||
AppScope(
|
||||
state: app,
|
||||
child: MaterialApp(
|
||||
theme: buildTheme(Brightness.light),
|
||||
home: Scaffold(
|
||||
body: CustomScrollView(
|
||||
slivers: [OutlineSliver(controller: controller)],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
return Harness(backend, app, controller, page.id);
|
||||
}
|
||||
|
||||
/// Focuses the block with `content` and puts the caret at `caret` (end by default).
|
||||
Future<void> edit(
|
||||
WidgetTester tester,
|
||||
Harness h,
|
||||
String content, {
|
||||
int? caret,
|
||||
}) async {
|
||||
final n = h.controller.nodes.firstWhere((n) => n.content == content);
|
||||
h.controller.focus(n.id, caret: caret ?? content.length);
|
||||
await tester.pump();
|
||||
await tester.pump();
|
||||
}
|
||||
|
||||
TextEditingController field(WidgetTester tester) =>
|
||||
tester.widget<TextField>(find.byType(TextField)).controller!;
|
||||
|
||||
String? focusedContent(Harness h) {
|
||||
final id = h.controller.focusedId;
|
||||
return id == null ? null : h.controller.byId(id)?.content;
|
||||
}
|
||||
|
||||
Future<void> settle(WidgetTester tester, Harness h) async {
|
||||
await tester.pump(const Duration(milliseconds: 100));
|
||||
await h.app.queue.idle;
|
||||
await tester.pump(const Duration(seconds: 1));
|
||||
await h.app.queue.idle;
|
||||
await tester.pump();
|
||||
}
|
||||
|
||||
Future<void> withShift(WidgetTester tester, LogicalKeyboardKey key) async {
|
||||
await tester.sendKeyDownEvent(LogicalKeyboardKey.shiftLeft);
|
||||
await tester.sendKeyEvent(key);
|
||||
await tester.sendKeyUpEvent(LogicalKeyboardKey.shiftLeft);
|
||||
await tester.pump();
|
||||
}
|
||||
|
||||
void main() {
|
||||
testWidgets('Enter splits the block at the caret and focuses the new one', (
|
||||
tester,
|
||||
) async {
|
||||
final h = await pumpOutline(tester, [(0, 'hello world')]);
|
||||
await edit(tester, h, 'hello world', caret: 5);
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.enter);
|
||||
await tester.pump();
|
||||
await tester.pump();
|
||||
|
||||
expect(h.local, [(0, 'hello'), (0, ' world')]);
|
||||
expect(focusedContent(h), ' world');
|
||||
expect(field(tester).selection.baseOffset, 0);
|
||||
|
||||
await settle(tester, h);
|
||||
expect(h.stored, [(0, 'hello'), (0, ' world')]);
|
||||
expect(h.controller.nodes.every((n) => !n.id.startsWith('_tmp')), isTrue);
|
||||
});
|
||||
|
||||
testWidgets('Enter on a block with children inserts its first child', (
|
||||
tester,
|
||||
) async {
|
||||
final h = await pumpOutline(tester, [(0, 'parent'), (1, 'child')]);
|
||||
await edit(tester, h, 'parent');
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.enter);
|
||||
await settle(tester, h);
|
||||
expect(h.stored, [(0, 'parent'), (1, ''), (1, 'child')]);
|
||||
});
|
||||
|
||||
testWidgets('Shift+Enter inserts a newline inside the block', (tester) async {
|
||||
final h = await pumpOutline(tester, [(0, 'a')]);
|
||||
await edit(tester, h, 'a');
|
||||
await tester.sendKeyDownEvent(LogicalKeyboardKey.shiftLeft);
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.enter);
|
||||
// The platform inserts the newline as text input.
|
||||
await tester.enterText(find.byType(TextField), 'a\n');
|
||||
await tester.sendKeyUpEvent(LogicalKeyboardKey.shiftLeft);
|
||||
await settle(tester, h);
|
||||
expect(h.stored, [(0, 'a\n')]);
|
||||
});
|
||||
|
||||
testWidgets('a newline typed by a soft keyboard splits the block', (
|
||||
tester,
|
||||
) async {
|
||||
final h = await pumpOutline(tester, [(0, 'ab')]);
|
||||
await edit(tester, h, 'ab', caret: 1);
|
||||
tester.testTextInput.updateEditingValue(
|
||||
const TextEditingValue(
|
||||
text: 'a\nb',
|
||||
selection: TextSelection.collapsed(offset: 2),
|
||||
),
|
||||
);
|
||||
await settle(tester, h);
|
||||
expect(h.stored, [(0, 'a'), (0, 'b')]);
|
||||
});
|
||||
|
||||
testWidgets('Tab indents and Shift+Tab outdents, keeping focus', (
|
||||
tester,
|
||||
) async {
|
||||
final h = await pumpOutline(tester, [(0, 'a'), (0, 'b'), (0, 'c')]);
|
||||
await edit(tester, h, 'b', caret: 1);
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.tab);
|
||||
await tester.pump();
|
||||
expect(h.local, [(0, 'a'), (1, 'b'), (0, 'c')]);
|
||||
expect(focusedContent(h), 'b');
|
||||
expect(field(tester).selection.baseOffset, 1);
|
||||
|
||||
await edit(tester, h, 'c');
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.tab);
|
||||
await tester.pump();
|
||||
expect(h.local, [(0, 'a'), (1, 'b'), (1, 'c')]);
|
||||
|
||||
// Outdenting `b` adopts `c` (Logseq's direct outdenting).
|
||||
await edit(tester, h, 'b');
|
||||
await withShift(tester, LogicalKeyboardKey.tab);
|
||||
expect(h.local, [(0, 'a'), (0, 'b'), (1, 'c')]);
|
||||
|
||||
await settle(tester, h);
|
||||
expect(h.stored, h.local);
|
||||
expect(h.backend.calls.where((c) => c == 'indent').length, 2);
|
||||
expect(h.backend.calls.where((c) => c == 'outdent').length, 1);
|
||||
});
|
||||
|
||||
testWidgets('Tab on a first child does nothing', (tester) async {
|
||||
final h = await pumpOutline(tester, [(0, 'a'), (1, 'b')]);
|
||||
await edit(tester, h, 'b');
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.tab);
|
||||
await settle(tester, h);
|
||||
expect(h.stored, [(0, 'a'), (1, 'b')]);
|
||||
expect(h.backend.calls, isNot(contains('indent')));
|
||||
});
|
||||
|
||||
testWidgets('Backspace at the start merges into the previous block', (
|
||||
tester,
|
||||
) async {
|
||||
final h = await pumpOutline(tester, [(0, 'foo'), (0, 'bar'), (1, 'kid')]);
|
||||
await edit(tester, h, 'bar', caret: 0);
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.backspace);
|
||||
await tester.pump();
|
||||
await tester.pump();
|
||||
expect(h.local, [(0, 'foobar'), (1, 'kid')]);
|
||||
expect(focusedContent(h), 'foobar');
|
||||
expect(field(tester).selection.baseOffset, 3);
|
||||
await settle(tester, h);
|
||||
expect(h.stored, [(0, 'foobar'), (1, 'kid')]);
|
||||
});
|
||||
|
||||
testWidgets('Backspace in the middle of text edits normally', (tester) async {
|
||||
final h = await pumpOutline(tester, [(0, 'foo'), (0, 'bar')]);
|
||||
await edit(tester, h, 'bar', caret: 2);
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.backspace);
|
||||
await settle(tester, h);
|
||||
expect(h.backend.calls, isNot(contains('merge')));
|
||||
});
|
||||
|
||||
testWidgets('Delete at the end pulls the next block in', (tester) async {
|
||||
final h = await pumpOutline(tester, [(0, 'foo'), (0, 'bar')]);
|
||||
await edit(tester, h, 'foo');
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.delete);
|
||||
await settle(tester, h);
|
||||
expect(h.stored, [(0, 'foobar')]);
|
||||
expect(focusedContent(h), 'foobar');
|
||||
});
|
||||
|
||||
testWidgets('arrows move between blocks, skipping collapsed children', (
|
||||
tester,
|
||||
) async {
|
||||
final h = await pumpOutline(tester, [
|
||||
(0, 'one'),
|
||||
(1, 'hidden'),
|
||||
(0, 'two'),
|
||||
]);
|
||||
h.controller.nodes.first.collapsed = true;
|
||||
await edit(tester, h, 'one', caret: 1);
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.arrowDown);
|
||||
await tester.pump();
|
||||
await tester.pump();
|
||||
expect(focusedContent(h), 'two');
|
||||
expect(field(tester).selection.baseOffset, 1);
|
||||
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.arrowUp);
|
||||
await tester.pump();
|
||||
await tester.pump();
|
||||
expect(focusedContent(h), 'one');
|
||||
|
||||
// Right at the end moves to the start of the next block.
|
||||
field(tester).selection = const TextSelection.collapsed(offset: 3);
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.arrowRight);
|
||||
await tester.pump();
|
||||
await tester.pump();
|
||||
expect(focusedContent(h), 'two');
|
||||
expect(field(tester).selection.baseOffset, 0);
|
||||
await settle(tester, h);
|
||||
});
|
||||
|
||||
testWidgets('Alt+Shift+Up swaps a block with its previous sibling', (
|
||||
tester,
|
||||
) async {
|
||||
final h = await pumpOutline(tester, [(0, 'a'), (1, 'a1'), (0, 'b')]);
|
||||
await edit(tester, h, 'b');
|
||||
await tester.sendKeyDownEvent(LogicalKeyboardKey.altLeft);
|
||||
await tester.sendKeyDownEvent(LogicalKeyboardKey.shiftLeft);
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.arrowUp);
|
||||
await tester.sendKeyUpEvent(LogicalKeyboardKey.shiftLeft);
|
||||
await tester.sendKeyUpEvent(LogicalKeyboardKey.altLeft);
|
||||
await settle(tester, h);
|
||||
expect(h.stored, [(0, 'b'), (0, 'a'), (1, 'a1')]);
|
||||
});
|
||||
|
||||
testWidgets('Ctrl+Enter cycles TODO and DONE', (tester) async {
|
||||
final h = await pumpOutline(tester, [(0, 'buy milk')]);
|
||||
await edit(tester, h, 'buy milk');
|
||||
Future<void> ctrlEnter() async {
|
||||
await tester.sendKeyDownEvent(LogicalKeyboardKey.controlLeft);
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.enter);
|
||||
await tester.sendKeyUpEvent(LogicalKeyboardKey.controlLeft);
|
||||
await tester.pump();
|
||||
}
|
||||
|
||||
await ctrlEnter();
|
||||
expect(field(tester).text, 'TODO buy milk');
|
||||
await ctrlEnter();
|
||||
expect(field(tester).text, 'DONE buy milk');
|
||||
await ctrlEnter();
|
||||
expect(field(tester).text, 'buy milk');
|
||||
await settle(tester, h);
|
||||
expect(h.stored, [(0, 'buy milk')]);
|
||||
});
|
||||
|
||||
testWidgets('[[ autocompletes page titles', (tester) async {
|
||||
final h = await pumpOutline(
|
||||
tester,
|
||||
[(0, '')],
|
||||
otherPages: ['Rust', 'Ruby'],
|
||||
);
|
||||
await edit(tester, h, '');
|
||||
final finder = find.byType(TextField);
|
||||
// Typing the second bracket auto-pairs the closing ones.
|
||||
await tester.enterText(finder, '[');
|
||||
await tester.enterText(finder, '[[');
|
||||
expect(field(tester).text, '[[]]');
|
||||
expect(field(tester).selection.baseOffset, 2);
|
||||
|
||||
tester.testTextInput.updateEditingValue(
|
||||
const TextEditingValue(
|
||||
text: '[[Rus]]',
|
||||
selection: TextSelection.collapsed(offset: 5),
|
||||
),
|
||||
);
|
||||
await tester.pump();
|
||||
await tester.pump();
|
||||
expect(find.text('Rust'), findsOneWidget);
|
||||
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.enter);
|
||||
await tester.pump();
|
||||
expect(field(tester).text, '[[Rust]]');
|
||||
expect(field(tester).selection.baseOffset, 8);
|
||||
expect(
|
||||
h.controller.nodes,
|
||||
hasLength(1),
|
||||
reason: 'Enter was taken by the popup',
|
||||
);
|
||||
await settle(tester, h);
|
||||
expect(h.stored, [(0, '[[Rust]]')]);
|
||||
});
|
||||
|
||||
testWidgets('Escape leaves editing and the block renders', (tester) async {
|
||||
final h = await pumpOutline(tester, [(0, 'some **bold** text')]);
|
||||
await edit(tester, h, 'some **bold** text');
|
||||
expect(find.byType(TextField), findsOneWidget);
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.escape);
|
||||
await settle(tester, h);
|
||||
expect(find.byType(TextField), findsNothing);
|
||||
expect(h.controller.focusedId, isNull);
|
||||
expect(find.textContaining('bold', findRichText: true), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('tapping an empty page creates its first block', (tester) async {
|
||||
final h = await pumpOutline(tester, []);
|
||||
await tester.tap(find.text('Click to start writing…'));
|
||||
await tester.pump();
|
||||
await tester.pump();
|
||||
expect(find.byType(TextField), findsOneWidget);
|
||||
await tester.enterText(find.byType(TextField), 'first');
|
||||
await settle(tester, h);
|
||||
expect(h.stored, [(0, 'first')]);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,170 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:synapse/src/backend/graph_backend.dart';
|
||||
import 'package:synapse/src/editor/completion.dart';
|
||||
import 'package:synapse/src/editor/markdown.dart';
|
||||
import 'package:synapse/src/state/app_state.dart';
|
||||
import 'package:synapse/src/state/outline_ops.dart';
|
||||
import 'package:synapse/src/state/write_queue.dart';
|
||||
import 'package:synapse/src/theme/theme.dart';
|
||||
|
||||
import 'support/memory_backend.dart';
|
||||
|
||||
List<OutlineNode> tree(List<(int, String)> rows) {
|
||||
var k = 0;
|
||||
final l = [
|
||||
for (final (d, c) in rows) OutlineNode(id: 'n${k++}', depth: d, content: c),
|
||||
];
|
||||
OutlineOps.fixParents(l);
|
||||
return l;
|
||||
}
|
||||
|
||||
List<(int, String)> shape(List<OutlineNode> l) => [
|
||||
for (final n in l) (n.depth, n.content),
|
||||
];
|
||||
|
||||
void main() {
|
||||
group('OutlineOps', () {
|
||||
test('merge into parent keeps grandchildren in place', () {
|
||||
final l = tree([(0, 'p'), (1, 'c'), (2, 'gc'), (1, 's')]);
|
||||
OutlineOps.merge(l, 1, 0);
|
||||
expect(shape(l), [(0, 'pc'), (1, 'gc'), (1, 's')]);
|
||||
expect(l[1].parentId, 'n0');
|
||||
});
|
||||
|
||||
test('merge into a deeper previous row re-parents children', () {
|
||||
final l = tree([(0, 'a'), (1, 'a1'), (0, 'b'), (1, 'b1')]);
|
||||
OutlineOps.merge(l, 2, 1);
|
||||
expect(shape(l), [(0, 'a'), (1, 'a1b'), (2, 'b1')]);
|
||||
});
|
||||
|
||||
test('move with subtree, before and as first child', () {
|
||||
final l = tree([(0, 'a'), (1, 'a1'), (0, 'b')]);
|
||||
expect(OutlineOps.move(l, 0, PlaceKind.after, 2), isTrue);
|
||||
expect(shape(l), [(0, 'b'), (0, 'a'), (1, 'a1')]);
|
||||
expect(OutlineOps.move(l, 0, PlaceKind.firstChildOf, 1), isTrue);
|
||||
expect(shape(l), [(0, 'a'), (1, 'b'), (1, 'a1')]);
|
||||
expect(OutlineOps.move(l, 0, PlaceKind.after, 1), isFalse);
|
||||
});
|
||||
|
||||
test('visible skips collapsed subtrees', () {
|
||||
final l = tree([(0, 'a'), (1, 'a1'), (2, 'a2'), (0, 'b')]);
|
||||
l[0].collapsed = true;
|
||||
expect(OutlineOps.visible(l), [0, 3]);
|
||||
});
|
||||
});
|
||||
|
||||
group('completion', () {
|
||||
test('detects page, tag and block triggers', () {
|
||||
expect(
|
||||
detectCompletion('see [[Ru', 8),
|
||||
const CompletionQuery(CompletionKind.page, 6, 'Ru'),
|
||||
);
|
||||
expect(detectCompletion('see [[Ru]] x', 12), isNull);
|
||||
expect(
|
||||
detectCompletion('a #pro', 6),
|
||||
const CompletionQuery(CompletionKind.tag, 3, 'pro'),
|
||||
);
|
||||
expect(detectCompletion('a#pro', 5), isNull);
|
||||
expect(
|
||||
detectCompletion('((abc', 5),
|
||||
const CompletionQuery(CompletionKind.block, 2, 'abc'),
|
||||
);
|
||||
});
|
||||
|
||||
test('applies completions', () {
|
||||
final q = detectCompletion('#my ta', 6);
|
||||
expect(q, isNull, reason: 'tags stop at whitespace');
|
||||
final tag = detectCompletion('x #se', 5)!;
|
||||
final v = applyCompletion(
|
||||
'x #se',
|
||||
5,
|
||||
tag,
|
||||
const CompletionItem('Second Brain', 'Second Brain'),
|
||||
);
|
||||
expect(v.text, 'x #[[Second Brain]] ');
|
||||
final page = detectCompletion('[[Ru]]', 4)!;
|
||||
expect(
|
||||
applyCompletion(
|
||||
'[[Ru]]',
|
||||
4,
|
||||
page,
|
||||
const CompletionItem('Rust', 'Rust'),
|
||||
).text,
|
||||
'[[Rust]]',
|
||||
);
|
||||
});
|
||||
|
||||
test('auto-pairs brackets', () {
|
||||
final v = autoPair(
|
||||
const TextEditingValue(
|
||||
text: 'a[',
|
||||
selection: TextSelection.collapsed(offset: 2),
|
||||
),
|
||||
const TextEditingValue(
|
||||
text: 'a[[',
|
||||
selection: TextSelection.collapsed(offset: 3),
|
||||
),
|
||||
);
|
||||
expect(v!.text, 'a[[]]');
|
||||
expect(v.selection.baseOffset, 3);
|
||||
});
|
||||
});
|
||||
|
||||
group('markdown', () {
|
||||
RenderedBlock render(String s) {
|
||||
final backend = MemoryBackend();
|
||||
return BlockRenderer(
|
||||
RenderContext(
|
||||
colors: SynapseColors.light,
|
||||
base: const TextStyle(fontSize: 15),
|
||||
refs: BlockRefCache(backend),
|
||||
assetPath: (_) => null,
|
||||
onPage: (_) {},
|
||||
onBlockRef: (_) {},
|
||||
onUrl: (_) {},
|
||||
),
|
||||
).render(s);
|
||||
}
|
||||
|
||||
String plain(RenderedBlock r) => r.span.toPlainText(
|
||||
includePlaceholders: true,
|
||||
includeSemanticsLabels: false,
|
||||
);
|
||||
|
||||
test('hides markup and maps taps back to the source', () {
|
||||
final r = render('a **bold** [[Page]] end');
|
||||
expect(plain(r), 'a bold Page end');
|
||||
// "b" of bold: rendered 2 → source 4.
|
||||
expect(r.toSource(2), 4);
|
||||
// "P" of Page: rendered 7 → source 13.
|
||||
expect(r.toSource(7), 13);
|
||||
expect(r.toSource(plain(r).length), 'a **bold** [[Page]] end'.length);
|
||||
r.dispose();
|
||||
});
|
||||
|
||||
test('renders tags, headings and todo markers', () {
|
||||
expect(plain(render('# Title')), 'Title');
|
||||
expect(plain(render('note #idea, ok')), 'note #idea, ok');
|
||||
final todo = render('DONE ship it');
|
||||
expect(plain(todo), 'ship it');
|
||||
expect(todo.toSource(0), 5);
|
||||
});
|
||||
});
|
||||
|
||||
test(
|
||||
'WriteQueue runs tasks in order even when they finish out of order',
|
||||
() async {
|
||||
final q = WriteQueue();
|
||||
final log = <int>[];
|
||||
final f1 = q.run(() async {
|
||||
await Future<void>.delayed(const Duration(milliseconds: 20));
|
||||
log.add(1);
|
||||
});
|
||||
final f2 = q.run(() async => log.add(2));
|
||||
await Future.wait([f1, f2]);
|
||||
expect(log, [1, 2]);
|
||||
expect(q.isIdle, isTrue);
|
||||
},
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,278 @@
|
||||
import 'package:synapse/src/backend/graph_backend.dart';
|
||||
import 'package:synapse/src/state/outline_ops.dart';
|
||||
|
||||
/// In-memory [GraphBackend] for widget tests. Outline semantics reuse [OutlineOps], which
|
||||
/// mirror the Rust FFI (the Rust side has its own tests).
|
||||
class MemoryBackend implements GraphBackend {
|
||||
MemoryBackend({this.todayDate = '2026-10-21'});
|
||||
|
||||
final String todayDate;
|
||||
final pages = <String, PageDto>{};
|
||||
final outlines = <String, List<OutlineNode>>{};
|
||||
final aliases = <String, String>{};
|
||||
final calls = <String>[];
|
||||
int _seq = 0;
|
||||
|
||||
String _id() => 'id-${_seq++}';
|
||||
|
||||
PageDto createPage(String title, {String? journalDate}) {
|
||||
final p = PageDto(
|
||||
id: journalDate != null ? 'j-$journalDate' : _id(),
|
||||
title: title,
|
||||
journalDate: journalDate,
|
||||
createdAt: 0,
|
||||
updatedAt: 0,
|
||||
);
|
||||
pages[p.id] = p;
|
||||
outlines[p.id] = [];
|
||||
return p;
|
||||
}
|
||||
|
||||
/// Adds blocks `(depth, content)` to a page.
|
||||
List<String> seed(String pageId, List<(int, String)> rows) {
|
||||
final l = outlines[pageId]!;
|
||||
final ids = <String>[];
|
||||
for (final (d, c) in rows) {
|
||||
final id = _id();
|
||||
l.add(OutlineNode(id: id, depth: d, content: c));
|
||||
ids.add(id);
|
||||
}
|
||||
OutlineOps.fixParents(l);
|
||||
return ids;
|
||||
}
|
||||
|
||||
List<BlockDto> _dto(String pageId) {
|
||||
final l = outlines[pageId]!;
|
||||
return [
|
||||
for (var i = 0; i < l.length; i++)
|
||||
BlockDto(
|
||||
id: l[i].id,
|
||||
parentId: l[i].parentId,
|
||||
depth: l[i].depth,
|
||||
content: l[i].content,
|
||||
collapsed: l[i].collapsed,
|
||||
hasChildren: OutlineOps.hasChildren(l, i),
|
||||
),
|
||||
];
|
||||
}
|
||||
|
||||
(String, int) _find(String blockId) {
|
||||
for (final e in outlines.entries) {
|
||||
final i = e.value.indexWhere((n) => n.id == blockId);
|
||||
if (i >= 0) return (e.key, i);
|
||||
}
|
||||
throw StateError('block $blockId not found');
|
||||
}
|
||||
|
||||
PageData _view(PageDto p) => PageData(
|
||||
page: p,
|
||||
blocks: _dto(p.id),
|
||||
aliases: [
|
||||
for (final e in aliases.entries)
|
||||
if (e.value == p.id) e.key,
|
||||
],
|
||||
);
|
||||
|
||||
PageDto? _byTitle(String title) {
|
||||
final k = title.trim().toLowerCase();
|
||||
for (final p in pages.values) {
|
||||
if (p.title.toLowerCase() == k) return p;
|
||||
}
|
||||
final a = aliases[k];
|
||||
return a == null ? null : pages[a];
|
||||
}
|
||||
|
||||
@override
|
||||
String today() => todayDate;
|
||||
|
||||
@override
|
||||
Future<List<PageData>> journals({String? before, required int limit}) async {
|
||||
if (before == null) await journal(todayDate);
|
||||
final js =
|
||||
pages.values
|
||||
.where(
|
||||
(p) =>
|
||||
p.journalDate != null &&
|
||||
(before == null || p.journalDate!.compareTo(before) < 0),
|
||||
)
|
||||
.toList()
|
||||
..sort((a, b) => b.journalDate!.compareTo(a.journalDate!));
|
||||
return js.take(limit).map(_view).toList();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<PageData> journal(String date) async =>
|
||||
_view(pages['j-$date'] ?? createPage(date, journalDate: date));
|
||||
|
||||
@override
|
||||
Future<PageData?> page(String id) async {
|
||||
final p = pages[id];
|
||||
return p == null ? null : _view(p);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<PageData?> pageByTitle(String title) async {
|
||||
final p = _byTitle(title);
|
||||
return p == null ? null : _view(p);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<PageData> openPage(String title) async =>
|
||||
_view(_byTitle(title) ?? createPage(title));
|
||||
|
||||
@override
|
||||
Future<List<PageDto>> listPages({
|
||||
bool includeJournals = false,
|
||||
required int limit,
|
||||
int offset = 0,
|
||||
}) async => pages.values
|
||||
.where((p) => includeJournals || p.journalDate == null)
|
||||
.skip(offset)
|
||||
.take(limit)
|
||||
.toList();
|
||||
|
||||
@override
|
||||
Future<PageDto> renamePage(String id, String newTitle) async {
|
||||
final p = pages[id]!;
|
||||
return pages[id] = PageDto(
|
||||
id: p.id,
|
||||
title: newTitle,
|
||||
createdAt: p.createdAt,
|
||||
updatedAt: p.updatedAt,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> deletePage(String id) async {
|
||||
pages.remove(id);
|
||||
outlines.remove(id);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> addAlias(String pageId, String alias) async =>
|
||||
aliases[alias.toLowerCase()] = pageId;
|
||||
|
||||
@override
|
||||
Future<void> removeAlias(String alias) async =>
|
||||
aliases.remove(alias.toLowerCase());
|
||||
|
||||
int? _index(String pageId, String? id) =>
|
||||
id == null ? null : outlines[pageId]!.indexWhere((n) => n.id == id);
|
||||
|
||||
@override
|
||||
Future<BlockDto> insertBlock(
|
||||
String pageId,
|
||||
Place place,
|
||||
String content,
|
||||
) async {
|
||||
calls.add('insert');
|
||||
final n = OutlineNode(id: _id(), depth: 0, content: content);
|
||||
OutlineOps.insert(
|
||||
outlines[pageId]!,
|
||||
n,
|
||||
place.kind,
|
||||
_index(pageId, place.anchor),
|
||||
);
|
||||
return BlockDto(
|
||||
id: n.id,
|
||||
parentId: n.parentId,
|
||||
depth: n.depth,
|
||||
content: content,
|
||||
collapsed: false,
|
||||
hasChildren: false,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> updateBlock(String id, String content) async {
|
||||
calls.add('update');
|
||||
final (p, i) = _find(id);
|
||||
outlines[p]![i].content = content;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> setCollapsed(String id, bool collapsed) async {
|
||||
final (p, i) = _find(id);
|
||||
outlines[p]![i].collapsed = collapsed;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<BlockDto>> deleteBlock(String id) async {
|
||||
final (p, i) = _find(id);
|
||||
OutlineOps.delete(outlines[p]!, i);
|
||||
return _dto(p);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<BlockDto>> moveBlock(
|
||||
String id,
|
||||
String pageId,
|
||||
Place place,
|
||||
) async {
|
||||
final (p, i) = _find(id);
|
||||
OutlineOps.move(outlines[p]!, i, place.kind, _index(p, place.anchor));
|
||||
return _dto(p);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<BlockDto>> indentBlock(String id) async {
|
||||
calls.add('indent');
|
||||
final (p, i) = _find(id);
|
||||
OutlineOps.indent(outlines[p]!, i);
|
||||
return _dto(p);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<BlockDto>> outdentBlock(String id) async {
|
||||
calls.add('outdent');
|
||||
final (p, i) = _find(id);
|
||||
OutlineOps.outdent(outlines[p]!, i);
|
||||
return _dto(p);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<BlockDto>> mergeBlock(String id, String into) async {
|
||||
calls.add('merge');
|
||||
final (p, i) = _find(id);
|
||||
OutlineOps.merge(outlines[p]!, i, _index(p, into)!);
|
||||
return _dto(p);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<BlockDto>> outline(String pageId) async => _dto(pageId);
|
||||
|
||||
@override
|
||||
Future<List<BacklinkGroup>> backlinks(String pageId) async => [];
|
||||
@override
|
||||
Future<List<RelatedDto>> related(String pageId, {int limit = 12}) async => [];
|
||||
@override
|
||||
Future<void> recordVisit(String? from, String to) async {}
|
||||
@override
|
||||
Future<BlockRefDto?> blockRef(String id) async => null;
|
||||
@override
|
||||
Future<TimelineDto> timeline(String from, String to) async =>
|
||||
const TimelineDto(days: [], pages: []);
|
||||
|
||||
@override
|
||||
Future<List<TitleHit>> searchTitles(String query, {int limit = 20}) async {
|
||||
final q = query.toLowerCase();
|
||||
return pages.values
|
||||
.where(
|
||||
(p) => p.journalDate == null && p.title.toLowerCase().contains(q),
|
||||
)
|
||||
.take(limit)
|
||||
.map((p) => TitleHit(pageId: p.id, title: p.title, isAlias: false))
|
||||
.toList();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<BlockHit>> searchBlocks(String query, {int limit = 20}) async =>
|
||||
[];
|
||||
|
||||
@override
|
||||
String? assetPath(String hash) => null;
|
||||
|
||||
@override
|
||||
Future<AssetDto> putAssetFile(String path, {String? mime}) =>
|
||||
throw UnimplementedError();
|
||||
}
|
||||
Reference in new issue
Block a user