Skip to content

Fix TypeScript lint issues: remove broad type casts, refactor test helpers, add user ID validation#77

Merged
warengonzaga merged 2 commits intodevfrom
copilot/remove-broad-type-cast
Oct 13, 2025
Merged

Fix TypeScript lint issues: remove broad type casts, refactor test helpers, add user ID validation#77
warengonzaga merged 2 commits intodevfrom
copilot/remove-broad-type-cast

Conversation

Copy link
Contributor

Copilot AI commented Oct 12, 2025

Summary

This PR addresses three TypeScript linting issues to improve type safety and code quality:

  1. Removes a broad as any type cast in admin command tests
  2. Refactors mock test helper classes to eliminate unused variable warnings
  3. Adds user ID validation to the email manager

Changes

1. Replace broad type cast in adminCommands.test.ts

The test was using a broad as any cast when mocking the Telegram API, which bypasses TypeScript's type checking:

// Before
mockContext.telegram = {
    sendMessage: vi.fn().mockResolvedValue({ message_id: 123 })
} as any;

// After
mockContext.telegram = {
    sendMessage: vi.fn().mockResolvedValue({ message_id: 123 })
} as { sendMessage: (_chatId: number | string, _text: string, _options?: unknown) => Promise<{ message_id: number }> };

Now uses a minimal, typed interface that precisely defines only the sendMessage method with its correct signature, maintaining type safety while still serving as an effective test mock.

2. Refactor test helper classes in commandRegistry.test.ts

The mock helper classes (MockCommand, MockConversationProcessor, MockCallbackProcessor) were using TypeScript parameter property shorthand, which triggered no-unused-vars lint warnings:

// Before
class MockCommand implements ICommand {
  constructor(
    public metadata: CommandMetadata,
    public executeImpl: (ctx: BotContext) => Promise<void> = vi.fn()
  ) {}
}

// After
class MockCommand implements ICommand {
  public metadata: CommandMetadata;
  public executeImpl: (_ctx: BotContext) => Promise<void>;

  constructor(
    metadata: CommandMetadata,
    executeImpl: (_ctx: BotContext) => Promise<void> = vi.fn()
  ) {
    this.metadata = metadata;
    this.executeImpl = executeImpl;
  }
}

Now declares properties explicitly on the class and assigns them in the constructor body, eliminating lint warnings while preserving the same behavior.

3. Add user ID validation in emailManager.ts

The updateUserEmail function now validates user IDs before processing, preventing potential issues with invalid inputs:

// Validate user ID
if (userId === null || userId === undefined || !Number.isInteger(userId) || userId <= 0 || Number.isNaN(userId)) {
    return {
        success: false,
        error: 'Invalid user ID'
    };
}

This validation:

  • Rejects null, undefined, NaN, non-integer, or non-positive values
  • Returns a consistent error response matching the function's return type
  • Ensures all existing tests continue to pass, including edge case tests in enhancedEmailManager.test.ts

Testing

  • ✅ All 737 tests pass (1 skipped)
  • ✅ All 33 test files pass
  • ✅ Specific lint errors eliminated in all three modified files
  • ✅ No new lint errors introduced
  • ✅ User ID validation edge cases properly handled

Impact

These changes improve code quality with zero behavioral changes to existing functionality. Type safety is enhanced while maintaining full backward compatibility.

Original prompt

In src/tests/adminCommands.test.ts around lines 318 to 321, remove the broad
"as any" cast on mockContext.telegram and instead cast (or type) it to a minimal
Telegram-like shape that only includes sendMessage with the correct signature
and return type used in the test (e.g., a function that accepts
chatId/text/(optional)options and returns a Promise resolving to an object with
message_id: number); update the assignment to use that narrow type (or a
Partial/typed stub imported from your Telegram types) so TypeScript linting
passes while keeping the minimal stub for the test.

In src/tests/commandRegistry.test.ts around lines 43 to 86, the helper
classes use TypeScript parameter-property shorthand in their constructors which
triggers no-unused-vars; change each constructor to declare the properties
explicitly on the class (e.g. public metadata: CommandMetadata; public
executeImpl: ...) and then assign them inside the constructor body
(this.metadata = metadata; this.executeImpl = executeImpl) instead of using the
shorthand parameters; do this for MockCommand, MockConversationProcessor, and
MockCallbackProcessor so the classes keep the same behavior but no longer emit
unused-variable lint warnings.

In src/utils/emailManager.ts around the start of the updateUserEmail function
(add guard before any email logic), the function currently allows
non-integer/invalid IDs; add an explicit validation that rejects null/undefined,
NaN, non-integer, or non-positive IDs and immediately returns { success: false,
error: 'Invalid user ID' } so the tests in
src/tests/enhancedEmailManager.test.ts (lines ~489-497) pass; ensure the
check uses Number.isInteger and >0 and preserves the function's return shape and
typing.

Use the dev branch as base code, and then PR this to dev branch.

Created from VS Code via the GitHub Pull Request extension.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

…ctors, add user ID validation

Co-authored-by: warengonzaga <15052701+warengonzaga@users.noreply.github.com>
Copilot AI changed the title [WIP] Remove broad 'as any' cast and update parameter properties Fix TypeScript lint issues: remove broad type casts, refactor test helpers, add user ID validation Oct 12, 2025
Copilot AI requested a review from warengonzaga October 12, 2025 06:37
@warengonzaga warengonzaga marked this pull request as ready for review October 12, 2025 17:18
@warengonzaga
Copy link
Member

@copilot what's the compatibility of this to dev branch?

Copy link
Contributor Author

Copilot AI commented Oct 13, 2025

@copilot what's the compatibility of this to dev branch?

This PR is fully compatible with the dev branch:

Clean merge: The changes merge cleanly into dev with no conflicts
Common base: Both branches share the same base commit (a4625f6)
All tests pass: All 737 tests pass on both the feature branch and after merging into dev
Only 3 files modified: Changes are minimal and surgical:

  • src/__tests__/adminCommands.test.ts (1 line changed)
  • src/__tests__/commandRegistry.test.ts (refactored 3 mock classes)
  • src/utils/emailManager.ts (added 8 lines for user ID validation)

The changes are ready to be merged into dev without any compatibility issues.

@warengonzaga warengonzaga merged commit 878bdca into dev Oct 13, 2025
5 checks passed
@warengonzaga warengonzaga deleted the copilot/remove-broad-type-cast branch October 13, 2025 01:53
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