test: extend coverage for credential validators, ai-handler, and admin-api - #313
Merged
Conversation
added 2 commits
August 10, 2026 13:01
Covers lib/discord-validator.js, lib/spotify-validator.js, and lib/slack-validator.js, which previously had 20-35% coverage and no dedicated test files for the discord/spotify validators. Each suite exercises: missing/malformed input handled before any network call, success responses, API-reported failures, and thrown network errors, using sinon to stub global fetch (discord/spotify) and WebClient.prototype.apiCall (slack).
…config edge cases ai-handler.js (was 25.71% covered, only getSeasonalContext tested): - user context lifecycle: setUserContext/getUserContext/clearUserContext, per-scope isolation, timeout expiry, suggestedAction sanitization - initialize(): missing/malformed API key, successful validation ping, 401/429/ENOTFOUND/generic error branches, empty-choices response - parseNaturalLanguage(): disabled short-circuit, valid plan parsing, structured-output-error retry into JSON mode, refusal handling, invalid JSON / invalid plan handling, the play-as-music-request normalization, follow-up context clearing, and network failure Stubs the OpenAI SDK by patching the shared Completions prototype (same approach as the WebClient.prototype.apiCall stub added for slack-validator), since the module only exposes initialize() as its entry point for configuring the client. admin-api.js updateConfigValue (was 30.35% covered): - key not on the allow-list - valid/invalid logLevel (live logger.setLevel application) - non-numeric value for a numeric key - boolean coercion for truthy/falsy strings and non-string values - config.save() error path (still reports success, logs the error) - unexpected thrown error inside the try block
|
Bugbot is not enabled for your account, so this pull request was not reviewed. Enable Bugbot in the Cursor dashboard to get automatic reviews on future PRs. |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR expands the Mocha test suite to cover previously thin/untested validation and configuration paths, improving confidence in credential validation (Discord/Spotify/Slack), OpenAI-powered natural language parsing behaviors, and admin config update coercion/edge cases.
Changes:
- Added new test suites for Discord/Spotify credential validators and Slack token validator, including network-success/error branches.
- Extended
ai-handlertests to cover initialization outcomes, parse retries/failures, and user context lifecycle/sanitization. - Added additional
admin-apitests forupdateConfigValueallow-list enforcement and coercion/error branches.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| test/spotify-validator.test.mjs | New tests for Spotify client credentials validation, including network and error-path handling. |
| test/slack-validator.test.mjs | New tests for Slack app/bot token validation, including auth.test success/failure branches. |
| test/discord-validator.test.mjs | New tests for Discord token validation, including format/length guards and API failure modes. |
| test/ai-handler.test.mjs | Expanded AI handler coverage for initialization, parsing, and user context lifecycle behaviors. |
| test/admin-api.test.mjs | Added edge-case coverage for admin config updates (allow-list, coercion, save errors, unexpected exceptions). |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
+19
to
+23
| // Every OpenAI client shares the same Completions prototype, so stubbing it | ||
| // here intercepts the `openai.chat.completions.create(...)` calls made deep | ||
| // inside lib/ai-handler.js without needing to inject a fake client. | ||
| nconf.use('memory'); | ||
| const completionsProto = Object.getPrototypeOf(new OpenAI({ apiKey: 'sk-probe-0000000000000000' }).chat.completions); |
Comment on lines
+131
to
+140
| it('expires context after the timeout window', function() { | ||
| const clock = sinon.useFakeTimers({ now: Date.now(), toFake: ['Date'] }); | ||
|
|
||
| setUserContext('dave', 'add queen', 'wants queen music'); | ||
| expect(getUserContext('dave')).to.not.equal(null); | ||
|
|
||
| clock.tick(5 * 60 * 1000 + 1000); // just past the 5 minute context timeout | ||
|
|
||
| expect(getUserContext('dave')).to.equal(null); | ||
| }); |
Comment on lines
+36
to
+41
| it('rejects a whitespace-only token', async function() { | ||
| const result = await validateDiscordToken(' '); | ||
|
|
||
| expect(result.valid).to.equal(false); | ||
| expect(result.error).to.equal('Discord token is required'); | ||
| }); |
Comment on lines
+29
to
+34
| it('rejects a whitespace-only client ID', async function() { | ||
| const result = await validateSpotifyCredentials(' ', 'secret'); | ||
|
|
||
| expect(result.valid).to.equal(false); | ||
| expect(result.error).to.equal('Client ID is 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Extends test coverage for several previously thin/untested modules, following up on a coverage review (Discord/Spotify/Slack validators were at 20-35%, ai-handler.js at 25.71% with only
getSeasonalContexttested, admin-api.js'supdateConfigValueat 30.35%).Credential validators (
lib/discord-validator.js,lib/spotify-validator.js,lib/slack-validator.js):global.fetch(discord/spotify) andWebClient.prototype.apiCall(slack)lib/ai-handler.js:setUserContext/getUserContext/clearUserContext, per-scope isolation, timeout expiry,suggestedActionsanitizationinitialize(): missing/malformed API key, successful validation ping, 401/429/ENOTFOUND/generic error branches, empty-choices responseparseNaturalLanguage(): disabled short-circuit, valid plan parsing, structured-output-error retry into JSON mode, refusal handling, invalid JSON/invalid plan handling, the "play <music descriptors>" →addnormalization, follow-up context clearing, network failureCompletionsprototype, since the module only exposesinitialize()as its client-configuration entry pointlib/admin-api.js(updateConfigValue):logLevel(livelogger.setLevelapplication)config.save()error path (still reports success, logs the error)Test plan
🤖 Generated with Claude Code