Skip to content

Frontend tests #32

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

Merged
merged 69 commits into from
Oct 3, 2024
Merged

Frontend tests #32

merged 69 commits into from
Oct 3, 2024

Conversation

ken-zlai
Copy link
Contributor

@ken-zlai ken-zlai commented Oct 3, 2024

Summary

Added unit tests to ensure that our core API implementation is working, and to ensure type consistency with the backend. If the backend changes/removes any existing fields, then the frontend tests will fail. If the backend adds an additional field, the tests will succeed but show a warning in the console.

First, start the backend:
sbt "project hub" run

Next, run your unit tests:
npm run test:unit

They will automatically re-run if you change the testing code. When we need CI/CD we can start the backend before running tests.

Checklist

  • Added Unit Tests
  • Covered by existing CI
  • Integration tested
  • Documentation update

Summary by CodeRabbit

Release Notes

  • New Features

    • Added a "Running Tests" section in the README to guide users on executing tests, including backend startup requirements.
    • Introduced unit tests for API functions and model response structures to ensure data integrity.
  • Bug Fixes

    • Updated import paths for type definitions in various files to improve organization without affecting functionality.
  • Chores

    • Removed an obsolete unit test file.
    • Modified the test configuration to include all test files across the source directory for better test coverage.

Copy link

coderabbitai bot commented Oct 3, 2024

Walkthrough

The changes introduce a new section in the frontend/README.md that outlines the process for running tests, emphasizing the need to start the backend first. New unit tests are added in api.test.ts and Model.test.ts to validate the behavior of API functions and the structure of API responses, respectively. Type definitions for models and time series data are introduced in Model.ts. Additionally, import paths are updated in several files to reflect changes in file organization, and a test file is removed. The Vite configuration is modified to include all test files in the ./src/ directory.

Changes

File Path Change Summary
frontend/README.md Added "Running Tests" section detailing backend startup and test commands.
frontend/src/lib/api/api.test.ts Introduced unit tests for get function with scenarios for successful, empty, and error responses.
frontend/src/lib/types/Model/Model.test.ts Added unit tests to validate API response structures for models and time series data.
frontend/src/lib/types/Model/Model.ts Defined new TypeScript types: Model, ModelsResponse, TimeSeriesItem, TimeSeriesResponse.
frontend/src/routes/models/+page.server.ts Updated import path for ModelsResponse type.
frontend/src/routes/models/[slug]/observability/+page.server.ts Updated import path for TimeSeriesResponse type.
frontend/src/routes/models/+page.svelte Updated import path for Model type.
frontend/src/test/unit/index.test.ts Removed unit test file for simple addition operation.
frontend/vite.config.ts Modified test file inclusion pattern to include all test files in ./src/ directory.

Possibly related PRs

Suggested reviewers

  • nikhil-zlai
  • piyush-zlai

Poem

🐰 In the garden of code, we hop and play,
With tests now running, come what may.
Backend first, then let’s explore,
New types and tests, oh what a score!
In every function, we find delight,
Hopping through code, from morning till night! 🌼


🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 5

🧹 Outside diff range and nitpick comments (9)
frontend/src/routes/models/+page.server.ts (1)

Line range hint 1-10: Reminder: Add unit tests and complete PR checklist

While the changes in this file look good, I noticed that the PR objectives mention adding unit tests. Please ensure that you've added appropriate tests for these changes, even if they're in a separate file.

Additionally, don't forget to complete the PR checklist in the description. This helps ensure all necessary steps have been taken before merging.

If you need any assistance with writing unit tests for these changes, feel free to ask, and I'd be happy to help!

frontend/src/routes/models/[slug]/observability/+page.server.ts (1)

Line range hint 1-15: Add unit tests as mentioned in PR objectives.

The PR objectives mention adding unit tests, but no tests are visible in this file. Consider adding unit tests for the load function to ensure its correct behavior, especially regarding the API call and the returned data structure.

