Skip to content

Introduce TimeProvider for testable time-dependent behavior#22

Merged
jeanlrnt merged 5 commits intomainfrom
copilot/add-timeprovider-constructor
Dec 22, 2025
Merged

Introduce TimeProvider for testable time-dependent behavior#22
jeanlrnt merged 5 commits intomainfrom
copilot/add-timeprovider-constructor

Conversation

Copy link
Contributor

Copilot AI commented Dec 22, 2025

  • Add Microsoft.Bcl.TimeProvider package reference to MiniJwt.Core.csproj for .NET 6 and 7
  • Add private readonly TimeProvider field to MiniJwtService
  • Create new constructor accepting TimeProvider parameter (primary constructor with implementation)
  • Modify existing constructor to chain to new constructor with TimeProvider.System
  • Replace DateTime.UtcNow with _timeProvider.GetUtcNow().UtcDateTime on line 112
  • Add Microsoft.Extensions.TimeProvider.Testing package to test project
  • Update tests to use FakeTimeProvider for time-sensitive tests
  • Update documentation (README.md) to mention TimeProvider support for testing
  • Update examples.md with TimeProvider testing examples using correct API
  • Update faq.md to reference TimeProvider testing
  • Build and test all changes
Original prompt

This section details on the original issue you should resolve

<issue_title>Introduire TimeProvider (standard .NET 8, disponible via package Microsoft.Bcl.TimeProvider pour les vieilles versions).</issue_title>
<issue_description>Ajouter un constructeur qui accepte TimeProvider, et garder l'ancien constructeur qui appelle le nouveau avec TimeProvider.System.


private readonly TimeProvider _timeProvider;

// Constructeur existant (modifié pour chain)
public MiniJwtService(IOptionsMonitor<MiniJwtOptions> optionsMonitor, ILogger<MiniJwtService> logger, JwtSecurityTokenHandler tokenHandler) 
    : this(optionsMonitor, logger, tokenHandler, TimeProvider.System)
{
}

// Nouveau constructeur (optionnel pour l'utilisateur, vital pour tes tests)
public MiniJwtService(IOptionsMonitor<MiniJwtOptions> optionsMonitor, ILogger<MiniJwtService> logger, JwtSecurityTokenHandler tokenHandler, TimeProvider timeProvider)
{
    _timeProvider = timeProvider;
    // ... reste de l'init
}

// Utilisation ligne 394
var now = _timeProvider.GetUtcNow().UtcDateTime;

Appliquer ces modifications dans les tests et dans la documentation</issue_description>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits December 22, 2025 15:11
Co-authored-by: jeanlrnt <63308635+jeanlrnt@users.noreply.github.com>
Co-authored-by: jeanlrnt <63308635+jeanlrnt@users.noreply.github.com>
Copilot AI changed the title [WIP] Add constructor for TimeProvider in MiniJwtService Introduce TimeProvider for testable time-dependent behavior Dec 22, 2025
Copilot AI requested a review from jeanlrnt December 22, 2025 15:16
@jeanlrnt jeanlrnt marked this pull request as ready for review December 22, 2025 15:59
Copilot AI review requested due to automatic review settings December 22, 2025 15:59
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces TimeProvider support to enable deterministic testing of JWT token generation timestamps. The implementation adds a new optional constructor parameter that accepts a TimeProvider, with backward compatibility maintained through constructor chaining that defaults to TimeProvider.System.

Key Changes

  • Added TimeProvider field and constructor overload to MiniJwtService for testable time-dependent behavior
  • Replaced DateTime.UtcNow with _timeProvider.GetUtcNow().UtcDateTime in token generation
  • Added conditional package references for Microsoft.Bcl.TimeProvider (8.0.1) for .NET 6/7 targets and Microsoft.Extensions.TimeProvider.Testing (8.10.0) for tests

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/MiniJwt.Core/Services/MiniJwtService.cs Adds TimeProvider field, new constructor accepting TimeProvider, and updates token generation to use injected time provider
src/MiniJwt.Core/MiniJwt.Core.csproj Adds conditional package reference to Microsoft.Bcl.TimeProvider for .NET 6/7 targets
src/MiniJwt.Tests/MiniJwtTests.Service.cs Adds two new tests demonstrating FakeTimeProvider usage for fixed-time and time-advancement scenarios
src/MiniJwt.Tests/MiniJwt.Tests.csproj Adds Microsoft.Extensions.TimeProvider.Testing package reference
docs/examples.md Adds new testing section with FakeTimeProvider usage example
docs/faq.md Updates testing guidance to mention FakeTimeProvider
README.md Adds new "Testing with TimeProvider" section with usage example

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
…mpleOptionsMonitor helper

Co-authored-by: jeanlrnt <63308635+jeanlrnt@users.noreply.github.com>
@jeanlrnt jeanlrnt changed the base branch from main to develop December 22, 2025 17:08
@jeanlrnt jeanlrnt changed the base branch from develop to main December 22, 2025 17:09
@jeanlrnt jeanlrnt merged commit def4bc1 into main Dec 22, 2025
15 checks passed
@jeanlrnt jeanlrnt deleted the copilot/add-timeprovider-constructor branch December 22, 2025 17:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Introduire TimeProvider (standard .NET 8, disponible via package Microsoft.Bcl.TimeProvider pour les vieilles versions).

2 participants

Comments