[![Build Status](https://jenkins.evidation.com/buildStatus/icon?job=study-manager-pipeline)](https://jenkins.evidation.com/job/study-manager-pipeline/)

# Study Manager

A  tool for helping setup studies by visualizing nodes that dictate study behavior.

 ** It is highly suggested to use NVM as a Node version management

## Prerequisites

1.  Yarn or Npm installed locally (preferably Yarn as that is what our production build uses)
2.  You have been added to our NPM group, and logged in - create an account at npmjs.com, and provide your username to It/SysAdmin. (https://docs.npmjs.com/getting-started/installing-node#login-to-npm-from-a-terminal)
3.  You have been added to our Bitsrc group, and logged in - create an account at bitsrc.io, and provide your username to It/SysAdmin. (https://docs.bitsrc.io/)

## Getting Started

After that's setup, run:

    `yarn`

This will install of the dependencies needed to build the app. If you don't have npm, you're going to need to install it.

** IMPORTANT ** before running the app, you must run `yarn run build-css` to build the css file initially, before you can run `npm run start`

Then, to build the app, run:

    `yarn start`

This will build the app and start a development server. The app will then be available at `localhost:8001`.

If you're looking to access the study node graph, you can find it at `/studies/:studySlug/edit` with `:studySlug` being the name of the study you want to see the nodes from. Currently, the tool support nodes from the hypertension study and that can be access via `localhost:8001/studies/hypertension/edit`.

## Building

To build Scaffold, run

    `yarn run build`

This will build the necessary files to a `/build` directory. The app should be able to run with the files located in the generated directory.

## Environment Variables

Environment variables live in an `.env.development.local` file in the root of the project. Since the project is built on top of the Create React App base project, most variables need to be prefixed with `REACT_APP_`.

PORT='8001'
NODE_ENV='(production/development)'

REACT_APP_TRIAGE_API_KEY
REACT_APP_TRIAGE_API_URL
REACT_APP_AUTH_API_URL

REACT_APP_BROWSERSTACK_USERNAME
REACT_APP_BROWSERSTACK_ACCESS_KEY

Environment variables for testing live in `.envrc` file. When specifying the script command in `package.json`, add `source .envrc` before each command.

REACT_APP_TRIAGE_API_VERSION
INTEGRATION_TEST_AUTH_TOKEN

## Testing

If a test involves going through Visit Portal, one would need to configure the environment variable `INTEGRATION_TEST_AUTH_TOKEN` in both Triage and Study Manger (test will run on staging only because of security concerns to use `INTEGRATION_TEST_AUTH_TOKEN` in production environment).

Current tests in Study Manager (Visit Portal tests against `staging`, with roles of Production Support and CRC):
1. `activitypatterns`. For each role there're 4 test cases that DQ in Billboard / before 6 min walk test / within 6 min walk test, and one qualifying test case.
2. `memory`. For each role there're 3 test cases that DQ in Billboard / in Visit Portal, and one qualifying test case.

### Puppeteer Tests

Selenium tests are migrated to Puppeteer tests.

Since sourcing `.envrc` is not allowed in CI, if one want to run `yarn test` locally with integration tests, one would need to add sourcing before the command. Currently the test avaiable is `activitypatterns` on staging. By default it only test two cases in headless mode. To run the test (to run like Selenium, simply add `--debug` after the command below):
> $ . ./.envrc && yarn test:i:staging:activitypatterns

If the test cannot run and the error seems to be related to setup, try deleting `package-lock.json`, `yarn.lock` and folder `node_module` and run respective yarn run command again.

[related discussion](https://github.com/facebook/jest/issues/5119)

Situations that might cause a test case to time out in `memory` tests:
- Someone accidently time out all enrollments in Participant Explorer
- Study Manager gets deployed in the middle of the test.
- Mailing service down and emails can't be sent.
- Triage / your network experiencing a congestion on registration / informed consent.
- Some tile in the study not configured properly (for example, no event emitted event to assign external id).

## Dev Resources

[Adding a New Tile (FE)](https://docs.google.com/document/d/11h2Y4lnVqxXjyPl61aDsxymB6Qrqm3QNxgNv9WeF79s/edit)