Would you like assistance in generating unit tests for the load function?

frontend/src/lib/types/Model/Model.ts (1)

1-26: Overall structure is good, consider adding documentation.

The overall structure of this file is well-organized, with clear and logical type definitions for Model, ModelsResponse, TimeSeriesItem, and TimeSeriesResponse. These types provide a solid foundation for type-safe operations related to models and time series data in your application.

To further improve this file:

  1. Consider adding JSDoc comments for each type definition. This will provide inline documentation and improve IDE support. For example:

    /**
     * Represents a model in the system.
     */
    export type Model = {
      // ... properties ...
    };
  2. If these types are part of a public API, consider creating a separate markdown file with more detailed documentation about each type and its usage.

  3. Ensure that any changes made to these types are reflected in the API documentation, if applicable.

By adding documentation, you'll be addressing the "Documentation update" item in the PR checklist and improving the maintainability of your codebase.

Would you like assistance in generating the JSDoc comments for these type definitions?

frontend/src/lib/api/api.test.ts (3)

23-38: LGTM: Successful GET request test is comprehensive.

The test effectively covers the happy path scenario for the get function. It verifies that the correct URL is called with the appropriate method and headers, and that the response is properly parsed and returned.

Consider adding a test case with query parameters to ensure they are properly handled in the URL construction.


51-61: Error handling test could be improved.

The test correctly verifies that the error function is called with the appropriate status code when the API returns a non-ok response. This is good for checking the integration with @sveltejs/kit.

Consider enhancing this test to verify that the get function actually throws an error or rejects the promise. This can be done by wrapping the get call in a try-catch block or using expect().rejects.toThrow(). For example:

it('should throw an error if the response is not ok', async () => {
  mockFetch.mockResolvedValueOnce({
    ok: false,
    status: 404
  });

  await expect(get('error-path')).rejects.toThrow();
  expect(error).toHaveBeenCalledWith(404);
});

This change would ensure that the error is properly propagated from the get function.


1-62: Overall, the test suite is well-structured and comprehensive.

The tests cover the main scenarios for the get function, including successful requests, empty responses, and error handling. The use of Vitest and appropriate mocking techniques demonstrates good testing practices.

To further improve the test suite, consider:

  1. Adding tests for edge cases, such as handling of query parameters or different content types.
  2. Testing the behavior with different HTTP methods, if applicable.
  3. Verifying the handling of network errors (e.g., connection timeouts).
  4. Adding tests for any retry logic or specific error messages, if implemented in the get function.

These additions would provide even more robust coverage and help catch potential edge case bugs.

frontend/README.md (1)

71-72: Great addition of the "Running Tests" section!

The new section provides clear instructions for running frontend tests, which aligns well with the PR objectives. The note about starting the backend before running tests is crucial information for developers.

Consider adding a brief explanation of why the backend needs to be started before running tests. This could help developers understand the test setup better.

frontend/src/lib/types/Model/Model.test.ts (2)

6-42: LGTM with suggestion: Consider adding type checks for fields.

The test case for ModelsResponse is well-structured and comprehensive. It correctly checks for the presence of expected keys and logs warnings for additional fields, which is a good practice for catching API changes.

Consider adding type checks for the fields in addition to checking their presence. This would provide stronger guarantees about the API response structure. For example:

expect(typeof result.offset).toBe('number');
expect(Array.isArray(result.items)).toBe(true);
if (result.items.length > 0) {
  const model = result.items[0];
  expect(typeof model.name).toBe('string');
  expect(typeof model.id).toBe('string');
  expect(typeof model.online).toBe('boolean');
  // ... and so on for other fields
}

44-74: LGTM with suggestions: Consider parameterizing the test and adding more assertions.

The test case for TimeSeriesResponse is well-structured and consistent with the previous test. However, there are a few areas where it could be improved:

  1. The modelId is hardcoded as '0'. Consider parameterizing this value or using a test fixture.
  2. The query parameters in the API call are hardcoded. These could be made more flexible or meaningful.
  3. Similar to the previous test, type checking for the response fields could be added.

