test(app): types, sorted tags, tag view and tag nodes

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
dasemuandClaude Sonnet 5.5 committed 2026-10-26 03:39:33 +01:00
1 parent 42a87b1a2a
commit 5026fde92b
3 files changed
+259 -101

No files matched your search

+85
View File
@@ -175,6 +175,7 @@ void main() {
id: t, id: t,
title: t, title: t,
createdAt: 0, createdAt: 0,
isTag: false,
degree: 1, degree: 1,
backlinks: 0, backlinks: 0,
cluster: 0, cluster: 0,
@@ -258,6 +259,88 @@ void main() {
expect(hull.contains(const Offset(5, 5)), isFalse); 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<TreeCanvasState>(find.byType(TreeCanvas));
final tags = buildNamespaceTree(m).children
.firstWhere((c) => c.label == 'Tags');
state.openItem(tags.children.single);
expect(opened, 'ocio');
});
});
group('hubs', () { group('hubs', () {
// Media (hub) -> TV (hub) -> Anime (page); Media -> Books; Notes (page) in Media; // 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). // a TV -> Media CONTAINS edge (a cycle the tree must survive); a journal (never a member).
@@ -273,6 +356,7 @@ void main() {
title: title, title: title,
journalDate: journal, journalDate: journal,
createdAt: 0, createdAt: 0,
isTag: false,
degree: 1, degree: 1,
backlinks: 0, backlinks: 0,
cluster: 0, cluster: 0,
@@ -370,6 +454,7 @@ void main() {
id: '$i', id: '$i',
title: hub ? 'Library' : 'Page $i', title: hub ? 'Library' : 'Page $i',
createdAt: 0, createdAt: 0,
isTag: false,
degree: 1, degree: 1,
backlinks: 0, backlinks: 0,
cluster: 0, cluster: 0,
+72 -17
View File
@@ -519,58 +519,113 @@ void main() {
} }
void tagTests() { 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 b = demo();
final note = byTitle(b, 'Games'); final note = byTitle(b, 'Games');
await b.setPageTags(note.id, ['tema']); await b.setPageTags(note.id, ['tema']);
await pump(tester, b, route: PageTarget(id: note.id)); await pump(tester, b, route: PageTarget(id: note.id));
expect(find.byKey(const ValueKey('tag-tema')), findsOneWidget); expect(find.byKey(const ValueKey('tag-tema')), findsOneWidget);
expect(byTitle(b, 'tema').isTag, isTrue);
await tester.tap(find.byKey(const ValueKey('add-tag'))); await tester.tap(find.byKey(const ValueKey('add-tag')));
await settle(tester); 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); expect(find.byKey(const ValueKey('tag-opt-tema')), findsNothing);
await tester.enterText(find.byKey(const ValueKey('tag-input')), '#ocio'); await tester.enterText(find.byKey(const ValueKey('tag-input')), '#ocio');
await settle(tester); await settle(tester);
await tester.tap(find.byKey(const ValueKey('tag-create'))); await tester.tap(find.byKey(const ValueKey('tag-create')));
await settle(tester); await settle(tester);
expect(byTitle(b, 'Games').tags, ['tema', 'ocio']); await tester.tap(find.byKey(const ValueKey('tag-done')));
expect(byTitle(b, 'ocio').isTag, isTrue); 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 tester.tap(find.byKey(const ValueKey('untag-tema')));
await settle(tester); await settle(tester);
expect(byTitle(b, 'Games').tags, ['ocio']); 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', ( testWidgets('a page has one type: set, change, remove', (tester) async {
tester,
) async {
final b = demo(); final b = demo();
final note = byTitle(b, 'Games'); final note = byTitle(b, 'Games');
await pump(tester, b, route: PageTarget(id: note.id)); 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 tester.tap(find.byKey(const ValueKey('add-type')));
await settle(tester); await settle(tester);
expect(find.byKey(const ValueKey('type-opt-persona')), findsOneWidget); expect(find.byKey(const ValueKey('type-opt-persona')), findsOneWidget);
await tester.tap(find.byKey(const ValueKey('type-opt-lugar'))); await tester.tap(find.byKey(const ValueKey('type-opt-lugar')));
await settle(tester); 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); await settle(tester);
expect(find.byKey(const ValueKey('type-opt-lugar')), findsNothing);
await tester.enterText(find.byKey(const ValueKey('type-input')), 'ocio'); await tester.enterText(find.byKey(const ValueKey('type-input')), 'ocio');
await settle(tester); await settle(tester);
await tester.tap(find.byKey(const ValueKey('type-create'))); await tester.tap(find.byKey(const ValueKey('type-create')));
await settle(tester); await settle(tester);
expect(byTitle(b, 'Games').types, ['lugar', 'ocio']); expect(byTitle(b, 'Games').pageType, 'ocio');
// Types are names, never pages.
expect(b.pages.values.any((p) => p.title == 'ocio'), isFalse); 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); 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);
}); });
} }
+102 -84
View File
@@ -32,8 +32,6 @@ class MemoryBackend implements GraphBackend {
isHub: false, isHub: false,
description: '', description: '',
tags: const [], tags: const [],
isTag: false,
types: const [],
); );
pages[p.id] = p; pages[p.id] = p;
outlines[p.id] = []; outlines[p.id] = [];
@@ -156,35 +154,12 @@ class MemoryBackend implements GraphBackend {
n.content = n.content.replaceAll('[[${p.title}]]', '[[$t]]'); n.content = n.content.replaceAll('[[${p.title}]]', '[[$t]]');
} }
} }
final old = p.title;
_put(p, title: t); _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]!; return pages[id]!;
} }
@override @override
Future<void> deletePage(String id) async { Future<void> 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); pages.remove(id);
outlines.remove(id); outlines.remove(id);
_forget(id); _forget(id);
@@ -355,8 +330,8 @@ class MemoryBackend implements GraphBackend {
bool? isHub, bool? isHub,
String? description, String? description,
List<String>? tags, List<String>? tags,
bool? isTag, String? pageType,
List<String>? types, bool clearType = false,
}) { }) {
pages[p.id] = PageDto( pages[p.id] = PageDto(
id: p.id, id: p.id,
@@ -368,8 +343,7 @@ class MemoryBackend implements GraphBackend {
isHub: isHub ?? p.isHub, isHub: isHub ?? p.isHub,
description: description ?? p.description, description: description ?? p.description,
tags: tags ?? p.tags, tags: tags ?? p.tags,
isTag: isTag ?? p.isTag, pageType: clearType ? null : (pageType ?? p.pageType),
types: types ?? p.types,
); );
} }
@@ -463,56 +437,37 @@ class MemoryBackend implements GraphBackend {
next.add(t); next.add(t);
} }
} }
final stored = <String>[]; next.sort((a, b) => _tagKey(a).compareTo(_tagKey(b)));
for (final t in next) { _put(p, tags: 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);
return pages[pageId]!; return pages[pageId]!;
} }
/// Tag names of a page: its tags plus its type (which implies a tag), once each.
List<String> 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']; static const suggestedTypes = ['persona', 'lugar', 'empresa'];
@override @override
Future<PageDto> setPageTypes(String pageId, List<String> types) async { Future<PageDto> setPageType(String pageId, String? type) async {
final p = pages[pageId]!; final p = pages[pageId]!;
if (p.journalDate != null) throw Exception('Journals cannot have types'); final t = type?.trim() ?? '';
final next = <String>[]; if (p.journalDate != null && t.isNotEmpty) {
for (final raw in types) { throw Exception('Journals cannot have a type');
final t = raw.trim();
if (t.isNotEmpty && !next.any((x) => _tagKey(x) == _tagKey(t))) {
next.add(t);
} }
} _put(p, pageType: t, clearType: t.isEmpty);
_put(p, types: next);
return pages[pageId]!; return pages[pageId]!;
} }
@override
Future<PageDto> addPageType(String pageId, String ty) =>
setPageTypes(pageId, [...pages[pageId]!.types, ty]);
@override
Future<PageDto> removePageType(String pageId, String ty) =>
setPageTypes(pageId, [
for (final t in pages[pageId]!.types)
if (_tagKey(t) != _tagKey(ty)) t,
]);
@override @override
Future<List<TagDto>> typeCatalog() async { Future<List<TagDto>> typeCatalog() async {
final used = <String, TagDto>{}; final used = <String, TagDto>{};
for (final p in pages.values) { 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); final k = _tagKey(t);
used[k] = TagDto( used[k] = TagDto(
name: used[k]?.name ?? t, name: used[k]?.name ?? t,
@@ -544,26 +499,68 @@ class MemoryBackend implements GraphBackend {
@override @override
Future<List<TagDto>> tagCatalog() async { Future<List<TagDto>> tagCatalog() async {
final out = [ final used = <String, TagDto>{};
for (final t in pages.values) for (final p in pages.values) {
if (t.isTag) for (final t in allTags(p)) {
TagDto( final k = _tagKey(t);
name: t.title, used[k] = TagDto(
count: pages.values name: used[k]?.name ?? t,
.where((p) => p.tags.any((x) => _tagKey(x) == _tagKey(t.title))) count: (used[k]?.count ?? 0) + 1,
.length,
suggested: false, suggested: false,
), );
]..sort((a, b) => b.count.compareTo(a.count)); }
return out; }
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 @override
Future<List<PageBrief>> pagesWithTag(String tag) async => [ Future<List<PageBrief>> pagesWithTag(String tag) async => [
for (final p in pages.values) 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<int> 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 = <String>{};
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<int> 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 @override
Future<String> hubTemplate(String hubId) async { Future<String> hubTemplate(String hubId) async {
if (!_isHub(hubId)) throw Exception('Not a hub'); if (!_isHub(hubId)) throw Exception('Not a hub');
@@ -889,20 +886,28 @@ class MemoryBackend implements GraphBackend {
t.add(index[m]!); t.add(index[m]!);
k.add(EdgeKind.contains.index); 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 = <String, String>{};
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) { for (final p in pages.values) {
final i = index[p.id]; final i = index[p.id];
if (i == null) continue; if (i == null) continue;
for (final tg in p.tags) { for (final tg in allTags(p)) {
final tp = _byTitle(tg);
final j = tp == null ? null : index[tp.id];
if (j == null) continue;
s.add(i); s.add(i);
t.add(j); t.add(tagIndex[_tagKey(tg)]!);
k.add(EdgeKind.tagged.index); k.add(EdgeKind.tagged.index);
} }
} }
while (degree.length < ids.length) { final total = ids.length + tagKeys.length;
while (degree.length < total) {
degree.add(0); degree.add(0);
} }
for (var e = 0; e < s.length; e++) { for (var e = 0; e < s.length; e++) {
@@ -922,7 +927,20 @@ class MemoryBackend implements GraphBackend {
cluster: 0, cluster: 0,
isHub: _isHub(ids[i]), isHub: _isHub(ids[i]),
homeHub: home[ids[i]] == null ? null : index[home[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), sources: Uint32List.fromList(s),