Implements the OQ-1 follow-up to #215 v0.2.0: replace the placeholder GemmaLlmService stub with a real flutter_gemma 0.16.5 backend driving Gemma 4 E2B (litert-community/gemma-4-E2B-it-litert-lm, 2.41GB). Highlights: - GemmaLlmService.load → FlutterGemma.initialize + installModel.fromFile + getActiveModel; idempotent + FileSystemException on missing file. - generateStructured uses Gemma 4 native function calling via createChat(tools: [Tool(...)], toolChoice: required). Stream parsed by collectFunctionCall — first FCR wins, ParallelFCR first-call wins, TextResponse/ThinkingResponse skipped, errors sanitized to prevent prompt leakage. - main.dart wires _LazyLlmService adapter that resolves to GemmaLlmService when ModelLifecycle reports ready, MockLlmService otherwise. - ai_providers.dart pins real model URL + SHA-256 (181938...39a63c). - F2 hardening: ModelLifecycle.purge wraps each delete + meta remove in try/catch so a single OS-level flake cannot block opt-out. - Android: INTERNET / FOREGROUND_SERVICE / POST_NOTIFICATIONS permissions + R8 proguard-rules.pro keeping MediaPipe / LiteRT / TFLite / protobuf JNI entry points (release builds otherwise crash on first inference). Design-First: fn-gemma_llm_service.md updated to v2 — §C (_appendSchemaInstruction) deprecated after reading flutter_gemma 0.16.5 source (Gemma 4 SDK injects tool declarations via template; prompt-side append would double-wrap). Tests: - 10 new unit tests for collectFunctionCall covering all 8 fn-spec cases + 2 ParallelFunctionCallResponse paths. - All 81 existing tests still pass. - flutter analyze: 0 issues. Refs #218 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
151 lines
4.6 KiB
Dart
151 lines
4.6 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:flutter_gemma/flutter_gemma.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:life_helper/data/ai/gemma_llm_service.dart';
|
|
|
|
/// Unit tests for `collectFunctionCall` (fn-spec §D, 8 cases).
|
|
///
|
|
/// `GemmaLlmService.load` / `.generateStructured` themselves require the
|
|
/// flutter_gemma native runtime and are covered by AC-7 (on-device E2E),
|
|
/// not by host tests. The pure stream-parsing helper is unit-testable in
|
|
/// isolation because we can feed a synthetic `Stream<ModelResponse>`.
|
|
void main() {
|
|
const fn = 'emit_frame_candidates';
|
|
|
|
test('1. single FCR with expected name returns args', () async {
|
|
final stream = Stream<ModelResponse>.fromIterable([
|
|
const FunctionCallResponse(
|
|
name: fn,
|
|
args: {
|
|
'candidates': [
|
|
{'text': 'a', 'level': 'L2'},
|
|
{'text': 'b', 'level': 'L2'},
|
|
{'text': 'c', 'level': 'L3'},
|
|
],
|
|
},
|
|
),
|
|
]);
|
|
final args = await collectFunctionCall(stream, fn);
|
|
expect(args['candidates'], hasLength(3));
|
|
});
|
|
|
|
test('2. TextResponse before FCR is skipped', () async {
|
|
final stream = Stream<ModelResponse>.fromIterable([
|
|
const TextResponse('hello'),
|
|
const FunctionCallResponse(name: fn, args: {'candidates': []}),
|
|
]);
|
|
final args = await collectFunctionCall(stream, fn);
|
|
expect(args['candidates'], isEmpty);
|
|
});
|
|
|
|
test('3. ThinkingResponse + TextResponse before empty-args FCR', () async {
|
|
final stream = Stream<ModelResponse>.fromIterable([
|
|
const ThinkingResponse('reasoning...'),
|
|
const TextResponse('preamble'),
|
|
const FunctionCallResponse(name: fn, args: {}),
|
|
]);
|
|
final args = await collectFunctionCall(stream, fn);
|
|
expect(args, isEmpty);
|
|
});
|
|
|
|
test('4. wrong function name throws FormatException', () async {
|
|
final stream = Stream<ModelResponse>.fromIterable([
|
|
const FunctionCallResponse(name: 'wrong_name', args: {}),
|
|
]);
|
|
expect(
|
|
() => collectFunctionCall(stream, fn),
|
|
throwsA(
|
|
isA<FormatException>().having(
|
|
(e) => e.message,
|
|
'message',
|
|
contains('wrong_name'),
|
|
),
|
|
),
|
|
);
|
|
});
|
|
|
|
test('5. text-only stream throws "no function call emitted"', () async {
|
|
final stream = Stream<ModelResponse>.fromIterable([
|
|
const TextResponse('only text, no call'),
|
|
]);
|
|
expect(
|
|
() => collectFunctionCall(stream, fn),
|
|
throwsA(
|
|
isA<FormatException>().having(
|
|
(e) => e.message,
|
|
'message',
|
|
contains('no function call emitted'),
|
|
),
|
|
),
|
|
);
|
|
});
|
|
|
|
test('6. stream error throws sanitized FormatException (no leak)', () async {
|
|
final stream = Stream<ModelResponse>.error(
|
|
Exception('SENSITIVE: user_prompt_leaked_in_error'),
|
|
);
|
|
expect(
|
|
() => collectFunctionCall(stream, fn),
|
|
throwsA(
|
|
isA<FormatException>().having(
|
|
(e) => e.message,
|
|
'message',
|
|
allOf(
|
|
equals('stream error'),
|
|
isNot(contains('SENSITIVE')),
|
|
isNot(contains('user_prompt_leaked_in_error')),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
});
|
|
|
|
test('7. FCR with empty args map returns empty map (no throw)', () async {
|
|
final stream = Stream<ModelResponse>.fromIterable([
|
|
const FunctionCallResponse(name: fn, args: {}),
|
|
]);
|
|
final args = await collectFunctionCall(stream, fn);
|
|
expect(args, isEmpty);
|
|
});
|
|
|
|
test('8. empty stream throws "no function call emitted"', () async {
|
|
final stream = const Stream<ModelResponse>.empty();
|
|
expect(
|
|
() => collectFunctionCall(stream, fn),
|
|
throwsA(
|
|
isA<FormatException>().having(
|
|
(e) => e.message,
|
|
'message',
|
|
contains('no function call emitted'),
|
|
),
|
|
),
|
|
);
|
|
});
|
|
|
|
group('ParallelFunctionCallResponse', () {
|
|
test('first call with expected name returns its args', () async {
|
|
final stream = Stream<ModelResponse>.fromIterable([
|
|
ParallelFunctionCallResponse(calls: [
|
|
const FunctionCallResponse(name: fn, args: {'x': 1}),
|
|
const FunctionCallResponse(name: 'other', args: {'y': 2}),
|
|
]),
|
|
]);
|
|
final args = await collectFunctionCall(stream, fn);
|
|
expect(args['x'], 1);
|
|
});
|
|
|
|
test('first call with wrong name throws', () async {
|
|
final stream = Stream<ModelResponse>.fromIterable([
|
|
ParallelFunctionCallResponse(calls: [
|
|
const FunctionCallResponse(name: 'wrong_first', args: {}),
|
|
]),
|
|
]);
|
|
expect(
|
|
() => collectFunctionCall(stream, fn),
|
|
throwsA(isA<FormatException>()),
|
|
);
|
|
});
|
|
});
|
|
}
|