Skip to content

Conversation

@devondragon
Copy link
Owner

Summary

  • Add 301 unit tests covering pure utility functions with focus on security-critical and complex logic
  • Configure vitest with test setup that mocks logger to silence console output
  • Prioritize testing pure functions that require no mocking over integration tests

Test Coverage

File Tests Focus
sanitize.test.ts 56 XSS prevention, HTML stripping, whitespace normalization
frequencyTransform.test.ts 80 All frequency patterns (daily/weekly/biweekly/monthly/quarterly)
parameterUtils.test.ts 69 Date normalization, autoScheduled parsing, validation
constants.test.ts 34 Relative date parsing, priority validation, payload cleaning
cache.test.ts 26 LRU eviction, TTL expiry, pattern-based invalidation
toolConfiguration.test.ts 36 Preset counts (3/7/10), custom config parsing

Key Testing Principles

  • Pure functions first: Test deterministic logic that needs no mocking
  • Security-critical: Sanitization functions protect against XSS
  • Edge cases: Focus on complex branching, not just happy paths
  • No fragile mocks: Avoid tests that break when implementation details change

Test Plan

  • All 301 tests pass with npm test
  • TypeScript compilation succeeds with npm run type-check
  • Verify tests run in CI

🤖 Generated with Claude Code

Add 301 unit tests covering pure utility functions with focus on
security-critical sanitization and complex frequency transformation logic.

Test coverage includes:
- sanitize.ts: XSS prevention, HTML stripping, whitespace normalization
- frequencyTransform.ts: All daily/weekly/biweekly/monthly/quarterly patterns
- parameterUtils.ts: Date normalization, autoScheduled parsing, validation
- constants.ts: Relative date parsing, priority validation, payload cleaning
- cache.ts: LRU eviction, TTL expiry, pattern-based invalidation
- ToolRegistry/ToolConfigurator: Preset counts, custom config parsing

Setup:
- vitest.config.ts with coverage targets for src/utils and src/tools
- tests/setup.ts mocking logger to silence console output during tests

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings January 25, 2026 01:54
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR introduces a Vitest configuration and a new, comprehensive unit test suite targeting core utility modules and tool configuration behavior, with a focus on deterministic, security‑sensitive, and branching logic.

Changes:

  • Add vitest.config.ts with custom test discovery, setup file, coverage include/exclude, and a 10s test timeout.
  • Add new unit tests for tool configuration/registry, sanitization, parameter utilities, frequency transformation, constants, and cache behavior under tests/unit/*.test.ts.
  • Add a shared Vitest setup file that mocks the logger to suppress console output during tests.

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
vitest.config.ts Configures Vitest test discovery, setup, coverage targeting src/utils and src/tools, and a global timeout; note that the include glob currently excludes existing *.spec.ts tests.
tests/unit/toolConfiguration.test.ts Adds unit tests covering ToolRegistry (tool registration, presets, custom configs) and ToolConfigurator (preset validation, custom config parsing/validation, behavior of static helpers).
tests/unit/sanitize.test.ts Adds detailed tests for text sanitization (XSS stripping, HTML removal, whitespace/unicode normalization, length limits) and higher‑level APIs sanitizeCommentContent, sanitizeDescription, and sanitizeName.
tests/unit/parameterUtils.test.ts Tests normalizeDueDateForApi, parseTaskArgs, validateRequiredParams, sanitizeStringParams, workspace/search/project parsing, defaults handling, type validation, and workspace validation.
tests/unit/frequencyTransform.test.ts Adds extensive coverage for transformFrequencyToApiString, validateFrequencyObject, and isValidFrequencyObject across daily/weekly/biweekly/monthly/quarterly/yearly/custom patterns and error cases.
tests/unit/constants.test.ts Tests parseFilterDate handling of relative and absolute dates, priority validation helpers, minimal payload creation, and null/undefined conversion utilities.
tests/unit/cache.test.ts Provides thorough tests for SimpleCache covering TTL expiration, LRU eviction/promotion, pattern‑based invalidation, withCache behavior, stats, cleanup, destroy, and auto‑cleanup.
tests/setup.ts Global Vitest setup that mocks the logger module to silence logging during tests.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

vitest.config.ts Outdated

export default defineConfig({
test: {
include: ['tests/**/*.test.ts'],
Copy link

Copilot AI Jan 25, 2026

Choose a reason for hiding this comment

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

The include pattern here is restricted to tests/**/*.test.ts, which means all existing *.spec.ts test files under tests/ (e.g., tests/cache.spec.ts, tests/handlers.*.spec.ts) will no longer be discovered or executed by Vitest. To avoid silently dropping the current test suite, broaden the pattern to also match *.spec.ts (for example by using a glob that includes both .test.ts and .spec.ts, or by adding a second include entry).

