-
Notifications
You must be signed in to change notification settings - Fork 2
refactor: modularize events.js and add missing tests #240
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 32 commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
d73ef96
refactor(events): extract ready handler to events/ready.js
bdfe4f8
refactor(events): extract messageCreate handler to events/messageCrea…
f13be71
refactor(events): extract guildMemberAdd and voiceState handlers
549a9e6
refactor(events): extract interaction handlers to events/interactionC…
5930224
refactor(events): extract reaction handlers to events/reactions.js
08b1fda
refactor(events): extract error handlers to events/errors.js
f250bfb
refactor(events): extract voice state handler to events/voiceState.js
e4c7b93
refactor(events): update main events.js to use extracted modules
24195e9
refactor(events): modularize event handlers
ecc6714
test(utils): add cronParser and flattenToLeafPaths tests
45e3569
fix(test): correct cronParser timezone handling and dangerousKeys imp…
7c1df72
Merge branch 'main' into feat/code-quality-improvements
BillChirico 011237f
refactor: wire guildMemberAdd handler from dedicated module
2b6bcfa
fix: use safeReply when interaction not deferred, sanitize error mess…
de39ceb
fix: add error logging for background tasks
64d6e16
fix: add mobile responsive header
0b88cd2
fix(test): use JSON.parse for __proto__ test reliability
c53c502
style: apply biome lint fixes
d69a2d9
fix(a11y): resolve accessibility errors in landing page
a3e1415
fix(a11y): add aria-expanded and aria-controls to mobile nav toggle
0d942a8
fix: check both deferred and replied in showcase error handler
043d801
docs: fix markdownlint MD022/MD031 violations in task files
59d72f6
fix: await accumulateMessage before evaluateNow for immediate triage
4f9c4a8
fix: import DANGEROUS_KEYS instead of duplicating
a442741
fix(messageCreate): improve async error handling and abort on buffer …
80b6482
fix(interactionCreate): improve error handling for deferred interactions
7aba485
fix(events): wrap sendSpamAlert in try/catch to prevent unhandled rej…
2b5d10f
fix(interactionCreate): add config gates and wrap fallback replies
48d53db
fix(test): update ticket handler tests for sanitized error messages
c4ff232
fix: resolve biome lint issues
abd62a7
fix: update tests for events module refactoring
daf4354
fix(test): add config mocks for challenge and poll handlers
e9096a4
fix: resolve biome lint errors (duplicate keys + formatting)
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| # Task: Refactor events.js | ||
|
|
||
| ## Goal | ||
|
|
||
| Split src/modules/events.js into smaller modules. | ||
|
|
||
| ## CRITICAL RULES | ||
|
|
||
| 1. Read the source file FIRST before doing anything | ||
| 2. Create ONE file at a time | ||
| 3. COMMIT after EVERY file you create | ||
| 4. DO NOT try to do everything at once | ||
|
|
||
| ## Step-by-Step | ||
|
|
||
| ### Step 1: Read and Understand | ||
|
|
||
| Read src/modules/events.js completely. Identify all the handler functions. | ||
|
|
||
| ### Step 2: Create Directory | ||
|
|
||
| ```bash | ||
| mkdir -p src/modules/events | ||
| ``` | ||
|
|
||
| COMMIT: `git add src/modules/events && git commit -m "refactor(events): create events directory"` | ||
|
|
||
| ### Step 3: Extract ready.js | ||
|
|
||
| Create src/modules/events/ready.js with the registerReadyHandler function. | ||
|
|
||
| Keep exports simple: `export function registerReadyHandler(client, config, healthMonitor) { ... }` | ||
|
|
||
| COMMIT immediately after creating the file. | ||
|
|
||
| ### Step 4: Extract messageCreate.js | ||
|
|
||
| Create src/modules/events/messageCreate.js with the messageCreate handler. | ||
|
|
||
| Export: `export function handleMessageCreate(message, client) { ... }` | ||
|
|
||
| COMMIT immediately. | ||
|
|
||
| ### Step 5: Extract interactionCreate.js | ||
|
|
||
| Create src/modules/events/interactionCreate.js with all interaction handlers. | ||
|
|
||
| Export: `export function handleInteractionCreate(interaction) { ... }` | ||
BillChirico marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
BillChirico marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| COMMIT immediately. | ||
|
|
||
| ### Step 6: Update events.js | ||
|
|
||
| Modify src/modules/events.js to import from the new files instead of having inline handlers. | ||
|
|
||
| Keep the same public exports for backward compatibility. | ||
|
|
||
| Run `pnpm lint` and fix any issues. | ||
|
|
||
| Run `pnpm test` and ensure tests pass. | ||
|
|
||
| COMMIT: `git commit -m "refactor(events): update main events.js to use extracted modules"` | ||
|
|
||
| ## Standards | ||
BillChirico marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| - ESM imports/exports | ||
| - Single quotes | ||
| - 2-space indent | ||
| - Semicolons required | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| # Task: Add Missing Tests | ||
|
|
||
| ## Goal | ||
|
|
||
| Add test coverage for files without tests. | ||
|
|
||
| ## CRITICAL RULES | ||
|
|
||
| 1. Read ONE source file at a time | ||
| 2. Create its test file | ||
| 3. COMMIT immediately after each test file | ||
| 4. DO NOT batch multiple files | ||
|
|
||
| ## Priority Order | ||
|
|
||
| ### Test 1: cronParser.js | ||
|
|
||
| Source: src/utils/cronParser.js | ||
|
|
||
| Test: tests/utils/cronParser.test.js | ||
|
|
||
| Steps: | ||
|
|
||
| 1. Read src/utils/cronParser.js | ||
| 2. Understand what it does (parses cron expressions?) | ||
| 3. Create tests/utils/cronParser.test.js | ||
| 4. Test valid inputs, invalid inputs, edge cases | ||
| 5. Run `pnpm test tests/utils/cronParser.test.js` | ||
| 6. COMMIT: `git add tests/utils/cronParser.test.js && git commit -m "test(utils): add cronParser tests"` | ||
|
|
||
| ### Test 2: flattenToLeafPaths.js | ||
|
|
||
| Source: src/utils/flattenToLeafPaths.js | ||
|
|
||
| Test: tests/utils/flattenToLeafPaths.test.js | ||
|
|
||
| Steps: | ||
|
|
||
| 1. Read src/utils/flattenToLeafPaths.js | ||
| 2. Understand what it does (flattens nested objects?) | ||
| 3. Create tests/utils/flattenToLeafPaths.test.js | ||
| 4. Test nested objects, arrays, edge cases | ||
| 5. Run `pnpm test tests/utils/flattenToLeafPaths.test.js` | ||
| 6. COMMIT: `git add tests/utils/flattenToLeafPaths.test.js && git commit -m "test(utils): add flattenToLeafPaths tests"` | ||
|
|
||
| ### Test 3: dangerousKeys.js | ||
|
|
||
| Source: src/api/utils/dangerousKeys.js | ||
|
|
||
| Test: tests/api/utils/dangerousKeys.test.js | ||
|
|
||
| Steps: | ||
|
|
||
| 1. Read src/api/utils/dangerousKeys.js | ||
| 2. Create tests/api/utils/dangerousKeys.test.js | ||
| 3. Run `pnpm test tests/api/utils/dangerousKeys.test.js` | ||
| 4. COMMIT: `git add tests/api/utils/dangerousKeys.test.js && git commit -m "test(api): add dangerousKeys tests"` | ||
|
|
||
| Continue with remaining files if time permits. | ||
|
|
||
| ## Standards | ||
BillChirico marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| - Use Vitest (describe, it, expect) | ||
| - Mock external dependencies | ||
| - Test happy paths AND error cases | ||
| - Follow existing test patterns in tests/ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| # Code Quality Improvements | ||
|
|
||
| ## Task 1: Refactor events.js | ||
|
|
||
| Split src/modules/events.js (959 lines) into smaller, focused handler modules. | ||
|
|
||
| ### Current Structure | ||
|
|
||
| - events.js has ~959 lines with many event handlers mixed together | ||
| - Handles: ready, messageCreate, interactionCreate, reactionAdd/Remove, voiceStateUpdate, etc. | ||
BillChirico marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| ### Target Structure | ||
|
|
||
BillChirico marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Create separate modules in src/modules/events/: | ||
|
|
||
| - ready.js - Client ready handler | ||
| - messageCreate.js - Message handling (AI, moderation, spam, etc.) | ||
| - interactionCreate.js - Slash commands, buttons, modals | ||
| - reactionHandlers.js - Starboard, reaction roles, polls | ||
| - voiceStateUpdate.js - Voice channel tracking | ||
BillChirico marked this conversation as resolved.
Show resolved
Hide resolved
BillChirico marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| - guildMemberAdd.js - Welcome messages | ||
BillChirico marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| ### Steps | ||
|
|
||
| 1. Create src/modules/events/ directory | ||
| 2. Move each handler to its own file | ||
| 3. Update events.js to import and register all handlers | ||
| 4. Keep the same exports (registerReadyHandler, registerEventHandlers, etc.) | ||
| 5. Run pnpm lint and pnpm test after changes | ||
| 6. Commit with conventional commits | ||
|
|
||
| ## Task 2: Add Missing Tests | ||
|
|
||
| Add test coverage for files without tests. | ||
|
|
||
| ### Files to Test (priority order) | ||
|
|
||
| 1. src/utils/cronParser.js | ||
| 2. src/utils/flattenToLeafPaths.js | ||
| 3. src/api/utils/dangerousKeys.js | ||
| 4. src/modules/pollHandler.js | ||
| 5. src/modules/reviewHandler.js | ||
| 6. src/modules/reputationDefaults.js | ||
|
|
||
| ### Steps | ||
|
|
||
| 1. Create test files in tests/ matching source structure | ||
| 2. Follow existing test patterns (Vitest, describe/it/expect) | ||
| 3. Test both happy paths and edge cases | ||
| 4. Mock external dependencies (Discord.js, DB, etc.) | ||
| 5. Run pnpm test to verify coverage increases | ||
| 6. Commit with conventional commits | ||
|
|
||
| ## Standards | ||
BillChirico marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| - ESM imports/exports | ||
| - Single quotes | ||
| - 2-space indent | ||
| - Semicolons required | ||
| - Use Winston logger (no console.*) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| # March 3, 2026 — Code Quality Improvements | ||
|
|
||
| ## Events.js Refactoring (PR #240) | ||
|
|
||
| ### Completed | ||
| - Split 959-line `src/modules/events.js` into 7 focused modules: | ||
| - `events/ready.js` — Client ready handler | ||
| - `events/messageCreate.js` — Message processing (spam, AI, triage) | ||
| - `events/interactionCreate.js` — Slash commands, buttons, modals | ||
| - `events/reactions.js` — Starboard and reaction roles | ||
| - `events/errors.js` — Error handling | ||
| - `events/voiceState.js` — Voice channel tracking | ||
| - `events/guildMemberAdd.js` — Welcome messages | ||
|
|
||
| - Main `events.js` now ~60 lines (re-exports for backward compatibility) | ||
| - Added welcome.enabled config gate to guildMemberAdd handler | ||
| - Fixed interaction error handling (removed !interaction.replied checks) | ||
|
|
||
| ### Tests Added | ||
| - `tests/utils/cronParser.test.js` — 15 tests for cron parsing | ||
| - `tests/utils/flattenToLeafPaths.test.js` — 11 tests for object flattening | ||
| - `tests/api/utils/dangerousKeys.test.js` — 5 tests for dangerous keys | ||
|
|
||
| ### Fixes Applied | ||
| - Fixed timezone issues in cronParser tests (use local time) | ||
| - Fixed __proto__ test to use computed key for actual own property | ||
| - Fixed dangerousKeys import path | ||
| - Fixed all CodeRabbit review comments | ||
| - Fixed pre-existing a11y errors in landing page (button types, SVG titles) | ||
|
|
||
| ### PR Status | ||
| - PR #240 approved and ready to merge | ||
| - All lint errors resolved (0 errors) | ||
| - 31 new tests passing | ||
| - Pre-existing backup.test.js failures unrelated to this work | ||
|
|
||
| ## Key Decisions | ||
| - Use GLM-5 for sub-agents (Claude rate limits) | ||
| - Commit after every file change (prevents lost work) | ||
| - Import ordering matters for Biome lint |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.