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

Add tests for static_react_task example #795

Merged
merged 32 commits into from
Jul 8, 2022
Merged

Conversation

Etesam913
Copy link
Contributor

@Etesam913 Etesam913 commented Jun 16, 2022

Summary

Tests

Configured cypress with the static_react_task webapp.
Added tests that test if:

  • a request is made to the "/request_agent" endpoint
  • the correct react elements are displayed
  • a request is made to the "/submit_task" endpoint after clicking the green button
  • a request is made to the "/submit_task" endpoint after clicking the red button
  • an alert shows up with the correct text after clicking the green button
  • an alert shows up with the correct text after clicking the red button

GitHub Actions

Added a github action workflow named, cypress-e2e-tests which runs the cypress tests as described above.

Linking (RESOLVED)

To make sure that tests on a task adapt when doing local package development, the example task's webapp should be linked to the package.

This can easily be done by writing something like this in the package.json :

"mephisto-task": "file: ../../../packages/mephisto-task",

However, this is not ideal as an individual would have to go to mephisto-task run npm link, and then go to the examples task's webapp folder and run npm link mephisto-task, and then cd out and type python run_task. This is clearly many extra steps.

The best of both worlds would allow someone who is just running the task without doing any local package development work to avoid having to do all the npm linking. This intuitively should work by the following steps:

  1. Setting the following in the package.json: "mephisto-task": "^2.0.1",

  2. running npm link mephisto-task in the webapp folder

  3. Running python run_task.py in the root folder for the task.

However, this is not possible as running python run_task.py runs npm install in the webapp folder (done under the hood), which undoes the link and installs mephisto-task 2.01 from the package.json.

To resolve this issue for local development a new command line arg is defined.

Example:

Step 1:

cd packages/mephisto-task
npm link

Step 2:

cd examples/static_react_task
python run_task.py mephisto.task.package_to_link_to=mephisto-task

This works as this command line arg is read by the program and runs npm link mephisto-task after the npm install to get the local version of the package.

Removing the mephisto.task.package_to_link_to command line arg skips the npm link command. This means that the version of the package as specified in the package.json is used.

This achieves the best of both worlds, easy running of a task when doing no local development, and local development without having to tinker with the package.json by adding a weird file path.

Downsides

https://github.com/facebookresearch/Mephisto/pull/795/files/ac59bbdcde1c861f892d32485aaee87b2c7db8bc#r901965814

Testing

static_react_task_test.mov

To see the GitHub action output you can look at the "cypress-e2e-tests" test and click on "details" in the bottom of this pr.

TODO:

  • Planning on adding the information to run these tests in the CONTRIBUTING.md file
  • Configure this with GitHub actions
  • Look at more Cypress file/test structures to understand if my file structure makes sense

This pr will fix the issues: #787 #788

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jun 16, 2022
@codecov-commenter
Copy link

codecov-commenter commented Jun 16, 2022

Codecov Report

Merging #795 (28c8431) into main (ef1abf9) will increase coverage by 0.08%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##             main     #795      +/-   ##
==========================================
+ Coverage   64.25%   64.33%   +0.08%     
==========================================
  Files         107      107              
  Lines        9176     9178       +2     
