-
Notifications
You must be signed in to change notification settings - Fork 415
Fix: Check status code in Action.sendGoal instead of result field #1103
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
EzraBrooks
merged 7 commits into
RobotWebTools:develop
from
ElshadHu:fix/action-sendgoal-status
Nov 19, 2025
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
38ab287
fix: Check status code instead of result
ElshadHu d1e1af6
Merge branch 'develop' into fix/action-sendgoal-status
ElshadHu 676956e
Merge branch 'develop' into fix/action-sendgoal-status
ElshadHu 8f95687
fix: validate both status and result fields
ElshadHu a4f9725
fix: add custom error class
ElshadHu 7cc71ac
Revert "fix: add custom error class"
EzraBrooks 183de9f
invert condition to restore type narrowing, extract error templating
EzraBrooks 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
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,318 @@ | ||
| /** | ||
| * @fileOverview | ||
| * Test for Action.sendGoal status code handling | ||
| * Tests that sendGoal properly handles STATUS_CANCELED and other status codes | ||
| */ | ||
|
|
||
| import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"; | ||
| import Action from "../src/core/Action.ts"; | ||
| import { GoalStatus } from "../src/core/GoalStatus.ts"; | ||
| import type Ros from "../src/core/Ros.ts"; | ||
|
|
||
| // Strict types matching the real protocol | ||
| interface ActionResultMessageBase { | ||
| op: "action_result"; | ||
| id: string; | ||
| action: string; | ||
| status: number; | ||
| } | ||
|
|
||
| interface FailedActionResultMessage extends ActionResultMessageBase { | ||
| result: false; | ||
| values?: string; | ||
| } | ||
|
|
||
| interface SuccessfulActionResultMessage extends ActionResultMessageBase { | ||
| result: true; | ||
| values: { result: number }; | ||
| } | ||
|
|
||
| type ActionResultMessage = | ||
| | FailedActionResultMessage | ||
| | SuccessfulActionResultMessage; | ||
|
|
||
| interface ActionFeedbackMessage { | ||
| op: "action_feedback"; | ||
| id: string; | ||
| action: string; | ||
| values: { current: number }; | ||
| } | ||
|
|
||
| type ActionMessage = ActionResultMessage | ActionFeedbackMessage; | ||
|
|
||
| describe("Action.sendGoal", () => { | ||
| let action: Action< | ||
| { target: number }, | ||
| { current: number }, | ||
| { result: number } | ||
| >; | ||
| let mockRos: Ros; | ||
| let messageHandler: ((msg: ActionMessage) => void) | null = null; | ||
|
|
||
| beforeEach(() => { | ||
| messageHandler = null; | ||
| mockRos = { | ||
| on: vi.fn((_id: string, callback: (msg: ActionMessage) => void) => { | ||
| messageHandler = callback; | ||
| }), | ||
| callOnConnection: vi.fn(), | ||
| } as unknown as Ros; | ||
|
|
||
| action = new Action({ | ||
| ros: mockRos, | ||
| name: "/test_action", | ||
| actionType: "test_msgs/TestAction", | ||
| }); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| describe("status code handling", () => { | ||
| it("should call resultCallback when action succeeds (STATUS_SUCCEEDED)", () => { | ||
| const resultCallback = vi.fn(); | ||
| const failedCallback = vi.fn(); | ||
|
|
||
| action.sendGoal( | ||
| { target: 10 }, | ||
| resultCallback, | ||
| undefined, | ||
| failedCallback, | ||
| ); | ||
|
|
||
| const successMessage: SuccessfulActionResultMessage = { | ||
| op: "action_result", | ||
| id: "test-id", | ||
| action: "/test_action", | ||
| values: { result: 42 }, | ||
| status: GoalStatus.STATUS_SUCCEEDED, | ||
| result: true, | ||
| }; | ||
|
|
||
| messageHandler?.(successMessage); | ||
|
|
||
| expect(resultCallback).toHaveBeenCalledWith({ result: 42 }); | ||
| expect(failedCallback).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("should call failedCallback when action is cancelled (STATUS_CANCELED)", () => { | ||
| const resultCallback = vi.fn(); | ||
| const failedCallback = vi.fn(); | ||
|
|
||
| action.sendGoal( | ||
| { target: 10 }, | ||
| resultCallback, | ||
| undefined, | ||
| failedCallback, | ||
| ); | ||
|
|
||
| const cancelledMessage: FailedActionResultMessage = { | ||
| op: "action_result", | ||
| id: "test-id", | ||
| action: "/test_action", | ||
| values: "Action was cancelled by user", | ||
| status: GoalStatus.STATUS_CANCELED, | ||
| result: false, | ||
| }; | ||
|
|
||
| messageHandler?.(cancelledMessage); | ||
|
|
||
| expect(failedCallback).toHaveBeenCalled(); | ||
| expect(resultCallback).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("should call failedCallback when action is aborted (STATUS_ABORTED)", () => { | ||
| const resultCallback = vi.fn(); | ||
| const failedCallback = vi.fn(); | ||
|
|
||
| action.sendGoal( | ||
| { target: 10 }, | ||
| resultCallback, | ||
| undefined, | ||
| failedCallback, | ||
| ); | ||
|
|
||
| const abortedMessage: FailedActionResultMessage = { | ||
| op: "action_result", | ||
| id: "test-id", | ||
| action: "/test_action", | ||
| values: "Action aborted due to error", | ||
| status: GoalStatus.STATUS_ABORTED, | ||
| result: false, | ||
| }; | ||
|
|
||
| messageHandler?.(abortedMessage); | ||
|
|
||
| expect(failedCallback).toHaveBeenCalled(); | ||
| expect(resultCallback).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("should call failedCallback when action is canceling (STATUS_CANCELING)", () => { | ||
| const resultCallback = vi.fn(); | ||
| const failedCallback = vi.fn(); | ||
|
|
||
| action.sendGoal( | ||
| { target: 10 }, | ||
| resultCallback, | ||
| undefined, | ||
| failedCallback, | ||
| ); | ||
|
|
||
| const cancelingMessage: FailedActionResultMessage = { | ||
| op: "action_result", | ||
| id: "test-id", | ||
| action: "/test_action", | ||
| values: "Action is being cancelled", | ||
| status: GoalStatus.STATUS_CANCELING, | ||
| result: false, | ||
| }; | ||
|
|
||
| messageHandler?.(cancelingMessage); | ||
|
|
||
| expect(failedCallback).toHaveBeenCalled(); | ||
| expect(resultCallback).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("should handle unknown status gracefully (STATUS_UNKNOWN)", () => { | ||
| const resultCallback = vi.fn(); | ||
| const failedCallback = vi.fn(); | ||
|
|
||
| action.sendGoal( | ||
| { target: 10 }, | ||
| resultCallback, | ||
| undefined, | ||
| failedCallback, | ||
| ); | ||
|
|
||
| const unknownMessage: FailedActionResultMessage = { | ||
| op: "action_result", | ||
| id: "test-id", | ||
| action: "/test_action", | ||
| values: "Unknown status", | ||
| status: GoalStatus.STATUS_UNKNOWN, | ||
| result: false, | ||
| }; | ||
|
|
||
| messageHandler?.(unknownMessage); | ||
|
|
||
| expect(failedCallback).toHaveBeenCalled(); | ||
| expect(resultCallback).not.toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
|
|
||
| describe("feedback handling", () => { | ||
| it("should handle feedback messages correctly", () => { | ||
| const resultCallback = vi.fn(); | ||
| const feedbackCallback = vi.fn(); | ||
| const failedCallback = vi.fn(); | ||
|
|
||
| action.sendGoal( | ||
| { target: 10 }, | ||
| resultCallback, | ||
| feedbackCallback, | ||
| failedCallback, | ||
| ); | ||
|
|
||
| const feedbackMessage: ActionFeedbackMessage = { | ||
| op: "action_feedback", | ||
| id: "test-id", | ||
| action: "/test_action", | ||
| values: { current: 5 }, | ||
| }; | ||
|
|
||
| messageHandler?.(feedbackMessage); | ||
|
|
||
| expect(feedbackCallback).toHaveBeenCalledWith({ current: 5 }); | ||
| expect(resultCallback).not.toHaveBeenCalled(); | ||
| expect(failedCallback).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("should handle multiple feedback messages", () => { | ||
| const resultCallback = vi.fn(); | ||
| const feedbackCallback = vi.fn(); | ||
| const failedCallback = vi.fn(); | ||
|
|
||
| action.sendGoal( | ||
| { target: 10 }, | ||
| resultCallback, | ||
| feedbackCallback, | ||
| failedCallback, | ||
| ); | ||
|
|
||
| const feedbackMessage1: ActionFeedbackMessage = { | ||
| op: "action_feedback", | ||
| id: "test-id", | ||
| action: "/test_action", | ||
| values: { current: 3 }, | ||
| }; | ||
|
|
||
| const feedbackMessage2: ActionFeedbackMessage = { | ||
| op: "action_feedback", | ||
| id: "test-id", | ||
| action: "/test_action", | ||
| values: { current: 7 }, | ||
| }; | ||
|
|
||
| messageHandler?.(feedbackMessage1); | ||
| messageHandler?.(feedbackMessage2); | ||
|
|
||
| expect(feedbackCallback).toHaveBeenCalledTimes(2); | ||
| expect(feedbackCallback).toHaveBeenNthCalledWith(1, { current: 3 }); | ||
| expect(feedbackCallback).toHaveBeenNthCalledWith(2, { current: 7 }); | ||
| }); | ||
| }); | ||
|
|
||
| describe("edge cases", () => { | ||
| it("should prioritize status code over result field", () => { | ||
| const resultCallback = vi.fn(); | ||
| const failedCallback = vi.fn(); | ||
|
|
||
| action.sendGoal( | ||
| { target: 10 }, | ||
| resultCallback, | ||
| undefined, | ||
| failedCallback, | ||
| ); | ||
|
|
||
| // This is the problematic case: result=true but status=CANCELED | ||
| // According to protocol, this shouldn't happen, but we test it anyway | ||
| // We have to cast to test this edge case since TypeScript prevents it | ||
| const confusingMessage = { | ||
| op: "action_result", | ||
| id: "test-id", | ||
| action: "/test_action", | ||
| values: { result: 0 }, | ||
| status: GoalStatus.STATUS_CANCELED, | ||
| result: true, // This violates the type but could happen at runtime | ||
| } as ActionMessage; | ||
|
|
||
| messageHandler?.(confusingMessage); | ||
|
|
||
| // Should call failedCallback because status is CANCELED | ||
| expect(failedCallback).toHaveBeenCalled(); | ||
| expect(resultCallback).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("should handle missing feedback callback gracefully", () => { | ||
| const resultCallback = vi.fn(); | ||
| const failedCallback = vi.fn(); | ||
|
|
||
| action.sendGoal( | ||
| { target: 10 }, | ||
| resultCallback, | ||
| undefined, | ||
| failedCallback, | ||
| ); | ||
|
|
||
| const feedbackMessage: ActionFeedbackMessage = { | ||
| op: "action_feedback", | ||
| id: "test-id", | ||
| action: "/test_action", | ||
| values: { current: 5 }, | ||
| }; | ||
|
|
||
| expect(() => messageHandler?.(feedbackMessage)).not.toThrow(); | ||
| }); | ||
| }); | ||
| }); |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if we made a
class GoalError extends Errorthat did this template interpolation in its constructor and then we calledString()on the error before sending it to thefailedCallback? That way in the future if the error handling here changes to include athrow, we already have an error type for it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it okay to put
GoalErroron top ofAction.ts? To be honest i did not find any file that contains custom error classes.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, that's fine!