feat: 原生支持 /v1/responses 接口与 Chat Completions 双协议自适应,增加端到端联调测试

This commit is contained in:
shen
2026-09-15 20:44:27 +08:00
parent a717365c10
commit 524788291b
3 changed files with 201 additions and 97 deletions
+3 -3
View File
@@ -6,7 +6,7 @@ import 'package:kouyu_english/core/models.dart';
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
group('AiService resolveEndpointUri (/v1/chat/completions)', () {
group('AiService resolveEndpointUri', () {
test('resolves OpenAI endpoint ending in /v1 to /v1/chat/completions', () {
final uri = AiService.resolveEndpointUri(
provider: AiProviderType.openAi,
@@ -43,13 +43,13 @@ void main() {
expect(uri.toString(), equals('https://codex.slcydia.fun/v1/chat/completions'));
});
test('converts /responses endpoint to /chat/completions', () {
test('preserves /responses endpoint for OpenAI Responses API support', () {
final uri = AiService.resolveEndpointUri(
provider: AiProviderType.compatible,
endpoint: 'https://codex.slcydia.fun/v1/responses',
model: 'gpt-4o-mini',
);
expect(uri.toString(), equals('https://codex.slcydia.fun/v1/chat/completions'));
expect(uri.toString(), equals('https://codex.slcydia.fun/v1/responses'));
});
test('resolves Gemini endpoint to :generateContent', () {
@@ -0,0 +1,55 @@
import 'dart:io';
import 'package:flutter_test/flutter_test.dart';
import 'package:kouyu_english/core/ai_service.dart';
import 'package:kouyu_english/core/models.dart';
class RealHttpOverrides extends HttpOverrides {
@override
HttpClient createHttpClient(SecurityContext? context) {
return super.createHttpClient(context);
}
}
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
HttpOverrides.global = RealHttpOverrides();
const testKey = '***REMOVED***';
AiService.instance.setFallbackApiKey(testKey);
test('Live test: /v1/responses endpoint testConnection', () async {
final resResponses = await AiService.instance.testConnection(
provider: AiProviderType.compatible,
endpoint: 'https://codex.slcydia.fun/v1/responses',
model: 'gemini-3.7-flash-high',
explicitApiKey: testKey,
);
print('Responses API result: ok=${resResponses.ok}, msg=${resResponses.message}');
expect(resResponses.ok, isTrue);
});
test('Live test: /v1 (Chat Completions) endpoint testConnection', () async {
final resChat = await AiService.instance.testConnection(
provider: AiProviderType.compatible,
endpoint: 'https://codex.slcydia.fun/v1',
model: 'gemini-3.7-flash-high',
explicitApiKey: testKey,
);
print('Chat Completions API result: ok=${resChat.ok}, msg=${resChat.message}');
expect(resChat.ok, isTrue);
});
test('Live test: /v1/responses dialogueReply', () async {
final reply = await AiService.instance.dialogueReply(
provider: AiProviderType.compatible,
endpoint: 'https://codex.slcydia.fun/v1/responses',
model: 'gemini-3.7-flash-high',
history: [
{'role': 'user', 'content': 'Hello, my name is Alex.'}
],
requiredTask: 'Greet learner and ask what is their name',
);
print('Dialogue reply from /v1/responses: reply="${reply?.reply}", slots=${reply?.slots}, suggestsComplete=${reply?.suggestsComplete}');
expect(reply, isNotNull);
expect(reply!.reply, isNotEmpty);
});
}