diff --git a/code_walkthrough.md b/code_walkthrough.md new file mode 100644 index 0000000..1e3e18b --- /dev/null +++ b/code_walkthrough.md @@ -0,0 +1,50 @@ +# Code Walkthrough - Node.js Hapi Template + +This document provides a walkthrough of the codebase, explaining the architecture, key directories, and technologies used in this Hapi-based template. + +## Architecture Overview + +The project follows a standard Hapi.js server structure, organized by functionality within the `lib/` directory. It uses a layered architecture to separate concerns: + +- **Routing Layer**: Handles incoming HTTP requests and maps them to handler functions. +- **Service Layer**: Contains business logic and interacts with external services or DAOs. +- **DAO Layer (Data Access Object)**: Manages interactions with the database or data storage. +- **Schema Layer**: Defines validation schemas for requests and responses using Joi. + +## Key Directories + +### [lib/routes](file:///Users/rac/Desktop/day-2/nodejs-hapi-template/lib/routes) +Contains the API route definitions. Routes are organized by feature (e.g., `login`, `music`, `signup`). Each feature directory typically contains a `routes.js` file defining the endpoints and their configurations. + +### [lib/services](file:///Users/rac/Desktop/day-2/nodejs-hapi-template/lib/services) +Houses the business logic of the application. Services handle complex operations, such as authentication (e.g., `supabaseAuth.js`) or interacting with external APIs (e.g., `itunes` services). + +### [lib/daos](file:///Users/rac/Desktop/day-2/nodejs-hapi-template/lib/daos) +Contains Data Access Objects that encapsulate the logic for interacting with the database. For example, `likedSongsDao.js` manages operations related to liked songs. + +### [lib/schema](file:///Users/rac/Desktop/day-2/nodejs-hapi-template/lib/schema) +Defines Joi schemas for request validation (payload, query parameters, headers). This ensures that incoming data adheres to the expected format and constraints. + +### [utils](file:///Users/rac/Desktop/day-2/nodejs-hapi-template/utils) +General-purpose utility functions used throughout the application, such as logging and environment configuration. + +## Key Technologies + +- **Hapi.js**: The core web framework. +- **Joi**: Used for powerful data validation. +- **Lodash**: Utility library for data manipulation. +- **Winston**: For logging. +- **Cls-rtracer**: For request tracing. +- **Hapi-swaggerui**: Automatically generates API documentation from route definitions. +- **Axios**: For making HTTP requests. + +## Request Flow + +1. **Request Received**: An HTTP request hits the Hapi server. +2. **Middleware/Extensions**: Global extensions like `onRequest`, `onPreHandler`, and `onPreResponse` in `server.js` may process the request (e.g., camelCase/snakeCase conversion). +3. **Route Matching**: Hapi matches the request to a defined route in `lib/routes`. +4. **Validation**: Joi schemas defined in `lib/schema` validate the request payload, headers, or query parameters. +5. **Handler Execution**: The route handler is executed. It typically calls one or more functions in `lib/services`. +6. **Service Logic**: Services perform business logic, potentially interacting with `lib/daos` for database access. +7. **DAO Interaction**: DAOs execute queries against the data store. +8. **Response Generation**: The handler returns a response, which may be transformed by extensions (e.g., `onPreResponse`) before being sent back to the client. diff --git a/testing.md b/testing.md new file mode 100644 index 0000000..ed0ff4f --- /dev/null +++ b/testing.md @@ -0,0 +1,42 @@ +# Testing - Node.js Hapi Template + +This document outlines the testing strategy, framework, and commands for this project. + +## Testing Framework + +The project uses **Jest** as its primary testing framework. Jest provides a comprehensive suite for unit and integration testing, including assertions, mocking, and coverage reporting. + +## Running Tests + +You can run the tests using the following commands: + +- **Run all tests**: `npm test` +- **Run tests and generate coverage badges**: `npm run test:badges` +- **Clear Jest cache**: `npm run clear:test-cache` + +## Test Structure + +Tests are typically located alongside the code they test, often in a `tests/` subdirectory. For example: + +- Route tests: `lib/routes//tests/routes.test.js` +- Service tests: `lib/services/tests/.test.js` +- Utility tests: `utils/tests/index.test.js` + +## Mocking Strategy + +The project uses Jest's built-in mocking capabilities to isolate components during testing. + +- **Manual Mocks**: Located in the `__mocks__/` directory (e.g., for external libraries like `@supabase/supabase-js`). +- **Function Mocking**: Using `jest.fn()` or `jest.spyOn()` to mock internal functions or service calls. +- **Request/Response Mocking**: The `lib/testServer.js` utility is used to initialize a Hapi server instance specifically for testing, allowing you to inject requests and inspect responses without running a full server. + +## Coverage + +Test coverage is tracked using Jest's built-in coverage reporter. Running `npm test` will generate a coverage report in the `coverage/` directory. Coverage badges are also maintained in the `badges/` folder when running `npm run test:badges`. + +## Best Practices + +- **Isolate Tests**: Ensure each test is independent and doesn't rely on the state of other tests. +- **Mock External Dependencies**: Use mocks for database calls, API requests, and other external systems to ensure tests are fast and reliable. +- **Aim for High Coverage**: Regularly review coverage reports to identify untested areas of the codebase. +- **Use Meaningful Assertions**: Write clear and descriptive test cases and assertions to make failures easier to debug.