|
| 1 | +import 'package:flutter_taerae/flutter_taerae.dart'; |
| 2 | +import 'package:flutter_taerae/flutter_taerae_platform_interface.dart'; |
| 3 | +import 'package:flutter_test/flutter_test.dart'; |
| 4 | + |
| 5 | +class _ThrowingBasePlatform extends TaeraeFlutterPlatform {} |
| 6 | + |
| 7 | +void main() { |
| 8 | + group('TaeraeFlutterPlatform', () { |
| 9 | + test('base implementation throws for unimplemented API', () { |
| 10 | + final _ThrowingBasePlatform platform = _ThrowingBasePlatform(); |
| 11 | + expect(platform.getPlatformVersion, throwsUnimplementedError); |
| 12 | + }); |
| 13 | + }); |
| 14 | + |
| 15 | + group('TaeraeGraphController', () { |
| 16 | + test('creates from json and returns a defensive graph copy', () { |
| 17 | + final TaeraeGraphController controller = TaeraeGraphController.fromJson( |
| 18 | + <String, Object?>{ |
| 19 | + 'nodes': <Object?>[ |
| 20 | + <String, Object?>{ |
| 21 | + 'id': 'n1', |
| 22 | + 'labels': <Object?>['User'], |
| 23 | + }, |
| 24 | + <String, Object?>{'id': 'n2'}, |
| 25 | + ], |
| 26 | + 'edges': <Object?>[ |
| 27 | + <String, Object?>{'id': 'e1', 'from': 'n1', 'to': 'n2'}, |
| 28 | + ], |
| 29 | + }, |
| 30 | + ); |
| 31 | + |
| 32 | + final TaeraeGraph detached = controller.graph..removeNode('n1'); |
| 33 | + expect(detached.containsNode('n1'), isFalse); |
| 34 | + expect(controller.containsNode('n1'), isTrue); |
| 35 | + expect(controller.edgeById('e1')?.id, equals('e1')); |
| 36 | + expect(controller.nodeById('n1')?.id, equals('n1')); |
| 37 | + }); |
| 38 | + |
| 39 | + test('nodes and edges getters are sorted and cache until invalidated', () { |
| 40 | + final TaeraeGraphController controller = TaeraeGraphController() |
| 41 | + ..upsertNode('b') |
| 42 | + ..upsertNode('a') |
| 43 | + ..upsertEdge('e2', 'a', 'b') |
| 44 | + ..upsertEdge('e1', 'b', 'a'); |
| 45 | + |
| 46 | + final List<TaeraeNode> firstNodes = controller.nodes; |
| 47 | + final List<TaeraeNode> secondNodes = controller.nodes; |
| 48 | + final List<TaeraeEdge> firstEdges = controller.edges; |
| 49 | + final List<TaeraeEdge> secondEdges = controller.edges; |
| 50 | + |
| 51 | + expect( |
| 52 | + firstNodes.map((TaeraeNode node) => node.id), |
| 53 | + equals(<String>['a', 'b']), |
| 54 | + ); |
| 55 | + expect( |
| 56 | + firstEdges.map((TaeraeEdge edge) => edge.id), |
| 57 | + equals(<String>['e1', 'e2']), |
| 58 | + ); |
| 59 | + expect(identical(firstNodes, secondNodes), isTrue); |
| 60 | + expect(identical(firstEdges, secondEdges), isTrue); |
| 61 | + |
| 62 | + controller.upsertNode('c'); |
| 63 | + final List<TaeraeNode> thirdNodes = controller.nodes; |
| 64 | + expect(identical(firstNodes, thirdNodes), isFalse); |
| 65 | + expect( |
| 66 | + thirdNodes.map((TaeraeNode node) => node.id), |
| 67 | + equals(<String>['a', 'b', 'c']), |
| 68 | + ); |
| 69 | + }); |
| 70 | + |
| 71 | + test('query helpers delegate to graph operations', () { |
| 72 | + final TaeraeGraphController controller = TaeraeGraphController() |
| 73 | + ..upsertNode('n1', labels: const <String>['User']) |
| 74 | + ..upsertNode( |
| 75 | + 'n2', |
| 76 | + labels: const <String>['User'], |
| 77 | + properties: const <String, Object?>{'team': 'A'}, |
| 78 | + ) |
| 79 | + ..upsertNode('n3') |
| 80 | + ..upsertEdge('e1', 'n1', 'n2', type: 'friend') |
| 81 | + ..upsertEdge('e2', 'n3', 'n1', type: 'friend'); |
| 82 | + |
| 83 | + expect(controller.nodesByLabel('User').length, equals(2)); |
| 84 | + expect( |
| 85 | + controller.nodesWhereProperty('team', 'A').single.id, |
| 86 | + equals('n2'), |
| 87 | + ); |
| 88 | + expect(controller.outgoing('n1').single.id, equals('e1')); |
| 89 | + expect(controller.incoming('n1').single.id, equals('e2')); |
| 90 | + expect( |
| 91 | + controller.neighbors('n1').map((TaeraeNode node) => node.id), |
| 92 | + unorderedEquals(<String>['n2', 'n3']), |
| 93 | + ); |
| 94 | + expect( |
| 95 | + controller.shortestPathBfs('n3', 'n2'), |
| 96 | + equals(const <String>['n3', 'n1', 'n2']), |
| 97 | + ); |
| 98 | + }); |
| 99 | + |
| 100 | + test('notifies only on effective mutations and clear behavior', () { |
| 101 | + final TaeraeGraphController controller = TaeraeGraphController(); |
| 102 | + int notifyCount = 0; |
| 103 | + controller.addListener(() { |
| 104 | + notifyCount += 1; |
| 105 | + }); |
| 106 | + |
| 107 | + expect(controller.removeNode('missing'), isFalse); |
| 108 | + expect(controller.removeEdge('missing'), isFalse); |
| 109 | + controller.clear(); // Empty no-op. |
| 110 | + expect(notifyCount, equals(0)); |
| 111 | + |
| 112 | + controller |
| 113 | + ..upsertNode('n1') |
| 114 | + ..upsertNode('n2') |
| 115 | + ..upsertEdge('e1', 'n1', 'n2'); |
| 116 | + expect(notifyCount, equals(3)); |
| 117 | + |
| 118 | + expect(controller.removeEdge('e1'), isTrue); |
| 119 | + expect(notifyCount, equals(4)); |
| 120 | + expect(controller.removeNode('n1'), isTrue); |
| 121 | + expect(notifyCount, equals(5)); |
| 122 | + |
| 123 | + controller.clear(); |
| 124 | + expect(notifyCount, equals(6)); |
| 125 | + controller.clear(); // Already empty. |
| 126 | + expect(notifyCount, equals(6)); |
| 127 | + }); |
| 128 | + |
| 129 | + test( |
| 130 | + 'replace/import/export helpers work with pretty json and validation', |
| 131 | + () { |
| 132 | + final TaeraeGraph replacement = TaeraeGraph() |
| 133 | + ..upsertNode('r1') |
| 134 | + ..upsertNode('r2') |
| 135 | + ..upsertEdge('re1', 'r1', 'r2'); |
| 136 | + |
| 137 | + final TaeraeGraphController controller = TaeraeGraphController(); |
| 138 | + int notifyCount = 0; |
| 139 | + controller.addListener(() { |
| 140 | + notifyCount += 1; |
| 141 | + }); |
| 142 | + |
| 143 | + controller.replaceGraph(replacement); |
| 144 | + expect(controller.containsEdge('re1'), isTrue); |
| 145 | + expect(notifyCount, equals(1)); |
| 146 | + |
| 147 | + final Map<String, Object?> exported = controller.exportToJson(); |
| 148 | + expect(exported['nodes'], isA<List<Object?>>()); |
| 149 | + final String pretty = controller.exportToJsonString(pretty: true); |
| 150 | + expect(pretty.contains('\n'), isTrue); |
| 151 | + |
| 152 | + controller.importFromJson(<String, Object?>{ |
| 153 | + 'nodes': <Object?>[ |
| 154 | + <String, Object?>{'id': 'i1'}, |
| 155 | + ], |
| 156 | + 'edges': <Object?>[], |
| 157 | + }); |
| 158 | + expect(controller.containsNode('i1'), isTrue); |
| 159 | + expect(notifyCount, equals(2)); |
| 160 | + |
| 161 | + controller.importFromJsonString('{"nodes":[{"id":"s1"}],"edges":[]}'); |
| 162 | + expect(controller.containsNode('s1'), isTrue); |
| 163 | + expect(notifyCount, equals(3)); |
| 164 | + |
| 165 | + expect( |
| 166 | + () => controller.importFromJsonString('[]'), |
| 167 | + throwsFormatException, |
| 168 | + ); |
| 169 | + }, |
| 170 | + ); |
| 171 | + }); |
| 172 | +} |
0 commit comments