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

🐛 Fix AppConfig survey_info backwards compat + add tests for dynamicConfig #1115

Conversation

JGreenlee
Copy link
Collaborator

Fixes e-mission/e-mission-docs#1025

I reproduced the issue in the devapp by logging into dev-emulator-study, clearing localStorage and user cache, then manually storing into the usercache an app config that was missing survey_info.

Here is what that caused:


Immediately after applying 8c31b65 and waiting for autoreload, it was fixed:

While looking at dynamicConfig.ts I realized `labelVars` should be optional - if the `labelTemplate` doesn't reference any vars, `labelVars` can be omitted.
The fix is in getConfig(), lines 257 and 265 - _backwardsCompatSurveyFill needed to be applied there.

But while I was here, something else I noticed is that these functions mutate the config object parameter, which means they are not pure functions. For best practice, I changed them so they return a new object with the change rather than mutating their inputs.

While I was doing that, I applied the AppConfig type definition we have declared in types/appConfigTypes.ts.
And I cleaned up the log statements in readConfigFromServer()
@JGreenlee JGreenlee changed the title Fix AppConfig survey_info backwards compat 🐛 Fix AppConfig survey_info backwards compat Dec 8, 2023
Copy link

codecov bot commented Dec 8, 2023

Codecov Report

Merging #1115 (63732f0) into service_rewrite_2023 (1006165) will increase coverage by 6.46%.
Report is 43 commits behind head on service_rewrite_2023.
The diff coverage is 88.67%.

Additional details and impacted files
@@                   Coverage Diff                    @@
##           service_rewrite_2023    #1115      +/-   ##
========================================================
+ Coverage                 58.80%   65.26%   +6.46%     
========================================================
  Files                        26       26              
  Lines                      1420     1428       +8     
  Branches                    320      318       -2     
========================================================
+ Hits                        835      932      +97     
+ Misses                      585      496      -89     
Flag Coverage Δ
unit 65.26% <88.67%> (+6.46%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Coverage Δ
www/js/services/commHelper.ts 25.00% <100.00%> (+1.27%) ⬆️
www/js/config/dynamicConfig.ts 84.67% <86.66%> (+66.35%) ⬆️

... and 1 file with indirect coverage changes

@JGreenlee JGreenlee marked this pull request as draft December 9, 2023 02:05
@JGreenlee
Copy link
Collaborator Author

JGreenlee commented Dec 9, 2023

I didn't do anything for tests in this PR because it does not have any breaking changes for tests. But as I've realized, we don't even have tests yet for dynamicConfig.ts (it was written before we had Jest in place).

So, I'm marking this as a draft and will add tests before I mark it as ready again.

To be more consistent with the rest of the codebase, as a rule of thumb we're having top-level functions use 'function' synax while one-liners or nested functions are arrow functions.

Also, resetStoredConfig was renamed to _test_resetStoredConfig because that change is coming anyway in e-mission#1113 and this will make it easier to resolve merge conflicts
This will allow us to test how configs get stored (and then retrieved)
Instead of applying the backwards compat to fill 'survey_info' and 'name' by 2 function calls every time it needs to be done, let's have one function that does both
The dynamic config tests will want to actually test how the config is stored, so we don't want this to always fallback to some default 'fakeConfig'. Before the config gets stored we'll want to be able to tell if it's missing or not.

We'll remove that fallback, and any test that *does* want to use that 'fakeConfig' can pass it as an argument to the mockBEMUserCache() function
Tests for the biggest functions: getConfig and initByUser
Going to check coverage and see whether it's beneficial to test the smaller (helper) functions too
- test initByUser and getConfig using stub configs for 'nrel-commute' (which is old-style / does not have subgroups) and 'denver-casr' (which is new style with subgroups)
- check that the 'joined' field gets filled in
- isolate tests from one another by using await properly on async tests and by clearing storage in between test runs
When we split the string by '_', the resulting parts could be empty strings if the opcode is something like `nrelop_nrel-commute_`, or `nrelop__user1`. We should throw an 'invalid opcode' error in this case
@JGreenlee JGreenlee changed the title 🐛 Fix AppConfig survey_info backwards compat 🐛 Fix AppConfig survey_info backwards compat + add tests for dynamicConfig Dec 15, 2023
@JGreenlee JGreenlee marked this pull request as ready for review December 15, 2023 18:55

mockBEMUserCache();
mockBEMUserCache(fakeConfig);
Copy link
Contributor

Choose a reason for hiding this comment

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

This is not your change, but I would like to understand this better. Why do we pass in the config instead of calling putRWDocument

Comment on lines +308 to +309
storedConfig = _backwardsCompatFill(config);
return storedConfig;
Copy link
Contributor

Choose a reason for hiding this comment

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

I know we didn't do this before, but a future enhancement might be to also save the config after backwards filling it. That would ensure that we could eventually remove the backwards compat fill (or turn it into a NOP) instead of having it grow over time.

@shankari shankari merged commit 8f85e05 into e-mission:service_rewrite_2023 Jan 20, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Address code reviews with "future"
Development

Successfully merging this pull request may close these issues.

2 participants