Skip to content

Commit 62067e8

Browse files
committed
refactor: replace URL retrieval functions with constants for improved readability
1 parent a9b28be commit 62067e8

File tree

11 files changed

+38
-76
lines changed

11 files changed

+38
-76
lines changed

src/tools/testmanagement-utils/TCG-utils/api.ts

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { apiClient } from "../../../lib/apiClient.js";
22
import {
3-
getTCGTriggerURL,
4-
getTCGPollURL,
5-
getFetchDetailsURL,
6-
getFormFieldsURL,
7-
getBulkCreateURL,
3+
TCG_TRIGGER_URL,
4+
TCG_POLL_URL,
5+
FETCH_DETAILS_URL,
6+
FORM_FIELDS_URL,
7+
BULK_CREATE_URL,
88
} from "./config.js";
99
import {
1010
DefaultFieldMaps,
@@ -23,9 +23,8 @@ export async function fetchFormFields(
2323
projectId: string,
2424
config: BrowserStackConfig,
2525
): Promise<{ default_fields: any; custom_fields: any }> {
26-
const url = await getFormFieldsURL(projectId);
2726
const res = await apiClient.get({
28-
url,
27+
url: await FORM_FIELDS_URL(projectId),
2928
headers: {
3029
"API-TOKEN": getBrowserStackAuth(config),
3130
},
@@ -44,10 +43,9 @@ export async function triggerTestCaseGeneration(
4443
source: string,
4544
config: BrowserStackConfig,
4645
): Promise<string> {
47-
const url = await getTCGTriggerURL();
4846
const tmBaseUrl = await getTMBaseURL();
4947
const res = await apiClient.post({
50-
url,
48+
url: await TCG_TRIGGER_URL(),
5149
headers: {
5250
"API-TOKEN": getBrowserStackAuth(config),
5351
"Content-Type": "application/json",
@@ -82,9 +80,8 @@ export async function fetchTestCaseDetails(
8280
if (testCaseIds.length === 0) {
8381
throw new Error("No testCaseIds provided to fetchTestCaseDetails");
8482
}
85-
const url = await getFetchDetailsURL();
8683
const res = await apiClient.post({
87-
url,
84+
url: await FETCH_DETAILS_URL(),
8885
headers: {
8986
"API-TOKEN": getBrowserStackAuth(config),
9087
"request-source": source,
@@ -112,14 +109,14 @@ export async function pollTestCaseDetails(
112109
): Promise<Record<string, any>> {
113110
const detailMap: Record<string, any> = {};
114111
let done = false;
115-
const pollUrl = await getTCGPollURL();
112+
const TCG_POLL_URL_VALUE = await TCG_POLL_URL();
116113

117114
while (!done) {
118115
// add a bit of jitter to avoid synchronized polling storms
119116
await new Promise((r) => setTimeout(r, 10000 + Math.random() * 5000));
120117

121118
const poll = await apiClient.post({
122-
url: `${pollUrl}?x-bstack-traceRequestId=${encodeURIComponent(traceRequestId)}`,
119+
url: `${TCG_POLL_URL_VALUE}?x-bstack-traceRequestId=${encodeURIComponent(traceRequestId)}`,
123120
headers: {
124121
"API-TOKEN": getBrowserStackAuth(config),
125122
},
@@ -163,14 +160,14 @@ export async function pollScenariosTestDetails(
163160
const scenariosMap: Record<string, Scenario> = {};
164161
const detailPromises: Promise<Record<string, any>>[] = [];
165162
let iteratorCount = 0;
166-
const TCG_POLL_URL = await getTCGPollURL();
163+
const TCG_POLL_URL_VALUE = await TCG_POLL_URL();
167164

168165
// Promisify interval-style polling using a wrapper
169166
await new Promise<void>((resolve, reject) => {
170167
const intervalId = setInterval(async () => {
171168
try {
172169
const poll = await apiClient.post({
173-
url: `${TCG_POLL_URL}?x-bstack-traceRequestId=${encodeURIComponent(traceId)}`,
170+
url: `${TCG_POLL_URL_VALUE}?x-bstack-traceRequestId=${encodeURIComponent(traceId)}`,
174171
headers: {
175172
"API-TOKEN": getBrowserStackAuth(config),
176173
},
@@ -286,7 +283,7 @@ export async function bulkCreateTestCases(
286283
const total = Object.keys(scenariosMap).length;
287284
let doneCount = 0;
288285
let testCaseCount = 0;
289-
const BULK_CREATE_URL = await getBulkCreateURL(projectId, folderId);
286+
const BULK_CREATE_URL_VALUE = await BULK_CREATE_URL(projectId, folderId);
290287

291288
for (const { id, testcases } of Object.values(scenariosMap)) {
292289
const testCaseLength = testcases.length;
@@ -308,7 +305,7 @@ export async function bulkCreateTestCases(
308305

309306
try {
310307
const resp = await apiClient.post({
311-
url: BULK_CREATE_URL,
308+
url: BULK_CREATE_URL_VALUE,
312309
headers: {
313310
"API-TOKEN": getBrowserStackAuth(config),
314311
"Content-Type": "application/json",
Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,12 @@
11
import { getTMBaseURL } from "../../../lib/tm-base-url.js";
22

3-
export const getTCGTriggerURL = async (): Promise<string> => {
4-
const baseUrl = await getTMBaseURL();
5-
return `${baseUrl}/api/v1/integration/tcg/test-generation/suggest-test-cases`;
6-
};
7-
8-
export const getTCGPollURL = async (): Promise<string> => {
9-
const baseUrl = await getTMBaseURL();
10-
return `${baseUrl}/api/v1/integration/tcg/test-generation/test-cases-polling`;
11-
};
12-
13-
export const getFetchDetailsURL = async (): Promise<string> => {
14-
const baseUrl = await getTMBaseURL();
15-
return `${baseUrl}/api/v1/integration/tcg/test-generation/fetch-test-case-details`;
16-
};
17-
18-
export const getFormFieldsURL = async (projectId: string): Promise<string> => {
19-
const baseUrl = await getTMBaseURL();
20-
return `${baseUrl}/api/v1/projects/${projectId}/form-fields-v2`;
21-
};
22-
23-
export const getBulkCreateURL = async (
24-
projectId: string,
25-
folderId: string,
26-
): Promise<string> => {
27-
const baseUrl = await getTMBaseURL();
28-
return `${baseUrl}/api/v1/projects/${projectId}/folder/${folderId}/bulk-test-cases`;
29-
};
3+
export const TCG_TRIGGER_URL = async () =>
4+
`${await getTMBaseURL()}/api/v1/integration/tcg/test-generation/suggest-test-cases`;
5+
export const TCG_POLL_URL = async () =>
6+
`${await getTMBaseURL()}/api/v1/integration/tcg/test-generation/test-cases-polling`;
7+
export const FETCH_DETAILS_URL = async () =>
8+
`${await getTMBaseURL()}/api/v1/integration/tcg/test-generation/fetch-test-case-details`;
9+
export const FORM_FIELDS_URL = async (projectId: string) =>
10+
`${await getTMBaseURL()}/api/v1/projects/${projectId}/form-fields-v2`;
11+
export const BULK_CREATE_URL = async (projectId: string, folderId: string) =>
12+
`${await getTMBaseURL()}/api/v1/projects/${projectId}/folder/${folderId}/bulk-test-cases`;

src/tools/testmanagement-utils/add-test-result.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { getBrowserStackAuth } from "../../lib/get-auth.js";
33
import { z } from "zod";
44
import { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
55
import { BrowserStackConfig } from "../../lib/types.js";
6-
import { getTMBaseURL } from "../../lib/tm-base-url.js";
76

87
/**
98
* Schema for adding a test result to a test run.
@@ -38,8 +37,7 @@ export async function addTestResult(
3837
): Promise<CallToolResult> {
3938
try {
4039
const args = AddTestResultSchema.parse(rawArgs);
41-
const tmBaseUrl = await getTMBaseURL();
42-
const url = `${tmBaseUrl}/api/v2/projects/${encodeURIComponent(
40+
const url = `https://test-management.browserstack.com/api/v2/projects/${encodeURIComponent(
4341
args.project_identifier,
4442
)}/test-runs/${encodeURIComponent(args.test_run_id)}/results`;
4543

src/tools/testmanagement-utils/create-lca-steps.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
import { pollLCAStatus } from "./poll-lca-status.js";
99
import { getBrowserStackAuth } from "../../lib/get-auth.js";
1010
import { BrowserStackConfig } from "../../lib/types.js";
11-
import { getTMBaseURL } from "../../lib/tm-base-url.js";
1211

1312
/**
1413
* Schema for creating LCA steps for a test case
@@ -82,8 +81,7 @@ export async function createLCASteps(
8281
config,
8382
);
8483

85-
const tmBaseUrl = await getTMBaseURL();
86-
const url = `${tmBaseUrl}/api/v1/projects/${projectId}/test-cases/${testCaseId}/lcnc`;
84+
const url = `https://test-management.browserstack.com/api/v1/projects/${projectId}/test-cases/${testCaseId}/lcnc`;
8785

8886
const payload = {
8987
base_url: args.base_url,
@@ -92,7 +90,7 @@ export async function createLCASteps(
9290
test_name: args.test_name,
9391
test_case_details: args.test_case_details,
9492
version: "v2",
95-
webhook_path: `${tmBaseUrl}/api/v1/projects/${projectId}/test-cases/${testCaseId}/webhooks/lcnc`,
93+
webhook_path: `https://test-management.browserstack.com/api/v1/projects/${projectId}/test-cases/${testCaseId}/webhooks/lcnc`,
9694
};
9795

9896
await apiClient.post({

src/tools/testmanagement-utils/create-project-folder.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { formatAxiosError } from "../../lib/error.js";
55
import { projectIdentifierToId } from "../testmanagement-utils/TCG-utils/api.js";
66
import { getBrowserStackAuth } from "../../lib/get-auth.js";
77
import { BrowserStackConfig } from "../../lib/types.js";
8-
import { getTMBaseURL } from "../../lib/tm-base-url.js";
98

109
// Schema for combined project/folder creation
1110
export const CreateProjFoldSchema = z.object({
@@ -56,7 +55,6 @@ export async function createProjectOrFolder(
5655
);
5756
}
5857

59-
const tmBaseUrl = await getTMBaseURL();
6058
const authString = getBrowserStackAuth(config);
6159
const [username, password] = authString.split(":");
6260

@@ -68,7 +66,7 @@ export async function createProjectOrFolder(
6866
const authString = getBrowserStackAuth(config);
6967
const [username, password] = authString.split(":");
7068
const res = await apiClient.post({
71-
url: `${tmBaseUrl}/api/v2/projects`,
69+
url: "https://test-management.browserstack.com/api/v2/projects",
7270
headers: {
7371
"Content-Type": "application/json",
7472
Authorization:
@@ -98,7 +96,7 @@ export async function createProjectOrFolder(
9896
throw new Error("Cannot create folder without project_identifier.");
9997
try {
10098
const res = await apiClient.post({
101-
url: `${tmBaseUrl}/api/v2/projects/${encodeURIComponent(
99+
url: `https://test-management.browserstack.com/api/v2/projects/${encodeURIComponent(
102100
projId,
103101
)}/folders`,
104102
headers: {
@@ -132,7 +130,7 @@ export async function createProjectOrFolder(
132130
- ID: ${folder.id}
133131
- Name: ${folder.name}
134132
- Project Identifier: ${projId}
135-
Access it here: ${tmBaseUrl}/projects/${projectId}/folder/${folder.id}/`,
133+
Access it here: https://test-management.browserstack.com/projects/${projectId}/folder/${folder.id}/`,
136134
},
137135
],
138136
};

src/tools/testmanagement-utils/list-testcases.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
44
import { formatAxiosError } from "../../lib/error.js";
55
import { getBrowserStackAuth } from "../../lib/get-auth.js";
66
import { BrowserStackConfig } from "../../lib/types.js";
7-
import { getTMBaseURL } from "../../lib/tm-base-url.js";
87

98
/**
109
* Schema for listing test cases with optional filters.
@@ -50,8 +49,7 @@ export async function listTestCases(
5049
if (args.priority) params.append("priority", args.priority);
5150
if (args.p !== undefined) params.append("p", args.p.toString());
5251

53-
const tmBaseUrl = await getTMBaseURL();
54-
const url = `${tmBaseUrl}/api/v2/projects/${encodeURIComponent(
52+
const url = `https://test-management.browserstack.com/api/v2/projects/${encodeURIComponent(
5553
args.project_identifier,
5654
)}/test-cases?${params.toString()}`;
5755

src/tools/testmanagement-utils/list-testruns.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
44
import { formatAxiosError } from "../../lib/error.js";
55
import { getBrowserStackAuth } from "../../lib/get-auth.js";
66
import { BrowserStackConfig } from "../../lib/types.js";
7-
import { getTMBaseURL } from "../../lib/tm-base-url.js";
87

98
/**
109
* Schema for listing test runs with optional filters.
@@ -36,11 +35,10 @@ export async function listTestRuns(
3635
params.set("run_state", args.run_state);
3736
}
3837

39-
const tmBaseUrl = await getTMBaseURL();
4038
const url =
41-
`${tmBaseUrl}/api/v2/projects/${encodeURIComponent(
39+
`https://test-management.browserstack.com/api/v2/projects/${encodeURIComponent(
4240
args.project_identifier,
43-
)}/test-runs` + (params.toString() ? `?${params.toString()}` : "");
41+
)}/test-runs?` + params.toString();
4442

4543
const authString = getBrowserStackAuth(config);
4644
const [username, password] = authString.split(":");

src/tools/testmanagement-utils/poll-lca-status.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { apiClient } from "../../lib/apiClient.js";
22
import { getBrowserStackAuth } from "../../lib/get-auth.js";
33
import { BrowserStackConfig } from "../../lib/types.js";
4-
import { getTMBaseURL } from "../../lib/tm-base-url.js";
54

65
/**
76
* Interface for the test case response structure
@@ -40,8 +39,7 @@ export async function pollLCAStatus(
4039
pollIntervalMs: number = 10 * 1000, // 10 seconds interval
4140
config: BrowserStackConfig,
4241
): Promise<{ resource_path: string; status: string } | null> {
43-
const tmBaseUrl = await getTMBaseURL();
44-
const url = `${tmBaseUrl}/api/v1/projects/${projectId}/folder/${folderId}/test-cases/${testCaseId}`;
42+
const url = `https://test-management.browserstack.com/api/v1/projects/${projectId}/folder/${folderId}/test-cases/${testCaseId}`;
4543

4644
const startTime = Date.now();
4745

src/tools/testmanagement-utils/testcase-from-file.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import { signedUrlMap } from "../../lib/inmemory-store.js";
1414
import logger from "../../logger.js";
1515
import { projectIdentifierToId } from "./TCG-utils/api.js";
1616
import { BrowserStackConfig } from "../../lib/types.js";
17-
import { getTMBaseURL } from "../../lib/tm-base-url.js";
1817

1918
export async function createTestCasesFromFile(
2019
args: CreateTestCasesFromFileArgs,
@@ -88,8 +87,7 @@ export async function createTestCasesFromFile(
8887

8988
signedUrlMap.delete(args.documentId);
9089

91-
const tmBaseUrl = await getTMBaseURL();
92-
const dashboardURL = `${tmBaseUrl}/projects/${args.projectReferenceId}/folder/${args.folderId}/test-cases`;
90+
const dashboardURL = `https://test-management.browserstack.com/projects/${args.projectReferenceId}/folder/${args.folderId}/test-cases`;
9391

9492
return {
9593
content: [

src/tools/testmanagement-utils/update-testrun.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { z } from "zod";
44
import { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
55
import { formatAxiosError } from "../../lib/error.js";
66
import { BrowserStackConfig } from "../../lib/types.js";
7-
import { getTMBaseURL } from "../../lib/tm-base-url.js";
87

98
/**
109
* Schema for updating a test run with partial fields.
@@ -41,10 +40,9 @@ export async function updateTestRun(
4140
): Promise<CallToolResult> {
4241
try {
4342
const body = { test_run: args.test_run };
44-
const tmBaseUrl = await getTMBaseURL();
45-
const url = `${tmBaseUrl}/api/v2/projects/${encodeURIComponent(
43+
const url = `https://test-management.browserstack.com/api/v2/projects/${encodeURIComponent(
4644
args.project_identifier,
47-
)}/test-runs/${encodeURIComponent(args.test_run_id)}`;
45+
)}/test-runs/${encodeURIComponent(args.test_run_id)}/update`;
4846

4947
const authString = getBrowserStackAuth(config);
5048
const [username, password] = authString.split(":");

0 commit comments

Comments
 (0)