1
+import 'dart:async';
2
+import 'dart:convert';
3
+import 'dart:typed_data';
4
+
5
+import 'package:dio/dio.dart';
6
+import 'package:flutter_test/flutter_test.dart';
7
+import 'package:localmind/core/models/enums.dart';
8
+import 'package:localmind/features/chat/data/chat_service.dart';
9
+import 'package:localmind/features/chat/data/models/chat_parameters.dart';
10
+import 'package:localmind/features/chat/data/models/message.dart';
11
+import 'package:localmind/features/servers/data/models/server.dart';
12
+
13
+/// Captures the request body and answers with an empty stream.
14
+class _CapturingInterceptor extends Interceptor {
15
+ _CapturingInterceptor(this.lines);
16
+
17
+ final List<String> lines;
18
+ Map<String, dynamic>? body;
19
+
20
+ @override
21
+ void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
22
+ final data = options.data;
23
+ body = data is String
24
+ ? jsonDecode(data) as Map<String, dynamic>
25
+ : Map<String, dynamic>.from(data as Map);
26
+ final controller = StreamController<Uint8List>();
27
+ handler.resolve(
28
+ Response(
29
+ requestOptions: options,
30
+ statusCode: 200,
31
+ data: ResponseBody(controller.stream, 200),
32
+ ),
33
+ );
34
+ Future.microtask(() async {
35
+ for (final line in lines) {
36
+ controller.add(Uint8List.fromList(utf8.encode('$line\n')));
37
+ }
38
+ await controller.close();
39
+ });
40
+ }
41
+}
42
+
43
+Server _server(ServerType type) => Server(
44
+ id: 'test-${type.name}',
45
+ name: 'Test',
46
+ type: type,
47
+ host: 'localhost',
48
+ port: 8080,
49
+ createdAt: DateTime.utc(2026, 9, 28),
50
+ lastConnectedAt: DateTime.utc(2026, 9, 28),
51
+);
52
+
53
+final _messages = [
54
+ Message(
55
+ id: 'm1',
56
+ conversationId: 'c1',
57
+ role: MessageRole.user,
58
+ content: 'hi',
59
+ createdAt: DateTime.utc(2026, 9, 28),
60
+ ),
61
+];
62
+
63
+Future<Map<String, dynamic>> _captureBody(
64
+ ChatService Function(Dio dio) build,
65
+ ServerType type,
66
+ ChatParameters params, {
67
+ List<String> lines = const ['data: [DONE]'],
68
+}) async {
69
+ final interceptor = _CapturingInterceptor(lines);
70
+ final service = build(Dio()..interceptors.add(interceptor));
71
+ await service
72
+ .sendMessage(
73
+ server: _server(type),
74
+ modelId: 'test-model',
75
+ messages: _messages,
76
+ params: params,
77
+ )
78
+ .toList();
79
+ return interceptor.body!;
80
+}
81
+
82
+void main() {
83
+ final omitBoth = ChatParameters.defaults().copyWith(
84
+ sendTemperature: false,
85
+ sendTopP: false,
86
+ );
87
+
88
+ test('OpenAI-compatible sends sampling params by default', () async {
89
+ final body = await _captureBody(
90
+ OpenAICompatibleChatService.new,
91
+ ServerType.openAICompatible,
92
+ ChatParameters.defaults(),
93
+ );
94
+ expect(body, containsPair('temperature', isA<num>()));
95
+ expect(body, containsPair('top_p', isA<num>()));
96
+ });
97
+
98
+ test('OpenAI-compatible omits disabled sampling params (#81)', () async {
99
+ final body = await _captureBody(
100
+ OpenAICompatibleChatService.new,
101
+ ServerType.openAICompatible,
102
+ omitBoth,
103
+ );
104
+ expect(body.containsKey('temperature'), isFalse);
105
+ expect(body.containsKey('top_p'), isFalse);
106
+ });
107
+
108
+ test('OpenRouter omits disabled sampling params (#81)', () async {
109
+ final body = await _captureBody(
110
+ OpenRouterChatService.new,
111
+ ServerType.openRouter,
112
+ ChatParameters.defaults().copyWith(sendTopP: false),
113
+ );
114
+ expect(body.containsKey('temperature'), isTrue);
115
+ expect(body.containsKey('top_p'), isFalse);
116
+ });
117
+
118
+ test('Ollama omits disabled sampling params from options (#81)', () async {
119
+ final body = await _captureBody(
120
+ OllamaChatService.new,
121
+ ServerType.ollama,
122
+ omitBoth,
123
+ lines: [
124
+ jsonEncode({
125
+ 'message': {'content': ''},
126
+ 'done': true,
127
+ }),
128
+ ],
129
+ );
130
+ final options = body['options'] as Map<String, dynamic>;
131
+ expect(options.containsKey('temperature'), isFalse);
132
+ expect(options.containsKey('top_p'), isFalse);
133
+ expect(options.containsKey('num_predict'), isTrue);
134
+ });
135
+}