Configure the connect screen's node banner from a DNS TXT record

The "get a node" banner's link, and optionally its title, body and button
text, now come from the _banner.meshcoreopen.org TXT record, looked up
over Cloudflare DNS-over-HTTPS and cached for the record's TTL (1 min to
6 h). The record is `v=1;on=1;url=<https link>` with URL-encoded values;
`on=0`, a missing record or any lookup failure hides the banner.
This commit is contained in:
zjs81 committed 2026-10-24 21:29:08 -07:00
1 parent ae2db54a24
commit ee382f9177
4 files changed
+321 -16

No files matched your search

@@ -0,0 +1,95 @@
import 'dart:convert';
import 'package:flutter_test/flutter_test.dart';
import 'package:http/http.dart' as http;
import 'package:http/testing.dart';
import 'package:meshcore_open/services/node_banner_service.dart';
import 'package:meshcore_open/storage/prefs_manager.dart';
import 'package:shared_preferences/shared_preferences.dart';
const _record =
'v=1;on=1;url=https%3A%2F%2Fshop.example.com%2Fnode%3Fref%3Da%26b%3Dc;'
'title=Need%20a%20node%3F';
String _doh(String data, {int status = 0, int ttl = 300}) => jsonEncode({
'Status': status,
'Answer': [
{'name': '_banner.meshcoreopen.org', 'type': 16, 'TTL': ttl, 'data': data},
],
});
void main() {
setUp(() async {
SharedPreferences.setMockInitialValues({});
PrefsManager.reset();
await PrefsManager.initialize();
});
group('NodeBannerConfig.parse', () {
test('reads the link and decodes text overrides', () {
final config = NodeBannerConfig.parse(_record)!;
expect(config.url.toString(), 'https://shop.example.com/node?ref=a&b=c');
expect(config.title, 'Need a node?');
expect(config.body, isNull);
expect(config.action, isNull);
});
test('is off for on=0, unknown versions, and non-https links', () {
expect(
NodeBannerConfig.parse(_record.replaceAll('on=1', 'on=0')),
isNull,
);
expect(NodeBannerConfig.parse(_record.replaceAll('v=1', 'v=2')), isNull);
expect(NodeBannerConfig.parse('v=1;on=1;url=http%3A%2F%2Fx.com'), isNull);
expect(NodeBannerConfig.parse('v=1;on=1'), isNull);
expect(NodeBannerConfig.parse('v=1;on=1;url=%E0%A4%A'), isNull);
});
});
test('joins split TXT character-strings and unescapes them', () {
final answer = NodeBannerService.txtFromDohJson(
_doh(r'"v=1;on=1;url=https%3A%2F%2Fa.com" ";title=Say \"hi\""'),
)!;
expect(answer.text, 'v=1;on=1;url=https%3A%2F%2Fa.com;title=Say "hi"');
expect(answer.ttl, 300);
});
test('no record or NXDOMAIN gives no answer', () {
expect(NodeBannerService.txtFromDohJson(jsonEncode({'Status': 3})), isNull);
expect(NodeBannerService.txtFromDohJson(jsonEncode({'Status': 0})), isNull);
});
test('load queries Cloudflare DoH and caches for the TTL', () async {
var calls = 0;
var now = DateTime(2026, 1, 1);
final client = MockClient((request) async {
calls++;
expect(request.url.host, 'cloudflare-dns.com');
expect(request.url.queryParameters['name'], '_banner.meshcoreopen.org');
expect(request.url.queryParameters['type'], 'TXT');
expect(request.headers['accept'], 'application/dns-json');
return http.Response(_doh('"$_record"'), 200);
});
final service = NodeBannerService(client: client, now: () => now);
expect((await service.load())?.title, 'Need a node?');
expect((await service.load())?.title, 'Need a node?');
expect(calls, 1);
now = now.add(const Duration(minutes: 6));
await service.load();
expect(calls, 2);
});
test('load returns null when the lookup fails', () async {
final failing = NodeBannerService(
client: MockClient((_) async => throw http.ClientException('offline')),
);
expect(await failing.load(), isNull);
final serverError = NodeBannerService(
client: MockClient((_) async => http.Response('', 503)),
);
expect(await serverError.load(), isNull);
});
}
+43 -2
View File
@@ -1,13 +1,18 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:meshcore_open/l10n/app_localizations.dart';
import 'package:meshcore_open/services/node_banner_service.dart';
import 'package:meshcore_open/widgets/get_node_banner.dart';
final _config = NodeBannerConfig(url: Uri.parse('https://example.com/node'));
void main() {
Future<void> pumpBanner(
WidgetTester tester, {
required Size size,
required double textScale,
NodeBannerConfig? config,
bool hidden = false,
}) async {
tester.view.physicalSize = size;
tester.view.devicePixelRatio = 1;
@@ -21,7 +26,11 @@ void main() {
size: size,
textScaler: TextScaler.linear(textScale),
),
child: const Scaffold(bottomNavigationBar: GetNodeBanner()),
child: Scaffold(
bottomNavigationBar: GetNodeBanner(
loadConfig: () async => hidden ? null : config ?? _config,
),
),
),
),
);
@@ -29,11 +38,11 @@ void main() {
testWidgets('shows the pitch and affiliate link', (tester) async {
await pumpBanner(tester, size: const Size(390, 844), textScale: 1);
await tester.pump();
expect(find.text('Need a new node?'), findsOneWidget);
expect(find.text('Get one here'), findsOneWidget);
expect(find.textContaining('Affiliate link'), findsOneWidget);
expect(getNodeUrl, contains('sensecap_affiliate=JFWID0g'));
// Leaves most of the screen for the device list.
expect(
tester.getSize(find.byType(GetNodeBanner)).height,
@@ -43,6 +52,38 @@ void main() {
testWidgets('lays out on a narrow phone with large text', (tester) async {
await pumpBanner(tester, size: const Size(320, 800), textScale: 1.5);
await tester.pump();
expect(tester.takeException(), isNull);
});
testWidgets('uses text overrides from the record', (tester) async {
await pumpBanner(
tester,
size: const Size(390, 844),
textScale: 1,
config: NodeBannerConfig(
url: Uri.parse('https://example.com'),
title: 'Spring sale',
action: 'Shop now',
),
);
await tester.pump();
expect(find.text('Spring sale'), findsOneWidget);
expect(find.text('Shop now'), findsOneWidget);
expect(find.textContaining('Affiliate link'), findsOneWidget);
});
testWidgets('shows nothing when there is no config', (tester) async {
await pumpBanner(
tester,
size: const Size(390, 844),
textScale: 1,
hidden: true,
);
await tester.pump();
expect(find.byType(InkWell), findsNothing);
expect(tester.getSize(find.byType(GetNodeBanner)).height, 0);
});
}