Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
11 changes: 11 additions & 0 deletions src/tools/testmanagement-utils/create-testcase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from "./TCG-utils/api.js";
import { BrowserStackConfig } from "../../lib/types.js";
import { getTMBaseURL } from "../../lib/tm-base-url.js";
import { wrapRichText, wrapTestCaseSteps } from "./rich-text.js";
import logger from "../../logger.js";

interface TestCaseStep {
Expand Down Expand Up @@ -324,6 +325,16 @@ export async function createTestCase(
new Set([...(testCaseParams.tags ?? []), "MCP Generated"]),
);

testCaseParams.test_case_steps = wrapTestCaseSteps(
testCaseParams.test_case_steps,
);
if (testCaseParams.preconditions !== undefined) {
testCaseParams.preconditions = wrapRichText(testCaseParams.preconditions);
}
if (testCaseParams.description !== undefined) {
testCaseParams.description = wrapRichText(testCaseParams.description);
}

if (
testCaseParams.priority !== undefined ||
testCaseParams.case_type !== undefined
Expand Down
28 changes: 28 additions & 0 deletions src/tools/testmanagement-utils/rich-text.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// TM renders these fields as rich text only when the value is HTML;
const TM_ALLOWED_TAG =
/^\s*<(div|img|b|em|i|strong|u|span|abbr|br|cite|code|dd|dfn|dl|dt|kbd|li|mark|ol|p|pre|q|s|samp|small|strike|sub|sup|time|ul|var|table|td|th|thead|tbody|tr|col|colgroup|a)[\s/>]/i;
const HAS_ENCODABLE_CHARS = /[<>&]/;

function escapeHtml(text: string): string {
return text
.replace(/&/g, "&amp;")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;");
}

export function wrapRichText(text: string): string {
if (TM_ALLOWED_TAG.test(text) || !HAS_ENCODABLE_CHARS.test(text)) {
return text;
}
return `<p>${escapeHtml(text)}</p>`;
}

export function wrapTestCaseSteps<T extends { step: string; result: string }>(
steps: T[],
): T[] {
return steps.map((s) => ({
...s,
step: wrapRichText(s.step),
result: wrapRichText(s.result),
}));
}
7 changes: 4 additions & 3 deletions src/tools/testmanagement-utils/update-testcase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import { BrowserStackConfig } from "../../lib/types.js";
import { getTMBaseURL } from "../../lib/tm-base-url.js";
import { getBrowserStackAuth } from "../../lib/get-auth.js";
import { wrapRichText, wrapTestCaseSteps } from "./rich-text.js";
import logger from "../../logger.js";

export interface TestCaseUpdateRequest {
Expand Down Expand Up @@ -178,11 +179,11 @@ export async function updateTestCase(

if (params.name !== undefined) testCaseBody.name = params.name;
if (params.description !== undefined)
testCaseBody.description = params.description;
testCaseBody.description = wrapRichText(params.description);
if (params.preconditions !== undefined)
testCaseBody.preconditions = params.preconditions;
testCaseBody.preconditions = wrapRichText(params.preconditions);
if (params.test_case_steps !== undefined)
testCaseBody.test_case_steps = params.test_case_steps;
testCaseBody.test_case_steps = wrapTestCaseSteps(params.test_case_steps);
if (params.owner !== undefined) testCaseBody.owner = params.owner;
if (params.status !== undefined) testCaseBody.status = params.status;
if (params.tags !== undefined) testCaseBody.tags = params.tags;
Expand Down
96 changes: 96 additions & 0 deletions tests/tools/richText.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { describe, it, expect } from 'vitest';
import {
wrapRichText,
wrapTestCaseSteps,
} from '../../src/tools/testmanagement-utils/rich-text';

describe('wrapRichText', () => {
it('escapes and wraps bare text containing >', () => {
expect(wrapRichText('Navigate to Settings > Users')).toBe(
'<p>Navigate to Settings &gt; Users</p>',
);
});

it('escapes and wraps bare text containing <', () => {
expect(wrapRichText('if a < b then pass')).toBe(
'<p>if a &lt; b then pass</p>',
);
});

it('escapes and wraps bare text containing &', () => {
expect(wrapRichText('Q&A section loads')).toBe(
'<p>Q&amp;A section loads</p>',
);
});

it('preserves tag-like tokens that TM would otherwise strip', () => {
expect(wrapRichText('Press <Enter> to submit the form')).toBe(
'<p>Press &lt;Enter&gt; to submit the form</p>',
);
expect(wrapRichText('Use List<String> where A > B & C')).toBe(
'<p>Use List&lt;String&gt; where A &gt; B &amp; C</p>',
);
// Leading tag-like token that is NOT a TM-allowed tag is literal text too.
expect(wrapRichText('<Enter> key submits the form')).toBe(
'<p>&lt;Enter&gt; key submits the form</p>',
);
expect(wrapRichText('<h1>not an allowed TM tag</h1>')).toBe(
'<p>&lt;h1&gt;not an allowed TM tag&lt;/h1&gt;</p>',
);
});

it('leaves text without <, > or & untouched', () => {
expect(wrapRichText('Click "Save" then \'Confirm\'')).toBe(
'Click "Save" then \'Confirm\'',
);
});

it('leaves content starting with a TM-allowed tag untouched', () => {
expect(wrapRichText('<p>Settings &gt; Users</p>')).toBe(
'<p>Settings &gt; Users</p>',
);
expect(wrapRichText(' <ul><li>A &amp; B</li></ul>')).toBe(
' <ul><li>A &amp; B</li></ul>',
);
expect(wrapRichText('<br/>')).toBe('<br/>');
});

it('treats a leading < that is not a tag as literal text', () => {
expect(wrapRichText('< 5 items are shown')).toBe(
'<p>&lt; 5 items are shown</p>',
);
});

it('handles any characters, not just fixed strings', () => {
expect(wrapRichText('émojis 🎉 and ünïcode ≥ 5 stay bare')).toBe(
'émojis 🎉 and ünïcode ≥ 5 stay bare',
);
expect(wrapRichText('unicode plus html char: 温度 > 30°C')).toBe(
'<p>unicode plus html char: 温度 &gt; 30°C</p>',
);
expect(wrapRichText('line1 a > b\nline2 c < d')).toBe(
'<p>line1 a &gt; b\nline2 c &lt; d</p>',
);
expect(wrapRichText('a>=b && c<=d & "e"')).toBe(
'<p>a&gt;=b &amp;&amp; c&lt;=d &amp; "e"</p>',
);
});
});

describe('wrapTestCaseSteps', () => {
it('wraps step and result independently and preserves other keys', () => {
expect(
wrapTestCaseSteps([
{ step: 'Go to A > B', result: 'B page loads' },
{ step: 'Click Save', result: 'Count > 0' },
]),
).toEqual([
{ step: '<p>Go to A &gt; B</p>', result: 'B page loads' },
{ step: 'Click Save', result: '<p>Count &gt; 0</p>' },
]);
});

it('returns an empty array unchanged', () => {
expect(wrapTestCaseSteps([])).toEqual([]);
});
});
Loading