Here's an example of how you might improve this test:

it('should match TimeSeriesResponse type', async () => {
  const modelId = 'test-model-id';
  const startTs = Date.now() - 24 * 60 * 60 * 1000; // 24 hours ago
  const endTs = Date.now();
  const offset = '1h';
  const algorithm = 'psi';

  const result = (await api.get(
    `model/${modelId}/timeseries?startTs=${startTs}&endTs=${endTs}&offset=${offset}&algorithm=${algorithm}`
  )) as TimeSeriesResponse;

  // ... (rest of the test)

  if (result.items.length > 0) {
    const item = result.items[0];
    expect(typeof item.value).toBe('number');
    expect(typeof item.ts).toBe('number');
    expect(typeof item.label).toBe('string');
  }
});

This version uses more meaningful values for the query parameters and adds type checking for the item fields.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between cb33b6c and 5eb7ccb.

📒 Files selected for processing (8)
  • frontend/README.md (1 hunks)
  • frontend/src/lib/api/api.test.ts (1 hunks)
  • frontend/src/lib/types/Model/Model.test.ts (1 hunks)
  • frontend/src/lib/types/Model/Model.ts (1 hunks)
  • frontend/src/routes/models/+page.server.ts (1 hunks)
  • frontend/src/routes/models/[slug]/observability/+page.server.ts (1 hunks)
  • frontend/src/test/unit/index.test.ts (0 hunks)
  • frontend/vite.config.ts (1 hunks)
💤 Files with no reviewable changes (1)
  • frontend/src/test/unit/index.test.ts
🔇 Additional comments (9)
frontend/vite.config.ts (1)

7-7: Improved test file discovery configuration. Please verify test additions.

The change to the test include pattern is a good improvement. It allows for more flexible placement of test files within the src directory and supports both .test and .spec file extensions for JavaScript and TypeScript.

However, I noticed that the PR description checklist items, including "Added Unit Tests", are not marked. Given this configuration change:

  1. Have you added new unit tests taking advantage of this more inclusive pattern?
  2. If so, could you please update the PR description to reflect this?
  3. If not, do you plan to add more tests before this PR is merged?

To help verify the addition of new tests, you can run the following command:

This will list all test files matching the new include pattern. Please review the output to ensure it includes any newly added tests.

✅ Verification successful

To verify the addition of new test files, please run the following command:


Test files successfully added and align with the updated configuration.

  • Please update the PR description to mark the "Added Unit Tests" checklist item.
  • Ensure that all new tests take full advantage of the more inclusive include pattern.
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: List all test files in the src directory
# Expected result: If new tests were added, they should appear in this list

fd -e ts -e js . frontend/src -x echo {} | grep -E '(test|spec)\.(js|ts)$'

Length of output: 192


Script:

#!/bin/bash
# Description: List test files added in this PR
# Expected result: New test files added should appear in this list

git diff --name-status origin/main...HEAD | grep '^A' | grep -E 'frontend/src/.*\.(test|spec)\.(js|ts)$' | awk '{print $2}'

Length of output: 201

frontend/src/routes/models/+page.server.ts (1)

2-2: LGTM! Verify other imports of ModelsResponse.

The import path update for ModelsResponse looks good. This change likely reflects a project restructuring.

To ensure consistency across the codebase, please verify that all other imports of ModelsResponse have been updated accordingly. You can use the following script to check for any remaining old import statements:

✅ Verification successful

Re-verifying imports without specifying file type.

Please run the following script to check for any remaining old import statements of ModelsResponse:


Re-verifying imports using AST pattern matching.

Please run the following script to search for any remaining old import statements of ModelsResponse using ast-grep:


All import statements of ModelsResponse have been successfully updated.

No remaining old import statements of ModelsResponse were found in the codebase.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for old import statements of ModelsResponse

