From 5026fde92b7c6e619c8d0595a797a346103f26f6 Mon Sep 17 00:00:00 2001 From: Dasemu Date: Mon, 26 Oct 2026 03:39:33 +0100 Subject: [PATCH] test(app): types, sorted tags, tag view and tag nodes Co-Authored-By: Claude Sonnet 5.5 --- app/test/graph_view_test.dart | 85 ++++++++++++ app/test/hubs_ui_test.dart | 89 ++++++++++--- app/test/support/memory_backend.dart | 188 +++++++++++++++------------ 3 files changed, 260 insertions(+), 102 deletions(-) diff --git a/app/test/graph_view_test.dart b/app/test/graph_view_test.dart index bbfdae3..c942795 100644 --- a/app/test/graph_view_test.dart +++ b/app/test/graph_view_test.dart @@ -175,6 +175,7 @@ void main() { id: t, title: t, createdAt: 0, + isTag: false, degree: 1, backlinks: 0, cluster: 0, @@ -258,6 +259,88 @@ void main() { expect(hull.contains(const Offset(5, 5)), isFalse); }); + group('virtual tag nodes', () { + // Two pages tagged "ocio" through TAGGED edges (kind 8) to one tag node. + GraphData tagData() { + GraphNode n(String id, String title, {bool tag = false}) => GraphNode( + id: id, + title: title, + createdAt: 0, + isTag: tag, + degree: 1, + backlinks: 0, + cluster: 0, + isHub: false, + tags: const [], + ); + final nodes = [ + n('0', 'Games'), + n('1', 'Chess'), + n('tag:ocio', 'ocio', tag: true), + n('tag:a/b', 'a/b', tag: true), + ]; + final edges = [(0, 2, 8), (1, 2, 8)]; + return 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]), + weights: Float32List.fromList([for (final _ in edges) 1.0]), + seq: 1, + ); + } + + test('the Tags branch is built from tag nodes, not loose notes', () { + final m = GraphModel(tagData()); + expect(m.isTag(2), isTrue); + expect(m.tags, [2]); + final root = buildNamespaceTree(m); + final tags = root.children.firstWhere((c) => c.label == 'Tags'); + final ocio = tags.children.single; + expect(ocio.label, '#ocio'); + expect(ocio.global, 2); + expect(ocio.children.map((c) => c.label).toSet(), {'Games', 'Chess'}); + // Tag nodes are neither loose notes nor namespaces. + expect( + root.children.where((c) => c.label == 'ocio' || c.label == 'a'), + isEmpty, + ); + }); + + test('filtering by a tag node keeps the tagged pages', () { + final m = GraphModel(tagData()); + expect(const GraphFilter().copyWith(tag: () => 2).apply(m), [0, 1, 2]); + }); + + testWidgets('a tag in the tree opens its tag view by name', (tester) async { + String? opened; + final m = GraphModel(tagData()); + tester.view.physicalSize = const Size(1600, 1000); + tester.view.devicePixelRatio = 1; + addTearDown(tester.view.reset); + await tester.pumpWidget( + AppScope( + state: AppState(MemoryBackend()), + child: MaterialApp( + theme: buildTheme(Brightness.light), + home: Scaffold( + body: TreeCanvas( + model: m, + onTap: (_) {}, + onTag: (name) => opened = name, + ), + ), + ), + ), + ); + final state = tester.state(find.byType(TreeCanvas)); + final tags = buildNamespaceTree(m).children + .firstWhere((c) => c.label == 'Tags'); + state.openItem(tags.children.single); + expect(opened, 'ocio'); + }); + }); + group('hubs', () { // Media (hub) -> TV (hub) -> Anime (page); Media -> Books; Notes (page) in Media; // a TV -> Media CONTAINS edge (a cycle the tree must survive); a journal (never a member). @@ -273,6 +356,7 @@ void main() { title: title, journalDate: journal, createdAt: 0, + isTag: false, degree: 1, backlinks: 0, cluster: 0, @@ -370,6 +454,7 @@ void main() { id: '$i', title: hub ? 'Library' : 'Page $i', createdAt: 0, + isTag: false, degree: 1, backlinks: 0, cluster: 0, diff --git a/app/test/hubs_ui_test.dart b/app/test/hubs_ui_test.dart index 1b2a0f4..22662eb 100644 --- a/app/test/hubs_ui_test.dart +++ b/app/test/hubs_ui_test.dart @@ -519,58 +519,113 @@ void main() { } void tagTests() { - testWidgets('tags are pages: pick one, create one, remove', (tester) async { + testWidgets('tags are names: pick one, create one, remove, sorted', ( + tester, + ) async { final b = demo(); final note = byTitle(b, 'Games'); await b.setPageTags(note.id, ['tema']); await pump(tester, b, route: PageTarget(id: note.id)); expect(find.byKey(const ValueKey('tag-tema')), findsOneWidget); - expect(byTitle(b, 'tema').isTag, isTrue); await tester.tap(find.byKey(const ValueKey('add-tag'))); await settle(tester); - // Tags already on the page are not offered again; a new name creates a tag page. + // Tags already on the page are not offered again; a new name just creates a tag. expect(find.byKey(const ValueKey('tag-opt-tema')), findsNothing); await tester.enterText(find.byKey(const ValueKey('tag-input')), '#ocio'); await settle(tester); await tester.tap(find.byKey(const ValueKey('tag-create'))); await settle(tester); - expect(byTitle(b, 'Games').tags, ['tema', 'ocio']); - expect(byTitle(b, 'ocio').isTag, isTrue); + await tester.tap(find.byKey(const ValueKey('tag-done'))); + await settle(tester); + // Stored and shown sorted. + expect(byTitle(b, 'Games').tags, ['ocio', 'tema']); + expect( + tester.getTopLeft(find.byKey(const ValueKey('tag-ocio'))).dx, + lessThan(tester.getTopLeft(find.byKey(const ValueKey('tag-tema'))).dx), + ); + // Tags are names, never pages. + expect(b.pages.values.any((p) => p.title == 'ocio'), isFalse); + final gesture = await tester.createGesture(kind: PointerDeviceKind.mouse); + await gesture.addPointer(location: Offset.zero); + await gesture.moveTo( + tester.getCenter(find.byKey(const ValueKey('tag-tema'))), + ); + await tester.pump(); + await tester.pump(const Duration(milliseconds: 300)); await tester.tap(find.byKey(const ValueKey('untag-tema'))); await settle(tester); expect(byTitle(b, 'Games').tags, ['ocio']); - // The tag page survives losing its last member. - expect(byTitle(b, 'tema').isTag, isTrue); }); - testWidgets('types: pick a suggestion, create a new one, remove', ( - tester, - ) async { + testWidgets('a page has one type: set, change, remove', (tester) async { final b = demo(); final note = byTitle(b, 'Games'); await pump(tester, b, route: PageTarget(id: note.id)); + expect(find.byKey(const ValueKey('type-pill')), findsNothing); await tester.tap(find.byKey(const ValueKey('add-type'))); await settle(tester); expect(find.byKey(const ValueKey('type-opt-persona')), findsOneWidget); await tester.tap(find.byKey(const ValueKey('type-opt-lugar'))); await settle(tester); - expect(find.byKey(const ValueKey('type-lugar')), findsOneWidget); + expect(byTitle(b, 'Games').pageType, 'lugar'); + expect(find.byKey(const ValueKey('type-pill')), findsOneWidget); - await tester.tap(find.byKey(const ValueKey('add-type'))); + // Change it: the single-select picker replaces the type, with a new name too. + await tester.tap(find.byKey(const ValueKey('type-pill'))); await settle(tester); - expect(find.byKey(const ValueKey('type-opt-lugar')), findsNothing); await tester.enterText(find.byKey(const ValueKey('type-input')), 'ocio'); await settle(tester); await tester.tap(find.byKey(const ValueKey('type-create'))); await settle(tester); - expect(byTitle(b, 'Games').types, ['lugar', 'ocio']); - // Types are names, never pages. + expect(byTitle(b, 'Games').pageType, 'ocio'); expect(b.pages.values.any((p) => p.title == 'ocio'), isFalse); - await tester.tap(find.byKey(const ValueKey('untype-lugar'))); + await tester.tap(find.byKey(const ValueKey('type-pill'))); await settle(tester); - expect(byTitle(b, 'Games').types, ['ocio']); + await tester.tap(find.byKey(const ValueKey('type-remove'))); + await settle(tester); + expect(byTitle(b, 'Games').pageType, isNull); + expect(find.byKey(const ValueKey('add-type')), findsOneWidget); + }); + + testWidgets('a tag chip opens the tag view with typed and tagged pages', ( + tester, + ) async { + final b = demo(); + final games = byTitle(b, 'Games'); + final other = b.pages.values.firstWhere((p) => p.id != games.id); + await b.setPageTags(games.id, ['ocio']); + await b.setPageType(other.id, 'ocio'); + await pump(tester, b, route: PageTarget(id: games.id)); + await tester.tap(find.byKey(const ValueKey('tag-ocio'))); + await settle(tester); + expect(find.byKey(const ValueKey('tag-title')), findsOneWidget); + expect(find.byKey(ValueKey('tag-page-${games.id}')), findsOneWidget); + expect(find.byKey(ValueKey('tag-page-${other.id}')), findsOneWidget); + }); + + testWidgets('renaming and removing a tag from its view', (tester) async { + final b = demo(); + final games = byTitle(b, 'Games'); + await b.setPageTags(games.id, ['ocio']); + await pump(tester, b, route: const TagRoute('ocio')); + await tester.tap(find.byKey(const ValueKey('tag-rename'))); + await settle(tester); + await tester.enterText( + find.byKey(const ValueKey('tag-rename-input')), + 'tiempo libre', + ); + await tester.tap(find.byKey(const ValueKey('tag-rename-confirm'))); + await settle(tester); + expect(byTitle(b, 'Games').tags, ['tiempo libre']); + expect(find.byKey(ValueKey('tag-page-${games.id}')), findsOneWidget); + + await tester.tap(find.byKey(const ValueKey('tag-remove'))); + await settle(tester); + await tester.tap(find.byKey(const ValueKey('tag-remove-confirm'))); + await settle(tester); + expect(byTitle(b, 'Games').tags, isEmpty); }); } diff --git a/app/test/support/memory_backend.dart b/app/test/support/memory_backend.dart index d32d432..2d08591 100644 --- a/app/test/support/memory_backend.dart +++ b/app/test/support/memory_backend.dart @@ -32,8 +32,6 @@ class MemoryBackend implements GraphBackend { isHub: false, description: '', tags: const [], - isTag: false, - types: const [], ); pages[p.id] = p; outlines[p.id] = []; @@ -156,35 +154,12 @@ class MemoryBackend implements GraphBackend { n.content = n.content.replaceAll('[[${p.title}]]', '[[$t]]'); } } - final old = p.title; _put(p, title: t); - if (p.isTag) { - for (final q in pages.values.toList()) { - if (q.tags.any((x) => _tagKey(x) == _tagKey(old))) { - _put( - q, - tags: [for (final x in q.tags) _tagKey(x) == _tagKey(old) ? t : x], - ); - } - } - } return pages[id]!; } @override Future deletePage(String id) async { - final gone = pages[id]; - if (gone != null && gone.isTag) { - for (final q in pages.values.toList()) { - _put( - q, - tags: [ - for (final x in q.tags) - if (_tagKey(x) != _tagKey(gone.title)) x, - ], - ); - } - } pages.remove(id); outlines.remove(id); _forget(id); @@ -355,8 +330,8 @@ class MemoryBackend implements GraphBackend { bool? isHub, String? description, List? tags, - bool? isTag, - List? types, + String? pageType, + bool clearType = false, }) { pages[p.id] = PageDto( id: p.id, @@ -368,8 +343,7 @@ class MemoryBackend implements GraphBackend { isHub: isHub ?? p.isHub, description: description ?? p.description, tags: tags ?? p.tags, - isTag: isTag ?? p.isTag, - types: types ?? p.types, + pageType: clearType ? null : (pageType ?? p.pageType), ); } @@ -463,56 +437,37 @@ class MemoryBackend implements GraphBackend { next.add(t); } } - final stored = []; - for (final t in next) { - var tp = _byTitle(t); - if (tp == null) { - tp = createPage(t); - _put(tp, isTag: true); - } else if (tp.journalDate != null || tp.isHub) { - throw Exception('"$t" cannot be a tag'); - } else if (!tp.isTag) { - _put(tp, isTag: true); - } - stored.add(pages[tp.id]!.title); - } - _put(p, tags: stored); + 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!, + ]; + static const suggestedTypes = ['persona', 'lugar', 'empresa']; @override - Future setPageTypes(String pageId, List types) async { + Future setPageType(String pageId, String? type) async { final p = pages[pageId]!; - if (p.journalDate != null) throw Exception('Journals cannot have types'); - final next = []; - for (final raw in types) { - final t = raw.trim(); - if (t.isNotEmpty && !next.any((x) => _tagKey(x) == _tagKey(t))) { - next.add(t); - } + final t = type?.trim() ?? ''; + if (p.journalDate != null && t.isNotEmpty) { + throw Exception('Journals cannot have a type'); } - _put(p, types: next); + _put(p, pageType: t, clearType: t.isEmpty); return pages[pageId]!; } - @override - Future addPageType(String pageId, String ty) => - setPageTypes(pageId, [...pages[pageId]!.types, ty]); - - @override - Future removePageType(String pageId, String ty) => - setPageTypes(pageId, [ - for (final t in pages[pageId]!.types) - if (_tagKey(t) != _tagKey(ty)) t, - ]); - @override Future> typeCatalog() async { final used = {}; for (final p in pages.values) { - for (final t in p.types) { + for (final t in [if (p.pageType != null) p.pageType!]) { final k = _tagKey(t); used[k] = TagDto( name: used[k]?.name ?? t, @@ -544,26 +499,68 @@ class MemoryBackend implements GraphBackend { @override Future> tagCatalog() async { - final out = [ - for (final t in pages.values) - if (t.isTag) - TagDto( - name: t.title, - count: pages.values - .where((p) => p.tags.any((x) => _tagKey(x) == _tagKey(t.title))) - .length, - suggested: false, - ), - ]..sort((a, b) => b.count.compareTo(a.count)); - return out; + final used = {}; + for (final p in pages.values) { + for (final t in allTags(p)) { + final k = _tagKey(t); + used[k] = TagDto( + name: used[k]?.name ?? t, + count: (used[k]?.count ?? 0) + 1, + suggested: false, + ); + } + } + return used.values.toList()..sort((a, b) { + final c = b.count.compareTo(a.count); + return c != 0 ? c : _tagKey(a.name).compareTo(_tagKey(b.name)); + }); } @override Future> pagesWithTag(String tag) async => [ for (final p in pages.values) - if (p.tags.any((t) => _tagKey(t) == _tagKey(tag))) _brief(p), + if (allTags(p).any((t) => _tagKey(t) == _tagKey(tag))) _brief(p), ]; + @override + Future renameTag(String old, String new_) async { + final name = new_.trim().replaceFirst(RegExp(r'^#+'), '').trim(); + if (name.isEmpty) throw Exception('Empty tag name'); + var n = 0; + for (final p in pages.values.toList()) { + final hasTag = p.tags.any((t) => _tagKey(t) == _tagKey(old)); + final hasType = + p.pageType != null && _tagKey(p.pageType!) == _tagKey(old); + if (!hasTag && !hasType) continue; + final seen = {}; + final tags = [ + for (final t in p.tags) + if (seen.add(_tagKey(_tagKey(t) == _tagKey(old) ? name : t))) + _tagKey(t) == _tagKey(old) ? name : t, + ]..sort((a, b) => _tagKey(a).compareTo(_tagKey(b))); + _put(p, tags: tags, pageType: hasType ? name : null); + n++; + } + return n; + } + + @override + Future removeTag(String name) async { + var n = 0; + for (final p in pages.values.toList()) { + if (!p.tags.any((t) => _tagKey(t) == _tagKey(name))) continue; + _put( + p, + tags: [ + for (final t in p.tags) + if (_tagKey(t) != _tagKey(name)) t, + ], + ); + n++; + } + return n; + } + @override Future hubTemplate(String hubId) async { if (!_isHub(hubId)) throw Exception('Not a hub'); @@ -889,20 +886,28 @@ class MemoryBackend implements GraphBackend { t.add(index[m]!); k.add(EdgeKind.contains.index); } - // A note points at each of its tag pages. + // Tags are virtual nodes after the pages; a page points at each of its tags. + final tagNames = {}; + for (final p in pages.values) { + for (final tg in allTags(p)) { + tagNames.putIfAbsent(_tagKey(tg), () => tg); + } + } + final tagKeys = tagNames.keys.toList()..sort(); + final tagIndex = { + for (var j = 0; j < tagKeys.length; j++) tagKeys[j]: ids.length + j, + }; for (final p in pages.values) { final i = index[p.id]; if (i == null) continue; - for (final tg in p.tags) { - final tp = _byTitle(tg); - final j = tp == null ? null : index[tp.id]; - if (j == null) continue; + for (final tg in allTags(p)) { s.add(i); - t.add(j); + t.add(tagIndex[_tagKey(tg)]!); k.add(EdgeKind.tagged.index); } } - while (degree.length < ids.length) { + final total = ids.length + tagKeys.length; + while (degree.length < total) { degree.add(0); } for (var e = 0; e < s.length; e++) { @@ -922,7 +927,20 @@ class MemoryBackend implements GraphBackend { cluster: 0, isHub: _isHub(ids[i]), homeHub: home[ids[i]] == null ? null : index[home[ids[i]]!], - tags: pages[ids[i]]!.tags, + tags: allTags(pages[ids[i]]!), + isTag: false, + ), + for (var j = 0; j < tagKeys.length; j++) + GraphNode( + id: 'tag:${tagKeys[j]}', + title: tagNames[tagKeys[j]]!, + createdAt: 0, + degree: degree[ids.length + j], + backlinks: 0, + cluster: 0, + isHub: false, + tags: const [], + isTag: true, ), ], sources: Uint32List.fromList(s),