==========================================
+ Hits         5896     5905       +9     
+ Misses       3280     3273       -7     
Impacted Files Coverage Δ
mephisto/data_model/task_run.py 84.57% <100.00%> (+0.14%) ⬆️
mephisto/abstractions/architects/mock_architect.py 88.23% <0.00%> (-2.62%) ⬇️
...tractions/architects/channels/websocket_channel.py 76.56% <0.00%> (+8.59%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ef1abf9...28c8431. Read the comment docs.

@Etesam913 Etesam913 added the dependencies Pull requests that update a dependency file label Jun 17, 2022
mephisto/tools/scripts.py Outdated Show resolved Hide resolved
mephisto/tools/scripts.py Show resolved Hide resolved
@pringshia
Copy link
Contributor

pringshia commented Jun 21, 2022

Thanks for describing the issue in the Linking section above. I think I understand the problem here, the Mephisto builder does indeed call npm install and I can see how that would blow out the npm link set up for local development.

I think that adding a config such as mephisto.task.package_to_link_to could work here, though I wonder if we could do something even more generic that could be reusable for more cases in the future and enable flexibility. The current solution seems extremely overfit to the current problem we are facing.

My alternative suggestion here would be something like mephisto.task.post_build_script instead.


The usage would then be something like this:

python run_task.py mephisto.task.post_build_script=./link_mephisto_task.sh

and then you have a file link_mephisto_task.sh:

npm link mephisto-task

This way you could run any arbitrary code after the build step. For example, in other dialog tasks we have another local package we use called bootstrap-chat. For that, the post build script would look like:

npm link mephisto-task bootstrap-chat
# here we link two packages instead of just one. running a script allows us this flexibility

In sum, I propose introducing "lifecycle hooks"-style config params to allow for more flexible configuration that not only solves for the current problem we have now, but could handle future diverse needs gracefully.

Also see my comment above for how to solve for the optimization issue. We would detect when post_build_script is set and could potentially bypass the optimization in that case?

@Etesam913
Copy link
Contributor Author

The post_build flag definitely seems like a good suggestion. It allows for much more freedom in choosing what to run.

I'm not sure that checking if post_build_script is not none can be used to sufficiently skip the optimization, however. What I mean is that it doesn't work in both directions.

If the flag does exist then you can skip the optimization and build it. What if you want to go back to the package.json package version and remove your link? Well then you would remove the flag. However, if the webapp is not touched, then no rebuild will be triggered and the linked version will still be used. You would have to make some arbitrary change to the webapp to force the rebuild.

I think the simplest way to do this would be to add a param called force_rebuild that when set to true would skip over the optimization. It would be good for local development and to prevent these weird situations with building.

@pringshia
Copy link
Contributor

I think the simplest way to do this would be to add a param called force_rebuild that when set to true would skip over the optimization. It would be good for local development and to prevent these weird situations with building.

Let's do it. Explicit is always better.

ℹ️ This makes sure to create the link with the local version of the package
@Etesam913 Etesam913 mentioned this pull request Jun 22, 2022
ℹ️ These sections are 'Cypress Testing' and 'Local Package Development'
@Etesam913 Etesam913 requested a review from pringshia June 24, 2022 20:53
Copy link
Contributor

@pringshia pringshia left a comment

Choose a reason for hiding this comment

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

there a bunch of places we need to update to post install script now

.github/workflows/cypress-end-to-end-tests.yml Outdated Show resolved Hide resolved
CONTRIBUTING.md Outdated Show resolved Hide resolved
CONTRIBUTING.md Outdated Show resolved Hide resolved
examples/static_react_task/README.md Outdated Show resolved Hide resolved
mephisto/data_model/task_run.py Outdated Show resolved Hide resolved
mephisto/tools/scripts.py Outdated Show resolved Hide resolved
mephisto/tools/scripts.py Outdated Show resolved Hide resolved
mephisto/tools/scripts.py Outdated Show resolved Hide resolved
@Etesam913
Copy link
Contributor Author

Etesam913 commented Jun 27, 2022

Yeah, for some reason I forgot to update it

Copy link
Contributor

@Rebecca-Qian Rebecca-Qian left a comment

Choose a reason for hiding this comment

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

Looks good - thanks for adding tests!!

Copy link
Contributor

@JackUrb JackUrb left a comment

Choose a reason for hiding this comment

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

Will need to execute the update to a 3-param build_custom_bundle as discussed, but overall I'm really excited for this PR. Thanks for getting this together!

We'll definitely want to take the learnings from here to eventually have a test for all of the example tasks, as well as a workflow for being able to create new tests for new tasks (with different configuration options as well like onboarding for instance). Will leave as future work for now though!

Comment on lines +49 to +51
Setting `mephisto.task.force_rebuild=true` runs `npm build` before running your task. By default the task is only rebuilt if a file is changed in the webapp, not if a linked package is changed.

Setting `mephisto.task.post_install_script=link_mephisto_task.sh` runs the `link_mephisto_task.sh` script after `npm install` is ran and before the task is started. This script should be located in the webapp folder of the task. While this script can do many things, for local package development the primary purpose of it is to link to a local package.
Copy link
Contributor

Choose a reason for hiding this comment

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

As a note - we can probably get these to automatically trigger as part of the test suite when any file in packages is updated, such that we get this 'for free' on PRs.

Comment on lines 25 to 26
post_install_script: ""
force_rebuild: false
Copy link
Contributor

Choose a reason for hiding this comment

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

Given these have a default hydra value (introduced in TaskRun's file) is it required that we include these defaults in all of these files?

I imagine the tradeoff between discoverability and boilerplate is an important one to think about if not, but I'd lean towards these more landing in the camp of a heavy user or contributor.

Copy link
Contributor Author

@Etesam913 Etesam913 Jul 5, 2022

Choose a reason for hiding this comment

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

As most people will not do local package development, I think it makes sense to remove them from the hydra config.

I just tested it, and it works without defining them in the hydra config(uses the TaskRun default value).

Comment on lines 241 to 251
if "force_rebuild" not in kwargs:
warnings.warn(
"Consider adding the force_rebuild property to your hydra config and passing it into build_custom_bundle()\nSteps on how to do this: \nhttps://github.com/facebookresearch/Mephisto/issues/811",
stacklevel=2,
)

if "post_install_script" not in kwargs:
warnings.warn(
"Consider adding the post_install_script property to your hydra config and passing it into build_custom_bundle()\nSteps on how to do this: \nhttps://github.com/facebookresearch/Mephisto/issues/811",
stacklevel=2,
)
Copy link
Contributor Author

@Etesam913 Etesam913 Jul 5, 2022

Choose a reason for hiding this comment

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

New warning messages
They link to a closed issue that describes how to fix this

Comment on lines +68 to +69
force_rebuild=cfg.mephisto.task.force_rebuild,
post_install_script=cfg.mephisto.task.post_install_script,
Copy link
Contributor Author

@Etesam913 Etesam913 Jul 5, 2022

Choose a reason for hiding this comment

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

An example of passing in the new parameters

@@ -221,7 +224,7 @@ def augment_config_from_db(script_cfg: DictConfig, db: "MephistoDB") -> DictConf
return script_cfg


def build_custom_bundle(custom_src_dir, run_config: DictConfig):
def build_custom_bundle(custom_src_dir, **kwargs: Union[str, bool]):
Copy link
Contributor Author

Choose a reason for hiding this comment

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

kwargs is type Union[str, bool] because the parameter value can be a string(post_install_script) or bool(force_rebuild) currently.

Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't you be able to just write this as:

def build_custom_bundle(custom_src_dir, force_rebuild: Optional[bool] = None, post_install_script: Optional[str] = None)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I can do it like that, I was just following Pratik's suggestion here: #791 (comment)

Copy link
Contributor

Choose a reason for hiding this comment

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

@Etesam913, you'd still be using kwargs with @JackUrb's suggestion above by naming the arguments in the list

more info: https://treyhunner.com/2018/04/keyword-arguments-in-python/

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh so kwargs just refers to passing the arguments with keywords as opposed to using the **kwargs parameter. I got it 👍

install: false
project: ./examples/static_react_task/webapp
config-file: ./cypress.config.js
start: python examples/static_react_task/run_task.py mephisto.task.post_install_script=link_mephisto_task.sh
Copy link
Contributor Author

@Etesam913 Etesam913 Jul 5, 2022

Choose a reason for hiding this comment

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

#795 (comment)

As a note - we can probably get these to automatically trigger as part of the test suite when any file in packages is updated, such that we get this 'for free' on PRs.

I think I am already doing that in this github action (the start field)

Copy link
Contributor

Choose a reason for hiding this comment

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

Indeed - missed that on first pass!

🚨 Added an exception when the post_install_script does not execute correctly

💡 Added a comment that has a link to a GitHub issue that explains how to set up force_rebuild and post_install_script
@pringshia
Copy link
Contributor

Thanks for getting e2e testing finally added to the codebase here

@Etesam913 Etesam913 merged commit 40d6922 into main Jul 8, 2022
@JackUrb JackUrb deleted the add-cypress-to-static-task branch July 13, 2022 18:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. dependencies Pull requests that update a dependency file
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants