feat(block2): geohash encoder in commons_core (for 'use my location')

Pure Dart lat/lon -> low-precision geohash (public-domain algorithm), the piece
the coarse-area capture needs so people never type a code. Precision-controlled
(5 chars ~ ±2.4 km). Tested against the canonical u4pruydqqvj vector + prefix/
proximity properties. 5 tests green.
This commit is contained in:
vjrj 2026-07-10 03:18:30 +02:00
parent 6d2d0517cc
commit 2d4e14e0da
3 changed files with 92 additions and 0 deletions

View file

@ -0,0 +1,35 @@
import 'package:commons_core/commons_core.dart';
import 'package:test/test.dart';
void main() {
group('Geohash.encode', () {
test('matches the classic reference vector', () {
// (57.64911, 10.40744) "u4pruydqqvj" is the canonical geohash example.
expect(Geohash.encode(57.64911, 10.40744, precision: 11),
'u4pruydqqvj');
});
test('precision controls length and coarseness (prefix relation)', () {
final fine = Geohash.encode(57.64911, 10.40744, precision: 9);
final coarse = Geohash.encode(57.64911, 10.40744, precision: 5);
expect(coarse.length, 5);
expect(fine, startsWith(coarse)); // coarser is a prefix of finer
});
test('nearby points share a coarse prefix; far ones do not', () {
final a = Geohash.encode(41.39, 2.16, precision: 5); // Barcelona-ish
final b = Geohash.encode(41.40, 2.17, precision: 5); // ~1.5 km away
final far = Geohash.encode(40.41, -3.70, precision: 5); // Madrid
expect(a.substring(0, 3), b.substring(0, 3));
expect(a, isNot(far));
});
test('origin encodes without error', () {
expect(Geohash.encode(0, 0, precision: 5), hasLength(5));
});
test('rejects non-positive precision', () {
expect(() => Geohash.encode(0, 0, precision: 0), throwsArgumentError);
});
});
}