Release recipient inbox SDKs 0.1.0 preview
This commit is contained in:
commit
d0781a9f3b
23 files changed
+1846
No files matched your search
@@ -0,0 +1,113 @@
|
||||
import 'dart:convert';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:http/testing.dart';
|
||||
import 'package:wrnexus_inbox/wrnexus_inbox.dart';
|
||||
|
||||
void main() {
|
||||
test(
|
||||
'recipient transport, token refresh, writes and nullable delivery rules',
|
||||
() async {
|
||||
final calls = <http.Request>[];
|
||||
var tokens = 0;
|
||||
final client = InboxClient(
|
||||
token: () => 'recipient-${++tokens}',
|
||||
transport: MockClient((r) async {
|
||||
calls.add(r);
|
||||
if (r.method == 'GET') {
|
||||
return http.Response('{"messages":[],"unreadCount":3}', 200);
|
||||
}
|
||||
return http.Response('{"id":"d","revision":2,"replayed":false}', 200);
|
||||
}),
|
||||
);
|
||||
expect((await client.messages(limit: 20)).unreadCount, 3);
|
||||
await client.markRead('m');
|
||||
await client.archive('m');
|
||||
await client.savePreferences(
|
||||
const PreferenceInput(
|
||||
category: '*',
|
||||
inAppEnabled: true,
|
||||
marketingEnabled: false,
|
||||
deliveryRules: DeliveryRules(timezone: 'UTC'),
|
||||
),
|
||||
);
|
||||
await client.registerMobilePush(
|
||||
const MobileRegistration(
|
||||
connectionId: 'c',
|
||||
installationId: 'i',
|
||||
name: 'Phone',
|
||||
platform: MobilePlatform.android,
|
||||
token: 'fcm',
|
||||
marketingConsent: false,
|
||||
),
|
||||
);
|
||||
await client.unregisterMobilePush('d');
|
||||
await client.unregisterWebPush('w');
|
||||
expect(
|
||||
calls.map((r) => r.headers['authorization']),
|
||||
List.generate(7, (i) => 'Bearer recipient-${i + 1}'),
|
||||
);
|
||||
expect(calls.first.url.path, '/api/v1/inbox/messages');
|
||||
expect(calls.first.url.query, 'limit=20');
|
||||
expect(jsonDecode(calls[1].body), {'id': 'm', 'action': 'read'});
|
||||
expect((jsonDecode(calls[3].body) as Map)['deliveryRules'], {
|
||||
'timezone': 'UTC',
|
||||
'quietHours': null,
|
||||
'marketingDailyLimit': null,
|
||||
});
|
||||
expect((jsonDecode(calls[4].body) as Map)['action'], 'register-mobile');
|
||||
client.close();
|
||||
await expectLater(
|
||||
client.messages(),
|
||||
throwsA(isA<InboxException>().having((e) => e.code, 'code', 'closed')),
|
||||
);
|
||||
},
|
||||
);
|
||||
test('API errors stop without retry', () async {
|
||||
var calls = 0;
|
||||
final client = InboxClient(
|
||||
token: () => 'expired',
|
||||
transport: MockClient((_) async {
|
||||
calls++;
|
||||
return http.Response(
|
||||
'{"error":{"code":"expired-token","message":"Expired"}}',
|
||||
401,
|
||||
);
|
||||
}),
|
||||
);
|
||||
await expectLater(
|
||||
client.markRead('m'),
|
||||
throwsA(
|
||||
isA<InboxException>()
|
||||
.having((e) => e.status, 'status', 401)
|
||||
.having((e) => e.code, 'code', 'expired-token'),
|
||||
),
|
||||
);
|
||||
expect(calls, 1);
|
||||
client.close();
|
||||
});
|
||||
test('origin, actions and frame parsing', () {
|
||||
for (final text in [
|
||||
'http://example.com',
|
||||
'https://u:p@example.com',
|
||||
'https://example.com/a',
|
||||
'https://example.com?q=x',
|
||||
]) {
|
||||
expect(
|
||||
() => InboxClient(endpoint: Uri.parse(text), token: () => 'r'),
|
||||
throwsA(isA<InboxException>()),
|
||||
);
|
||||
}
|
||||
expect(InboxClient.safeActionUri('javascript:alert(1)'), isNull);
|
||||
expect(InboxClient.safeActionUri('https://u:p@example.com'), isNull);
|
||||
final parser = InboxEventParser();
|
||||
expect(parser.line(': heartbeat'), isNull);
|
||||
expect(parser.line(''), isNull);
|
||||
parser.line('event: inbox');
|
||||
parser.line('data: {"messages":[],"unreadCount":2}');
|
||||
expect(parser.line('')!.unreadCount, 2);
|
||||
parser.line('event: inbox');
|
||||
parser.line('data: broken');
|
||||
expect(() => parser.line(''), throwsFormatException);
|
||||
});
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,93 @@
|
||||
Copyright 2020 The Inter Project Authors (https://github.com/rsms/inter)
|
||||
|
||||
This Font Software is licensed under the SIL Open Font License, Version 1.1.
|
||||
This license is copied below, and is also available with a FAQ at:
|
||||
https://scripts.sil.org/OFL
|
||||
|
||||
|
||||
-----------------------------------------------------------
|
||||
SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
|
||||
-----------------------------------------------------------
|
||||
|
||||
PREAMBLE
|
||||
The goals of the Open Font License (OFL) are to stimulate worldwide
|
||||
development of collaborative font projects, to support the font creation
|
||||
efforts of academic and linguistic communities, and to provide a free and
|
||||
open framework in which fonts may be shared and improved in partnership
|
||||
with others.
|
||||
|
||||
The OFL allows the licensed fonts to be used, studied, modified and
|
||||
redistributed freely as long as they are not sold by themselves. The
|
||||
fonts, including any derivative works, can be bundled, embedded,
|
||||
redistributed and/or sold with any software provided that any reserved
|
||||
names are not used by derivative works. The fonts and derivatives,
|
||||
however, cannot be released under any other type of license. The
|
||||
requirement for fonts to remain under this license does not apply
|
||||
to any document created using the fonts or their derivatives.
|
||||
|
||||
DEFINITIONS
|
||||
"Font Software" refers to the set of files released by the Copyright
|
||||
Holder(s) under this license and clearly marked as such. This may
|
||||
include source files, build scripts and documentation.
|
||||
|
||||
"Reserved Font Name" refers to any names specified as such after the
|
||||
copyright statement(s).
|
||||
|
||||
"Original Version" refers to the collection of Font Software components as
|
||||
distributed by the Copyright Holder(s).
|
||||
|
||||
"Modified Version" refers to any derivative made by adding to, deleting,
|
||||
or substituting -- in part or in whole -- any of the components of the
|
||||
Original Version, by changing formats or by porting the Font Software to a
|
||||
new environment.
|
||||
|
||||
"Author" refers to any designer, engineer, programmer, technical
|
||||
writer or other person who contributed to the Font Software.
|
||||
|
||||
PERMISSION & CONDITIONS
|
||||
Permission is hereby granted, free of charge, to any person obtaining
|
||||
a copy of the Font Software, to use, study, copy, merge, embed, modify,
|
||||
redistribute, and sell modified and unmodified copies of the Font
|
||||
Software, subject to the following conditions:
|
||||
|
||||
1) Neither the Font Software nor any of its individual components,
|
||||
in Original or Modified Versions, may be sold by itself.
|
||||
|
||||
2) Original or Modified Versions of the Font Software may be bundled,
|
||||
redistributed and/or sold with any software, provided that each copy
|
||||
contains the above copyright notice and this license. These can be
|
||||
included either as stand-alone text files, human-readable headers or
|
||||
in the appropriate machine-readable metadata fields within text or
|
||||
binary files as long as those fields can be easily viewed by the user.
|
||||
|
||||
3) No Modified Version of the Font Software may use the Reserved Font
|
||||
Name(s) unless explicit written permission is granted by the corresponding
|
||||
Copyright Holder. This restriction only applies to the primary font name as
|
||||
presented to the users.
|
||||
|
||||
4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
|
||||
Software shall not be used to promote, endorse or advertise any
|
||||
Modified Version, except to acknowledge the contribution(s) of the
|
||||
Copyright Holder(s) and the Author(s) or with their explicit written
|
||||
permission.
|
||||
|
||||
5) The Font Software, modified or unmodified, in part or in whole,
|
||||
must be distributed entirely under this license, and must not be
|
||||
distributed under any other license. The requirement for fonts to
|
||||
remain under this license does not apply to any document created
|
||||
using the Font Software.
|
||||
|
||||
TERMINATION
|
||||
This license becomes null and void if any of the above conditions are
|
||||
not met.
|
||||
|
||||
DISCLAIMER
|
||||
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
|
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
|
||||
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
|
||||
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
|
||||
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
|
||||
OTHER DEALINGS IN THE FONT SOFTWARE.
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 14 KiB |
@@ -0,0 +1,206 @@
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:http/testing.dart';
|
||||
import 'package:wrnexus_inbox/wrnexus_inbox.dart';
|
||||
|
||||
class IdleStreamClient extends http.BaseClient {
|
||||
final controller = StreamController<List<int>>();
|
||||
@override
|
||||
Future<http.StreamedResponse> send(http.BaseRequest request) async =>
|
||||
http.StreamedResponse(controller.stream, 200);
|
||||
@override
|
||||
void close() {
|
||||
unawaited(controller.close());
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
setUpAll(() async {
|
||||
final loader = FontLoader('Inter');
|
||||
loader.addFont(
|
||||
Future.value(
|
||||
ByteData.sublistView(await File('test/fonts/Inter.ttf').readAsBytes()),
|
||||
),
|
||||
);
|
||||
await loader.load();
|
||||
final iconPath =
|
||||
'${Platform.environment['FLUTTER_ROOT'] ?? '../../.wrnexus/tools/flutter'}/bin/cache/artifacts/material_fonts/MaterialIcons-Regular.otf';
|
||||
final icons = FontLoader('MaterialIcons');
|
||||
icons.addFont(
|
||||
Future.value(ByteData.sublistView(await File(iconPath).readAsBytes())),
|
||||
);
|
||||
await icons.load();
|
||||
});
|
||||
testWidgets('branding, actions, read/archive and recipient change', (
|
||||
tester,
|
||||
) async {
|
||||
var read = false, archived = false;
|
||||
final writes = <Map<String, dynamic>>[];
|
||||
final streams = <IdleStreamClient>[];
|
||||
final client = InboxClient(
|
||||
token: () => 'recipient',
|
||||
streamTransport: () {
|
||||
final c = IdleStreamClient();
|
||||
streams.add(c);
|
||||
return c;
|
||||
},
|
||||
transport: MockClient((r) async {
|
||||
if (r.method == 'POST') {
|
||||
final body = jsonDecode(r.body) as Map<String, dynamic>;
|
||||
writes.add(body);
|
||||
read = body['action'] == 'read' || read;
|
||||
archived = body['action'] == 'archive' || archived;
|
||||
return http.Response('{"id":"m"}', 200);
|
||||
}
|
||||
if (r.url.path.endsWith('preferences')) {
|
||||
return http.Response(
|
||||
jsonEncode({
|
||||
'branding': {'displayName': 'Acme', 'brandColor': '#123456'},
|
||||
'deliveryRules': {
|
||||
'timezone': 'UTC',
|
||||
'quietHours': null,
|
||||
'marketingDailyLimit': null,
|
||||
},
|
||||
'preferences': [],
|
||||
'channelPreferences': [],
|
||||
}),
|
||||
200,
|
||||
);
|
||||
}
|
||||
return http.Response(
|
||||
jsonEncode({
|
||||
'messages': archived
|
||||
? []
|
||||
: [
|
||||
{
|
||||
'id': 'm',
|
||||
'recipient_id': 'r',
|
||||
'title': 'Order ready',
|
||||
'body': 'Your order is ready for collection.',
|
||||
'purpose': 'transactional',
|
||||
'category': 'orders',
|
||||
'created_at': '2026-10-09T00:00:00Z',
|
||||
'read_at': read ? '2026-10-09' : null,
|
||||
'archived_at': null,
|
||||
'url': 'javascript:alert(1)',
|
||||
'actions': [
|
||||
{'id': 'track', 'label': 'Track order'},
|
||||
],
|
||||
},
|
||||
],
|
||||
'unreadCount': read || archived ? 0 : 1,
|
||||
}),
|
||||
200,
|
||||
);
|
||||
}),
|
||||
);
|
||||
final actions = <String>[];
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
theme: ThemeData(
|
||||
fontFamily: 'Inter',
|
||||
scaffoldBackgroundColor: Colors.white,
|
||||
),
|
||||
home: Scaffold(
|
||||
body: RepaintBoundary(
|
||||
key: const Key('inbox'),
|
||||
child: ColoredBox(
|
||||
color: Colors.white,
|
||||
child: WRNexusInboxView(
|
||||
client: client,
|
||||
onAction: (m, a, u) => actions.add(a!.id),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.text('Acme'), findsOneWidget);
|
||||
expect(find.text('Order ready'), findsOneWidget);
|
||||
expect(find.text('Open'), findsNothing);
|
||||
await tester.tap(find.text('Track order'));
|
||||
expect(actions, ['track']);
|
||||
await expectLater(
|
||||
find.byKey(const Key('inbox')),
|
||||
matchesGoldenFile('goldens/branded-inbox.png'),
|
||||
);
|
||||
await tester.tap(find.text('Mark read'));
|
||||
await tester.pumpAndSettle();
|
||||
expect(writes.last, {'id': 'm', 'action': 'read'});
|
||||
expect(find.text('Mark read'), findsNothing);
|
||||
await tester.tap(find.text('Archive'));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.text('No notifications yet'), findsOneWidget);
|
||||
await tester.pumpWidget(const SizedBox());
|
||||
client.close();
|
||||
expect(streams.every((s) => s.controller.isClosed), isTrue);
|
||||
});
|
||||
testWidgets(
|
||||
'replacing the client clears the previous recipient before the new request resolves',
|
||||
(tester) async {
|
||||
final pending = Completer<http.Response>();
|
||||
String settings(String name) => jsonEncode({
|
||||
'branding': {'displayName': name},
|
||||
'deliveryRules': {'timezone': 'UTC'},
|
||||
'preferences': [],
|
||||
'channelPreferences': [],
|
||||
});
|
||||
InboxClient make(String name, {bool wait = false}) => InboxClient(
|
||||
token: () => name,
|
||||
streamTransport: IdleStreamClient.new,
|
||||
transport: MockClient((request) async {
|
||||
if (request.url.path.endsWith('preferences')) {
|
||||
return wait ? pending.future : http.Response(settings(name), 200);
|
||||
}
|
||||
return http.Response(
|
||||
jsonEncode({
|
||||
'messages': [
|
||||
{
|
||||
'id': name,
|
||||
'recipient_id': name,
|
||||
'title': '$name message',
|
||||
'body': 'Private content',
|
||||
'purpose': 'transactional',
|
||||
'category': 'orders',
|
||||
'created_at': '2026-10-09',
|
||||
},
|
||||
],
|
||||
'unreadCount': 1,
|
||||
}),
|
||||
200,
|
||||
);
|
||||
}),
|
||||
);
|
||||
final first = make('First');
|
||||
final second = make('Second', wait: true);
|
||||
Widget app(InboxClient client) => MaterialApp(
|
||||
home: Scaffold(
|
||||
body: WRNexusInboxView(
|
||||
key: const ValueKey('same-view'),
|
||||
client: client,
|
||||
),
|
||||
),
|
||||
);
|
||||
await tester.pumpWidget(app(first));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.text('First message'), findsOneWidget);
|
||||
await tester.pumpWidget(app(second));
|
||||
await tester.pump();
|
||||
expect(find.text('First message'), findsNothing);
|
||||
expect(find.text('First'), findsNothing);
|
||||
pending.complete(http.Response(settings('Second'), 200));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.text('Second message'), findsOneWidget);
|
||||
expect(find.text('First message'), findsNothing);
|
||||
await tester.pumpWidget(const SizedBox());
|
||||
first.close();
|
||||
second.close();
|
||||
},
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user