mirror of
https://github.com/Dasemu/Synapse.git
synced 2026-11-26 23:36:20 +00:00
test(app): MemoryBackend follows the core's type spelling, name cap and icon clearing
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
872073a0a0
commit
ad03383933
2 files changed
+30
-2
No files matched your search
@@ -514,6 +514,16 @@ class MemoryBackend implements GraphBackend {
|
|||||||
/// Type definitions like the core's `type_defs`: key -> (name, icon, removed).
|
/// Type definitions like the core's `type_defs`: key -> (name, icon, removed).
|
||||||
final typeDefs = <String, ({String name, String? icon, bool removed})>{};
|
final typeDefs = <String, ({String name, String? icon, bool removed})>{};
|
||||||
|
|
||||||
|
/// Pages in the core's catalogue order (`created_at`, then id): the first spells a name.
|
||||||
|
List<PageDto> _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) {
|
({String name, String? icon, bool removed})? _liveDef(String name) {
|
||||||
final d = typeDefs[_tagKey(name)];
|
final d = typeDefs[_tagKey(name)];
|
||||||
return d == null || d.removed ? null : d;
|
return d == null || d.removed ? null : d;
|
||||||
@@ -522,7 +532,7 @@ class MemoryBackend implements GraphBackend {
|
|||||||
@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 _byCreation()) {
|
||||||
for (final t in [if (p.pageType != null) p.pageType!]) {
|
for (final t in [if (p.pageType != null) p.pageType!]) {
|
||||||
final k = _tagKey(t);
|
final k = _tagKey(t);
|
||||||
used[k] = _tag(
|
used[k] = _tag(
|
||||||
@@ -580,6 +590,9 @@ class MemoryBackend implements GraphBackend {
|
|||||||
String _typeName(String raw) {
|
String _typeName(String raw) {
|
||||||
final n = _normTag(raw);
|
final n = _normTag(raw);
|
||||||
if (n.isEmpty) throw Exception('Empty type name');
|
if (n.isEmpty) throw Exception('Empty type name');
|
||||||
|
if (n.runes.length > maxTagChars) {
|
||||||
|
throw Exception('Type is longer than $maxTagChars characters');
|
||||||
|
}
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -587,7 +600,7 @@ class MemoryBackend implements GraphBackend {
|
|||||||
String _typeSpelling(String name) {
|
String _typeSpelling(String name) {
|
||||||
final d = _liveDef(name);
|
final d = _liveDef(name);
|
||||||
if (d != null) return d.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)) {
|
if (p.pageType != null && _tagKey(p.pageType!) == _tagKey(name)) {
|
||||||
return p.pageType!;
|
return p.pageType!;
|
||||||
}
|
}
|
||||||
@@ -616,6 +629,10 @@ class MemoryBackend implements GraphBackend {
|
|||||||
i.runes.every((r) => r < 128))) {
|
i.runes.every((r) => r < 128))) {
|
||||||
throw Exception('Not an emoji icon');
|
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)] = (
|
typeDefs[_tagKey(n)] = (
|
||||||
name: _typeSpelling(n),
|
name: _typeSpelling(n),
|
||||||
icon: (i == null || i.isEmpty) ? null : i,
|
icon: (i == null || i.isEmpty) ? null : i,
|
||||||
@@ -710,6 +727,9 @@ class MemoryBackend implements GraphBackend {
|
|||||||
Future<int> renameTag(String old, String new_) async {
|
Future<int> renameTag(String old, String new_) async {
|
||||||
final name = _normTag(new_);
|
final name = _normTag(new_);
|
||||||
if (name.isEmpty) throw Exception('Empty tag name');
|
if (name.isEmpty) throw Exception('Empty tag name');
|
||||||
|
if (name.runes.length > maxTagChars) {
|
||||||
|
throw Exception('Tag is longer than $maxTagChars characters');
|
||||||
|
}
|
||||||
var n = 0;
|
var n = 0;
|
||||||
for (final p in pages.values.toList()) {
|
for (final p in pages.values.toList()) {
|
||||||
final hasTag = p.tags.any((t) => _tagKey(t) == _tagKey(old));
|
final hasTag = p.tags.any((t) => _tagKey(t) == _tagKey(old));
|
||||||
|
|||||||
@@ -73,6 +73,14 @@ void main() {
|
|||||||
'memory backend keeps icons through rename and drops them on delete',
|
'memory backend keeps icons through rename and drops them on delete',
|
||||||
() async {
|
() async {
|
||||||
final b = await demo();
|
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);
|
expect(await b.renameType('persona', 'Gente'), 2);
|
||||||
var cat = await b.typeCatalog();
|
var cat = await b.typeCatalog();
|
||||||
expect(cat.firstWhere((t) => t.name == 'Gente').icon, '🧑');
|
expect(cat.firstWhere((t) => t.name == 'Gente').icon, '🧑');
|
||||||
|
|||||||
Reference in new issue
Block a user