Skip to content

test: extend coverage for credential validators, ai-handler, and admin-api - #313

Merged
htilly merged 3 commits into
masterfrom
test/validator-coverage
Aug 10, 2026
Merged

test: extend coverage for credential validators, ai-handler, and admin-api#313
htilly merged 3 commits into
masterfrom
test/validator-coverage

Conversation

@htilly

@htilly htilly commented Aug 10, 2026

Copy link
Copy Markdown
Owner

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 getSeasonalContext tested, admin-api.js's updateConfigValue at 30.35%).

Credential validators (lib/discord-validator.js, lib/spotify-validator.js, lib/slack-validator.js):

  • Missing/malformed input rejected before any network call
  • Success responses, API-reported failures, thrown network errors
  • Stubs global.fetch (discord/spotify) and WebClient.prototype.apiCall (slack)

lib/ai-handler.js:

  • 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 <music descriptors>" → add normalization, follow-up context clearing, network failure
  • Stubs the OpenAI SDK by patching the shared Completions prototype, since the module only exposes initialize() as its client-configuration entry point

lib/admin-api.js (updateConfigValue):

  • Key not on the admin 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

Test plan

  • 696 → 729 passing tests (63 net new), full suite green, no regressions
  • CI will report the updated coverage percentages on this PR

🤖 Generated with Claude Code

Henrik Tilly 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
Copilot AI lite review requested due to automatic review settings August 10, 2026 11:21
@cursor

cursor Bot commented Aug 10, 2026

Copy link
Copy Markdown

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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 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-handler tests to cover initialization outcomes, parse retries/failures, and user context lifecycle/sanitization.
  • Added additional admin-api tests for updateConfigValue allow-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 thread test/ai-handler.test.mjs
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 thread test/ai-handler.test.mjs
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');
});
@htilly
htilly merged commit 162f37e into master Aug 10, 2026
5 checks passed
@htilly
htilly deleted the test/validator-coverage branch August 10, 2026 11:29
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