fix: stabilize chat run lifecycle rendering
This commit is contained in:
@@ -0,0 +1,152 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:social_app/core/api/i_api_client.dart';
|
||||
import 'package:social_app/features/chat/data/models/ag_ui_event.dart';
|
||||
import 'package:social_app/features/chat/data/services/ag_ui_service.dart';
|
||||
|
||||
class _FakeApiClient implements IApiClient {
|
||||
_FakeApiClient({required this.sseLines});
|
||||
|
||||
final List<String> sseLines;
|
||||
|
||||
@override
|
||||
Future<Response<T>> delete<T>(String path, {data, Options? options}) {
|
||||
throw UnimplementedError();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Response<T>> get<T>(String path, {Options? options}) {
|
||||
throw UnimplementedError();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Stream<String>> getSseLines(
|
||||
String path, {
|
||||
Map<String, String>? headers,
|
||||
}) async {
|
||||
return Stream<String>.fromIterable(sseLines);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Response<T>> patch<T>(String path, {data, Options? options}) {
|
||||
throw UnimplementedError();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Response<T>> post<T>(String path, {data, Options? options}) async {
|
||||
final payload = <String, dynamic>{
|
||||
'taskId': 'task-1',
|
||||
'threadId': 'thread-1',
|
||||
'runId': 'run-new',
|
||||
'created': true,
|
||||
};
|
||||
return Response<T>(
|
||||
requestOptions: RequestOptions(path: path),
|
||||
data: payload as T,
|
||||
statusCode: 202,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
List<String> _buildSseEvent({
|
||||
required String id,
|
||||
required String type,
|
||||
required String payload,
|
||||
}) {
|
||||
return <String>['id: $id', 'event: $type', 'data: $payload', ''];
|
||||
}
|
||||
|
||||
void main() {
|
||||
test(
|
||||
'sendMessage ignores stale run events and waits for expected run',
|
||||
() async {
|
||||
final oldRunLines = _buildSseEvent(
|
||||
id: '1',
|
||||
type: AgUiEventTypeWire.runStarted,
|
||||
payload:
|
||||
'{"type":"RUN_STARTED","threadId":"thread-1","runId":"run-old"}',
|
||||
);
|
||||
final oldFinishedLines = _buildSseEvent(
|
||||
id: '2',
|
||||
type: AgUiEventTypeWire.runFinished,
|
||||
payload:
|
||||
'{"type":"RUN_FINISHED","threadId":"thread-1","runId":"run-old"}',
|
||||
);
|
||||
final newRunLines = _buildSseEvent(
|
||||
id: '3',
|
||||
type: AgUiEventTypeWire.runStarted,
|
||||
payload:
|
||||
'{"type":"RUN_STARTED","threadId":"thread-1","runId":"run-new"}',
|
||||
);
|
||||
final newFinishedLines = _buildSseEvent(
|
||||
id: '4',
|
||||
type: AgUiEventTypeWire.runFinished,
|
||||
payload:
|
||||
'{"type":"RUN_FINISHED","threadId":"thread-1","runId":"run-new"}',
|
||||
);
|
||||
|
||||
final service = AgUiService(
|
||||
apiClient: _FakeApiClient(
|
||||
sseLines: <String>[
|
||||
...oldRunLines,
|
||||
...oldFinishedLines,
|
||||
...newRunLines,
|
||||
...newFinishedLines,
|
||||
],
|
||||
),
|
||||
);
|
||||
final events = <AgUiEvent>[];
|
||||
service.onEvent = events.add;
|
||||
|
||||
await service.sendMessage('hello');
|
||||
|
||||
expect(events, hasLength(2));
|
||||
expect(events.first, isA<RunStartedEvent>());
|
||||
expect((events.first as RunStartedEvent).runId, 'run-new');
|
||||
expect(events.last, isA<RunFinishedEvent>());
|
||||
expect((events.last as RunFinishedEvent).runId, 'run-new');
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'sendMessage accepts in-run terminal event without runId after binding',
|
||||
() async {
|
||||
final newRunLines = _buildSseEvent(
|
||||
id: '11',
|
||||
type: AgUiEventTypeWire.runStarted,
|
||||
payload:
|
||||
'{"type":"RUN_STARTED","threadId":"thread-1","runId":"run-new"}',
|
||||
);
|
||||
final noRunIdTextLines = _buildSseEvent(
|
||||
id: '12',
|
||||
type: AgUiEventTypeWire.textMessageEnd,
|
||||
payload:
|
||||
'{"type":"TEXT_MESSAGE_END","threadId":"thread-1","messageId":"m1","answer":"ok","role":"assistant","status":"success"}',
|
||||
);
|
||||
final noRunIdFinishedLines = _buildSseEvent(
|
||||
id: '13',
|
||||
type: AgUiEventTypeWire.runFinished,
|
||||
payload: '{"type":"RUN_FINISHED","threadId":"thread-1"}',
|
||||
);
|
||||
|
||||
final service = AgUiService(
|
||||
apiClient: _FakeApiClient(
|
||||
sseLines: <String>[
|
||||
...newRunLines,
|
||||
...noRunIdTextLines,
|
||||
...noRunIdFinishedLines,
|
||||
],
|
||||
),
|
||||
);
|
||||
final events = <AgUiEvent>[];
|
||||
service.onEvent = events.add;
|
||||
|
||||
await service.sendMessage('hello');
|
||||
|
||||
expect(events, hasLength(3));
|
||||
expect(events[0], isA<RunStartedEvent>());
|
||||
expect(events[1], isA<TextMessageEndEvent>());
|
||||
expect(events[2], isA<RunFinishedEvent>());
|
||||
},
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user