Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 19 additions & 11 deletions pkgs/dart_mcp_server/test/test_harness.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,21 @@ import 'package:test/test.dart';
import 'package:test_process/test_process.dart';
import 'package:unified_analytics/unified_analytics.dart';

Future<T> callWithRetry<T>(
FutureOr<T> Function() fn, {
int maxTries = 5,
}) async {
var tryCount = 0;
while (true) {
try {
return await fn();
} catch (_) {
if (tryCount++ >= maxTries) rethrow;
}
await Future<void>.delayed(Duration(milliseconds: 100 * tryCount));
}
}

/// A full environment for integration testing the MCP server.
///
/// - Runs the counter app at `test_fixtures/counter_app` using `flutter run`.
Expand Down Expand Up @@ -187,17 +202,10 @@ class TestHarness {
CallToolRequest request, {
int maxTries = 5,
bool expectError = false,
}) async {
var tryCount = 0;
while (true) {
try {
return await callTool(request, expectError: expectError);
} catch (_) {
if (tryCount++ >= maxTries) rethrow;
}
await Future<void>.delayed(Duration(milliseconds: 100 * tryCount));
}
}
}) => callWithRetry(
() => callTool(request, expectError: expectError),
maxTries: maxTries,
);

/// Calls [getPrompt] on the [mcpServerConnection].
Future<GetPromptResult> getPrompt(GetPromptRequest request) =>
Expand Down
43 changes: 23 additions & 20 deletions pkgs/dart_mcp_server/test/tools/analyzer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -309,27 +309,30 @@ void main() {
],
},
);
final result = await testHarness.callToolWithRetry(request, maxTries: 10);
expect(result.isError, isNot(true));
expect(result.content, hasLength(2));
expect(
result.content,
containsAll([
isA<TextContent>().having(
(t) => t.text,
'text',
contains(
"The argument type 'String' can't be assigned to the "
"parameter type 'num'.",
// It may take a bit for the errors to show up.
await callWithRetry(() async {
final result = await testHarness.callTool(request);
expect(result.isError, isNot(true));
expect(result.content, hasLength(2));
expect(
result.content,
containsAll([
isA<TextContent>().having(
(t) => t.text,
'text',
contains(
"The argument type 'String' can't be assigned to the "
"parameter type 'num'.",
),
),
),
isA<TextContent>().having(
(t) => t.text,
'text',
contains("Undefined name 'foo'"),
),
]),
);
isA<TextContent>().having(
(t) => t.text,
'text',
contains("Undefined name 'foo'"),
),
]),
);
}, maxTries: 10);
});

test('can look up symbols in a workspace', () async {
Expand Down