Suggested change
include: ['tests/**/*.test.ts'],
include: ['tests/**/*.{test,spec}.ts'],

Copilot uses AI. Check for mistakes.
@claude
Copy link

claude bot commented Jan 25, 2026

PR Review: Comprehensive Unit Test Suite with Vitest

Overall Assessment

Verdict: ✅ Excellent Addition with Minor Suggestions

This PR adds a well-structured and comprehensive test suite covering 301 tests across 6 critical utility modules. The focus on testing pure functions (particularly security-critical sanitization) demonstrates strong engineering discipline. The tests are well-organized, thorough, and follow best practices.


Strengths 💪

1. Excellent Test Organization

  • Clean separation into focused test suites per module
  • Well-structured describe blocks with clear categorization
  • Consistent naming conventions and test descriptions

2. Security-First Approach 🔒

  • Comprehensive XSS prevention tests in sanitize.test.ts (56 tests)
  • Tests cover script tag removal, HTML stripping, event handler removal
  • Validates behavior when malicious content is entirely removed
  • Edge cases like nested script tags and case-insensitive matching covered

3. Smart Testing Strategy

  • Focus on pure functions that don't require mocking (excellent choice!)
  • Use of fake timers for deterministic date/time testing
  • Proper cleanup in beforeEach/afterEach hooks
  • Logger mocking in test setup to prevent console noise

4. Comprehensive Edge Case Coverage

  • cache.test.ts: LRU eviction, TTL expiry, pattern-based invalidation
  • frequencyTransform.test.ts: All frequency patterns
  • parameterUtils.test.ts: Date normalization, autoScheduled parsing
  • constants.test.ts: Relative date parsing, priority validation

5. Proper Test Configuration

  • vitest.config.ts properly configured with coverage targets
  • Reasonable 10s timeout for async operations
  • Setup file correctly mocks logger

Issues & Suggestions 🔍

Critical Issues

None identified - No blocking issues found.

High Priority Suggestions

1. Test Setup: Path Traversal Issue (tests/setup.ts:7)

The relative path ../src/ assumes the setup file location. Consider using path aliases configured in vitest.config.ts for robustness.

2. Cache Test: Fragile Timing (cache.test.ts:285-300)

The auto-cleanup test relies on internal timing (TTL/2 = 30s). If cleanup logic changes, the test breaks. Consider advancing timers by a full TTL cycle (60s) instead of assuming 30s intervals.

3. Missing Edge Case Tests

A. Script Tag Regex Edge Case
Current regex might not handle nested angle brackets in strings:

it('handles script tags with string literals containing closing tags', () => {
  expect(sanitizeTextContent('<script>var x = "</script>"; alert(1);</script>')).toBe('');
});

B. autoScheduled Parameter Edge Cases
Missing tests for empty objects and numeric values (0, 1, -1).

Medium Priority

4. Error Message Validation

Tests verify errors are thrown but don't validate messages. Consider using .toThrow(/pattern/) to ensure user-facing errors are clear and include documentation links.

5. Redundant Logger Mocking

toolConfiguration.test.ts mocks logger twice (in file and via setup.ts). Remove redundant mock.


Code Quality Assessment 📊

Adherence to CLAUDE.md Guidelines

Excellent - Tests align with project conventions and focus on utility layer

TypeScript Usage

Strong - Proper typing with appropriate use of generics and type assertions

Best Practices

Followed - Single responsibility, descriptive names, proper setup/teardown, test isolation


Security Review 🔒

XSS Prevention Tests

Comprehensive - Covers script tags, HTML stripping, event handlers, nested tags

Minor Gaps (potentially out of scope)

The sanitization doesn't test for:

  1. SVG-based XSS: <svg onload="alert(1)">
  2. Data URLs in images
  3. JavaScript protocols in links

Note: These may be handled by Motion API.


Test Coverage 📈

What's Covered

✅ sanitize, cache, frequencyTransform, parameterUtils, constants, tool configuration

What's Missing (for follow-up PRs)

  1. WorkspaceResolver (critical path)
  2. Error handling classes
  3. Input validation
  4. Response formatters
  5. Handler integration tests

Final Recommendations 📝

Before Merging

  1. Address test setup path issue (use path aliases)
  2. Fix fragile auto-cleanup test timing
  3. Add missing edge case tests
  4. ⚠️ Verify tests pass in CI (currently unchecked in test plan)

