Skip to content

Cdd 3363 fe bug logout warning countdown drifts from real session when tab is inactive#986

Open
dandammann wants to merge 5 commits into
mainfrom
CDD-3363-FE-Bug-LogoutWarning-countdown-drifts-from-real-session-when-tab-is-inactive
Open

Cdd 3363 fe bug logout warning countdown drifts from real session when tab is inactive#986
dandammann wants to merge 5 commits into
mainfrom
CDD-3363-FE-Bug-LogoutWarning-countdown-drifts-from-real-session-when-tab-is-inactive

Conversation

@dandammann

Copy link
Copy Markdown
Contributor

Description

Fixes #CDD-2469

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • [ ] New feature (non-breaking change which adds functionality)
  • [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • [ ] This change requires a documentation update

How Has This Been Tested?

  • Unit tests
  • Manually re-tested the three unit tests I have written
  • [ ] Playwright e2e tests
  • [ ] Mobile responsiveness
  • [ ] Accessibility (i.e. Lighthouse audit)
  • [ ] Disabled JavaScript

Checklist:

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • Any styles in this change follow the 'STYLES.md' guide
  • My changes are progressively enhanced with graceful degredagation for older browsers and non-JavaScript users
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

@dandammann dandammann requested a review from a team as a code owner June 22, 2026 16:50
@github-actions

github-actions Bot commented Jun 22, 2026

Copy link
Copy Markdown

Unit tests coverage

Lines Statements Branches Functions
Coverage: 96%
94.82% (4634/4887) 84.6% (1500/1773) 95.29% (850/892)
Tests Skipped Failures Errors Time
1732 0 💤 0 ❌ 0 🔥 29.516s ⏱️

@dw-ukhsa dw-ukhsa left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Looks good overall and the wall-clock approach makes sense. I've left one comment where I think the tab recovery path can still extend the session beyond timeoutMinutes.

}
}

window.addEventListener('visibilitychange', onVisibilityChange)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Should this be on document instead of window? visibilitychnage fires on document?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good spot. Fixed.

Worked for me before, but probably wasn't browser-compatible.

}, [clearTimers])

// Start the countdown when the warning is triggered
const startCountdown = useCallback(() => {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Won't this restart the full warning period when the tab becomes visible again? E.g, with timeoutMinutes = 4 and warningMinutes = 2, if lastActivity was 00:00 and the user returns at 03:00, this calls startCountdown(), which sets logoutAtRef to 03:00 + 2 mins = 05:00. I'd expect logout to still happen at lastActivity _timeoutMinutes, i.e 04:00 with the modal showing 01:00

@dandammann dandammann Jun 25, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Another good spot. Fixed.

startCountdown() gets called from different places, so I am passing an optional argument now to get round this.

// jest.setSystemTime moves Date.now() forward WITHOUT firing any timers,
// just like a real browser suspending JavaScript in a hidden tab.
// The visibilitychange then simulates the user returning.
const switchAwayFromTabThenReturn = (awayInMicroSeconds: number) => {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This looks to be in milliseconds not microseconds. Maybe rename it to awayInMilliseconds?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ha ha, yes, of course.

@sonarqubecloud

Copy link
Copy Markdown

@dandammann dandammann requested a review from dw-ukhsa June 25, 2026 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.

2 participants