Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(app-project): run YourStats tests in a non-UTC timezone #6412

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

eatyourgreens
Copy link
Contributor

@eatyourgreens eatyourgreens commented Oct 29, 2024

A couple of small refactors for the YourStats tests:

  • add missing timestamps to the mock ERAS data, to match real ERAS responses.
  • test the weekly stats in the -05:00 timezone, to check for timezone bugs. this doesn’t work. Tests always run in the OS time zone.
  • run the tests in the -06:00:00 timezone by setting the Node TZ env var, to catch Daily count stats bug #2244.
  • use getUTCDay to get the day number for daily stats, fixing Daily count stats bug #2244.

Please request review from @zooniverse/frontend team or an individual member of that team.

Package

  • app-project

Linked Issue and/or Talk Post

How to Review

I'm not seeing any errors when I run this, but I'm running it on a computer that's set to UTC. I think it might need to be run in a different time zone, in order to test it. Whatever timezone it's run in, the stats chart should always start on Monday.

Checklist

PR Creator - Please cater the checklist to fit the review needed for your code changes.
PR Reviewer - Use the checklist during your review. Each point should be checkmarked or discussed before PR approval.

General

  • Tests are passing locally and on Github
  • Documentation is up to date and changelog has been updated if appropriate
  • You can yarn panic && yarn bootstrap or docker-compose up --build and FEM works as expected
  • FEM works in all major desktop browsers: Firefox, Chrome, Edge, Safari (Use Browserstack account as needed)
  • FEM works in a mobile browser

@coveralls
Copy link

coveralls commented Oct 29, 2024

Coverage Status

coverage: 77.651% (+0.02%) from 77.635%
when pulling 2ba3612 on eatyourgreens:test-your-stats
into 860f09f on zooniverse:master.

goplayoutside3
goplayoutside3 previously approved these changes Oct 29, 2024
@goplayoutside3 goplayoutside3 dismissed their stale review October 29, 2024 13:55

Tests don't pass locally

@goplayoutside3
Copy link
Contributor

I read through the code and approved, but then I ran the tests locally on my local machine which is set to US Central Time. These tests don't pass

@goplayoutside3
Copy link
Contributor

I'm not seeing any errors when I run this, but I'm running it on a computer that's set to UTC

You can test this by changing your local machine's time settings. I just set mine to Monday Oct 28 UTC and the tests pass locally. They do not pass when mine is set to today's date and Chicago time.

@eatyourgreens
Copy link
Contributor Author

I read through the code and approved, but then I ran the tests locally on my local machine which is set to US Central Time. These tests don't pass.

Yep, these tests catch the bug that you found in #2244, which doesn't trigger when you run the code in a UTC time zone.

@goplayoutside3
Copy link
Contributor

goplayoutside3 commented Oct 29, 2024

See #2244 (comment). Reviewing this PR is on hold until design decisions are made about YourStats.

@eatyourgreens
Copy link
Contributor Author

eatyourgreens commented Oct 29, 2024

@goplayoutside3 it works now when it's run in CDT, but there doesn't seem to be an easy way to check that in CI.

EDIT: This version passes in Guam, Kuala Lumpur, London, and Chicago.

@@ -74,7 +80,7 @@ const YourStats = types
weekDay.setUTCDate(newDate)
const period = weekDay.toISOString().substring(0, 10)
const { count } = dailyCounts.find(count => count.period.startsWith(period)) || { count: 0, period }
const dayNumber = weekDay.getDay()
const dayNumber = weekDay.getUTCDay()
Copy link
Contributor Author

@eatyourgreens eatyourgreens Oct 29, 2024

Choose a reason for hiding this comment

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

This was causing the weekday bug in #2244, but it only triggers when you run getDay() in a time zone other than UTC. Otherwise, getDay() is identical to getUTCDay().

Copy link
Contributor Author

Choose a reason for hiding this comment

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

See #2528 for some context on why the code is getting the weekday here.

clock = sinon.useFakeTimers({ now: new Date(2019, 9, 1, 12), toFake: ['Date'] })
// Set the local clock to 1am on Tuesday 1 October 2019, UTC.
// Stats should be shown for Monday 30 September 2019, local time.
clock = sinon.useFakeTimers(new Date('2019-09-30T20:00:00-05:00'))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This won't set the clock timezone to -05:00. new Date() always runs in the time zone of the machine that's running the code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Methods like date.getDay() always return values in the OS time zone too.

@eatyourgreens
Copy link
Contributor Author

This is useful. How to run Node tests in different time zones, by setting the TZ environment variable at runtime.

https://www.stefanjudis.com/today-i-learned/set-the-default-time-zone-in-node-js/

@eatyourgreens eatyourgreens changed the title test(app-project): refactor YourStats tests to a non-UTC timezone test(app-project): run YourStats tests in a non-UTC timezone Nov 5, 2024
@eatyourgreens eatyourgreens force-pushed the test-your-stats branch 2 times, most recently from b5f0770 to 55d4d50 Compare November 5, 2024 08:56
@eatyourgreens
Copy link
Contributor Author

I've updated this to catch #2244 by running the tests in the -06:00:00 time zone. CI tests now catch the bug and fail.
https://github.com/zooniverse/front-end-monorepo/actions/runs/11680689280/job/32524223518?pr=6412

I've then updated the code to pass the tests. Test-driven development FTW!

Comment on lines +23 to +30
{ count: 12, period: '2019-09-29T00:00:00.000Z' },
{ count: 12, period: '2019-09-30T00:00:00.000Z' },
{ count: 13, period: '2019-10-01T00:00:00.000Z' },
{ count: 14, period: '2019-10-02T00:00:00.000Z' },
{ count: 10, period: '2019-10-03T00:00:00.000Z' },
{ count: 11, period: '2019-10-04T00:00:00.000Z' },
{ count: 8, period: '2019-10-05T00:00:00.000Z' },
{ count: 15, period: '2019-10-06T00:00:00.000Z' }
Copy link
Contributor Author

@eatyourgreens eatyourgreens Nov 5, 2024

Choose a reason for hiding this comment

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

These mocks should match the API responses for real ERAS data, which uses midnight UTC to mark the start of each day.

@@ -17,7 +17,7 @@ function DailyClassificationsChartContainer({
const [year, monthIndex, date] = period.split('-')
const utcDay = Date.UTC(year, monthIndex - 1, date)
const day = new Date(utcDay)
const isToday = day.getUTCDay() === TODAY.getDay()
const isToday = day.getUTCDay() === TODAY.getUTCDay()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

"Today" is always the current day in Greenwich.

A couple of small refactors for the `YourStats` tests:
- add missing timestamps to the mock ERAS data, to match real ERAS responses.
- test the weekly stats in the `-05:00` timezone, to check for timezone bugs.
@goplayoutside3
Copy link
Contributor

Just a heads up that this is superseded by #6472

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.

3 participants