Post-Merge Follow-ups

  1. Add coverage thresholds to vitest.config.ts
  2. Create tests for WorkspaceResolver and error handling
  3. Consider parameterized tests for frequency transforms
  4. Add integration tests for handlers
  5. Document testing strategy in tests/README.md

Conclusion

Recommendation: Approve with minor fixes

This is a high-quality PR that significantly improves test coverage and code reliability. The focus on pure functions and security-critical code demonstrates excellent engineering judgment. The suggested changes are mostly non-blocking enhancements.

Great work! 🎉

Critical fixes:
- Include *.spec.ts files in vitest config to run 60 existing tests
- Fix logger mocking to use console.error spy instead of module mock

Improvements:
- Add edge case test for script tags with nested closing tags
- Add numeric edge case tests for autoScheduled parameter (0, 1, -1)
- Fix fragile cache auto-cleanup test to use full TTL cycle
- Remove redundant logger mocks from toolConfiguration test

All 361 tests now pass (301 new + 60 existing).

Addresses feedback from:
- Copilot: Include pattern excludes existing tests
- Claude bot: Test setup, edge cases, timing assumptions

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@claude
Copy link

claude bot commented Jan 25, 2026

Pull Request Review: Comprehensive Unit Test Suite

Overall Assessment: Excellent work! ✅ This PR adds 301 well-structured unit tests with strong coverage of edge cases and security scenarios. The test suite demonstrates thoughtful testing practices and focuses appropriately on pure functions.

Grade: A- (91/100)


🎯 Strengths

  1. Excellent Test Organization: Clear describe blocks, focused test cases, proper use of vitest features
  2. Security-First Approach: Comprehensive XSS testing in sanitize.test.ts shows security awareness
  3. Edge Case Coverage: Thorough testing of null/undefined/empty values, boundary conditions
  4. Smart Testing Strategy: Focusing on pure functions over integration tests reduces fragility
  5. Proper Resource Management: Good use of fake timers, cleanup in afterEach hooks
  6. Error Validation: Tests verify not just success cases but also error messages

🔴 Critical Issues

1. XSS Vulnerability Acceptance (sanitize.test.ts:73)

// Edge case: script content contains "</script>" in a string
// This is acceptable behavior - the attack is still neutralized
expect(result).toContain('alert');

Problem: The test accepts that alert remains in output after sanitization. The regex pattern is vulnerable to malformed script tags:

// This could bypass sanitization:
<script>var x = "</script>"; alert(1);</script>

Recommendation: Either fix the sanitization regex or clearly document this as a known limitation with mitigation strategies.


2. Unclear Numeric autoScheduled Behavior (parameterUtils.test.ts:182-195)

it('returns undefined for numeric 0', () => {
  const result = parseTaskArgs({ autoScheduled: 0 });
  expect(result.autoScheduled).toBeUndefined();
});

Problem: Many APIs treat 0 as false and 1 as true. The current implementation silently returns undefined for numeric values, which could confuse users.

Recommendation:

  • Explicitly handle 0/1 with documented semantics, OR
  • Throw a validation error for invalid types, OR
  • Add clear JSDoc explaining this behavior

3. Silent Configuration Fallback (toolConfiguration.test.ts:209)

it('defaults to essential for invalid config', () => {
  const configurator = new ToolConfigurator('invalid', registry);
  expect(configurator.getConfig()).toBe('essential');
});

Problem: Silent fallback to 'essential' could hide typos in configuration (e.g., MOTION_MCP_TOOLS=minimol silently becomes 'essential').

Recommendation: Log a warning when falling back, or throw an error for invalid configurations.


⚠️ Minor Issues

4. Test Setup Approach Changed Between Commits

The first commit mocked the logger module, but the final version mocks console.error directly. This is actually fine, but worth noting that the PR description mentions mocking the logger which no longer happens.

5. Missing Security Tests (sanitize.test.ts)

  • No test for <img src=x onerror=alert(1)> with variations
  • No HTML entity encoding tests (&lt;script&gt;)
  • No URL-encoded attack vectors

6. Date Validation Gaps (constants.test.ts)

Missing tests for invalid dates:

// Missing:
expect(parseFilterDate('2024-02-30')).toBeNull();  // Feb 30th doesn't exist
expect(parseFilterDate('2024-13-01')).toBeNull();  // Invalid month
expect(parseFilterDate('2024-02-29')).toBe('2024-02-29'); // Leap year
expect(parseFilterDate('2023-02-29')).toBeNull(); // Non-leap year

7. Potential Race Condition (cache.test.ts:295)

The auto-cleanup test makes assumptions about cleanup timing:

