Testing
Test the agent's calls and the screen's response with scripted AG-UI events. No backend or model is required.
ScriptedAgent is an AgentConnection from package:tandem/. Pass it as connection: to TandemScope.. This test creates its OrderCubit in setUp.
testWidgets('the agent sets a quantity from the chat', (tester) async {
final agent = ScriptedAgent([
(_) => const [
AgentStep.callTool('set_quantity', {'sku': 'VM-27Q', 'quantity': 15}),
],
(_) => const [AgentStep.say('Done.')],
]);
await tester.pumpWidget(
MaterialApp(
home: BlocProvider.value(
value: cubit,
child: TandemScope.connected(
surface: (context) => context.read<OrderCubit>(),
connection: agent,
approvals: const .fromState(),
child: const AgentChat(),
),
),
),
);
await tester.enterText(find.byType(TextField), 'fifteen monitors');
await tester.tap(find.text('Send'));
await tester.pumpAndSettle();
expect(cubit.draft.lines.single.quantity, 15);
expect(find.text('Done.'), findsOneWidget);
});Each callback in the script handles one run. agent. records the request bodies, so a test can check the data and tools sent to the agent. A run after the last callback fails with ScriptedErrorCode.. ScriptedAgent. handles every run with the same callback, and AgentStep. draws a view.
Inside a callback, AgentRequest(raw) reads the run's request, such as the user's latest text, the offered tools and the actions the user pressed in a view.
Use AgentStep. to hold a run open and AgentStep. to simulate an error.
AgentListing. reads what a state class lists without opening a session, so a plain unit test can call a listed method as the agent would. A call needs approved: to run a method listed with .:
test('place_order runs only while listed and only once approved', () async {
await expectLater(
AgentListing.of(cubit).call('place_order', {}),
throwsA(isA<ToolCallRefused>()),
);
cubit.setQuantity('VM-27Q', 2);
final listing = AgentListing.of(cubit);
await expectLater(
listing.call('place_order', {}),
throwsA(isA<ToolCallRefused>()),
);
expect(await listing.call('place_order', {}, approved: true), 'ORD-1');
});TandemBridge. tests the coding-agent path by calling a listed method in the running screen. It uses the same validation and approval rules as a coding agent.
testWidgets('a coding agent fills the order screen', (tester) async {
await tester.pumpWidget(
RepositoryProvider.value(
value: orders,
child: const MaterialApp(home: OrderScreen()),
),
);
final answer = await TandemBridge.call(
'set_quantity',
arguments: {'sku': 'VM-27Q', 'quantity': 15},
);
await tester.pump();
expect(answer['status'], 'returned');
expect(find.text('15'), findsOneWidget);
});