Testing Strategy
Relevant source files
The following files were used as context for generating this wiki page:
- CLAUDE.md
- apps/web/tests/ai-choose-rule.test.ts
- apps/web/tests/ai-process-user-request.test.ts
- apps/web/app/(app)/[emailAccountId]/assistant/ProcessRules.tsx
- apps/web/app/(app)/[emailAccountId]/cold-email-blocker/TestRules.tsx
- apps/web/utils/ai/choose-rule/match-rules.test.ts
- apps/web/utils/risk.test.ts
- apps/web/utils/schedule.test.ts
- apps/web/utils/string.test.ts
The testing strategy for the inbox-zero project is multifaceted, combining automated unit and integration tests with interactive, component-driven testing interfaces. The primary testing framework is vitest. A key aspect of the strategy is the separation of standard tests from resource-intensive AI-related tests, which can be run on-demand.
This approach ensures core utility functions are robust and predictable, while also providing a framework for validating the complex, non-deterministic behavior of AI features. In-app testing components further bridge the gap between development and real-world usage by allowing developers to test rules against live or custom email data.
Test Execution
Tests are executed via pnpm scripts. There is a clear distinction between running all standard tests and running tests that specifically target AI functionality, which often involve actual LLM calls and are gated by an environment variable.
Sources: CLAUDE.md:5-10, [apps/web/tests/ai-choose-rule.test.ts:7-9]
Test Commands
The following commands are available to run different test suites:
| Command | Description |
|---|---|
pnpm test | Runs all non-AI tests. |
pnpm test-ai | Runs only the AI-related tests. |
pnpm test <path_to_file> | Runs a single, specific test file. |
pnpm test-ai <test_name> | Runs a specific AI test. |
Sources: [CLAUDE.md:7-10]
Conditional AI Testing
Tests for AI features are conditionally executed based on the RUN_AI_TESTS environment variable. This prevents time-consuming and potentially costly LLM API calls during regular test runs. The vitest describe.runIf() method is used to implement this pattern.
// apps/web/__tests__/ai-choose-rule.test.ts:7-9
const isAiTest = process.env.RUN_AI_TESTS === "true";
vi.mock("server-only", () => ({}));
describe.runIf(isAiTest)("aiChooseRule", () => {
// ... test cases
});
Sources: apps/web/tests/ai-choose-rule.test.ts:7-11, apps/web/tests/ai-process-user-request.test.ts:10-12, [apps/web/tests/writing-style.test.ts:8-10]
Unit and Utility Testing
The project includes standard unit tests for utility functions to ensure their correctness and reliability, especially for functions handling data manipulation and business logic.
Timezone-Independent Schedule Tests
Tests for scheduling logic in apps/web/utils/schedule.test.ts are designed to be timezone-independent. This is achieved by setting the process timezone to UTC before all tests and restoring it afterward, ensuring consistent results across different development and CI environments.
// apps/web/utils/schedule.test.ts:13-22
// Store original timezone
const originalTimezone = process.env.TZ;
// Set timezone to UTC for all tests to ensure consistent behavior
beforeAll(() => {
process.env.TZ = "UTC";
});
afterAll(() => {
// Restore original timezone
process.env.TZ = originalTimezone || "UTC";
});
This setup is crucial for testing functions like calculateNextScheduleDate which are highly sensitive to date and time.
Sources: [apps/web/utils/schedule.test.ts:1-22]
String Manipulation Tests
Utility functions for string manipulation, such as truncate, removeExcessiveWhitespace, and generalizeSubject, are tested with a variety of inputs to cover edge cases. For example, removeExcessiveWhitespace is tested for its ability to handle multiple spaces, newlines, and special whitespace characters like zero-width spaces.
Sources: [apps/web/utils/string.test.ts:1-70]
Risk Analysis Tests
A unique aspect of the testing strategy is the validation of risk assessment logic. Tests in apps/web/utils/risk.test.ts verify that the getActionRiskLevel and getRiskLevel functions correctly assign risk levels (low, medium, high, very-high) to rules based on their actions and automation status. This is critical for user safety, especially for automated actions involving dynamic content.
Sources: [apps/web/utils/risk.test.ts:1-120]
AI Feature Testing
A significant portion of the testing suite is dedicated to validating the behavior of AI-powered features. These tests follow a consistent structure and use helper functions to generate mock data.
Test Structure
AI tests typically follow this pattern:
- Setup: Mock data, such as emails and rules, is created using helper functions (e.g.,
getEmail,getRule). - Execution: The specific AI function (e.g.,
aiChooseRule,aiProcessUserRequest) is called with the mock data. - Assertion: The test asserts that the function's output matches the expected structure and values.
The diagram below illustrates the flow of a typical AI test case.
This diagram shows the standardized process for testing AI functions, from data setup to result validation.
Sources: apps/web/tests/ai-choose-rule.test.ts, [apps/web/tests/ai-process-user-request.test.ts]
Example: Testing aiChooseRule
The tests for aiChooseRule validate the AI's ability to select the correct rule from a list based on an email's content. The suite includes simple cases with one or two rules, as well as complex scenarios with over a dozen real-world rules to test the model's discernment.
// apps/web/__tests__/ai-choose-rule.test.ts:100-111
test("Should match technical issues", async () => {
const result = await aiChooseRule({
rules,
email: getEmail({
subject: "Server downtime reported",
content:
"We're experiencing critical server issues affecting production.",
}),
emailAccount: getEmailAccount(),
});
expect(result).toEqual({
rule: technicalIssues,
reason: expect.any(String),
});
});
Sources: [apps/web/tests/ai-choose-rule.test.ts:1-226]
Example: Testing aiProcessUserRequest
The tests for processUserRequest are designed to verify the AI's ability to understand user feedback and correctly modify existing rules. For instance, if a user states that an email was miscategorized, the test checks if the AI correctly invokes the update_ai_instructions or update_static_conditions tool to fix the rule.
Sources: [apps/web/tests/ai-process-user-request.test.ts:1-300]
Component-Driven Testing
In addition to automated tests, the application includes interactive testing interfaces built as React components. These components allow developers to test complex features like rule processing and the cold email blocker directly in the browser, using either real inbox data or custom inputs.
Rule Processing Tester
The ProcessRulesContent component provides an interface to test the entire rule-matching pipeline. It has a testMode prop that, when enabled, allows running rules against emails without executing the final actions (like archiving or replying). This is invaluable for debugging and refining rule logic.
The flow for testing rules via this component is as follows:
- The component fetches a list of recent emails.
- The user can click a "Test" button next to any email.
- This triggers the
runRulesActionserver action withisTest: true. - The action finds and "executes" a matching rule but does not perform the final mutations.
- The result, including the matched rule and reason, is displayed in the UI.
- Users can also test all emails on the page or provide a completely custom email for testing.
This sequence diagram shows how the UI facilitates testing rules without side effects.
Sources: [apps/web/app/(app)/[emailAccountId]/assistant/ProcessRules.tsx:1-320]
Cold Email Blocker Tester
Similarly, the TestRulesContent component in the cold email blocker feature provides a form to test custom email content or run tests against existing emails to see if the AI correctly identifies them as cold emails. This uses the testColdEmailAction to get a classification from the AI without taking any action.
Sources: [apps/web/app/(app)/[emailAccountId]/cold-email-blocker/TestRules.tsx:1-188]
Mocking and Dependencies
Tests are isolated from external dependencies like the database and server-only modules through mocking.
Database Mocking
The Prisma client is mocked using vi.mock and a mock implementation located at apps/web/utils/__mocks__/prisma.ts. This allows tests to simulate database interactions without needing a running database instance. Test files that interact with the database, such as match-rules.test.ts, can then control the data returned by Prisma calls.
// apps/web/utils/ai/choose-rule/match-rules.test.ts:21-22
vi.mock("server-only", () => ({}));
vi.mock("@/utils/prisma");
Sources: apps/web/utils/ai/choose-rule/match-rules.test.ts:21-22, [apps/web/tests/ai/choose-rule/draft-management.test.ts:6-13]
Other Mocks
The server-only package is consistently mocked across all test files to allow server-side code to be imported and tested in the Node.js-based vitest environment.
Sources: apps/web/tests/ai-choose-rule.test.ts:9, [apps/web/tests/writing-style.test.ts:7]