Files
logic-puzzle/test/postcards_test.dart
T
2026-11-14 08:55:48 +01:00

279 lines
9.5 KiB
Dart

import 'dart:convert';
import 'package:flutter_test/flutter_test.dart';
import 'package:lumen/domain/book.dart';
import 'package:lumen/domain/game.dart';
import 'package:lumen/domain/library.dart';
import 'package:lumen/domain/postcards.dart';
import 'package:lumen/domain/puzzle.dart';
import 'package:lumen/services/ad_pacing.dart';
import 'package:lumen/services/play_metrics.dart';
import 'package:lumen/services/progress.dart';
void solve(Game game) {
for (var i = 0; i < game.cells.length; i++) {
final target = game.puzzle!.solution[i] == 1 ? 1 : 2;
while (game.cells[i] != target) {
game.mark(i);
}
}
}
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
test('hidden totals and mirrored pairs agree with the saved solutions', () {
var hidden = 0, mirrors = 0, trails = 0;
for (final card in islandPostcards) {
for (var slot = 0; slot < 3; slot++) {
final p = card.puzzle(slot);
for (final trail in p.footprintTrails) {
trails++;
expect(trail.length, 3);
expect(p.solution[trail[1]], 1);
expect(p.solution[trail[0]] + p.solution[trail[2]], 1);
}
final row = p.hiddenRow;
if (row != null) {
hidden++;
expect(p.rows[row], p.rows[row - 1] + 1);
}
for (final pair in p.mirrorPairs) {
mirrors++;
expect(p.solution[pair.first], p.solution[pair.second]);
final c = p.constraints.firstWhere((c) => c.equal);
final solver = LogicSolver(49, [c]);
for (final value in [0, 1]) {
final known = List<int>.filled(49, -1)..[pair.first] = value;
expect(solver.nextHint(known)!.value, value);
}
}
}
}
expect(hidden, greaterThan(0));
expect(mirrors, greaterThan(0));
expect(trails, greaterThan(0));
});
test('every bundled story puzzle is at least seven by seven', () async {
final library = await StoryLibrary.load();
for (final definition in library.stories) {
final book = await library.open(definition.id);
for (final level in book.levels) {
expect(
level.puzzle().size,
greaterThanOrEqualTo(7),
reason: '${definition.id}: ${level.id}',
);
}
}
});
test(
'all 42 frozen boards are distinct, unique and solvable without guessing',
() {
final pictures = <String>{}, keys = <String>{};
for (final card in islandPostcards) {
for (var slot = 0; slot < 3; slot++) {
final p = card.puzzle(slot);
expect(p.regions.map((r) => r.label), ['A', 'B', 'C']);
expect(
p.regions.every(
(r) =>
r.top >= 0 &&
r.left >= 0 &&
r.top + r.height <= p.size &&
r.left + r.width <= p.size,
),
isTrue,
);
expect(
p.constraints.where((c) => c.name.startsWith('Area ')).length,
3,
);
expect(pictures.add(p.solution.join()), isTrue);
expect(keys.add(card.puzzleKey(slot)), isTrue);
expect(p.countSolutions(), 1);
expect(
LogicSolver(49, p.constraints).analyze(p.initial).solved,
isTrue,
);
}
}
expect(pictures.length, 42);
},
);
test('later postcard lanterns link two editable opposite cells and give correct hints', () {
for (var index = 0; index < islandPostcards.length; index++) {
for (var slot = 0; slot < 3; slot++) {
final puzzle = islandPostcards[index].puzzle(slot);
expect(puzzle.lanternPairs.length, index >= 2 && slot == 2 ? 1 : 0);
for (final pair in puzzle.lanternPairs) {
expect(puzzle.given[pair.first], isFalse);
expect(puzzle.given[pair.second], isFalse);
expect(puzzle.solution[pair.first] + puzzle.solution[pair.second], 1);
final constraint = puzzle.constraints.firstWhere(
(c) => c.name == 'Lantern pair',
);
final solver = LogicSolver(49, [constraint]);
for (final first in [0, 1]) {
final known = List<int>.filled(49, -1)..[pair.first] = first;
final hint = solver.nextHint(known)!;
expect(hint.cell, pair.second);
expect(hint.value, 1 - first);
expect(hint.explanation, contains('lantern'));
}
}
}
}
});
test('postcards resume marks across books and never overwrite a reading position', () async {
final book = await Book.load();
final store = Progress.memory();
final g = Game(book, store)..showStory(2);
final saved = store.text('notebook_last_page');
g.startPostcard(0);
final editable = g.puzzle!.given.indexOf(false);
g.mark(editable);
final marks = List<int>.of(g.cells);
final again = Game(book, Progress.memory(store.snapshot))..startPostcard(0);
expect(again.cells, marks);
solve(again);
again.check();
expect(again.solved, 1);
expect(again.postcardSolved(0), 1);
expect(again.postcardsCollected, 0);
expect(again.claimPostcardBreak(), isFalse);
again.startPostcard(0);
expect(again.postcardSlot, 1);
solve(again);
again.startPostcard(0);
solve(again);
expect(again.postcardsCollected, 1);
expect(again.claimPostcardBreak(), isTrue);
final reloaded = Game(book, Progress.memory(again.store.snapshot))
..startPostcard(0);
expect(reloaded.claimPostcardBreak(), isFalse);
expect(reloaded.solved, 3);
expect(reloaded.store.text('notebook_last_page'), saved);
expect(reloaded.store.text('flutter_screen'), 'story');
expect(reloaded.store.number('practiceCompleted'), 0);
expect(reloaded.store.number('level_EASY', 1), 1);
expect(
reloaded.store
.forStory('paper-boat')
.flag('done_${islandPostcards[0].puzzleKey(0)}'),
isTrue,
);
await reloaded.restore();
expect(reloaded.storyIndex, 2);
});
test('legacy completions survive the 7x7 upgrade', () async {
final card = islandPostcards.first;
final store = Progress.memory({
for (var i = 0; i < 3; i++) 'done_${card.legacyPuzzleKey(i)}': true,
'grid_${islandPostcards[1].legacyPuzzleKey(0)}': '1' * 25,
});
final g = Game(await Book.load(), store);
expect(g.postcardsCollected, 1);
g.startPostcard(1);
expect(g.puzzle!.size, 7);
expect(g.cells.length, 49);
expect(g.key, islandPostcards[1].puzzleKey(0));
expect(g.cells.where((cell) => cell == 0), isNotEmpty);
});
test(
'a practice milestone can offer only one break across restarts',
() async {
final book = await Book.load();
final store = Progress.memory({'practiceCompleted': 4});
final g = Game(book, store);
expect(g.claimPracticeBreak(), isTrue);
expect(
Game(book, Progress.memory(store.snapshot)).claimPracticeBreak(),
isFalse,
);
await store.set('practiceCompleted', 5);
expect(g.claimPracticeBreak(), isFalse);
await store.set('practiceCompleted', 8);
expect(g.claimPracticeBreak(), isTrue);
},
);
test('ad spacing covers opening, rewarded views, daily cap and restart', () {
var time = DateTime(2026, 11, 13, 12);
final store = Progress.memory();
final pacing = AdPacing(store, clock: () => time);
expect(pacing.allowed, isFalse);
time = time.add(const Duration(minutes: 3));
expect(pacing.allowed, isTrue);
pacing.shown(automatic: false);
expect(store.number('ad_cap_count'), 0);
expect(pacing.allowed, isFalse);
time = time.add(const Duration(minutes: 3));
for (var i = 0; i < 6; i++) {
expect(pacing.allowed, isTrue);
pacing.shown(automatic: true);
time = time.add(const Duration(minutes: 3));
}
expect(pacing.allowed, isFalse);
final restarted = AdPacing(
Progress.memory(store.snapshot),
clock: () => time,
);
time = time.add(const Duration(minutes: 4));
expect(restarted.allowed, isFalse);
time = time.add(const Duration(days: 1));
expect(restarted.allowed, isTrue);
time = DateTime(2026, 11, 12); // A backwards clock cannot bypass spacing.
expect(restarted.allowed, isFalse);
});
test('local metrics separate currencies, test revenue, impressions and partial outings', () {
final store = Progress.memory();
final metrics = PlayMetrics(store, clock: () => DateTime.utc(2026, 11, 13));
metrics.visit();
metrics.visit();
metrics.event('configured_interstitial_impressions');
metrics.paid(
format: 'interstitial',
micros: 3000,
currency: 'EUR',
precision: 'estimated',
test: false,
);
metrics.paid(
format: 'rewarded',
micros: 4000,
currency: 'USD',
precision: 'precise',
test: false,
);
metrics.paid(
format: 'interstitial',
micros: 10000,
currency: 'EUR',
precision: 'unknown',
test: true,
);
for (var i = 0; i < 3; i++) {
store.set('done_${islandPostcards[i].puzzleKey(0)}', true);
}
final report = jsonDecode(
PlayMetrics(Progress.memory(store.snapshot)).export(),
);
expect(report['sessions'], 1);
expect(report['postcardsCompleted'], 0);
expect(report['revenueMicros'], {
'configured:interstitial:EUR:estimated': 3000,
'configured:rewarded:USD:precise': 4000,
'test:interstitial:EUR:unknown': 10000,
});
expect(
report['days']['2026-11-13']['configured_interstitial_impressions'],
1,
);
});
}