@@ -20,22 +20,26 @@ void main() {
2020 late CommonDatabase raw;
2121 late PowerSyncDatabase database;
2222 late MockSyncService syncService;
23- late StreamingSyncImplementation syncClient;
23+ late StreamingSync syncClient;
24+ var credentialsCallbackCount = 0 ;
2425
2526 setUp (() async {
27+ credentialsCallbackCount = 0 ;
2628 final (client, server) = inMemoryServer ();
2729 syncService = MockSyncService ();
2830 server.mount (syncService.router.call);
2931
3032 factory = await testUtils.testFactory ();
3133 (raw, database) = await factory .openInMemoryDatabase ();
3234 await database.initialize ();
35+
3336 syncClient = database.connectWithMockService (
3437 client,
3538 TestConnector (() async {
39+ credentialsCallbackCount++ ;
3640 return PowerSyncCredentials (
3741 endpoint: server.url.toString (),
38- token: 'token not used here ' ,
42+ token: 'token$ credentialsCallbackCount ' ,
3943 expiresAt: DateTime .now (),
4044 );
4145 }),
@@ -312,18 +316,37 @@ void main() {
312316 },
313317 );
314318 });
319+
320+ test ('reconnects when token expires' , () async {
321+ await waitForConnection ();
322+ expect (credentialsCallbackCount, 1 );
323+ // When the sync service says the token has expired
324+ syncService
325+ ..addLine ({'token_expires_in' : 0 })
326+ ..endCurrentListener ();
327+
328+ final nextRequest = await syncService.waitForListener;
329+ expect (nextRequest.headers['Authorization' ], 'Token token2' );
330+ expect (credentialsCallbackCount, 2 );
331+ });
315332 });
316333}
317334
318335TypeMatcher <SyncStatus > isSyncStatus (
319- {Object ? downloading, Object ? connected, Object ? hasSynced}) {
336+ {Object ? downloading,
337+ Object ? connected,
338+ Object ? connecting,
339+ Object ? hasSynced}) {
320340 var matcher = isA <SyncStatus >();
321341 if (downloading != null ) {
322342 matcher = matcher.having ((e) => e.downloading, 'downloading' , downloading);
323343 }
324344 if (connected != null ) {
325345 matcher = matcher.having ((e) => e.connected, 'connected' , connected);
326346 }
347+ if (connecting != null ) {
348+ matcher = matcher.having ((e) => e.connecting, 'connecting' , connecting);
349+ }
327350 if (hasSynced != null ) {
328351 matcher = matcher.having ((e) => e.hasSynced, 'hasSynced' , hasSynced);
329352 }
0 commit comments