# Search for old import statements
echo "Searching for old import statements:"
rg --type typescript "from '\$lib/types/Model'" -g '!frontend/src/routes/models/+page.server.ts'

# Search for new import statements (for reference)
echo "Searching for new import statements:"
rg --type typescript "from '\$lib/types/Model/Model'"

Length of output: 400


Script:

# 
#!/bin/bash
# Description: Check for old and new import statements of ModelsResponse without specifying file type

# Search for old import statements
echo "Searching for old import statements:"
rg "from '\$lib/types/Model'" -g '!frontend/src/routes/models/+page.server.ts'

# Search for new import statements (for reference)
echo "Searching for new import statements:"
rg "from '\$lib/types/Model/Model'"

Length of output: 294


Script:

# 
#!/bin/bash
# Description: Use ast-grep to find import statements of ModelsResponse from old paths

# Search for import statements importing ModelsResponse from '$lib/types/Model'
echo "Searching for old import statements of ModelsResponse using ast-grep:"
ast-grep --lang typescript --pattern "import { ModelsResponse } from '\$lib/types/Model';"

Length of output: 246

frontend/src/routes/models/[slug]/observability/+page.server.ts (1)

3-3: LGTM! Verify consistent import updates across the project.

The import path update for TimeSeriesResponse looks correct. However, ensure that similar imports have been updated consistently across the project to maintain coherence.

To verify the consistency of import updates, run the following script:

frontend/src/lib/api/api.test.ts (3)

1-12: LGTM: Imports and mocks are well-structured.

The imports from Vitest and the module under test are appropriate. Mocking the global fetch function and the error function from @sveltejs/kit is a good practice for isolating the tests and controlling the behavior of external dependencies.


14-21: LGTM: Test suite setup follows best practices.

The use of beforeEach and afterEach hooks to reset and clear mocks ensures a clean state for each test, preventing interference between tests. This is a good practice for maintaining test isolation.


40-49: LGTM: Empty response handling is correctly tested.

This test effectively covers the edge case of an empty API response. Verifying that the get function returns an empty object in this scenario is appropriate and helps ensure robust error handling.

frontend/README.md (1)

Line range hint 71-110: Comprehensive test instructions, but PR checklist items need attention

The new section provides detailed instructions for running different types of tests (unit and integration) using Vitest and Playwright. This is excellent for developer guidance.

However, I noticed that the PR checklist items are not marked as completed. Please ensure that you've addressed the following:

  1. Added Unit Tests: Confirm that new unit tests have been added for the frontend changes.
  2. Covered by existing CI: Verify that these tests are included in your CI pipeline.
  3. Integration tested: Ensure that integration tests have been performed and documented.
  4. Documentation update: While this README update is great, check if any other documentation needs updating.

To help verify some of these items, you can run the following script:

This script will help identify test files, CI configurations, and documentation changes. Please review the output and ensure all necessary items are addressed.

frontend/src/lib/types/Model/Model.test.ts (2)

1-5: LGTM: Imports and test structure are well-defined.

The import statements and the describe block are correctly set up, providing a clear structure for the test suite.


1-75: Address PR objectives and complete checklist items.

This file successfully adds unit tests for the Model types, which aligns with the PR objectives. However, there are a few items from the PR checklist that still need to be addressed:

  1. Ensure these tests are covered by the existing CI pipeline.
  2. Perform integration testing to verify the interaction between these models and other parts of the system.
  3. Update documentation if necessary to reflect any changes in the Model types or their usage.

To help verify CI coverage, you can run the following script:

This script will help verify if the new test file is included in the test configuration and if the CI pipeline is set up to run the tests.

Would you like assistance in creating integration tests or updating the documentation?

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (1)
frontend/src/routes/models/+page.svelte (1)

Line range hint 1-35: Consider adding unit tests for this component.

