[Developer] #204 Phase 1 MVP — Flutter app skeleton complete
- Drift 21 tables (8 catalog + 11 user + habit_dose_variants + meta_kv) with R1~R10 CHECK constraints and 19 indexes - 8 hand-crafted seed JSON catalogs in app/assets/seed/ (refs 84, protocols 34, methodologies 21, frame_patterns 30, reward_menu_items 30, break_protocols 8, common_frames 5, diet_patterns 5) - 6 domain functions: recommend_variant, compute_streak, validate_frame_level, active_habit_quota, weekly_minimum_ratio, seed_importer (transactional, idempotent) - 4 vertical-slice Riverpod screens: HabitList, HabitCreate, CheckIn, Streak - 31 unit tests passing; flutter analyze clean - OQ-5 streak semantics: missing entry ≠ explicit blank (missing = end of history; only TrackerValue.blank triggers Never-miss-twice) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
52
app/test/data/seed/real_assets_import_test.dart
Normal file
52
app/test/data/seed/real_assets_import_test.dart
Normal file
@@ -0,0 +1,52 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:life_helper/data/db/app_database.dart';
|
||||
import 'package:life_helper/data/seed/seed_importer.dart';
|
||||
import 'package:path/path.dart' as p;
|
||||
|
||||
/// Loads the real assets/seed/*.json files from disk (bypasses rootBundle
|
||||
/// which needs Flutter binding) and runs them through SeedImporter end-to-end.
|
||||
/// This is the strongest signal that the adapters and the hand-crafted JSON
|
||||
/// agree on field names and CHECK-constraint values.
|
||||
void main() {
|
||||
late AppDatabase db;
|
||||
final repoRoot = Directory.current.path;
|
||||
|
||||
Future<String> diskLoader(String path) async {
|
||||
final f = File(p.join(repoRoot, path));
|
||||
return f.readAsString();
|
||||
}
|
||||
|
||||
setUp(() {
|
||||
db = AppDatabase.memory();
|
||||
});
|
||||
|
||||
tearDown(() async {
|
||||
await db.close();
|
||||
});
|
||||
|
||||
test('real seed assets import end-to-end', () async {
|
||||
final importer = SeedImporter(db, loadAsset: diskLoader);
|
||||
final ran = await importer.importIfNeeded();
|
||||
expect(ran, true);
|
||||
|
||||
final pCount = (await db.select(db.protocols).get()).length;
|
||||
final bpCount = (await db.select(db.breakProtocols).get()).length;
|
||||
final cfCount = (await db.select(db.commonFrames).get()).length;
|
||||
final mCount = (await db.select(db.methodologies).get()).length;
|
||||
final fpCount = (await db.select(db.framePatterns).get()).length;
|
||||
final rmiCount = (await db.select(db.rewardMenuItems).get()).length;
|
||||
final refCount = (await db.select(db.references).get()).length;
|
||||
final dpCount = (await db.select(db.dietPatterns).get()).length;
|
||||
|
||||
expect(pCount, greaterThanOrEqualTo(30));
|
||||
expect(bpCount, greaterThanOrEqualTo(5));
|
||||
expect(cfCount, 5);
|
||||
expect(mCount, greaterThanOrEqualTo(15));
|
||||
expect(fpCount, greaterThanOrEqualTo(20));
|
||||
expect(rmiCount, greaterThanOrEqualTo(20));
|
||||
expect(refCount, greaterThanOrEqualTo(50));
|
||||
expect(dpCount, greaterThanOrEqualTo(3));
|
||||
});
|
||||
}
|
||||
188
app/test/data/seed/seed_importer_test.dart
Normal file
188
app/test/data/seed/seed_importer_test.dart
Normal file
@@ -0,0 +1,188 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:life_helper/core/constants.dart';
|
||||
import 'package:life_helper/data/db/app_database.dart';
|
||||
import 'package:life_helper/data/seed/seed_importer.dart';
|
||||
|
||||
const _protocols = '''
|
||||
[
|
||||
{
|
||||
"id": "morning_sunlight",
|
||||
"category": "health",
|
||||
"title": "아침 햇빛",
|
||||
"what": "기상 후 햇빛.",
|
||||
"when": "기상 후 30~60분.",
|
||||
"dose": "5~10분.",
|
||||
"why": "ipRGC 자극.",
|
||||
"how": ["나간다", "쳐다본다"],
|
||||
"check": "60분 이내 외출",
|
||||
"source_doc": "huberman-protocols.md"
|
||||
}
|
||||
]
|
||||
''';
|
||||
|
||||
const _breakProtocols = '''
|
||||
[
|
||||
{
|
||||
"id": "alcohol",
|
||||
"category": "alcohol",
|
||||
"title": "음주",
|
||||
"huberman_summary": "ep 86",
|
||||
"phases": [{"week": 1, "what": "환경 정리"}],
|
||||
"default_common_frames": ["dopamine_reset"]
|
||||
}
|
||||
]
|
||||
''';
|
||||
|
||||
const _commonFrames = '''
|
||||
[
|
||||
{
|
||||
"id": "dopamine_reset",
|
||||
"title": "도파민 리셋",
|
||||
"what": "30일 절제",
|
||||
"why": "수용체 회복",
|
||||
"check": "30일 무자극"
|
||||
}
|
||||
]
|
||||
''';
|
||||
|
||||
const _methodologies = '''
|
||||
[
|
||||
{
|
||||
"id": "atomic_habits",
|
||||
"name": "Atomic Habits",
|
||||
"originator": "James Clear",
|
||||
"one_line_definition": "1% 개선",
|
||||
"core_unit": "1회 행동",
|
||||
"huberman_fit_score": 5,
|
||||
"is_core_engine": true
|
||||
}
|
||||
]
|
||||
''';
|
||||
|
||||
const _framePatterns = '''
|
||||
[
|
||||
{
|
||||
"id": "fp_alcohol",
|
||||
"domain": "drink",
|
||||
"avoidance_keyword": "술 끊기",
|
||||
"l0_example": "술 끊기",
|
||||
"l2_suggestion": "저녁엔 무알콜",
|
||||
"l3_identity": "맑은 정신 추구"
|
||||
}
|
||||
]
|
||||
''';
|
||||
|
||||
const _rewardMenuItems = '''
|
||||
[
|
||||
{
|
||||
"id": "rmi_walk",
|
||||
"tier_recommended": "T1",
|
||||
"title": "산책 30분"
|
||||
}
|
||||
]
|
||||
''';
|
||||
|
||||
const _references = '''
|
||||
[
|
||||
{
|
||||
"id": "ref_x",
|
||||
"kind": "url",
|
||||
"title": "Sample",
|
||||
"url": "https://example.com"
|
||||
}
|
||||
]
|
||||
''';
|
||||
|
||||
const _dietPatterns = '''
|
||||
[
|
||||
{
|
||||
"id": "med",
|
||||
"name": "지중해 식단",
|
||||
"core": "올리브유 + 채소",
|
||||
"evidence_strength": "strong"
|
||||
}
|
||||
]
|
||||
''';
|
||||
|
||||
Future<String> _stubLoader(String path) async {
|
||||
switch (path) {
|
||||
case 'assets/seed/protocols.json':
|
||||
return _protocols;
|
||||
case 'assets/seed/break_protocols.json':
|
||||
return _breakProtocols;
|
||||
case 'assets/seed/common_frames.json':
|
||||
return _commonFrames;
|
||||
case 'assets/seed/methodologies.json':
|
||||
return _methodologies;
|
||||
case 'assets/seed/frame_patterns.json':
|
||||
return _framePatterns;
|
||||
case 'assets/seed/reward_menu_items.json':
|
||||
return _rewardMenuItems;
|
||||
case 'assets/seed/references.json':
|
||||
return _references;
|
||||
case 'assets/seed/diet_patterns.json':
|
||||
return _dietPatterns;
|
||||
}
|
||||
throw StateError('unexpected asset: $path');
|
||||
}
|
||||
|
||||
void main() {
|
||||
late AppDatabase db;
|
||||
|
||||
setUp(() {
|
||||
db = AppDatabase.memory();
|
||||
});
|
||||
|
||||
tearDown(() async {
|
||||
await db.close();
|
||||
});
|
||||
|
||||
test('first run: imports all 8 catalogs and sets marker', () async {
|
||||
final importer = SeedImporter(db, loadAsset: _stubLoader);
|
||||
final ran = await importer.importIfNeeded();
|
||||
expect(ran, true);
|
||||
|
||||
expect((await db.select(db.protocols).get()).length, 1);
|
||||
expect((await db.select(db.breakProtocols).get()).length, 1);
|
||||
expect((await db.select(db.commonFrames).get()).length, 1);
|
||||
expect((await db.select(db.methodologies).get()).length, 1);
|
||||
expect((await db.select(db.framePatterns).get()).length, 1);
|
||||
expect((await db.select(db.rewardMenuItems).get()).length, 1);
|
||||
expect((await db.select(db.references).get()).length, 1);
|
||||
expect((await db.select(db.dietPatterns).get()).length, 1);
|
||||
|
||||
final marker = await (db.select(db.metaKv)
|
||||
..where((t) => t.key.equals(kSeededV1Flag)))
|
||||
.getSingleOrNull();
|
||||
expect(marker?.value, 'true');
|
||||
});
|
||||
|
||||
test('idempotent: second run is no-op', () async {
|
||||
final importer = SeedImporter(db, loadAsset: _stubLoader);
|
||||
await importer.importIfNeeded();
|
||||
final ran2 = await importer.importIfNeeded();
|
||||
expect(ran2, false);
|
||||
expect((await db.select(db.protocols).get()).length, 1);
|
||||
});
|
||||
|
||||
test('partial failure rolls back (transactional)', () async {
|
||||
Future<String> brokenLoader(String path) async {
|
||||
if (path.endsWith('diet_patterns.json')) {
|
||||
// Trigger a CHECK violation: evidence_strength must be one of the allowed values.
|
||||
return '''
|
||||
[{"id":"bad","name":"X","core":"Y","evidence_strength":"bogus"}]
|
||||
''';
|
||||
}
|
||||
return _stubLoader(path);
|
||||
}
|
||||
|
||||
final importer = SeedImporter(db, loadAsset: brokenLoader);
|
||||
await expectLater(importer.importIfNeeded(), throwsA(isA<Object>()));
|
||||
|
||||
expect((await db.select(db.protocols).get()).length, 0);
|
||||
final marker = await (db.select(db.metaKv)
|
||||
..where((t) => t.key.equals(kSeededV1Flag)))
|
||||
.getSingleOrNull();
|
||||
expect(marker, isNull);
|
||||
});
|
||||
}
|
||||
86
app/test/domain/frame/validate_frame_level_test.dart
Normal file
86
app/test/domain/frame/validate_frame_level_test.dart
Normal file
@@ -0,0 +1,86 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:life_helper/domain/frame/validate_frame_level.dart';
|
||||
import 'package:life_helper/domain/models/frame_pattern.dart';
|
||||
import 'package:life_helper/domain/models/habit.dart';
|
||||
|
||||
final _patterns = <FramePatternModel>[
|
||||
const FramePatternModel(
|
||||
id: 'fp_alcohol',
|
||||
domain: 'drink',
|
||||
avoidanceKeyword: '술 끊기',
|
||||
l0Example: '술 끊기',
|
||||
l1SimpleReplace: '음주 중단',
|
||||
l2Suggestion: '저녁엔 무알콜 음료 마시기',
|
||||
l3Identity: '나는 맑은 정신을 우선시하는 사람이다',
|
||||
),
|
||||
const FramePatternModel(
|
||||
id: 'fp_general',
|
||||
domain: 'general',
|
||||
avoidanceKeyword: '안 하기',
|
||||
l0Example: '안 하기',
|
||||
l2Suggestion: '대신 다른 행동 정의하기',
|
||||
),
|
||||
];
|
||||
|
||||
void main() {
|
||||
test('empty framed text → reject', () {
|
||||
final r = validateFrameLevel(
|
||||
const FrameInput(level: FrameLevel.l2, framedText: ''),
|
||||
knownPatterns: _patterns,
|
||||
);
|
||||
expect(r.status, FrameValidationStatus.reject);
|
||||
});
|
||||
|
||||
test('L0 → reject + suggestions', () {
|
||||
final r = validateFrameLevel(
|
||||
const FrameInput(level: FrameLevel.l0, framedText: '술 끊기'),
|
||||
knownPatterns: _patterns,
|
||||
);
|
||||
expect(r.status, FrameValidationStatus.reject);
|
||||
expect(r.suggestions, isNotEmpty);
|
||||
expect(r.suggestions.any((s) => s.level == FrameLevel.l2), true);
|
||||
});
|
||||
|
||||
test('L1 → reject + suggestions even when not matching any keyword', () {
|
||||
final r = validateFrameLevel(
|
||||
const FrameInput(level: FrameLevel.l1, framedText: '담배 줄이기'),
|
||||
knownPatterns: _patterns,
|
||||
);
|
||||
expect(r.status, FrameValidationStatus.reject);
|
||||
// Should fall back to 'general' domain suggestion.
|
||||
expect(r.suggestions, isNotEmpty);
|
||||
});
|
||||
|
||||
test('L2 clean → accept', () {
|
||||
final r = validateFrameLevel(
|
||||
const FrameInput(level: FrameLevel.l2, framedText: '저녁엔 무알콜 음료 마시기'),
|
||||
knownPatterns: _patterns,
|
||||
);
|
||||
expect(r.status, FrameValidationStatus.accept);
|
||||
expect(r.suggestions, isEmpty);
|
||||
});
|
||||
|
||||
test('L2 with embedded avoidance keyword → warn', () {
|
||||
final r = validateFrameLevel(
|
||||
const FrameInput(level: FrameLevel.l2, framedText: '술 끊기로 다짐'),
|
||||
knownPatterns: _patterns,
|
||||
);
|
||||
expect(r.status, FrameValidationStatus.warn);
|
||||
expect(r.avoidanceHits, isNotEmpty);
|
||||
expect(r.avoidanceHits.first.keyword, '술 끊기');
|
||||
});
|
||||
|
||||
test('L3 identity frame, clean → accept', () {
|
||||
final r = validateFrameLevel(
|
||||
const FrameInput(level: FrameLevel.l3, framedText: '나는 맑은 정신을 우선시한다'),
|
||||
knownPatterns: _patterns,
|
||||
);
|
||||
expect(r.status, FrameValidationStatus.accept);
|
||||
});
|
||||
|
||||
test('detectAvoidanceKeywords finds multiple occurrences', () {
|
||||
final hits =
|
||||
detectAvoidanceKeywords('술 끊기 / 다시 술 끊기 / 끝', _patterns);
|
||||
expect(hits.length, 2);
|
||||
});
|
||||
}
|
||||
96
app/test/domain/recommend/recommend_variant_test.dart
Normal file
96
app/test/domain/recommend/recommend_variant_test.dart
Normal file
@@ -0,0 +1,96 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:life_helper/domain/models/habit.dart';
|
||||
import 'package:life_helper/domain/recommend/recommend_variant.dart';
|
||||
|
||||
HabitModel _habit(List<HabitDoseVariantModel> variants) => HabitModel(
|
||||
id: 'hb_x',
|
||||
userId: 'u_local_default',
|
||||
type: HabitType.build,
|
||||
status: HabitStatus.active,
|
||||
title: 't',
|
||||
protocolId: 'p',
|
||||
frameLevel: FrameLevel.l2,
|
||||
frameFramedText: 't',
|
||||
startedAt: '2026-06-01',
|
||||
doseVariants: variants,
|
||||
);
|
||||
|
||||
HabitDoseVariantModel _v({
|
||||
required String id,
|
||||
required int sort,
|
||||
bool isMin = false,
|
||||
List<String> ctx = const [],
|
||||
List<String> cond = const [],
|
||||
}) =>
|
||||
HabitDoseVariantModel(
|
||||
variantId: id,
|
||||
habitId: 'hb_x',
|
||||
label: id,
|
||||
doseText: '1x',
|
||||
contextTags: ctx,
|
||||
conditionTags: cond,
|
||||
isMinimum: isMin,
|
||||
sortOrder: sort,
|
||||
);
|
||||
|
||||
void main() {
|
||||
test('empty variants → null', () {
|
||||
expect(recommendVariant(_habit(const []), const CheckInContext()), isNull);
|
||||
});
|
||||
|
||||
test('exact match wins (location + condition, score 4)', () {
|
||||
final habit = _habit([
|
||||
_v(id: 'A', sort: 0, ctx: ['집']),
|
||||
_v(id: 'B', sort: 1, ctx: ['회사'], cond: ['좋음']),
|
||||
_v(id: 'C', sort: 2, isMin: true),
|
||||
]);
|
||||
final pick = recommendVariant(
|
||||
habit, const CheckInContext(location: '회사', condition: '좋음'));
|
||||
expect(pick!.variant.variantId, 'B');
|
||||
expect(pick.reason, RecommendReason.exactMatch);
|
||||
expect(pick.score, 4);
|
||||
});
|
||||
|
||||
test('partial match (only location)', () {
|
||||
final habit = _habit([
|
||||
_v(id: 'A', sort: 0, ctx: ['집']),
|
||||
_v(id: 'B', sort: 1, ctx: ['회사']),
|
||||
]);
|
||||
final pick = recommendVariant(habit, const CheckInContext(location: '집'));
|
||||
expect(pick!.variant.variantId, 'A');
|
||||
expect(pick.reason, RecommendReason.partial);
|
||||
expect(pick.score, 2);
|
||||
});
|
||||
|
||||
test('fallback to minimum when nothing matches', () {
|
||||
final habit = _habit([
|
||||
_v(id: 'A', sort: 0, ctx: ['집']),
|
||||
_v(id: 'B', sort: 1, isMin: true),
|
||||
]);
|
||||
final pick = recommendVariant(
|
||||
habit, const CheckInContext(location: '카페', condition: '나쁨'));
|
||||
// is_minimum + condition=='나쁨' → score 1 → partial pick, not fallback.
|
||||
expect(pick!.variant.variantId, 'B');
|
||||
expect(pick.reason, RecommendReason.partial);
|
||||
});
|
||||
|
||||
test('fallback to first by sortOrder when no minimum, no match', () {
|
||||
final habit = _habit([
|
||||
_v(id: 'A', sort: 5, ctx: ['집']),
|
||||
_v(id: 'B', sort: 2, ctx: ['회사']),
|
||||
]);
|
||||
final pick = recommendVariant(habit, const CheckInContext(location: '카페'));
|
||||
expect(pick!.variant.variantId, 'B');
|
||||
expect(pick.reason, RecommendReason.fallbackFirst);
|
||||
expect(pick.score, 0);
|
||||
});
|
||||
|
||||
test('tie-break: same score → smaller sortOrder wins', () {
|
||||
final habit = _habit([
|
||||
_v(id: 'A', sort: 10, ctx: ['집']),
|
||||
_v(id: 'B', sort: 1, ctx: ['집']),
|
||||
]);
|
||||
final pick = recommendVariant(habit, const CheckInContext(location: '집'));
|
||||
expect(pick!.variant.variantId, 'B');
|
||||
});
|
||||
}
|
||||
38
app/test/domain/rules/active_habit_quota_test.dart
Normal file
38
app/test/domain/rules/active_habit_quota_test.dart
Normal file
@@ -0,0 +1,38 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:life_helper/domain/models/habit.dart';
|
||||
import 'package:life_helper/domain/rules/active_habit_quota.dart';
|
||||
|
||||
void main() {
|
||||
test('build allows up to 3', () {
|
||||
expect(
|
||||
judgeActiveHabitQuota(type: HabitType.build, currentActiveCount: 2).allowed,
|
||||
true,
|
||||
);
|
||||
expect(
|
||||
judgeActiveHabitQuota(type: HabitType.build, currentActiveCount: 3).allowed,
|
||||
false,
|
||||
);
|
||||
});
|
||||
|
||||
test('break allows only 1', () {
|
||||
expect(
|
||||
judgeActiveHabitQuota(type: HabitType.breakHabit, currentActiveCount: 0)
|
||||
.allowed,
|
||||
true,
|
||||
);
|
||||
expect(
|
||||
judgeActiveHabitQuota(type: HabitType.breakHabit, currentActiveCount: 1)
|
||||
.allowed,
|
||||
false,
|
||||
);
|
||||
});
|
||||
|
||||
test('reason describes the limit when blocked', () {
|
||||
final r = judgeActiveHabitQuota(
|
||||
type: HabitType.build, currentActiveCount: 3);
|
||||
expect(r.reason, contains('3'));
|
||||
final br = judgeActiveHabitQuota(
|
||||
type: HabitType.breakHabit, currentActiveCount: 1);
|
||||
expect(br.reason, contains('1'));
|
||||
});
|
||||
}
|
||||
115
app/test/domain/streak/compute_streak_test.dart
Normal file
115
app/test/domain/streak/compute_streak_test.dart
Normal file
@@ -0,0 +1,115 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:life_helper/domain/models/tracker_entry.dart';
|
||||
import 'package:life_helper/domain/streak/compute_streak.dart';
|
||||
|
||||
TrackerEntryModel _e(String date, TrackerValue v) =>
|
||||
TrackerEntryModel(id: 'te_$date', habitId: 'hb', date: date, value: v);
|
||||
|
||||
void main() {
|
||||
group('computeStreak', () {
|
||||
test('empty → all zero, T0, not broken', () {
|
||||
final s = computeStreak(
|
||||
entries: const [],
|
||||
asOf: DateTime(2026, 6, 11),
|
||||
habitStartedAt: '2026-06-01',
|
||||
);
|
||||
expect(s.currentStreak, 0);
|
||||
expect(s.currentTier, RewardTier.t0);
|
||||
expect(s.neverMissTwiceBroken, false);
|
||||
});
|
||||
|
||||
test('3 consecutive done → T1', () {
|
||||
final s = computeStreak(
|
||||
entries: [
|
||||
_e('2026-06-09', TrackerValue.done),
|
||||
_e('2026-06-10', TrackerValue.done),
|
||||
_e('2026-06-11', TrackerValue.done),
|
||||
],
|
||||
asOf: DateTime(2026, 6, 11),
|
||||
habitStartedAt: '2026-06-01',
|
||||
);
|
||||
expect(s.currentStreak, 3);
|
||||
expect(s.currentTier, RewardTier.t1);
|
||||
});
|
||||
|
||||
test('7 consecutive done → T2', () {
|
||||
final entries = <TrackerEntryModel>[];
|
||||
for (var i = 0; i < 7; i++) {
|
||||
final d = DateTime(2026, 6, 5).add(Duration(days: i));
|
||||
entries.add(_e(_ymd(d), TrackerValue.done));
|
||||
}
|
||||
final s = computeStreak(
|
||||
entries: entries,
|
||||
asOf: DateTime(2026, 6, 11),
|
||||
habitStartedAt: '2026-06-01',
|
||||
);
|
||||
expect(s.currentStreak, 7);
|
||||
expect(s.currentTier, RewardTier.t2);
|
||||
});
|
||||
|
||||
test('OQ-5: 1 blank → streak=0, tier stays (not broken)', () {
|
||||
// 6/9 done, 6/10 blank entry, 6/11 done.
|
||||
final s = computeStreak(
|
||||
entries: [
|
||||
_e('2026-06-09', TrackerValue.done),
|
||||
_e('2026-06-10', TrackerValue.blank),
|
||||
_e('2026-06-11', TrackerValue.done),
|
||||
],
|
||||
asOf: DateTime(2026, 6, 11),
|
||||
habitStartedAt: '2026-06-01',
|
||||
);
|
||||
// Walks back: 6/11 done (+1), 6/10 blank → streak resets to 0.
|
||||
expect(s.currentStreak, 0);
|
||||
expect(s.neverMissTwiceBroken, false);
|
||||
});
|
||||
|
||||
test('OQ-5: 2 consecutive blank → neverMissTwiceBroken=true', () {
|
||||
final s = computeStreak(
|
||||
entries: [
|
||||
_e('2026-06-09', TrackerValue.done),
|
||||
_e('2026-06-10', TrackerValue.blank),
|
||||
_e('2026-06-11', TrackerValue.blank),
|
||||
],
|
||||
asOf: DateTime(2026, 6, 11),
|
||||
habitStartedAt: '2026-06-01',
|
||||
);
|
||||
expect(s.currentStreak, 0);
|
||||
expect(s.neverMissTwiceBroken, true);
|
||||
});
|
||||
|
||||
test('window30: 24/30 done → T3', () {
|
||||
final entries = <TrackerEntryModel>[];
|
||||
// 24 done in last 30 days, but not as a streak.
|
||||
for (var i = 0; i < 24; i++) {
|
||||
final d = DateTime(2026, 6, 11).subtract(Duration(days: i));
|
||||
entries.add(_e(_ymd(d), TrackerValue.done));
|
||||
}
|
||||
final s = computeStreak(
|
||||
entries: entries,
|
||||
asOf: DateTime(2026, 6, 11),
|
||||
habitStartedAt: '2026-05-01',
|
||||
);
|
||||
expect(s.doneCountInWindow30, 24);
|
||||
expect(s.currentTier.rank, greaterThanOrEqualTo(RewardTier.t3.rank));
|
||||
});
|
||||
|
||||
test('longestStreak picks largest run regardless of current', () {
|
||||
final s = computeStreak(
|
||||
entries: [
|
||||
for (final d in ['2026-06-01', '2026-06-02', '2026-06-03', '2026-06-04'])
|
||||
_e(d, TrackerValue.done),
|
||||
_e('2026-06-05', TrackerValue.blank),
|
||||
_e('2026-06-11', TrackerValue.done),
|
||||
],
|
||||
asOf: DateTime(2026, 6, 11),
|
||||
habitStartedAt: '2026-06-01',
|
||||
);
|
||||
expect(s.longestStreak, 4);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
String _ymd(DateTime d) =>
|
||||
'${d.year.toString().padLeft(4, '0')}-'
|
||||
'${d.month.toString().padLeft(2, '0')}-'
|
||||
'${d.day.toString().padLeft(2, '0')}';
|
||||
81
app/test/domain/streak/weekly_minimum_ratio_test.dart
Normal file
81
app/test/domain/streak/weekly_minimum_ratio_test.dart
Normal file
@@ -0,0 +1,81 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:life_helper/domain/models/habit.dart';
|
||||
import 'package:life_helper/domain/models/tracker_entry.dart';
|
||||
import 'package:life_helper/domain/streak/weekly_minimum_ratio.dart';
|
||||
|
||||
HabitDoseVariantModel _v(String id, {bool isMin = false}) =>
|
||||
HabitDoseVariantModel(
|
||||
variantId: id,
|
||||
habitId: 'hb',
|
||||
label: id,
|
||||
doseText: '1x',
|
||||
isMinimum: isMin,
|
||||
);
|
||||
|
||||
TrackerEntryModel _done(String date, {String? vId}) => TrackerEntryModel(
|
||||
id: 'te_$date',
|
||||
habitId: 'hb',
|
||||
date: date,
|
||||
value: TrackerValue.done,
|
||||
variantId: vId,
|
||||
);
|
||||
|
||||
void main() {
|
||||
final variantsById = {
|
||||
'min': _v('min', isMin: true),
|
||||
'full': _v('full'),
|
||||
};
|
||||
|
||||
test('no done entries → ratio 0.0', () {
|
||||
final r = computeWeeklyMinimumRatio(
|
||||
entries: const [],
|
||||
variantsById: variantsById,
|
||||
asOf: DateTime(2026, 6, 11),
|
||||
);
|
||||
expect(r.totalDone, 0);
|
||||
expect(r.ratio, 0.0);
|
||||
});
|
||||
|
||||
test('3 of 4 used minimum → 0.75', () {
|
||||
final r = computeWeeklyMinimumRatio(
|
||||
entries: [
|
||||
_done('2026-06-08', vId: 'min'),
|
||||
_done('2026-06-09', vId: 'min'),
|
||||
_done('2026-06-10', vId: 'full'),
|
||||
_done('2026-06-11', vId: 'min'),
|
||||
],
|
||||
variantsById: variantsById,
|
||||
asOf: DateTime(2026, 6, 11),
|
||||
);
|
||||
expect(r.totalDone, 4);
|
||||
expect(r.minimumUsed, 3);
|
||||
expect(r.ratio, closeTo(0.75, 1e-9));
|
||||
});
|
||||
|
||||
test('out-of-window entries are excluded', () {
|
||||
final r = computeWeeklyMinimumRatio(
|
||||
entries: [
|
||||
_done('2026-06-01', vId: 'min'), // 10 days before asOf
|
||||
_done('2026-06-11', vId: 'min'),
|
||||
],
|
||||
variantsById: variantsById,
|
||||
asOf: DateTime(2026, 6, 11),
|
||||
);
|
||||
expect(r.totalDone, 1);
|
||||
expect(r.minimumUsed, 1);
|
||||
});
|
||||
|
||||
test('done with null variantId counts as totalDone but not minimumUsed', () {
|
||||
final r = computeWeeklyMinimumRatio(
|
||||
entries: [
|
||||
_done('2026-06-10'),
|
||||
_done('2026-06-11', vId: 'min'),
|
||||
],
|
||||
variantsById: variantsById,
|
||||
asOf: DateTime(2026, 6, 11),
|
||||
);
|
||||
expect(r.totalDone, 2);
|
||||
expect(r.minimumUsed, 1);
|
||||
expect(r.ratio, 0.5);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user