- ModelConfig.filename 기본값 gemma4_e2b_q4.bin → gemma-4-E2B-it.litertlm (#342 근본 원인) - _migrateLegacyPath: 기존 .bin 설치본 rename + meta 갱신 (재다운로드 2.4GB 방지, 멱등) - maxTokens 2048 유지 + 1024 회귀 방지 주석 (KV cache < prefill signature 시 추론 실패) - LLM_BACKEND dart-define: 에뮬레이터 CPU 강제 (SwiftShader GPU 네이티브 SIGABRT 회피) - GEMMA_MODEL_URL dart-define: 호스트 로컬 모델 서버 주입 (기본값 = HF URL 불변) - debug 전용 cleartext manifest (10.0.2.2 모델 서버용, release 불변) - 마이그레이션 테스트 4건 신규 (AC-2/3/4). 171 passed, analyze clean 2026-07-14 에뮬레이터 E2E 검증: 다운로드→로드→tool call 왕복 전 구간 성공. Refs #657, #342 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
330 lines
11 KiB
Dart
330 lines
11 KiB
Dart
import 'dart:async';
|
|
import 'dart:convert';
|
|
import 'dart:io';
|
|
|
|
import 'package:crypto/crypto.dart';
|
|
import 'package:drift/native.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:http/http.dart' as http;
|
|
import 'package:life_helper/data/ai/model_lifecycle.dart';
|
|
import 'package:life_helper/data/db/app_database.dart';
|
|
import 'package:life_helper/data/db/daos/meta_dao.dart';
|
|
|
|
class _FakeStorage implements StorageAdapter {
|
|
_FakeStorage(this.dir);
|
|
final Directory dir;
|
|
Future<http.StreamedResponse> Function(Uri url, int from)? handler;
|
|
|
|
@override
|
|
Future<Directory> supportDir() async => dir;
|
|
|
|
@override
|
|
Future<http.StreamedResponse> rangeGet(Uri url, int from) {
|
|
final h = handler;
|
|
if (h == null) {
|
|
throw StateError('no handler');
|
|
}
|
|
return h(url, from);
|
|
}
|
|
}
|
|
|
|
http.StreamedResponse _streamed(
|
|
List<int> bytes, {
|
|
int status = 200,
|
|
}) {
|
|
return http.StreamedResponse(
|
|
Stream.value(bytes),
|
|
status,
|
|
contentLength: bytes.length,
|
|
);
|
|
}
|
|
|
|
void main() {
|
|
late AppDatabase db;
|
|
late MetaDao meta;
|
|
late Directory tmp;
|
|
late _FakeStorage storage;
|
|
const url = 'https://example/model.bin';
|
|
|
|
setUp(() async {
|
|
db = AppDatabase(NativeDatabase.memory());
|
|
meta = MetaDao(db);
|
|
tmp = await Directory.systemTemp.createTemp('lifecycle_test_');
|
|
storage = _FakeStorage(tmp);
|
|
});
|
|
|
|
tearDown(() async {
|
|
await db.close();
|
|
if (tmp.existsSync()) await tmp.delete(recursive: true);
|
|
});
|
|
|
|
test('checkAvailability missing when opt_in false', () async {
|
|
final lc = ModelLifecycle(
|
|
meta: meta,
|
|
config: ModelConfig(url: Uri.parse(url), expectedSha256: 'x'),
|
|
storage: storage,
|
|
);
|
|
expect(await lc.checkAvailability(), ModelAvailability.missing);
|
|
});
|
|
|
|
test('download writes file, sets meta keys, completes with ready', () async {
|
|
final payload = utf8.encode('hello world fake model');
|
|
final expected = sha256.convert(payload).toString();
|
|
final lc = ModelLifecycle(
|
|
meta: meta,
|
|
config: ModelConfig(
|
|
url: Uri.parse(url),
|
|
expectedSha256: expected,
|
|
filename: 'gemma_test.bin',
|
|
),
|
|
storage: storage,
|
|
);
|
|
storage.handler = (_, from) async => _streamed(payload);
|
|
await meta.put(AiMetaKeys.optIn, 'true');
|
|
|
|
final progresses = await lc.download().toList();
|
|
expect(progresses.last.state, DownloadState.completed);
|
|
expect(progresses.last.bytesReceived, payload.length);
|
|
|
|
expect(await meta.find(AiMetaKeys.downloadState), 'completed');
|
|
final p = await meta.find(AiMetaKeys.modelPath);
|
|
expect(p, isNotNull);
|
|
expect(File(p!).existsSync(), true);
|
|
expect(await meta.find(AiMetaKeys.modelSha), expected);
|
|
|
|
expect(await lc.checkAvailability(), ModelAvailability.ready);
|
|
});
|
|
|
|
test('SHA mismatch deletes file and emits failed', () async {
|
|
final payload = utf8.encode('payload');
|
|
final lc = ModelLifecycle(
|
|
meta: meta,
|
|
config: ModelConfig(
|
|
url: Uri.parse(url),
|
|
expectedSha256: 'deadbeef',
|
|
filename: 'gemma_bad.bin',
|
|
),
|
|
storage: storage,
|
|
);
|
|
storage.handler = (_, from) async => _streamed(payload);
|
|
await meta.put(AiMetaKeys.optIn, 'true');
|
|
|
|
final progresses = await lc.download().toList();
|
|
expect(progresses.last.state, DownloadState.failed);
|
|
expect(progresses.last.errorMessage, contains('sha'));
|
|
|
|
final pathStr = '${tmp.path}/gemma_bad.bin';
|
|
expect(File(pathStr).existsSync(), false);
|
|
});
|
|
|
|
test('network error → paused, file preserved for resume', () async {
|
|
final lc = ModelLifecycle(
|
|
meta: meta,
|
|
config: ModelConfig(
|
|
url: Uri.parse(url),
|
|
expectedSha256: 'x',
|
|
filename: 'gemma_net.bin',
|
|
),
|
|
storage: storage,
|
|
);
|
|
storage.handler = (_, from) async => throw const SocketException('down');
|
|
await meta.put(AiMetaKeys.optIn, 'true');
|
|
|
|
final progresses = await lc.download().toList();
|
|
expect(progresses.last.state, DownloadState.failed);
|
|
expect(await meta.find(AiMetaKeys.downloadState), 'paused');
|
|
});
|
|
|
|
test('purge deletes file + clears meta keys (idempotent)', () async {
|
|
final payload = utf8.encode('xx');
|
|
final expected = sha256.convert(payload).toString();
|
|
final lc = ModelLifecycle(
|
|
meta: meta,
|
|
config: ModelConfig(
|
|
url: Uri.parse(url),
|
|
expectedSha256: expected,
|
|
filename: 'gemma_purge.bin',
|
|
),
|
|
storage: storage,
|
|
);
|
|
storage.handler = (_, from) async => _streamed(payload);
|
|
await meta.put(AiMetaKeys.optIn, 'true');
|
|
await lc.download().toList();
|
|
|
|
final freed = await lc.purge();
|
|
expect(freed, payload.length);
|
|
expect(await meta.find(AiMetaKeys.modelPath), isNull);
|
|
expect(await meta.find(AiMetaKeys.modelSha), isNull);
|
|
expect(await meta.find(AiMetaKeys.downloadState), isNull);
|
|
|
|
// Idempotent — second purge returns 0 without throwing.
|
|
expect(await lc.purge(), 0);
|
|
});
|
|
|
|
test('checkAvailability detects download in progress', () async {
|
|
final lc = ModelLifecycle(
|
|
meta: meta,
|
|
config: ModelConfig(url: Uri.parse(url), expectedSha256: 'x'),
|
|
storage: storage,
|
|
);
|
|
await meta.put(AiMetaKeys.optIn, 'true');
|
|
await meta.put(AiMetaKeys.downloadState, 'paused');
|
|
expect(await lc.checkAvailability(), ModelAvailability.downloading);
|
|
});
|
|
|
|
test('quickCheck ready when meta_kv complete + file exists (no SHA)', () async {
|
|
const file = 'gemma_quick.bin';
|
|
final lc = ModelLifecycle(
|
|
meta: meta,
|
|
// 일부러 expectedSha 와 다르게 — quickCheck 는 SHA 비교 X.
|
|
config: ModelConfig(
|
|
url: Uri.parse(url),
|
|
expectedSha256: 'unused_by_quickcheck',
|
|
filename: file,
|
|
),
|
|
storage: storage,
|
|
);
|
|
await meta.put(AiMetaKeys.optIn, 'true');
|
|
final path = '${tmp.path}/$file';
|
|
File(path).writeAsStringSync('payload');
|
|
await meta.put(AiMetaKeys.modelPath, path);
|
|
await meta.put(AiMetaKeys.modelSha, 'whatever');
|
|
|
|
expect(await lc.quickCheck(), ModelAvailability.ready);
|
|
});
|
|
|
|
test('quickCheck missing when modelPath not set', () async {
|
|
final lc = ModelLifecycle(
|
|
meta: meta,
|
|
config: ModelConfig(url: Uri.parse(url), expectedSha256: 'x'),
|
|
storage: storage,
|
|
);
|
|
await meta.put(AiMetaKeys.optIn, 'true');
|
|
expect(await lc.quickCheck(), ModelAvailability.missing);
|
|
});
|
|
|
|
test('quickCheck missing when file deleted from disk', () async {
|
|
const file = 'gemma_gone.bin';
|
|
final lc = ModelLifecycle(
|
|
meta: meta,
|
|
config: ModelConfig(
|
|
url: Uri.parse(url),
|
|
expectedSha256: 'x',
|
|
filename: file,
|
|
),
|
|
storage: storage,
|
|
);
|
|
await meta.put(AiMetaKeys.optIn, 'true');
|
|
await meta.put(AiMetaKeys.modelPath, '${tmp.path}/$file');
|
|
await meta.put(AiMetaKeys.modelSha, 'sha');
|
|
// 파일 자체는 만들지 않음.
|
|
expect(await lc.quickCheck(), ModelAvailability.missing);
|
|
});
|
|
|
|
test('quickCheck downloading when state in progress', () async {
|
|
final lc = ModelLifecycle(
|
|
meta: meta,
|
|
config: ModelConfig(url: Uri.parse(url), expectedSha256: 'x'),
|
|
storage: storage,
|
|
);
|
|
await meta.put(AiMetaKeys.optIn, 'true');
|
|
await meta.put(AiMetaKeys.downloadState, 'downloading');
|
|
expect(await lc.quickCheck(), ModelAvailability.downloading);
|
|
});
|
|
|
|
test('checkAvailability returns corrupt when file SHA mismatches', () async {
|
|
const file = 'gemma_corrupt.bin';
|
|
final lc = ModelLifecycle(
|
|
meta: meta,
|
|
config: ModelConfig(
|
|
url: Uri.parse(url),
|
|
expectedSha256: 'wrong',
|
|
filename: file,
|
|
),
|
|
storage: storage,
|
|
);
|
|
await meta.put(AiMetaKeys.optIn, 'true');
|
|
final path = '${tmp.path}/$file';
|
|
File(path).writeAsStringSync('payload');
|
|
await meta.put(AiMetaKeys.modelPath, path);
|
|
await meta.put(AiMetaKeys.modelSha, 'expected_but_actual_will_differ');
|
|
expect(await lc.checkAvailability(), ModelAvailability.corrupt);
|
|
});
|
|
|
|
group('#657 legacy .bin filename migration', () {
|
|
// Pre-.litertlm installs: file on disk + meta path both use the old
|
|
// `gemma4_e2b_q4.bin` name that LiteRT-LM rejects.
|
|
const legacyName = 'gemma4_e2b_q4.bin';
|
|
const canonicalName = 'model.litertlm';
|
|
|
|
ModelLifecycle makeLc(String expectedSha) => ModelLifecycle(
|
|
meta: meta,
|
|
config: ModelConfig(
|
|
url: Uri.parse(url),
|
|
expectedSha256: expectedSha,
|
|
filename: canonicalName,
|
|
),
|
|
storage: storage,
|
|
);
|
|
|
|
Future<String> seedLegacy(List<int> payload) async {
|
|
final legacyPath = '${tmp.path}/$legacyName';
|
|
File(legacyPath).writeAsBytesSync(payload);
|
|
await meta.put(AiMetaKeys.optIn, 'true');
|
|
await meta.put(AiMetaKeys.modelPath, legacyPath);
|
|
await meta.put(
|
|
AiMetaKeys.modelSha, sha256.convert(payload).toString());
|
|
await meta.put(AiMetaKeys.downloadState, 'completed');
|
|
return legacyPath;
|
|
}
|
|
|
|
test('quickCheck renames legacy file + updates meta path (AC-2)',
|
|
() async {
|
|
final payload = utf8.encode('legacy model bytes');
|
|
final legacyPath = await seedLegacy(payload);
|
|
final lc = makeLc(sha256.convert(payload).toString());
|
|
|
|
expect(await lc.quickCheck(), ModelAvailability.ready);
|
|
expect(File(legacyPath).existsSync(), isFalse);
|
|
final canonicalPath = '${tmp.path}/$canonicalName';
|
|
expect(File(canonicalPath).existsSync(), isTrue);
|
|
expect(await meta.find(AiMetaKeys.modelPath), canonicalPath);
|
|
});
|
|
|
|
test('checkAvailability migrates and SHA meta stays valid (AC-2)',
|
|
() async {
|
|
final payload = utf8.encode('legacy model bytes');
|
|
await seedLegacy(payload);
|
|
final lc = makeLc(sha256.convert(payload).toString());
|
|
|
|
expect(await lc.checkAvailability(), ModelAvailability.ready);
|
|
expect(File('${tmp.path}/$canonicalName').existsSync(), isTrue);
|
|
});
|
|
|
|
test('migration is idempotent — second check is a no-op (AC-3)',
|
|
() async {
|
|
final payload = utf8.encode('legacy model bytes');
|
|
await seedLegacy(payload);
|
|
final lc = makeLc(sha256.convert(payload).toString());
|
|
|
|
expect(await lc.quickCheck(), ModelAvailability.ready);
|
|
expect(await lc.quickCheck(), ModelAvailability.ready);
|
|
expect(
|
|
await meta.find(AiMetaKeys.modelPath),
|
|
'${tmp.path}/$canonicalName',
|
|
);
|
|
});
|
|
|
|
test('meta path file gone → skip migration, stays missing (AC-4)',
|
|
() async {
|
|
await meta.put(AiMetaKeys.optIn, 'true');
|
|
await meta.put(AiMetaKeys.modelPath, '${tmp.path}/$legacyName');
|
|
await meta.put(AiMetaKeys.modelSha, 'irrelevant');
|
|
|
|
final lc = makeLc('irrelevant');
|
|
expect(await lc.quickCheck(), ModelAvailability.missing);
|
|
expect(File('${tmp.path}/$canonicalName').existsSync(), isFalse);
|
|
});
|
|
});
|
|
}
|