forked from eva/focus-flow
300 lines
9 KiB
Dart
300 lines
9 KiB
Dart
// SPDX-FileCopyrightText: 2026 FocusFlow contributors
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
/// Tests persistent SQLite composition behavior in the Flutter app.
|
|
library;
|
|
|
|
import 'dart:io';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:scheduler_core/scheduler_core.dart';
|
|
|
|
import 'package:focus_flow_flutter/app/focus_flow_app.dart';
|
|
import 'package:focus_flow_flutter/app/persistent_scheduler_composition.dart';
|
|
import 'package:focus_flow_flutter/controllers/scheduler_command_controller.dart';
|
|
|
|
/// Runs persistent composition smoke tests.
|
|
void main() {
|
|
testWidgets(
|
|
'persistent app starts without seeded demo tasks and reopens capture',
|
|
(tester) async {
|
|
final path = await _runAsync(tester, _tempDatabasePath);
|
|
final composition = await _runAsync(
|
|
tester,
|
|
() => PersistentSchedulerComposition.open(
|
|
sqlitePath: path,
|
|
selectedDate: _date,
|
|
clock: FixedClock(_instant(8)),
|
|
),
|
|
);
|
|
|
|
await tester.pumpWidget(FocusFlowApp(composition: composition));
|
|
await tester.pump();
|
|
await tester.pump(const Duration(milliseconds: 100));
|
|
await tester.pump(const Duration(seconds: 1));
|
|
|
|
expect(find.text('January 2, 2026'), findsOneWidget);
|
|
expect(find.text('Clean coffee maker'), findsNothing);
|
|
expect(find.text('Water plants'), findsNothing);
|
|
|
|
final commandController = composition.createCommandController(
|
|
refreshReads: () async {},
|
|
selectedDate: () => composition.initialDate,
|
|
);
|
|
addTearDown(commandController.dispose);
|
|
await tester.runAsync(
|
|
() => commandController.quickCaptureToBacklog(
|
|
'Captured from Flutter test',
|
|
),
|
|
);
|
|
expect(commandController.state, isA<SchedulerCommandSuccess>());
|
|
|
|
await tester.pumpWidget(const SizedBox.shrink());
|
|
await tester.pump();
|
|
await tester.runAsync(composition.dispose);
|
|
|
|
final reopened = await _runAsync(
|
|
tester,
|
|
() => PersistentSchedulerComposition.open(
|
|
sqlitePath: path,
|
|
selectedDate: _date,
|
|
clock: FixedClock(_instant(8, 10)),
|
|
),
|
|
);
|
|
addTearDown(() => tester.runAsync(reopened.dispose));
|
|
final backlog = await _runAsync(
|
|
tester,
|
|
() => reopened.managementUseCases.getBacklog(
|
|
GetBacklogRequest(
|
|
context: reopened.context('flutter-read-backlog'),
|
|
sortKey: BacklogSortKey.age,
|
|
),
|
|
),
|
|
);
|
|
|
|
expect(backlog.requireValue.items, hasLength(1));
|
|
expect(
|
|
backlog.requireValue.items.single.task.title,
|
|
'Captured from Flutter test',
|
|
);
|
|
},
|
|
);
|
|
|
|
testWidgets(
|
|
'selected future-day schedule completion and uncomplete persist across reopen',
|
|
(tester) async {
|
|
final path = await _runAsync(tester, _tempDatabasePath);
|
|
final tomorrow = _date.addDays(1);
|
|
const title = 'Schedule tomorrow from selected date';
|
|
|
|
final selectedDate = tomorrow;
|
|
var composition = await _openComposition(
|
|
tester,
|
|
path: path,
|
|
selectedDate: selectedDate,
|
|
clock: FixedClock(_instant(8)),
|
|
);
|
|
var commandController = composition.createCommandController(
|
|
refreshReads: () async {},
|
|
selectedDate: () => selectedDate,
|
|
);
|
|
await tester.runAsync(
|
|
() => commandController.quickCaptureToBacklog(title),
|
|
);
|
|
expect(commandController.state, isA<SchedulerCommandSuccess>());
|
|
|
|
final backlogBeforeSchedule = await _readBacklog(tester, composition);
|
|
final captured = backlogBeforeSchedule.items.single.task;
|
|
await tester.runAsync(
|
|
() => commandController.scheduleBacklogItem(
|
|
taskId: captured.id,
|
|
durationMinutes: 30,
|
|
expectedUpdatedAt: captured.updatedAt,
|
|
),
|
|
);
|
|
expect(commandController.state, isA<SchedulerCommandSuccess>());
|
|
expect((await _readBacklog(tester, composition)).items, isEmpty);
|
|
|
|
commandController.dispose();
|
|
await _closeComposition(tester, composition);
|
|
|
|
composition = await _openComposition(
|
|
tester,
|
|
path: path,
|
|
selectedDate: selectedDate,
|
|
clock: FixedClock(_instant(8, 10)),
|
|
);
|
|
var scheduled = await _readTodayItem(
|
|
tester,
|
|
composition,
|
|
tomorrow,
|
|
title,
|
|
);
|
|
expect(scheduled.taskStatus, TaskStatus.planned);
|
|
expect(CivilDate.fromDateTime(scheduled.start!.toUtc()), tomorrow);
|
|
|
|
commandController = composition.createCommandController(
|
|
refreshReads: () async {},
|
|
selectedDate: () => selectedDate,
|
|
);
|
|
await tester.runAsync(
|
|
() => commandController.completeTask(
|
|
taskId: scheduled.id,
|
|
taskType: scheduled.taskType,
|
|
),
|
|
);
|
|
expect(commandController.state, isA<SchedulerCommandSuccess>());
|
|
commandController.dispose();
|
|
await _closeComposition(tester, composition);
|
|
|
|
composition = await _openComposition(
|
|
tester,
|
|
path: path,
|
|
selectedDate: selectedDate,
|
|
clock: FixedClock(_instant(8, 20)),
|
|
);
|
|
final completed = await _readTodayItem(
|
|
tester,
|
|
composition,
|
|
tomorrow,
|
|
title,
|
|
);
|
|
expect(completed.taskStatus, TaskStatus.completed);
|
|
expect(completed.item.completedAt, isNotNull);
|
|
|
|
commandController = composition.createCommandController(
|
|
refreshReads: () async {},
|
|
selectedDate: () => selectedDate,
|
|
);
|
|
await tester.runAsync(
|
|
() => commandController.uncompleteTask(taskId: completed.id),
|
|
);
|
|
expect(commandController.state, isA<SchedulerCommandSuccess>());
|
|
commandController.dispose();
|
|
await _closeComposition(tester, composition);
|
|
|
|
composition = await _openComposition(
|
|
tester,
|
|
path: path,
|
|
selectedDate: selectedDate,
|
|
clock: FixedClock(_instant(8, 30)),
|
|
);
|
|
addTearDown(() => tester.runAsync(composition.dispose));
|
|
final reopened = await _readTodayItem(
|
|
tester,
|
|
composition,
|
|
tomorrow,
|
|
title,
|
|
);
|
|
expect(reopened.taskStatus, TaskStatus.planned);
|
|
expect(reopened.item.completedAt, isNull);
|
|
expect((await _readBacklog(tester, composition)).items, isEmpty);
|
|
},
|
|
);
|
|
}
|
|
|
|
/// Fixed date used by persistent Flutter tests.
|
|
final _date = CivilDate(2026, 1, 2);
|
|
|
|
/// Returns an instant on the fixed lifecycle date.
|
|
DateTime _instant(int hour, [int minute = 0]) {
|
|
return DateTime.utc(_date.year, _date.month, _date.day, hour, minute);
|
|
}
|
|
|
|
/// Returns a fresh temporary SQLite path.
|
|
Future<String> _tempDatabasePath() async {
|
|
final directory = await Directory.systemTemp.createTemp(
|
|
'focus-flow-flutter-persistence-',
|
|
);
|
|
addTearDown(() => directory.delete(recursive: true));
|
|
return '${directory.path}/scheduler.sqlite';
|
|
}
|
|
|
|
/// Opens a persistent composition for a test database.
|
|
Future<PersistentSchedulerComposition> _openComposition(
|
|
WidgetTester tester, {
|
|
required String path,
|
|
required CivilDate selectedDate,
|
|
required Clock clock,
|
|
}) {
|
|
return _runAsync(
|
|
tester,
|
|
() => PersistentSchedulerComposition.open(
|
|
sqlitePath: path,
|
|
selectedDate: selectedDate,
|
|
clock: clock,
|
|
),
|
|
);
|
|
}
|
|
|
|
/// Closes [composition] outside the widget-test fake async zone.
|
|
Future<void> _closeComposition(
|
|
WidgetTester tester,
|
|
PersistentSchedulerComposition composition,
|
|
) async {
|
|
await tester.runAsync(composition.dispose);
|
|
}
|
|
|
|
/// Reads the backlog through the management use case.
|
|
Future<BacklogQueryResult> _readBacklog(
|
|
WidgetTester tester,
|
|
PersistentSchedulerComposition composition,
|
|
) async {
|
|
final result = await _runAsync(
|
|
tester,
|
|
() => composition.managementUseCases.getBacklog(
|
|
GetBacklogRequest(
|
|
context: composition.context('flutter-read-backlog'),
|
|
sortKey: BacklogSortKey.age,
|
|
),
|
|
),
|
|
);
|
|
return result.requireValue;
|
|
}
|
|
|
|
/// Reads one Today timeline item by [title].
|
|
Future<TodayTimelineItem> _readTodayItem(
|
|
WidgetTester tester,
|
|
PersistentSchedulerComposition composition,
|
|
CivilDate date,
|
|
String title,
|
|
) async {
|
|
final state = await _readToday(tester, composition, date);
|
|
return state.timelineItems.singleWhere(
|
|
(item) => item.item.displayTitle == title,
|
|
);
|
|
}
|
|
|
|
/// Reads Today state for [date].
|
|
Future<TodayState> _readToday(
|
|
WidgetTester tester,
|
|
PersistentSchedulerComposition composition,
|
|
CivilDate date,
|
|
) async {
|
|
final result = await _runAsync(
|
|
tester,
|
|
() => composition.todayQuery.execute(
|
|
GetTodayStateRequest(
|
|
context: composition.context(
|
|
'flutter-read-today-${date.toIsoString()}',
|
|
),
|
|
date: date,
|
|
includeNextFlexibleItem: true,
|
|
),
|
|
),
|
|
);
|
|
return result.requireValue;
|
|
}
|
|
|
|
/// Runs real async work outside the widget-test fake async zone.
|
|
Future<T> _runAsync<T>(
|
|
WidgetTester tester,
|
|
Future<T> Function() callback,
|
|
) async {
|
|
final result = await tester.runAsync(callback);
|
|
if (result == null) {
|
|
throw StateError('runAsync returned null.');
|
|
}
|
|
return result;
|
|
}
|