Files
logic-puzzle/integration_test/notebook_test.dart
T
2026-11-02 06:14:48 +01:00

217 lines
7.7 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';
import 'package:lumen/main.dart';
import 'package:lumen/domain/book.dart';
import 'package:lumen/domain/game.dart';
import 'package:lumen/services/progress.dart';
import 'package:lumen/services/narrator.dart';
void main() {
final binding = IntegrationTestWidgetsFlutterBinding.ensureInitialized();
testWidgets('six leaves, recorded audio, discoveries and puzzle completion', (
tester,
) async {
expect(
const bool.fromEnvironment('QA_TEST'),
true,
reason: 'Run only with the QA flavor and QA_TEST=true',
);
final book = await Book.load(),
store = Progress.memory(),
g = Game(book, store);
await tester.pumpWidget(LumenApp(game: g, adsEnabled: false));
await tester.pumpAndSettle();
await binding.convertFlutterSurfaceToImage();
Future<void> shot(String name) async {
await tester.pumpAndSettle();
await binding.takeScreenshot(name);
}
Future<void> tap(String text) async {
final f = find.text(text);
await tester.ensureVisible(f);
await tester.tap(f);
await tester.pumpAndSettle();
}
await shot('00-home');
await tap('Open Mara’s notebook · 0 / 12');
expect(g.storyIndex, 0);
await shot('01-dedication');
final width = tester.view.physicalSize.width / tester.view.devicePixelRatio;
await tester.dragFrom(Offset(width - 12, 300), const Offset(-180, 0));
await tester.pumpAndSettle();
expect(g.storyIndex, 1);
// A normal swipe over the writing must work, not just the edge.
await tester.drag(find.text('By the cottage window'), const Offset(170, 0));
await tester.pumpAndSettle();
expect(g.storyIndex, 0);
await tester.drag(
find.text('For you, when you are ready'),
const Offset(-170, 0),
);
await tester.pumpAndSettle();
expect(g.storyIndex, 1);
expect(
tester
.getBottomRight(find.text('A recorded reading, kept with this page'))
.dy,
lessThan(tester.getTopLeft(find.text('Contents')).dy),
);
await shot('02-cottage');
// Actual native WAV playback, all five files. Position must survive pause/resume.
final narrator = Narrator(book.tracks);
for (final id in book.tracks.keys) {
await narrator.toggle(id, 1);
await Future<void>.delayed(const Duration(seconds: 2));
await narrator.pause();
expect(narrator.error, isEmpty, reason: id);
expect(narrator.position.inMilliseconds, greaterThan(600), reason: id);
final paused = narrator.position;
await Future<void>.delayed(const Duration(milliseconds: 350));
expect((narrator.position - paused).inMilliseconds.abs(), lessThan(200));
await narrator.toggle(id, 1);
await Future<void>.delayed(const Duration(seconds: 1));
await narrator.pause();
expect(narrator.position, greaterThan(paused));
await narrator.stop();
}
narrator.dispose();
await tap('Next leaf →');
expect(g.storyIndex, 2);
expect(
tester.getBottomRight(find.text('Hint · rewarded ad')).dy,
lessThan(tester.getTopLeft(find.text('Contents')).dy),
);
await shot('03-puzzle');
final before = List.of(g.cells);
await tester.drag(
find.byKey(const ValueKey('cell-20')),
const Offset(65, 0),
);
await tester.pumpAndSettle();
expect(g.storyIndex, 2);
await tester.tap(find.byKey(const ValueKey('cell-0')));
await tester.pumpAndSettle();
expect(g.cells[0], 1);
await tester.tap(find.byKey(const ValueKey('cell-1')));
await tester.pump();
await tester.tap(find.byKey(const ValueKey('cell-1')));
await tester.pumpAndSettle();
expect(g.cells[1], 2);
await shot('03-pencil-marks');
await tap('Undo a mark');
await tap('Undo a mark');
await tap('Undo a mark');
expect(g.cells, before);
await tap('Next leaf →');
await tap('Read ahead');
expect(g.solved, 0);
expect(g.storyIndex, 3);
await shot('04-memory');
await tap('Next leaf →');
await shot('05-folded-note');
await tap('Unfold Gran’s note');
expect(store.flag('notebook_note_open'), true);
await shot('05-open-note');
await tap('Bookmark');
await tap('Next leaf →');
await shot('06-lantern');
expect(
find.bySemanticsLabel(
'Watercolour of Mara’s blue storm lantern. Tap to examine the repaired handle.',
),
findsNothing,
);
expect(store.flag('notebook_handle_seen'), false);
await tap('Contents');
await tap('Return to my ribbon bookmark');
expect(g.storyIndex, 4);
final restarted = Game(book, Progress.memory(store.snapshot));
await restarted.restore();
expect(restarted.storyIndex, 4);
expect(restarted.progress.flag('notebook_note_open'), true);
g.showStory(2);
await tester.pumpAndSettle();
final p = g.puzzle!;
for (var i = 0; i < 49; i++) {
while (g.cells[i] != (p.solution[i] == 1 ? 1 : 2)) {
await tester.tap(find.byKey(ValueKey('cell-$i')));
await tester.pump();
}
}
await tester.pumpAndSettle();
expect(g.storyIndex, 3);
expect(g.solved, 1);
g.showStory(2);
g.check();
expect(g.solved, 1);
g.showStory(5);
await tester.pumpAndSettle();
await tap('Follow the lantern into the next entry →');
expect(g.storyIndex, 6);
await tap('Turn to the pattern →');
expect(g.storyIndex, 7);
final lantern = g.puzzle!;
for (var i = 0; i < 49; i++) {
while (g.cells[i] != (lantern.solution[i] == 1 ? 1 : 2)) {
g.mark(i);
}
}
await tester.pumpAndSettle();
expect(g.storyIndex, 8);
expect(g.solved, 2);
await shot('07-campaign-reveal');
g.showStory(5);
await tester.pumpAndSettle();
await tap('Read Gran’s margin note');
await tap('Keep that thought');
expect(store.flag('notebook_handle_seen'), true);
g.showStory(8);
await tester.pumpAndSettle();
// The later campaign leaves need swipes too, including read-ahead.
Future<void> swipeWriting(String text, double dx) async {
final target = find.text(text);
await tester.ensureVisible(target);
await tester.drag(target, Offset(dx, 0));
await tester.pumpAndSettle();
}
await swipeWriting('A light at the door', -170);
expect(g.storyIndex, 9);
await swipeWriting('Under the loose floorboard', -170);
expect(g.storyIndex, 10);
await tester.ensureVisible(find.byKey(const ValueKey('cell-0')));
await tester.tap(find.byKey(const ValueKey('cell-0')));
await tester.pumpAndSettle();
final keyMarks = List<int>.of(g.cells);
await swipeWriting('Under the loose floorboard', -170);
await tap('Keep solving');
expect(g.storyIndex, 10);
await swipeWriting('Under the loose floorboard', -170);
await tap('Read ahead');
expect(g.storyIndex, 11);
expect(g.recovered(2), false);
expect(g.solved, 2);
await shot('08-campaign-read-ahead');
await swipeWriting('A journey kept ready', -170);
expect(g.storyIndex, 12);
await swipeWriting('A direction in the dark', 170);
expect(g.storyIndex, 11);
await swipeWriting('A journey kept ready', 170);
expect(g.storyIndex, 10);
expect(g.cells, keyMarks);
expect(g.solved, 2);
// Exercise real plugin persistence without touching main-app data.
final disk = await Progress.load();
await disk.set('qa_roundtrip', 'notebook-and-audio');
await disk.flushed;
expect((await Progress.load()).text('qa_roundtrip'), 'notebook-and-audio');
expect(tester.takeException(), isNull);
await tester.pumpWidget(const SizedBox());
await tester.pumpAndSettle();
});
}