Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion lib/type_description_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ const loader = require('./interface_loader.js');
const rclnodejs = require('bindings')('rclnodejs');
const Service = require('./service.js');

const {
ParameterType,
Parameter,
ParameterDescriptor,
} = require('../lib/parameter.js');

// This class is used to create a TypeDescriptionService which can be used to
// retrieve information about types used by the node’s publishers, subscribers,
// services or actions.
Expand All @@ -32,10 +38,30 @@ class TypeDescriptionService {
'type_description_interfaces/srv/GetTypeDescription'
);
this._typeDescriptionService = null;

this._enabled = false;
const startTypeDescriptionServiceParam = 'start_type_description_service';
if (!node.hasParameter(startTypeDescriptionServiceParam)) {
node.declareParameter(
new Parameter(
startTypeDescriptionServiceParam,
ParameterType.PARAMETER_BOOL,
true
),
new ParameterDescriptor(
startTypeDescriptionServiceParam,
ParameterType.PARAMETER_BOOL,
'If enabled, start the ~/get_type_description service.',
true
)
);
}
const param = node.getParameter(startTypeDescriptionServiceParam);
Comment on lines +43 to +59
Copy link

Copilot AI Jun 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Consider extracting the parameter name 'start_type_description_service' into a top-level constant to avoid duplication and reduce risk of typos.

Suggested change
const startTypeDescriptionServiceParam = 'start_type_description_service';
if (!node.hasParameter(startTypeDescriptionServiceParam)) {
node.declareParameter(
new Parameter(
startTypeDescriptionServiceParam,
ParameterType.PARAMETER_BOOL,
true
),
new ParameterDescriptor(
startTypeDescriptionServiceParam,
ParameterType.PARAMETER_BOOL,
'If enabled, start the ~/get_type_description service.',
true
)
);
}
const param = node.getParameter(startTypeDescriptionServiceParam);
if (!node.hasParameter(START_TYPE_DESCRIPTION_SERVICE_PARAM)) {
node.declareParameter(
new Parameter(
START_TYPE_DESCRIPTION_SERVICE_PARAM,
ParameterType.PARAMETER_BOOL,
true
),
new ParameterDescriptor(
START_TYPE_DESCRIPTION_SERVICE_PARAM,
ParameterType.PARAMETER_BOOL,
'If enabled, start the ~/get_type_description service.',
true
)
);
}
const param = node.getParameter(START_TYPE_DESCRIPTION_SERVICE_PARAM);

Copilot uses AI. Check for mistakes.

this._enabled = param.value;
}

start() {
if (this._typeDescriptionService) {
if (!this._enabled || this._typeDescriptionService) {
return;
}

Expand Down
23 changes: 23 additions & 0 deletions test/test-type-description-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const assertUtils = require('./utils.js');
const DistroUtils = require('../lib/distro.js');
const rclnodejs = require('../index.js');
const TypeDescriptionService = require('../lib/type_description_service.js');
const { exec } = require('child_process');

describe('type description service test suite', function () {
this.timeout(60 * 1000);
Expand Down Expand Up @@ -73,4 +74,26 @@ describe('type description service test suite', function () {
done();
});
});

it('Test type description service configured by parameter', function (done) {
exec(
'ros2 param list /test_type_description_service',
(error, stdout, stderr) => {
if (error || stderr) {
done(
new Error(
'Test type description service configured by parameter failed.'
)
);
}
Comment on lines +88 to +94
Copy link

Copilot AI Jun 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Currently the test fails on any stderr output; consider only failing on a non-zero exit code and logging stderr to avoid false negatives from warnings.

Suggested change
if (error || stderr) {
done(
new Error(
'Test type description service configured by parameter failed.'
)
);
}
if (error) {
done(
new Error(
`Test type description service configured by parameter failed with error: ${error.message}`
)
);
return;
}
if (stderr) {
console.warn(`Warning: stderr output detected: ${stderr}`);
}

Copilot uses AI. Check for mistakes.

if (stdout.includes('start_type_description_service')) {
done();
Copy link

Copilot AI Jun 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test case may hang if 'start_type_description_service' is not found in stdout because there is no else branch to call done() with an error. Consider adding an else branch that terminates the test with an appropriate error message.

Suggested change
done();
done();
} else {
done(
new Error(
"'start_type_description_service' not found in stdout."
)
);

Copilot uses AI. Check for mistakes.

} else {
done(
new Error("'start_type_description_service' not found in stdout.")
);
}
}
);
Comment on lines +85 to +103
Copy link

Copilot AI Jun 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shelling out to ros2 in tests can be brittle; consider using the rclnodejs Node API to list or get parameters directly in code for a more reliable test.

Suggested change
exec(
'ros2 param list /test_type_description_service',
(error, stdout, stderr) => {
if (error || stderr) {
done(
new Error(
'Test type description service configured by parameter failed.'
)
);
}
if (stdout.includes('start_type_description_service')) {
done();
} else {
done(
new Error("'start_type_description_service' not found in stdout.")
);
}
}
);
const parameterName = 'start_type_description_service';
const parameters = node.getParameters([parameterName]);
if (parameters[parameterName] !== undefined) {
done();
} else {
done(
new Error(`'${parameterName}' not found in node parameters.`)
);
}

Copilot uses AI. Check for mistakes.

});
});
Loading