|
| 1 | +// Copyright 2025 The Flutter Authors. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:async'; |
| 6 | +import 'dart:convert'; |
| 7 | + |
| 8 | +import 'package:flutter/material.dart'; |
| 9 | +import 'package:flutter_test/flutter_test.dart'; |
| 10 | +import 'package:genui/genui.dart'; |
| 11 | +import 'package:json_schema_builder/json_schema_builder.dart'; |
| 12 | +import 'package:logging/logging.dart'; |
| 13 | + |
| 14 | +void main() { |
| 15 | + group('Secure Error Boundary Tests', () { |
| 16 | + setUp(() { |
| 17 | + hierarchicalLoggingEnabled = true; |
| 18 | + Logger.root.level = Level.ALL; |
| 19 | + Logger.root.onRecord.listen((record) { |
| 20 | + // ignore: avoid_print |
| 21 | + print('[${record.level.name}] ${record.message}'); |
| 22 | + if (record.error != null) { |
| 23 | + // ignore: avoid_print |
| 24 | + print(' Error: ${record.error}'); |
| 25 | + } |
| 26 | + }); |
| 27 | + }); |
| 28 | + test( |
| 29 | + 'A2uiValidationException is reported cleanly as VALIDATION_FAILED', |
| 30 | + () async { |
| 31 | + final surfaceController = SurfaceController(catalogs: []); |
| 32 | + final Completer<JsonMap> errorCompleter = Completer(); |
| 33 | + |
| 34 | + surfaceController.onSubmit.listen((event) { |
| 35 | + final String interaction = |
| 36 | + event.parts.first.asUiInteractionPart!.interaction; |
| 37 | + final data = jsonDecode(interaction) as JsonMap; |
| 38 | + errorCompleter.complete(data); |
| 39 | + }); |
| 40 | + |
| 41 | + surfaceController.reportError( |
| 42 | + A2uiValidationException( |
| 43 | + 'Invalid component properties', |
| 44 | + surfaceId: 'test-surface', |
| 45 | + path: '/components/0', |
| 46 | + ), |
| 47 | + StackTrace.current, |
| 48 | + ); |
| 49 | + |
| 50 | + final JsonMap result = await errorCompleter.future; |
| 51 | + expect(result['version'], equals('v0.9')); |
| 52 | + final error = result['error'] as JsonMap; |
| 53 | + expect(error['code'], equals('VALIDATION_FAILED')); |
| 54 | + expect(error['message'], equals('Invalid component properties')); |
| 55 | + expect(error['surfaceId'], equals('test-surface')); |
| 56 | + expect(error['path'], equals('/components/0')); |
| 57 | + expect(error.containsKey('stackTrace'), isFalse); |
| 58 | + }, |
| 59 | + ); |
| 60 | + |
| 61 | + test( |
| 62 | + 'A2uiFunctionException is reported as FUNCTION_EXECUTION_FAILED', |
| 63 | + () async { |
| 64 | + final surfaceController = SurfaceController(catalogs: []); |
| 65 | + final Completer<JsonMap> errorCompleter = Completer(); |
| 66 | + |
| 67 | + surfaceController.onSubmit.listen((event) { |
| 68 | + final String interaction = |
| 69 | + event.parts.first.asUiInteractionPart!.interaction; |
| 70 | + final data = jsonDecode(interaction) as JsonMap; |
| 71 | + errorCompleter.complete(data); |
| 72 | + }); |
| 73 | + |
| 74 | + surfaceController.reportError( |
| 75 | + A2uiFunctionException( |
| 76 | + 'Custom rule validation failed', |
| 77 | + functionName: 'validateEmail', |
| 78 | + argumentKey: 'email', |
| 79 | + ), |
| 80 | + StackTrace.current, |
| 81 | + ); |
| 82 | + |
| 83 | + final JsonMap result = await errorCompleter.future; |
| 84 | + expect(result['version'], equals('v0.9')); |
| 85 | + final error = result['error'] as JsonMap; |
| 86 | + expect(error['code'], equals('FUNCTION_EXECUTION_FAILED')); |
| 87 | + expect(error['message'], equals('Custom rule validation failed')); |
| 88 | + expect(error['functionName'], equals('validateEmail')); |
| 89 | + expect(error.containsKey('stackTrace'), isFalse); |
| 90 | + }, |
| 91 | + ); |
| 92 | + |
| 93 | + test('Raw VM exceptions are completely masked as INTERNAL_ERROR', () async { |
| 94 | + final surfaceController = SurfaceController(catalogs: []); |
| 95 | + final Completer<JsonMap> errorCompleter = Completer(); |
| 96 | + |
| 97 | + surfaceController.onSubmit.listen((event) { |
| 98 | + final String interaction = |
| 99 | + event.parts.first.asUiInteractionPart!.interaction; |
| 100 | + final data = jsonDecode(interaction) as JsonMap; |
| 101 | + errorCompleter.complete(data); |
| 102 | + }); |
| 103 | + |
| 104 | + // Simulate a VM/internal crash |
| 105 | + surfaceController.reportError(TypeError(), StackTrace.current); |
| 106 | + |
| 107 | + final JsonMap result = await errorCompleter.future; |
| 108 | + expect(result['version'], equals('v0.9')); |
| 109 | + final error = result['error'] as JsonMap; |
| 110 | + expect(error['code'], equals('INTERNAL_ERROR')); |
| 111 | + expect(error['message'], equals('An unexpected system error occurred.')); |
| 112 | + expect(error.containsKey('surfaceId'), isFalse); |
| 113 | + expect(error.containsKey('path'), isFalse); |
| 114 | + expect(error.containsKey('stackTrace'), isFalse); |
| 115 | + }); |
| 116 | + |
| 117 | + testWidgets('Button widget handles action VM throws by wrapping in ' |
| 118 | + 'A2uiFunctionException', (WidgetTester tester) async { |
| 119 | + final mockFunction = MockFunction( |
| 120 | + name: 'crashFunc', |
| 121 | + onExecute: (args, context) => throw TypeError(), |
| 122 | + ); |
| 123 | + |
| 124 | + final surfaceController = SurfaceController( |
| 125 | + catalogs: [ |
| 126 | + Catalog( |
| 127 | + [BasicCatalogItems.button, BasicCatalogItems.text], |
| 128 | + catalogId: 'test_catalog', |
| 129 | + functions: [mockFunction], |
| 130 | + ), |
| 131 | + ], |
| 132 | + ); |
| 133 | + |
| 134 | + final List<ChatMessage> messages = []; |
| 135 | + surfaceController.onSubmit.listen(messages.add); |
| 136 | + |
| 137 | + const surfaceId = 'testSurface'; |
| 138 | + final components = [ |
| 139 | + const Component( |
| 140 | + id: 'root', |
| 141 | + type: 'Button', |
| 142 | + properties: { |
| 143 | + 'child': 'button_text', |
| 144 | + 'action': { |
| 145 | + 'functionCall': {'call': 'crashFunc'}, |
| 146 | + }, |
| 147 | + }, |
| 148 | + ), |
| 149 | + const Component( |
| 150 | + id: 'button_text', |
| 151 | + type: 'Text', |
| 152 | + properties: {'text': 'Click Me'}, |
| 153 | + ), |
| 154 | + ]; |
| 155 | + |
| 156 | + surfaceController.handleMessage( |
| 157 | + UpdateComponents(surfaceId: surfaceId, components: components), |
| 158 | + ); |
| 159 | + surfaceController.handleMessage( |
| 160 | + const CreateSurface(surfaceId: surfaceId, catalogId: 'test_catalog'), |
| 161 | + ); |
| 162 | + |
| 163 | + await tester.pumpWidget( |
| 164 | + MaterialApp( |
| 165 | + home: Scaffold( |
| 166 | + body: Surface( |
| 167 | + surfaceContext: surfaceController.contextFor(surfaceId), |
| 168 | + ), |
| 169 | + ), |
| 170 | + ), |
| 171 | + ); |
| 172 | + |
| 173 | + await tester.pumpAndSettle(); |
| 174 | + |
| 175 | + expect(find.byType(ElevatedButton), findsOneWidget); |
| 176 | + final ElevatedButton button = tester.widget<ElevatedButton>( |
| 177 | + find.byType(ElevatedButton), |
| 178 | + ); |
| 179 | + expect(button.onPressed, isNotNull); |
| 180 | + await tester.runAsync(() async { |
| 181 | + await tester.tap(find.byType(ElevatedButton)); |
| 182 | + await Future<void>.delayed(const Duration(milliseconds: 100)); |
| 183 | + }); |
| 184 | + |
| 185 | + expect(messages, isNotEmpty); |
| 186 | + final String interaction = |
| 187 | + messages.first.parts.first.asUiInteractionPart!.interaction; |
| 188 | + final result = jsonDecode(interaction) as JsonMap; |
| 189 | + expect(result['version'], equals('v0.9')); |
| 190 | + final error = result['error'] as JsonMap; |
| 191 | + expect(error['code'], equals('FUNCTION_EXECUTION_FAILED')); |
| 192 | + expect(error['message'], contains('Function execution failed')); |
| 193 | + expect(error['functionName'], equals('crashFunc')); |
| 194 | + expect(error.containsKey('stackTrace'), isFalse); |
| 195 | + |
| 196 | + surfaceController.dispose(); |
| 197 | + }); |
| 198 | + }); |
| 199 | +} |
| 200 | + |
| 201 | +class MockFunction extends SynchronousClientFunction { |
| 202 | + MockFunction({required this.name, required this.onExecute}); |
| 203 | + |
| 204 | + @override |
| 205 | + final String name; |
| 206 | + |
| 207 | + final Object? Function(JsonMap, ExecutionContext) onExecute; |
| 208 | + |
| 209 | + @override |
| 210 | + String get description => 'Mock function for testing.'; |
| 211 | + |
| 212 | + @override |
| 213 | + ClientFunctionReturnType get returnType => ClientFunctionReturnType.empty; |
| 214 | + |
| 215 | + @override |
| 216 | + Schema get argumentSchema => S.object(); |
| 217 | + |
| 218 | + @override |
| 219 | + Object? executeSync(JsonMap args, ExecutionContext context) { |
| 220 | + return onExecute(args, context); |
| 221 | + } |
| 222 | +} |
0 commit comments