vi.advanceTimersByTime(61 * 1000);  // Expire the entry
vi.advanceTimersByTime(60 * 1000);  // Wait for cleanup

Consider explicitly verifying cleanup was called rather than advancing arbitrary time.

8. Custom Config Security (toolConfiguration.test.ts)

No test for path traversal in custom configs:

// Should add:
expect(() => new ToolConfigurator('custom:../../etc/passwd', registry))
  .toThrow();

📋 Missing Test Scenarios

High Priority

  1. sanitize.ts: Multi-byte character exploits, malformed HTML edge cases
  2. parameterUtils.ts: Malformed ISO date strings (2024-06-15T25:00:00)
  3. toolConfiguration.ts: Very long custom config strings (DoS potential)

Medium Priority

  1. cache.ts: Concurrent operations at max capacity
  2. constants.ts: Date boundary testing (year 9999, year 0)
  3. frequencyTransform.ts: Negative intervals, float/decimal intervals

🚀 Recommendations

Immediate Actions

  1. Fix or document the XSS regex limitation in sanitize.ts
  2. Clarify behavior for numeric autoScheduled values in parameterUtils.ts
  3. Add warning logging for invalid tool configurations

Future Enhancements

  1. Consider property-based testing for date/string parsing (using fast-check)
  2. Add integration tests to verify sanitization works end-to-end with the Motion API
  3. Consider mutation testing to verify test quality
  4. Add performance benchmarks for cache operations with large datasets
  5. Document security assumptions clearly (e.g., case-sensitivity requirements)

📊 Test Quality Breakdown

File Score Critical Issues Coverage Quality
sanitize.test.ts 9/10 1 Excellent XSS focus
cache.test.ts 8.5/10 0 Strong LRU/TTL coverage
constants.test.ts 8/10 0 Good, missing edge cases
frequencyTransform.test.ts 9.5/10 0 Exceptional completeness
parameterUtils.test.ts 9/10 1 Very comprehensive
toolConfiguration.test.ts 9/10 1 Excellent config testing

✅ Approval Status

APPROVED with recommendations - This is a high-quality test suite that significantly improves the codebase. The critical issues are manageable and primarily involve documentation clarity rather than fundamental flaws. The tests follow best practices and demonstrate a strong understanding of edge cases and security concerns.

Before Merge Checklist

  • Address or document the XSS regex limitation
  • Clarify numeric autoScheduled behavior in docs/code
  • Consider adding warning for invalid tool configs
  • Verify tests pass in CI

Great work on this comprehensive test suite! The focus on pure functions and security-critical code is exactly the right approach.


Review conducted using repository's CLAUDE.md guidelines and MCP architecture patterns.

Security enhancements:
- Document XSS regex limitation with script tag nested strings
- Add test for img onerror XSS vector
- Add test for HTML entity encoding (not decoded)
- Add path traversal security tests for custom tool configs
- Add malicious property name tests (__proto__, constructor)

Edge case coverage:
- Add numeric autoScheduled tests (0, 1, -1)
- Add date overflow behavior tests (Feb 30, month 13)
- Add leap year date handling tests
- Document JSDoc for autoScheduled parameter behavior

Configuration:
- Verify ToolConfigurator already logs ERROR/WARN for invalid configs

All 367 tests pass (301 new + 60 existing + 6 additional).

Addresses feedback from Claude bot review:
- Critical: XSS vulnerability documentation
- Minor: Missing security tests, date validation gaps
- Clarity: autoScheduled numeric behavior documentation

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@claude
Copy link

claude bot commented Jan 25, 2026

Code Review: Comprehensive Unit Test Suite

Overview

This PR adds 301 unit tests across 6 test files, focusing on pure utility functions. The testing approach is well-designed, prioritizing security-critical sanitization functions and complex logic.


✅ Strengths

1. Excellent Test Coverage

  • Security-Critical Testing: 56 tests for XSS prevention in sanitize.test.ts
  • Pure Functions First: Smart decision to test deterministic functions without complex mocking
  • Edge Case Coverage: Tests cover boundary conditions, invalid inputs, and complex branching
  • Comprehensive Frequency Testing: 80 tests covering all patterns (daily/weekly/biweekly/monthly/quarterly)

2. Clean Test Organization

  • Well-structured describe blocks grouping related tests logically
  • Clear test names describing what's being tested
  • Good use of beforeEach/afterEach for setup/teardown
  • Consistent patterns across all test files

3. Strong Security Testing

  • XSS attack vectors (script tags, onerror handlers, nested tags)
  • Case-insensitive attack detection
  • HTML entity handling
  • Known regex limitations documented with security analysis (sanitize.test.ts:77-91)

