diff --git a/app/test/support/memory_backend.dart b/app/test/support/memory_backend.dart index 2d08591..2baa0c4 100644 --- a/app/test/support/memory_backend.dart +++ b/app/test/support/memory_backend.dart @@ -426,36 +426,59 @@ class MemoryBackend implements GraphBackend { String _tagKey(String t) => t.trim().toLowerCase(); + /// Like the core's `normalize_tag`: leading `#` dropped, whitespace collapsed. + String _normTag(String raw) => raw + .trim() + .replaceFirst(RegExp(r'^#+'), '') + .split(RegExp(r'\s+')) + .where((w) => w.isNotEmpty) + .join(' '); + @override Future setPageTags(String pageId, List tags) async { final p = pages[pageId]!; - if (p.journalDate != null) throw Exception('Journals cannot have tags'); final next = []; for (final raw in tags) { - final t = raw.trim().replaceFirst(RegExp(r'^#+'), '').trim(); + final t = _normTag(raw); if (t.isNotEmpty && !next.any((x) => _tagKey(x) == _tagKey(t))) { next.add(t); } } + if (p.journalDate != null && next.isNotEmpty) { + throw Exception('Journals cannot have tags'); + } next.sort((a, b) => _tagKey(a).compareTo(_tagKey(b))); _put(p, tags: next); return pages[pageId]!; } - /// Tag names of a page: its tags plus its type (which implies a tag), once each. - List allTags(PageDto p) => [ - ...p.tags, - if (p.pageType != null && - !p.tags.any((t) => _tagKey(t) == _tagKey(p.pageType!))) - p.pageType!, - ]; + /// Title of the direct hub of a note: an implicit tag (hubs, templates and journals have none). + String? _hubTag(PageDto p) { + if (p.isHub || p.journalDate != null || templates.containsValue(p.id)) { + return null; + } + for (final h in hubsOfPage[p.id] ?? const {}) { + final hub = pages[h]; + if (hub != null && hub.isHub) return hub.title; + } + return null; + } + + /// Tag names of a page: its tags plus its type and its hub (which imply a tag), once each. + List allTags(PageDto p) { + final out = [...p.tags]; + for (final t in [p.pageType, _hubTag(p)]) { + if (t != null && !out.any((x) => _tagKey(x) == _tagKey(t))) out.add(t); + } + return out; + } static const suggestedTypes = ['persona', 'lugar', 'empresa']; @override Future setPageType(String pageId, String? type) async { final p = pages[pageId]!; - final t = type?.trim() ?? ''; + final t = _normTag(type ?? ''); if (p.journalDate != null && t.isNotEmpty) { throw Exception('Journals cannot have a type'); } @@ -524,7 +547,7 @@ class MemoryBackend implements GraphBackend { @override Future renameTag(String old, String new_) async { - final name = new_.trim().replaceFirst(RegExp(r'^#+'), '').trim(); + final name = _normTag(new_); if (name.isEmpty) throw Exception('Empty tag name'); var n = 0; for (final p in pages.values.toList()) { @@ -538,6 +561,10 @@ class MemoryBackend implements GraphBackend { if (seen.add(_tagKey(_tagKey(t) == _tagKey(old) ? name : t))) _tagKey(t) == _tagKey(old) ? name : t, ]..sort((a, b) => _tagKey(a).compareTo(_tagKey(b))); + final changedType = hasType && p.pageType != name; + if (tags.join('\u0000') == p.tags.join('\u0000') && !changedType) { + continue; + } _put(p, tags: tags, pageType: hasType ? name : null); n++; } @@ -906,6 +933,10 @@ class MemoryBackend implements GraphBackend { k.add(EdgeKind.tagged.index); } } + final tagPages = List.filled(tagKeys.length, 0); + for (final tg in t.skip(0).where((x) => x >= ids.length)) { + tagPages[tg - ids.length]++; + } final total = ids.length + tagKeys.length; while (degree.length < total) { degree.add(0); @@ -936,7 +967,7 @@ class MemoryBackend implements GraphBackend { title: tagNames[tagKeys[j]]!, createdAt: 0, degree: degree[ids.length + j], - backlinks: 0, + backlinks: tagPages[j], cluster: 0, isHub: false, tags: const [], diff --git a/app/test/tags_graph_test.dart b/app/test/tags_graph_test.dart new file mode 100644 index 0000000..c40449c --- /dev/null +++ b/app/test/tags_graph_test.dart @@ -0,0 +1,191 @@ +import 'dart:typed_data'; + +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:synapse/src/backend/graph_backend.dart'; +import 'package:synapse/src/graph/graph_model.dart'; +import 'package:synapse/src/state/app_state.dart'; +import 'package:synapse/src/theme/theme.dart'; +import 'package:synapse/src/views/graph_view.dart'; + +import 'support/memory_backend.dart'; + +/// Like the core: related() only understands page ids. +class StrictBackend extends MemoryBackend { + final relatedCalls = []; + + @override + Future> related(String pageId, {int limit = 12}) async { + relatedCalls.add(pageId); + if (pageId.startsWith('tag:')) throw Exception('invalid id $pageId'); + return []; + } +} + +GraphNode node( + String id, + String title, { + bool tag = false, + int createdAt = 0, + int degree = 1, +}) => GraphNode( + id: id, + title: title, + createdAt: createdAt, + isTag: tag, + degree: degree, + backlinks: 0, + cluster: 0, + isHub: false, + tags: const [], +); + +GraphModel model(List nodes, List<(int, int, EdgeKind)> edges) => + GraphModel( + GraphData( + nodes: nodes, + sources: Uint32List.fromList([for (final e in edges) e.$1]), + targets: Uint32List.fromList([for (final e in edges) e.$2]), + kinds: Uint8List.fromList([for (final e in edges) e.$3.index]), + weights: Float32List.fromList([for (final _ in edges) 1.0]), + seq: 1, + ), + ); + +int ms(int y, int m, int d) => DateTime(y, m, d).millisecondsSinceEpoch; + +void main() { + group('memory backend matches the core', () { + test('types are normalised like tags', () async { + final b = MemoryBackend(); + final p = b.createPage('A'); + final q = await b.setPageType(p.id, ' # my type '); + expect(q.pageType, 'my type'); + }); + + test('renameTag counts only the pages that changed', () async { + final b = MemoryBackend(); + final a = b.createPage('A'); + final c = b.createPage('C'); + await b.setPageTags(a.id, ['idea']); + await b.setPageTags(c.id, ['Other']); + expect(await b.renameTag('idea', 'idea'), 0); + expect(await b.renameTag('idea', 'plan'), 1); + expect(await b.renameTag('nothing', 'x'), 0); + }); + + test('journals accept an empty tag list', () async { + final b = MemoryBackend(); + final j = b.createPage('2024-01-02', journalDate: '2024-01-02'); + await b.setPageTags(j.id, []); + expect(() => b.setPageTags(j.id, ['x']), throwsException); + }); + + test('a hub is an implicit tag of its members, merged once', () async { + final b = MemoryBackend(); + final media = b.createPage('Media'); + b.makeHub(media.id); + final a = b.createPage('Naruto'); + final c = b.createPage('Bleach'); + await b.addToHub(a.id, media.id); + await b.addToHub(c.id, media.id); + await b.setPageType(c.id, 'media'); + final tmpl = await b.hubTemplate(media.id); + expect((await b.pagesWithTag('media')).map((p) => p.id).toSet(), { + a.id, + c.id, + }); + expect((await b.pagesWithTag('media')).any((p) => p.id == tmpl), isFalse); + final cat = await b.tagCatalog(); + expect(cat.single.count, 2); + expect(b.pages[a.id]!.tags, isEmpty); + + final data = await b.graphData(); + final tags = data.nodes.where((n) => n.isTag).toList(); + expect(tags.length, 1); + expect(tags.single.backlinks, 2); + final ti = data.nodes.indexOf(tags.single); + final from = [ + for (var e = 0; e < data.kinds.length; e++) + if (data.kinds[e] == EdgeKind.tagged.index && data.targets[e] == ti) + data.nodes[data.sources[e]].title, + ]..sort(); + expect(from, ['Bleach', 'Naruto']); + }); + }); + + group('graph model with tag nodes', () { + test('tags lists only virtual tag nodes, once', () { + // Page "Idea" (a former tag page) is targeted by an inline #Idea ref and the virtual + // tag node "Idea" by TAGGED edges. + final m = model( + [ + node('0', 'A'), + node('1', 'B'), + node('2', 'Idea'), + node('tag:idea', 'Idea', tag: true), + ], + [ + (0, 2, EdgeKind.tag), + (1, 3, EdgeKind.tagged), + (0, 3, EdgeKind.tagged), + ], + ); + expect(m.tags, [3]); + }); + + test('date filters judge tag nodes by their tagged pages', () { + final m = model( + [ + node('0', 'Old', createdAt: ms(2020, 1, 1)), + node('1', 'New', createdAt: ms(2024, 6, 1)), + node('tag:old', 'old', tag: true), + node('tag:new', 'new', tag: true), + ], + [(0, 2, EdgeKind.tagged), (1, 3, EdgeKind.tagged)], + ); + expect(const GraphFilter().apply(m), [0, 1, 2, 3]); + expect(const GraphFilter(from: '2023-01-01').apply(m), [1, 3]); + expect(const GraphFilter(to: '2021-01-01').apply(m), [0, 2]); + }); + }); + + group('brain mode and tag nodes', () { + Future pump(WidgetTester tester) async { + final b = StrictBackend(); + final hub = b.createPage('Hub'); + b.seed(hub.id, [(0, 'x')]); + for (final t in ['A', 'B', 'C']) { + final p = b.createPage(t); + await b.setPageTags(p.id, ['shared']); + } + tester.view.physicalSize = const Size(1600, 1000); + tester.view.devicePixelRatio = 1; + addTearDown(tester.view.reset); + await tester.pumpWidget( + AppScope( + state: AppState(b), + child: MaterialApp( + theme: buildTheme(Brightness.light), + home: const Scaffold(body: GraphView()), + ), + ), + ); + for (var i = 0; i < 6; i++) { + await tester.pump(const Duration(milliseconds: 50)); + } + await tester.tap(find.text('Synapses')); + for (var i = 0; i < 6; i++) { + await tester.pump(const Duration(milliseconds: 50)); + } + return b; + } + + testWidgets('the fallback focus is never a tag node', (tester) async { + final b = await pump(tester); + expect(b.relatedCalls, isNotEmpty); + expect(b.relatedCalls.any((c) => c.startsWith('tag:')), isFalse); + expect(tester.takeException(), isNull); + }); + }); +}