From ad03383933c0bd0ed35915179ef869e02dfd912c Mon Sep 17 00:00:00 2001 From: Dasemu Date: Mon, 26 Oct 2026 06:53:24 +0100 Subject: [PATCH] test(app): MemoryBackend follows the core's type spelling, name cap and icon clearing Co-Authored-By: Claude Opus 5.5 --- app/test/support/memory_backend.dart | 24 ++++++++++++++++++++++-- app/test/tags_types_test.dart | 8 ++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/app/test/support/memory_backend.dart b/app/test/support/memory_backend.dart index 7d98713..259e52b 100644 --- a/app/test/support/memory_backend.dart +++ b/app/test/support/memory_backend.dart @@ -514,6 +514,16 @@ class MemoryBackend implements GraphBackend { /// Type definitions like the core's `type_defs`: key -> (name, icon, removed). final typeDefs = {}; + /// Pages in the core's catalogue order (`created_at`, then id): the first spells a name. + List _byCreation() => pages.values.toList() + ..sort((a, b) { + final c = a.createdAt.compareTo(b.createdAt); + return c != 0 ? c : a.id.compareTo(b.id); + }); + + /// Like the core's `MAX_TAG_CHARS`. + static const maxTagChars = 48; + ({String name, String? icon, bool removed})? _liveDef(String name) { final d = typeDefs[_tagKey(name)]; return d == null || d.removed ? null : d; @@ -522,7 +532,7 @@ class MemoryBackend implements GraphBackend { @override Future> typeCatalog() async { final used = {}; - for (final p in pages.values) { + for (final p in _byCreation()) { for (final t in [if (p.pageType != null) p.pageType!]) { final k = _tagKey(t); used[k] = _tag( @@ -580,6 +590,9 @@ class MemoryBackend implements GraphBackend { String _typeName(String raw) { final n = _normTag(raw); if (n.isEmpty) throw Exception('Empty type name'); + if (n.runes.length > maxTagChars) { + throw Exception('Type is longer than $maxTagChars characters'); + } return n; } @@ -587,7 +600,7 @@ class MemoryBackend implements GraphBackend { String _typeSpelling(String name) { final d = _liveDef(name); if (d != null) return d.name; - for (final p in pages.values) { + for (final p in _byCreation()) { if (p.pageType != null && _tagKey(p.pageType!) == _tagKey(name)) { return p.pageType!; } @@ -616,6 +629,10 @@ class MemoryBackend implements GraphBackend { i.runes.every((r) => r < 128))) { throw Exception('Not an emoji icon'); } + final current = _liveDef(n); + final next = (i == null || i.isEmpty) ? null : i; + // Like the core: clearing an undefined type's icon defines nothing. + if (current == null ? next == null : current.icon == next) return; typeDefs[_tagKey(n)] = ( name: _typeSpelling(n), icon: (i == null || i.isEmpty) ? null : i, @@ -710,6 +727,9 @@ class MemoryBackend implements GraphBackend { Future renameTag(String old, String new_) async { final name = _normTag(new_); if (name.isEmpty) throw Exception('Empty tag name'); + if (name.runes.length > maxTagChars) { + throw Exception('Tag is longer than $maxTagChars characters'); + } var n = 0; for (final p in pages.values.toList()) { final hasTag = p.tags.any((t) => _tagKey(t) == _tagKey(old)); diff --git a/app/test/tags_types_test.dart b/app/test/tags_types_test.dart index 962aa14..5ba3d69 100644 --- a/app/test/tags_types_test.dart +++ b/app/test/tags_types_test.dart @@ -73,6 +73,14 @@ void main() { 'memory backend keeps icons through rename and drops them on delete', () async { final b = await demo(); + // Like the core: clearing an undefined type's icon defines nothing, names are capped, + // and renaming a plain tag defines no type. + await b.setTypeIcon('nueva', null); + expect(b.typeDefs.containsKey('nueva'), isFalse); + expect(() => b.renameTag('math', 'x' * 49), throwsException); + expect(() => b.createType('x' * 49), throwsException); + await b.renameTag('math', 'idea'); + expect(b.typeDefs.containsKey('math'), isFalse); expect(await b.renameType('persona', 'Gente'), 2); var cat = await b.typeCatalog(); expect(cat.firstWhere((t) => t.name == 'Gente').icon, '🧑');