summaryrefslogtreecommitdiff
path: root/player-android/test/screens/api_tokens_screen_test.dart
diff options
context:
space:
mode:
Diffstat (limited to 'player-android/test/screens/api_tokens_screen_test.dart')
-rw-r--r--player-android/test/screens/api_tokens_screen_test.dart96
1 files changed, 88 insertions, 8 deletions
diff --git a/player-android/test/screens/api_tokens_screen_test.dart b/player-android/test/screens/api_tokens_screen_test.dart
index 8d61ebc..913a921 100644
--- a/player-android/test/screens/api_tokens_screen_test.dart
+++ b/player-android/test/screens/api_tokens_screen_test.dart
@@ -11,14 +11,16 @@
// 8. Create dialog: cancel closes without calling createAPIToken.
// 9. Create dialog: validation — empty name is rejected.
// 10. Create dialog: submits and shows the plaintext token dialog.
-// 11. Plaintext dialog: copy button writes token to clipboard.
-// 12. Plaintext dialog: Done dismisses the dialog.
-// 13. Create optimistic UI: placeholder visible while in flight, replaced on success.
-// 14. Create optimistic UI: placeholder reverted and error SnackBar shown on failure.
-// 15. Empty state: shown when listAPITokens returns [].
-// 16. Error state: shown when listAPITokens throws.
-// 17. Retry button re-calls listAPITokens after an error.
-// 18. apiTokenErrorMessage unit tests (400, 404, connection, generic).
+// 11. Create dialog: submits with null expiresInDays when no expiry date selected.
+// 12. Plaintext dialog: copy button writes token to clipboard.
+// 13. Plaintext dialog: Done dismisses the dialog.
+// 14. Create optimistic UI: placeholder visible while in flight, replaced on success.
+// 15. Create optimistic UI: placeholder reverted and error SnackBar shown on failure.
+// 16. Empty state: shown when listAPITokens returns [].
+// 17. Error state: shown when listAPITokens throws.
+// 18. Retry button re-calls listAPITokens after an error.
+// 19. expiresInDays computation: correct day count, clamp min 1, clamp max 36500.
+// 20. apiTokenErrorMessage unit tests (400, 404, connection, generic).
//
// Riverpod providers are overridden with fakes so tests run without a real
// server or OS keychain.
@@ -460,6 +462,84 @@ void main() {
expect(fakeClient.createdName, equals('my-token'));
});
+
+ testWidgets('submits with null expiresInDays when no expiry date selected',
+ (tester) async {
+ // Verifies that submitting the create dialog without picking an expiry
+ // date passes null for expiresInDays so the server creates a non-expiring
+ // token.
+ final fakeClient = _FakeApiClient()
+ ..tokensResult = []
+ ..createResult = {
+ 'id': 50,
+ 'name': 'no-expiry-token',
+ 'token': 'plaintext-no-expiry',
+ 'created_at': '2026-05-22T12:00:00Z',
+ };
+
+ await _pumpApiTokensScreen(tester, fakeClient);
+ await tester.pumpAndSettle();
+
+ await tester.tap(find.byKey(const Key('api_tokens_fab')));
+ await tester.pumpAndSettle();
+
+ // Enter a name but do NOT tap the expiry tile.
+ await tester.enterText(
+ find.byKey(const Key('api_tokens_create_name')),
+ 'no-expiry-token',
+ );
+ await tester.tap(find.byKey(const Key('api_tokens_create_submit')));
+ await tester.pumpAndSettle();
+
+ // No expiry date → expiresInDays must be null.
+ expect(fakeClient.createdExpiresInDays, isNull);
+ });
+ });
+
+ // --------------------------------------------------------------------------
+ // expiresInDays computation
+ // --------------------------------------------------------------------------
+
+ group('expiresInDays computation', () {
+ // Unit tests for the clamp logic inside _CreateTokenDialogState._submit.
+ // Since _CreateTokenDialogState is private, the logic is tested
+ // indirectly by reproducing the same arithmetic used in _submit:
+ // diff = expiryDate.difference(today);
+ // expiresInDays = diff.inDays.clamp(1, 36500);
+ //
+ // This covers the positive path (expiresInDays computed correctly) and
+ // the clamp bounds, without requiring date-picker interaction.
+
+ test('computes correct expiresInDays for a date N days in the future', () {
+ final now = DateTime.now();
+ const targetDays = 30;
+ final expiryDate = now.add(const Duration(days: targetDays));
+ final today = DateTime(now.year, now.month, now.day);
+ final diff = expiryDate.difference(today);
+ final expiresInDays = diff.inDays.clamp(1, 36500);
+ // Depending on whether the test runs just before midnight, diff.inDays
+ // may be 30 or 31; clamp keeps it within [1, 36500].
+ expect(expiresInDays, greaterThanOrEqualTo(targetDays));
+ expect(expiresInDays, lessThanOrEqualTo(targetDays + 1));
+ });
+
+ test('clamps expiresInDays to minimum 1 for same-day date', () {
+ final now = DateTime.now();
+ final today = DateTime(now.year, now.month, now.day);
+ // expiryDate == today gives diff.inDays == 0, which clamps to 1.
+ final diff = today.difference(today);
+ final expiresInDays = diff.inDays.clamp(1, 36500);
+ expect(expiresInDays, equals(1));
+ });
+
+ test('clamps expiresInDays to maximum 36500 for distant future date', () {
+ final now = DateTime.now();
+ final today = DateTime(now.year, now.month, now.day);
+ final farFuture = today.add(const Duration(days: 100000));
+ final diff = farFuture.difference(today);
+ final expiresInDays = diff.inDays.clamp(1, 36500);
+ expect(expiresInDays, equals(36500));
+ });
});
// --------------------------------------------------------------------------