Skip to content

Commit

Permalink
cleaned up robots/utils, test compose-clean
Browse files Browse the repository at this point in the history
Signed-off-by: Aaron Chong <aaronchongth@gmail.com>
  • Loading branch information
aaronchongth committed Dec 6, 2024
1 parent 30a8f17 commit 66e3398
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 34 deletions.
24 changes: 0 additions & 24 deletions packages/rmf-dashboard-framework/src/components/robots/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { ApiServerModelsRmfApiRobotStateStatus as Status2 } from 'api-client';
import { RobotMode as RmfRobotMode } from 'rmf-models/ros/rmf_fleet_msgs/msg';

/**
* Returns a uniquely identifiable string representing a robot.
Expand Down Expand Up @@ -28,26 +27,3 @@ export function robotStatusToUpperCase(status: Status2): string {
return `UNKNOWN (${status})`;
}
}

export function robotModeToString(robotMode: RmfRobotMode): string {
switch (robotMode.mode) {
case RmfRobotMode.MODE_CHARGING:
return 'Charging';
case RmfRobotMode.MODE_DOCKING:
return 'Docking';
case RmfRobotMode.MODE_EMERGENCY:
return 'Emergency';
case RmfRobotMode.MODE_GOING_HOME:
return 'Going Home';
case RmfRobotMode.MODE_IDLE:
return 'Idle';
case RmfRobotMode.MODE_MOVING:
return 'Moving';
case RmfRobotMode.MODE_PAUSED:
return 'Paused';
case RmfRobotMode.MODE_WAITING:
return 'Waiting';
default:
return `Unknown (${robotMode.mode})`;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Meta, StoryObj } from '@storybook/react';

import { ComposeCleanTaskForm, makeDefaultComposeCleanTaskDescription } from './compose-clean';

export default {
title: 'Tasks/ComposeCleanTaskForm',
component: ComposeCleanTaskForm,
} satisfies Meta;

type Story = StoryObj<typeof ComposeCleanTaskForm>;

export const Default: Story = {
args: {
taskDesc: makeDefaultComposeCleanTaskDescription(),
cleaningZones: ['clean_zone_1', 'clean_zone_2'],
onChange: () => {},
onValidate: () => {},
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { render, screen } from '@testing-library/react';
import { describe, expect, it, vi } from 'vitest';

import {
ComposeCleanTaskDefinition,
ComposeCleanTaskForm,
insertCleaningZone,
isComposeCleanTaskDescriptionValid,
makeComposeCleanTaskBookingLabel,
makeComposeCleanTaskShortDescription,
makeDefaultComposeCleanTaskDescription,
} from './compose-clean';

describe('Compose clean task form', () => {
it('Renders compose clean task form', () => {
const onChange = vi.fn();
const onValidate = vi.fn();

render(
<ComposeCleanTaskForm
taskDesc={makeDefaultComposeCleanTaskDescription()}
cleaningZones={['test_clean_1', 'test_clean_2']}
onChange={onChange}
onValidate={onValidate}
/>,
);

expect(screen.getByText('Cleaning Zone')).toBeDefined();
});

it('Insert cleaning zone into task description', () => {
const desc = makeDefaultComposeCleanTaskDescription();
const insertedDesc = insertCleaningZone(desc, 'clean_zone_3');
expect(insertedDesc.phases[0].activity.description.activities[0].description).toBe(
'clean_zone_3',
);
expect(
insertedDesc.phases[0].activity.description.activities[1].description
.expected_finish_location,
).toBe('clean_zone_3');
expect(
insertedDesc.phases[0].activity.description.activities[1].description.description.zone,
).toBe('clean_zone_3');
});

it('Validate task description', () => {
const desc = makeDefaultComposeCleanTaskDescription();
const insertedDesc = insertCleaningZone(desc, 'clean_zone_3');
expect(isComposeCleanTaskDescriptionValid(insertedDesc)).toBeTruthy();
});

it('Booking label', () => {
const desc = makeDefaultComposeCleanTaskDescription();
const insertedDesc = insertCleaningZone(desc, 'clean_zone_3');
const bookingLabel = makeComposeCleanTaskBookingLabel(insertedDesc);
expect(bookingLabel.task_definition_id).toBe(ComposeCleanTaskDefinition.taskDefinitionId);
expect(bookingLabel.destination).toBe('clean_zone_3');
});

it('Short description', () => {
const desc = makeDefaultComposeCleanTaskDescription();
const insertedDesc = insertCleaningZone(desc, 'clean_zone_3');
const defaultShortDesc = makeComposeCleanTaskShortDescription(insertedDesc, undefined);
expect(defaultShortDesc).toBe('[Clean] zone [clean_zone_3]');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,16 @@ export function makeComposeCleanTaskShortDescription(
}]`;
}

export function insertCleaningZone(
taskDesc: ComposeCleanTaskDescription,
zone: string,
): ComposeCleanTaskDescription {
taskDesc.phases[0].activity.description.activities[0].description = zone;
taskDesc.phases[0].activity.description.activities[1].description.expected_finish_location = zone;
taskDesc.phases[0].activity.description.activities[1].description.description.zone = zone;
return taskDesc;
}

interface ComposeCleanTaskFormProps {
taskDesc: ComposeCleanTaskDescription;
cleaningZones: string[];
Expand Down Expand Up @@ -135,19 +145,13 @@ export function ComposeCleanTaskForm({
value={taskDesc.phases[0].activity.description.activities[0].description}
onChange={(_ev, newValue) => {
const zone = newValue ?? '';
taskDesc.phases[0].activity.description.activities[0].description = zone;
taskDesc.phases[0].activity.description.activities[1].description.expected_finish_location =
zone;
taskDesc.phases[0].activity.description.activities[1].description.description.zone = zone;
onInputChange(taskDesc);
const updatedTaskDesc = insertCleaningZone(taskDesc, zone);
onInputChange(updatedTaskDesc);
}}
onBlur={(ev) => {
const zone = (ev.target as HTMLInputElement).value;
taskDesc.phases[0].activity.description.activities[0].description = zone;
taskDesc.phases[0].activity.description.activities[1].description.expected_finish_location =
zone;
taskDesc.phases[0].activity.description.activities[1].description.description.zone = zone;
onInputChange(taskDesc);
const updatedTaskDesc = insertCleaningZone(taskDesc, zone);
onInputChange(updatedTaskDesc);
}}
renderInput={(params) => <TextField {...params} label="Cleaning Zone" required={true} />}
/>
Expand Down

0 comments on commit 66e3398

Please sign in to comment.