Skip to content

Conversation

@ryoppippi
Copy link
Contributor

@ryoppippi ryoppippi commented Oct 22, 2025

Related to #125

now we need to use msw and fs-fixture instead of mocking functions

Summary

Migrates feedback tests to MSW, adds comprehensive testing documentation, and fixes a validation bug discovered during migration.

Changes

MSW Migration:

  • Add MSW handler for feedback endpoint
  • Migrate feedback.spec.ts from spyOn(globalThis, 'fetch') to MSW
  • Remove redundant test lifecycle hooks (already configured globally)

Bug Fix (from #130):

  • Fix tool_names validation bug where whitespace-only strings were transformed to [] without error
  • MSW revealed this issue by showing actual request bodies

Documentation:

  • Add MSW testing guide (handlers, overrides, request verification)
  • Add fs-fixture guide with mandatory await using syntax
  • Update both CLAUDE.md and .cursor/rules/bun-test-mocks.mdc

Test Results

✅ All 10 feedback tests pass

Merge Order

⚠️ Please merge #130 first to maintain clean git history.

Why MSW?

  • Already configured globally in bun.test.setup.ts
  • Better consistency (other tests already use it)
  • More maintainable than spyOn(globalThis, 'fetch')
  • Helped discover the validation bug

Replace spyOn(globalThis, 'fetch') with MSW in feedback.spec.ts for better consistency with other tests

Changes:

- Add MSW handler for StackOne AI tool feedback endpoint in mocks/handlers.ts

- Migrate all fetch mocks in src/tools/tests/feedback.spec.ts to use MSW

- Update CLAUDE.md with comprehensive MSW testing guidance

- Update .cursor/rules/bun-test-mocks.mdc with MSW best practices

MSW provides better consistency across tests and is already configured globally in bun.test.setup.ts.

Note: One pre-existing test failure in validation (whitespace-only tool_names) is unrelated to this change.
Copilot AI review requested due to automatic review settings October 22, 2025 05:39
Copy link

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 migrates the feedback tool tests from using spyOn(globalThis, 'fetch') to MSW (Mock Service Worker) for HTTP request mocking, aligning with the testing patterns already established in other test files in the codebase.

Key changes:

  • Replaced all spyOn(globalThis, 'fetch') calls with MSW event listeners and handler overrides
  • Added a new MSW handler for the StackOne AI tool feedback endpoint
  • Updated documentation (CLAUDE.md and Cursor rules) with comprehensive MSW testing guidance

Reviewed Changes

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

File Description
src/tools/tests/feedback.spec.ts Migrated all test cases from fetch mocking to MSW patterns using event listeners and handler overrides
mocks/handlers.ts Added default MSW handler for the StackOne AI tool feedback endpoint
CLAUDE.md Added new "Testing with MSW" section with examples and best practices
.cursor/rules/bun-test-mocks.mdc Added new "HTTP Request Mocking with MSW" section with guidance for Cursor users

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment on lines +213 to +222
let callCount = 0;
server.use(
http.post('https://api.stackone.com/ai/tool-feedback', () => {
callCount++;
if (callCount === 1) {
return HttpResponse.json({ message: 'Success' });
}
return HttpResponse.json({ error: 'Unauthorized' }, { status: 401 });
})
);
Copy link

Copilot AI Oct 22, 2025

Choose a reason for hiding this comment

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

The callCount variable is shared across potentially concurrent requests and is not reset after the test. If the handler is reused or requests execute in parallel, this could lead to incorrect behavior. Consider resetting callCount inside the test or using a more robust pattern like checking request properties to determine the response.

Copilot uses AI. Check for mistakes.
@pkg-pr-new
Copy link

pkg-pr-new bot commented Oct 22, 2025

Open in StackBlitz

npm i https://pkg.pr.new/StackOneHQ/stackone-ai-node/@stackone/ai@129

commit: 8ddeb44

Add comprehensive fs-fixture documentation for file system testing:

- Basic usage with await using syntax for automatic cleanup

- Pre-populated fixtures for test setup

- Type-safe JSON operations with generics

- Complete API reference for fixture methods

- Benefits and best practices

Updated both CLAUDE.md and .cursor/rules/bun-test-mocks.mdc
Make await using syntax mandatory for fs-fixture usage:

- Add prominent warning to always use await using

- Remove fixture.rm() from API documentation to discourage manual cleanup

- Add direct link to official fs-fixture documentation for reference

- Emphasize automatic cleanup as the primary benefit
@ryoppippi ryoppippi changed the title test: migrate feedback tests from fetch mocking to MSW test: migrate to MSW and document testing best practices Oct 22, 2025
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

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

No issues found across 4 files

Add validation to ensure tool_names array is not empty after trimming and filtering whitespace-only strings.

Previously, tool_names like ['   ', '  '] would be transformed to [] but not rejected, allowing invalid requests to be sent.

This adds a .refine() check after the .transform() to ensure at least one non-empty tool name remains after filtering.
…ests-to-msw

Include validation fix discovered during MSW migration
Copy link
Contributor

@glebedel glebedel left a comment

Choose a reason for hiding this comment

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

LGTM

@ryoppippi ryoppippi merged commit 41af6e3 into main Oct 22, 2025
8 checks passed
@ryoppippi ryoppippi deleted the feat/migrate-feedback-tests-to-msw branch October 22, 2025 08:20
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