import 'dart:convert'; import 'dart:io'; import 'dart:math'; import 'package:flutter_test/flutter_test.dart'; import 'package:lumen/domain/book.dart'; import 'package:lumen/domain/puzzle.dart'; import 'package:lumen/domain/thinking.dart'; bool _satisfies(List cells, List constraints) => constraints .every((c) => c.cells.fold(0, (sum, i) => sum + cells[i]) == c.total); void main() { TestWidgetsFlutterBinding.ensureInitialized(); test( 'successive deductions are snapshot waves, independent of clue order', () { final constraints = [ Constraint([0], 1, 'A'), Constraint([0, 1], 1, 'B'), Constraint([1, 2], 1, 'C'), Constraint([2, 3], 1, 'D'), ]; final initial = List.filled(4, -1); for (final ordered in [constraints, constraints.reversed.toList()]) { final report = ThinkingAnalyzer(4, ordered).analyze(initial); expect(report.status, ThinkingStatus.solved); expect(report.rounds.length, 4); expect(report.rounds.map((r) => r.newCells), [1, 1, 1, 1]); expect(report.maxAssumptionDepthUsed, 0); expect(report.comparisonSteps, 0); expect(report.cells, [1, 0, 1, 0]); expect(initial, [-1, -1, -1, -1]); } }, ); test('overlap cancellation is a deduction, not hypothetical lookahead', () { final constraints = [ Constraint([0, 1], 1, 'A'), Constraint([0, 1, 2], 1, 'B'), Constraint([2, 3], 1, 'C'), Constraint([0, 3], 1, 'D'), ]; final report = ThinkingAnalyzer(4, constraints).analyze(List.filled(4, -1)); expect(report.status, ThinkingStatus.solved); expect(report.initialDirectMoves, 0); expect(report.comparisonSteps, greaterThan(0)); expect(report.maxAssumptionDepthUsed, 0); expect(report.cells, [0, 1, 0, 1]); }); final fixtures = jsonDecode( File('test/fixtures/thinking_depth.json').readAsStringSync(), ) as List; for (final fixture in fixtures) { test('recognises ${fixture['depth']} levels of hypothetical reasoning', () { final n = fixture['cellCount'] as int; final initial = (fixture['initial'] as List).cast(); final constraints = [ for (final c in fixture['constraints']) Constraint((c['cells'] as List).cast(), c['total'], c['name']), ]; final analyzer = ThinkingAnalyzer(n, constraints); final depth = fixture['depth'] as int; final limited = analyzer.analyze(initial, maxLookahead: depth - 1); expect(limited.status, ThinkingStatus.depthLimit); expect(limited.lookaheadDepth, isNull); expect(limited.solutionCount, 1); expect(limited.cells, contains(-1)); final solved = analyzer.analyze(initial, maxLookahead: depth); expect(solved.status, ThinkingStatus.solved); expect(solved.maxAssumptionDepthUsed, depth); expect(solved.lookaheadDepth, depth); expect(solved.cells, fixture['solution']); expect(solved.assumptionSteps, greaterThan(0)); expect( solved.steps .where((s) => s.rule == ThinkingRule.assumption) .every((s) => s.proof != null), true, ); // Independently enumerate completions to validate each rejection. The // analyzer itself receives no answer and must not copy the fixture solution. final known = List.of(initial); for (final round in solved.rounds) { for (final step in round.steps) { if (step.rule == ThinkingRule.assumption) { final mark = step.marks.entries.single; var alternatives = 0; for (var mask = 0; mask < (1 << n); mask++) { final cells = List.generate(n, (i) => mask >> i & 1); if (cells[mark.key] != step.rejectedValue) continue; if (List.generate( n, (i) => i, ).any((i) => known[i] >= 0 && known[i] != cells[i])) { continue; } if (_satisfies(cells, constraints)) alternatives++; } expect( alternatives, 0, reason: 'Rejected branch must be impossible', ); } for (final mark in step.marks.entries) { known[mark.key] = mark.value; } } } }); } test( 'multiple answers are not reported as a need to think or guess deeper', () { final report = ThinkingAnalyzer(2, [ Constraint([0, 1], 1, 'A'), ]).analyze([-1, -1]); expect(report.status, ThinkingStatus.nonUnique); expect(report.solutionCount, 2); expect(report.assumptionSteps, 0); }, ); test('contradicting and impossible clues are reported explicitly', () { for (final constraints in [ [ Constraint([0], 0, 'A'), Constraint([0], 1, 'B'), ], [ Constraint([0], 2, 'Impossible'), ], ]) { final report = ThinkingAnalyzer(1, constraints).analyze([-1]); expect(report.status, ThinkingStatus.inconsistent); expect(report.solutionCount, 0); expect(report.message, isNotEmpty); } }); test('a search-budget exit leaves depth and uniqueness unresolved', () { final fixture = fixtures.last; final constraints = [ for (final c in fixture['constraints']) Constraint((c['cells'] as List).cast(), c['total'], c['name']), ]; final report = ThinkingAnalyzer( fixture['cellCount'], constraints, ).analyze((fixture['initial'] as List).cast(), nodeLimit: 1); expect(report.status, ThinkingStatus.nodeLimit); expect(report.searchNodes, 1); expect(report.solutionCount, isNull); expect(report.lookaheadDepth, isNull); expect(report.cells, contains(-1)); expect(report.toJson()['status'], 'nodeLimit'); }); test('fully marked boards are validated, including incorrect marks', () { final analyzer = ThinkingAnalyzer(2, [ Constraint([0, 1], 1, 'A'), ]); final valid = analyzer.analyze([0, 1]); expect(valid.status, ThinkingStatus.solved); expect(valid.rounds, isEmpty); expect(valid.resolvedCells, 0); expect(analyzer.analyze([1, 1]).status, ThinkingStatus.inconsistent); }); test('invalid input encodings and constraint indices fail clearly', () { expect( () => ThinkingAnalyzer(2, [ Constraint([0, 0], 1, 'Duplicate'), ]), throwsArgumentError, ); expect( () => ThinkingAnalyzer(2, [ Constraint([2], 1, 'Outside'), ]), throwsArgumentError, ); final analyzer = ThinkingAnalyzer(2, []); expect(() => analyzer.analyze([0]), throwsArgumentError); expect(() => analyzer.analyze([2, -1]), throwsArgumentError); expect( () => analyzer.analyze([-1, -1], maxLookahead: 4), throwsArgumentError, ); expect(() => analyzer.analyze([-1, -1], nodeLimit: 0), throwsArgumentError); }); test( 'classification and forced marks agree with exhaustive small systems', () { final rng = Random(38); for (var trial = 0; trial < 200; trial++) { const n = 6; final constraints = List.generate(1 + rng.nextInt(8), (c) { final cells = [ for (var i = 0; i < n; i++) if (rng.nextBool()) i, ]; return Constraint(cells, rng.nextInt(cells.length + 1), 'C$c'); }); final initial = List.generate( n, (_) => rng.nextInt(5) == 0 ? rng.nextInt(2) : -1, ); final completions = >[]; for (var mask = 0; mask < 1 << n; mask++) { final cells = List.generate(n, (i) => mask >> i & 1); if (List.generate( n, (i) => i, ).any((i) => initial[i] >= 0 && initial[i] != cells[i])) { continue; } if (_satisfies(cells, constraints)) completions.add(cells); } final report = ThinkingAnalyzer(n, constraints).analyze(initial); expect( report.solutionCount, min(2, completions.length), reason: 'Trial $trial', ); if (completions.isEmpty) { expect(report.status, ThinkingStatus.inconsistent); } if (completions.length > 1) { expect(report.status, ThinkingStatus.nonUnique); } for (final step in report.steps) { for (final mark in step.marks.entries) { expect( completions.every((s) => s[mark.key] == mark.value), true, reason: 'Unsound deduction in trial $trial', ); } } } }, ); test( 'all campaign traces solve the picture without hypothetical assumptions', () async { final book = await Book.load(); for (var index = 0; index < book.levels.length; index++) { final p = book.levels[index].puzzle(); final report = ThinkingAnalyzer(49, p.constraints).analyze(p.initial); expect(report.status, ThinkingStatus.solved, reason: 'Campaign $index'); expect(report.cells, p.solution); expect(report.solutionCount, 1); expect(report.maxAssumptionDepthUsed, 0); expect( report.rounds.fold(report.givens, (sum, r) => sum + r.newCells), 49, ); if (index < 2) expect(report.comparisonSteps, 0); if (index >= 2) expect(report.comparisonSteps, greaterThan(0)); expect(report.toJson()['remainingCells'], isEmpty); } }, ); }