Skip to content

fix: Ensure s/ command only triggers on prefix and improve tests - #14

Open
thehunmonkgroup wants to merge 1 commit into
carmo-evan:masterfrom
thehunmonkgroup:master
Open

fix: Ensure s/ command only triggers on prefix and improve tests#14
thehunmonkgroup wants to merge 1 commit into
carmo-evan:masterfrom
thehunmonkgroup:master

Conversation

@thehunmonkgroup

Copy link
Copy Markdown

This PR addresses a bug where the s/ replacement command could be incorrectly triggered by messages that contained s/ but did not start with it.

It also improves test coverage and reliability.

Changes:

  1. server/plugin.go:
    • Modified MessageWillBePosted to strictly check if the trimmed message strings.HasPrefix("s/"). Messages not starting with s/ are now correctly ignored.
    • Added handling for invalid command formats detected by splitAndValidateInput (e.g., s/foo, s//). An ephemeral error message is now sent to the user, and the invalid command message is prevented from posting.
    • Corrected the handling when getLastPost fails to find a post. It now sends the specific error via ephemeral message and prevents the s/ command from posting (returns dismiss_post).
  2. server/plugin_test.go:
    • Removed premature validation checks that prevented testing the error handling within MessageWillBePosted.
    • Added new test cases to TestExecuteCommand to cover:
      • Messages that do not start with s/.
      • Messages with leading/trailing whitespace.
      • Invalid s/ command formats.
    • Refactored mock setup (setupAPI) and conditionalized mock expectations within the test loop to accurately reflect the different API calls made depending on whether the input is a valid command, an invalid format, or not a command at all.
    • Improved mock data initialization (testAPIConfig) to provide necessary details (like User ID, Team ID) required by the code under test, fixing specific mock mismatches.

Context:

These changes were made to fix the core prefix bug and improve user feedback for errors, while adapting the existing test structure to support properly testing the code changes, rather than performing a major refactor of the tests.

It was also necessary to make some minor modifications to the Makefile and go.mod to support more modern versions of Go.

Corrects the MessageWillBePosted hook to only process messages
explicitly starting with s/ after trimming whitespace. Previously,
any message containing s/ could incorrectly trigger the replacement
logic.

Improves error handling for invalid command formats and cases where
no previous post is found by sending an ephemeral message and
preventing the original s/ message from being posted.

Updates tests to align with the corrected logic, adds cases for
non-commands and invalid formats, and adjusts mock expectations
to be conditional based on the execution path, resolving previous
test failures.
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.

1 participant