Fix the token expiry check with an injected clock

Review the AI's codeEasy10 min

Your task

An assistant's tests for a token generator slept for 2 seconds and read the real clock, so they were slow and never pinned the expiry boundary. The replacement takes the time as an input, so a test can check any second it likes.

An assistant wrote token_expiry_states(issued_at, ttl_seconds, check_times). It is in the editor. It has a bug. Find it and fix it.

Input and output:

  • issued_at: the second the token was issued, an integer. It can be 0 or negative.
  • ttl_seconds: how many seconds the token lives, an integer. It can be 0 or negative.
  • check_times: the seconds to check, integers in any order. The list can be empty.
  • Return one state per check time, in the same order: "not_yet_issued", "valid" or "expired".

Rules the tests check:

  • The token expires at issued_at + ttl_seconds.
  • A time before issued_at is "not_yet_issued". This happens when two machines' clocks disagree. It wins over every other rule, even when the ttl is 0 or negative.
  • Otherwise, a time at or after the expiry is "expired". The token is already expired at exactly the expiry second.
  • Otherwise, the time is "valid".
  • So with a ttl of 0 or less, the token is never valid: it is expired from issued_at on.

Examples

token_expiry_states(issued_at: int, ttl_seconds: int, check_times: list[int]) -> list[str]
Example 1
Inputtoken_expiry_states(1000, 3600, [1000, 4599, 4600, 4601])
Output["valid", "valid", "expired", "expired"]
Example 2
Inputtoken_expiry_states(0, 1, [0, 1, 2])
Output["valid", "expired", "expired"]
Example 3
Inputtoken_expiry_states(1000, 3600, [])
Output[]