Files
logic-puzzle/test/puzzle_test.dart
T
2026-10-31 04:08:16 +01:00

164 lines
5.4 KiB
Dart

import 'dart:convert';
import 'dart:io';
import 'dart:math';
import 'package:flutter_test/flutter_test.dart';
import 'package:lumen/domain/puzzle.dart';
import 'package:lumen/domain/book.dart';
import 'package:lumen/domain/game.dart';
import 'package:lumen/services/progress.dart';
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
test('Dart generation matches 40 Java reference boards including signed 64-bit seeds', () {
final fixtures = jsonDecode(
File('test/fixtures/java_boards.json').readAsStringSync(),
) as List;
for (final f in fixtures) {
final seed = BigInt.parse(f['seed']),
p = f['kind'] == 'legacy'
? Puzzle.seeded(seed)
: Puzzle.generate(seed, Difficulty.fromStored(f['kind']));
expect(p.solution.join(), f['solution'], reason: '${f['kind']} $seed');
expect(
p.given.map((v) => v ? 1 : 0).join(),
f['given'],
reason: '${f['kind']} $seed givens',
);
}
});
test('300 generated practice boards have requested grades, unique answers and complete hints', () {
for (final d in Difficulty.values) {
for (var seed = 1000001; seed < 1000101; seed++) {
final p = Puzzle.generate(BigInt.from(seed), d);
expect(p.difficulty, d);
expect(p.countSolutions(), 1);
final solver = LogicSolver(25, p.constraints),
analysis = solver.analyze(p.initial);
expect(analysis.solved, true);
expect(analysis.cells, p.solution);
expect(p.solved(p.solution.map((v) => v == 1 ? 1 : 2).toList()), true);
expect(p.solved(List.filled(25, 0)), false);
}
}
});
test(
'area solver matches exhaustive enumeration for 300 random small systems',
() {
final rng = Random(42);
for (var trial = 0; trial < 300; trial++) {
final constraints = List.generate(1 + rng.nextInt(5), (k) {
final cells = [
for (var i = 0; i < 6; i++)
if (rng.nextBool()) i,
];
return Constraint(cells, rng.nextInt(cells.length + 1), '$k');
});
var count = 0;
for (var mask = 0; mask < 64; mask++) {
if (constraints.every(
(c) =>
c.cells.fold<int>(0, (sum, i) => sum + ((mask >> i) & 1)) ==
c.total,
)) {
count++;
}
}
expect(
LogicSolver(6, constraints).countSolutions(List.filled(6, -1)),
min(2, count),
);
}
},
);
test('all campaign pictures unique; later entries require comparisons, without guessing', () async {
final book = await Book.load();
expect(book.levels.length, 12);
for (var i = 0; i < 12; i++) {
final p = book.levels[i].puzzle(),
solver = LogicSolver(49, p.constraints);
expect(p.countSolutions(), 1, reason: 'campaign $i');
final a = solver.analyze(p.initial);
expect(a.solved, true, reason: 'campaign $i');
expect(a.cells, p.solution);
if (i >= 2) {
expect(solver.analyze(p.initial, useAreas: false).solved, false);
expect(a.areaDeductions, greaterThan(0));
}
}
});
test('calendar streak handles repeats, missed days and DST boundaries', () {
expect(completedStreak(100, 4, 100), 4);
expect(completedStreak(100, 4, 101), 5);
expect(completedStreak(100, 4, 102), 1);
expect(visibleStreak(100, 4, 102), 0);
expect(
epochDay(DateTime(2026, 10, 26)) - epochDay(DateTime(2026, 10, 25)),
1,
);
});
test(
'fresh Flutter progress ignores old saves; reading never awards completion',
() async {
final store = Progress.memory(), g = Game(await Book.load(), store);
await g.restore();
expect(g.screen, Screen.home);
expect(g.solved, 0);
expect(g.recoveredCount, 0);
g.showStory(2);
g.mark(0);
final marks = List.of(g.cells);
g.showStory(3);
expect(g.solved, 0);
expect(g.recovered(0), false);
g.showStory(2);
expect(g.cells, marks);
g.undo();
expect(g.cells[0], 0);
final p = g.puzzle!;
for (var i = 0; i < 49; i++) {
while (g.cells[i] != (p.solution[i] == 1 ? 1 : 2)) {
g.mark(i);
}
}
expect(g.screen, Screen.story);
expect(g.storyIndex, 3);
expect(g.solved, 1);
expect(g.recovered(0), true);
g.showStory(2);
g.check();
expect(g.solved, 1);
final restored = Game(g.book, Progress.memory(store.snapshot));
await restored.restore();
expect(restored.storyIndex, 2);
expect(restored.solved, 1);
},
);
test('daily completion awards once and practice difficulties keep separate levels', () async {
final store = Progress.memory(), g = Game(await Book.load(), store);
g.startDaily(DateTime(2026, 10, 31));
final p = g.puzzle!;
for (var i = 0; i < 25; i++) {
while (g.cells[i] != (p.solution[i] == 1 ? 1 : 2)) {
g.mark(i);
}
}
expect(g.solved, 1);
expect(g.progress.number('streak'), 1);
g.check();
expect(g.solved, 1);
g.selectDifficulty(Difficulty.hard);
await g.startPractice();
expect(g.puzzle!.difficulty, Difficulty.hard);
final hard = g.puzzle!;
for (var i = 0; i < 25; i++) {
while (g.cells[i] != (hard.solution[i] == 1 ? 1 : 2)) {
g.mark(i);
}
}
expect(g.practiceLevel, 2);
g.selectDifficulty(Difficulty.easy);
expect(g.practiceLevel, 1);
});
}