While this file change aligns with the PR objective of maintaining type consistency, I noticed that there are no unit tests visible for this component. To fully meet the PR objectives, consider adding unit tests to ensure the core API implementation functions correctly for this models page component.

Would you like assistance in generating unit tests for this component? I can help create a basic test suite that verifies the rendering of the models table and the correct usage of the Model type.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 5eb7ccb and 83f97d6.

📒 Files selected for processing (1)
  • frontend/src/routes/models/+page.svelte (1 hunks)
🔇 Additional comments (1)
frontend/src/routes/models/+page.svelte (1)

2-2: LGTM! Verify the new import path.

The import statement has been updated to reflect a new file structure for the Model type. This change looks good and likely contributes to better code organization.

To ensure the new import path is correct and consistent across the project, please run the following verification script:

✅ Verification successful

Verified the new import path. All imports correctly point to the updated Model type file.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the existence of the new Model type file and its usage across the project.

# Test 1: Check if the new Model type file exists
fd --type file "Model.ts" frontend/src/lib/types/Model

# Test 2: Check for any remaining imports from the old path
rg --type svelte --type typescript 'from .+/types/Model\.js' frontend/src

# Test 3: Check for usage of the new import path
rg --type svelte --type typescript 'from .+/types/Model/Model' frontend/src

Length of output: 308


Script:

#!/bin/bash
# Description: Verify the existence of the new Model type file and its usage across the project.

# Test 1: Check if the new Model type file exists
fd --type file "Model.ts" frontend/src/lib/types/Model

# Test 2: Check for any remaining imports from the old path
rg --type typescript 'from .+/types/Model\.js' frontend/src

# Test 3: Check for usage of the new import path
rg --type typescript 'from .+/types/Model/Model' frontend/src

Length of output: 288


Script:

#!/bin/bash
# Description: Verify the existence of the new Model type file and its usage across the project.

# Test 1: Check if the new Model type file exists
fd --type file "Model.ts" frontend/src/lib/types/Model

# Test 2: Check for any remaining imports from the old path in .svelte and .ts files
rg -g "*.svelte" -g "*.ts" 'from .+/types/Model\.js' frontend/src

# Test 3: Check for usage of the new import path in .svelte and .ts files
rg -g "*.svelte" -g "*.ts" 'from .+/types/Model/Model' frontend/src

Length of output: 691

Copy link
Contributor

@nikhil-zlai nikhil-zlai left a comment

Choose a reason for hiding this comment

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

LGTM - one question.

@ken-zlai ken-zlai merged commit d998f8d into main Oct 3, 2024
7 checks passed
@ken-zlai ken-zlai deleted the frontend-tests branch October 3, 2024 19:59
@coderabbitai coderabbitai bot mentioned this pull request Oct 4, 2024
4 tasks
nikhil-zlai pushed a commit that referenced this pull request Oct 24, 2024
## Summary

Added unit tests to ensure that our core API implementation is working,
and to ensure type consistency with the backend. If the backend
changes/removes any existing fields, then the frontend tests will fail.
If the backend adds an additional field, the tests will succeed but show
a warning in the console.

First, start the backend:
`sbt "project hub" run`

Next, run your unit tests:
`npm run test:unit`

They will automatically re-run if you change the testing code. When we
need CI/CD we can start the backend before running tests.

## Checklist
- [x] Added Unit Tests
- [x] Covered by existing CI
- [ ] Integration tested
- [x] Documentation update



<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

## Release Notes

- **New Features**
- Added a "Running Tests" section in the README to guide users on
executing tests, including backend startup requirements.
- Introduced unit tests for API functions and model response structures
to ensure data integrity.

- **Bug Fixes**
- Updated import paths for type definitions in various files to improve
organization without affecting functionality.

- **Chores**
  - Removed an obsolete unit test file.
- Modified the test configuration to include all test files across the
source directory for better test coverage.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: Piyush Narang <[email protected]>
@coderabbitai coderabbitai bot mentioned this pull request Oct 28, 2024
4 tasks
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