test-runs-get-test-run-summary.js•2.39 kB
import { getAuthToken } from './auth-get-token.js';
/**
* Function to get test run results from LoadRunner Cloud.
*
* @param {Object} args - Arguments for the test run results.
* @param {string} args.runId - The ID of the test run.
* @returns {Promise<Object>} - The result of the test run results retrieval.
*/
const executeFunction = async ({ runId }) => {
const baseUrl = process.env.LRC_BASE_URL;
const tenantId = process.env.LRC_TENANT_ID;
const token = await getAuthToken();
try {
// Construct the URL with query parameters
const url = new URL(`${baseUrl}/test-runs/${runId}/results`);
url.searchParams.append('TENANTID', tenantId);
// Set up headers for the request
const headers = {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
};
// Perform the fetch request
const response = await fetch(url.toString(), {
method: 'GET',
headers
});
// Check if the response was successful
if (!response.ok) {
const text = await response.text();
try {
const errorData = JSON.parse(text);
throw new Error(JSON.stringify(errorData));
} catch (jsonErr) {
// Not JSON, log the raw text
console.error('Non-JSON error response:', text);
throw new Error(text);
}
}
// Parse and return the response data
const text = await response.text();
try {
const data = JSON.parse(text);
return data;
} catch (jsonErr) {
// Not JSON, log the raw text
console.error('Non-JSON success response:', text);
return { error: 'Received non-JSON response from API', raw: text };
}
} catch (error) {
console.error('Error retrieving test run results:', error);
return { error: 'An error occurred while retrieving test run results.' };
}
};
/**
* Tool configuration for retrieving test run results.
* @type {Object}
*/
const apiTool = {
function: executeFunction,
definition: {
type: 'function',
function: {
name: 'test_runs_getTestRunResults',
description: 'Get test run results from LoadRunner Cloud.',
parameters: {
type: 'object',
properties: {
runId: {
type: 'string',
description: 'The ID of the test run.'
}
},
required: ['runId']
}
}
}
};
export { apiTool };