4. Documentation Quality

  • Clear JSDoc improvements in parameterUtils.ts:68-81
  • Test comments explaining security implications
  • PR description provides clear testing principles and coverage table

5. Test Setup Configuration

  • vitest.config.ts properly configured
  • tests/setup.ts correctly silences console output
  • Proper use of fake timers for date-dependent tests

@claude
Copy link

claude bot commented Jan 25, 2026

🔍 Issues & Concerns

1. Critical: Missing Dependencies ⚠️
The tests cannot run without npm install. When running npm test, vitest is not found.

Recommendation: The PR test plan shows all tests pass, but ensure CI workflow includes dependency installation. Add documentation about running npm install before testing locally.

2. Security: Documented Regex Limitation ⚠️
sanitize.test.ts:77-91 documents a known limitation where script tags with string literals containing closing tags can leave residual text like "; alert(1);"

While safe (no execution context remains), this deserves attention:

  • Recommendation: Consider using a proper HTML sanitization library (DOMPurify) for production, or add a TODO comment tracking this limitation.

3. Date Overflow Behavior ⚠️
constants.test.ts:73-84 shows invalid dates like '2024-02-30' are accepted.

Recommendation: Either implement strict validation to reject invalid dates, or explicitly document this as intended pass-through behavior.

4. Type Safety Concerns
Several tests use 'as any' to bypass TypeScript (sanitize.test.ts:171-173, parameterUtils.test.ts:179-195).

Recommendation: While testing runtime safety is good, consider strengthening type signatures where possible.

5. Test Completeness
The PR focuses on pure utility functions (excellent approach), but missing:

  • Integration tests for handlers (TaskHandler, ProjectHandler, etc.)
  • Error handling in API service layer

Recommendation: Acceptable for this PR. Consider a follow-up PR for integration tests.

@claude
Copy link

claude bot commented Jan 25, 2026

📊 Analysis Summary

Test Coverage Quality

File Tests Assessment
sanitize.test.ts 56 ⭐ Excellent - Security-critical coverage
frequencyTransform.test.ts 80 ⭐ Excellent - All patterns covered
parameterUtils.test.ts 69 ⭐ Excellent - Complex parsing logic
constants.test.ts 34 ✅ Good - Core utilities covered
cache.test.ts 26 ✅ Good - LRU and TTL logic
toolConfiguration.test.ts 36 ✅ Good - All configs (3/7/10) tested

Performance Considerations

  • Test Performance: 301 tests should run quickly (all pure functions, minimal I/O)
  • No performance issues introduced
  • Cache testing properly validates LRU eviction and TTL for production performance

Security Assessment

Strong Points:

  1. Comprehensive XSS prevention testing
  2. HTML stripping validation
  3. Input sanitization coverage
  4. Edge case attack vectors tested

Concerns:

  1. Regex-based sanitization has documented limitations
  2. SQL/Command injection tests not applicable to this codebase

Overall Security: Good for current scope, with documented limitations

@claude
Copy link

claude bot commented Jan 25, 2026

📝 Recommendations Summary

Must Fix Before Merge

  1. ✅ Verify all 301 tests pass in CI (PR test plan shows this checked)
  2. ⚠️ Add clear documentation about npm install requirement for running tests locally

Should Consider

  1. Document the regex sanitization limitation as a known issue
  2. Decide on strict vs. permissive date validation behavior
  3. Add a follow-up issue for integration tests

Nice to Have

  1. Consider using a proper HTML sanitization library for production
  2. Add code coverage reporting to see actual coverage percentages
  3. Add mutation testing to verify test quality

🎉 Conclusion

This is a high-quality PR that adds significant value:

  • ✅ Focuses on the right things (security, complex logic, edge cases)
  • ✅ Well-organized and documented
  • ✅ Follows testing best practices
  • ✅ Adds 301 meaningful tests with good coverage
  • ⚠️ Has some known limitations that are documented

The testing approach (pure functions first, avoiding fragile mocks) is pragmatic and will create a solid foundation for future testing efforts.

Recommendation: Approve with minor comments - The issues noted are mostly documentation and future enhancements. The core testing work is excellent.


📚 Additional Notes

The PR aligns well with the CLAUDE.md guidelines:

  • Tests are modular and focused
  • No unnecessary complexity
  • Clear separation of concerns
  • Type-safe where practical

Great work on building a comprehensive test suite! 🚀

@devondragon devondragon merged commit 83eba9d into main Jan 25, 2026
1 check passed
@devondragon devondragon deleted the feature/add-unit-tests branch January 25, 2026 02:31
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.

2 participants