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
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
20 changes: 20 additions & 0 deletions src/tools/testmanagement-utils/rich-text.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// TM UI renders these fields as rich text only when HTML-wrapped;
Comment thread
SavioBS629 marked this conversation as resolved.
Outdated
const LOOKS_LIKE_HTML = /^\s*<[a-z][^>]*>/i;
Comment thread
SavioBS629 marked this conversation as resolved.
Outdated
const HAS_ENCODABLE_CHARS = /[<>&]/;

export function wrapRichText(text: string): string {
if (LOOKS_LIKE_HTML.test(text) || !HAS_ENCODABLE_CHARS.test(text)) {
return text;
}
return `<p>${text}</p>`;
Comment thread
SavioBS629 marked this conversation as resolved.
Outdated
}

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
77 changes: 77 additions & 0 deletions tests/tools/richText.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { describe, it, expect } from 'vitest';
import {
wrapRichText,
wrapTestCaseSteps,
} from '../../src/tools/testmanagement-utils/rich-text';

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

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

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

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

it('leaves already-HTML content 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>',
);
});

it('wraps text starting with < that is not a tag', () => {
expect(wrapRichText('< 5 items are shown')).toBe(
'<p>< 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: 温度 > 30°C</p>',
);
expect(wrapRichText('line1 a > b\nline2 c < d')).toBe(
'<p>line1 a > b\nline2 c < d</p>',
);
expect(wrapRichText('a>=b && c<=d & "e"')).toBe(
'<p>a>=b && c<=d & "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 > B</p>', result: 'B page loads' },
{ step: 'Click Save', result: '<p>Count > 0</p>' },
]);
});

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