Skip to content

Commit a73f28d

Browse files
ihor-sokoliukclaude
andcommitted
fix(tests): make response mocks' text/json consistent + exact-match URL assertion (BUG-008)
The body-consumption fix (cc6fae6) reads the response body as text once then JSON.parses it. The test mocks modelled json() and text() as independent fields (text often '' while json held data), so a text-first read failed on them. Make the shared mock helpers and the one inline integration mock return text that mirrors json — a real Response exposes a single body for both. Also replace the createEmptyContentWarning URL .includes() assertion with full-message exact equality, clearing a CodeQL incomplete-URL-sanitization false positive. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 96f9b6a commit a73f28d

3 files changed

Lines changed: 20 additions & 4 deletions

File tree

__tests__/helpers/mock-fetch.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,17 @@ export function createMockFetch(options: FetchMockOptions = {}) {
3535
ok,
3636
status,
3737
statusText,
38-
text: async () => body,
38+
// text() and json() come from one body on a real Response — mirror json
39+
// into text when only json is given so a text-first reader stays consistent.
40+
text: async () => {
41+
if (body) {
42+
return body;
43+
}
44+
if (json !== null) {
45+
return JSON.stringify(json);
46+
}
47+
return '';
48+
},
3949
json: async () => {
4050
if (json !== null) {
4151
return json;
@@ -64,7 +74,7 @@ export function createCapturingMockFetch() {
6474
ok: true,
6575
status: 200,
6676
statusText: 'OK',
67-
text: async () => '<html><body>Test</body></html>',
77+
text: async () => JSON.stringify({ results: [] }),
6878
json: async () => ({ results: [] })
6979
} as Response;
7080
};

__tests__/integration/mcp-handlers.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,8 @@ async function runTests() {
187187
let capturedUrl = '';
188188
fetchMocker.mock(async (url, _opts) => {
189189
capturedUrl = url as string;
190-
return { ok: true, json: async () => ({ results: [{ title: 'R', url: 'https://x.com', content: 'c', score: 1 }] }), text: async () => '' } as any;
190+
const body = JSON.stringify({ results: [{ title: 'R', url: 'https://x.com', content: 'c', score: 1 }] });
191+
return { ok: true, json: async () => JSON.parse(body), text: async () => body } as any;
191192
});
192193
const { client } = await connect();
193194

__tests__/unit/error-handler.test.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,12 @@ async function runTests() {
118118

119119
await testFunction('createEmptyContentWarning includes the URL', () => {
120120
const warning = createEmptyContentWarning('https://test.com');
121-
assert.ok(warning.includes('https://test.com'));
121+
// Exact-match the full message (not url.includes) — a substring URL check
122+
// trips CodeQL's incomplete-URL-sanitization rule and asserts less anyway.
123+
assert.equal(
124+
warning,
125+
'📄 Content Warning: Page fetched but appears empty after conversion (https://test.com). May contain only media or require JavaScript.'
126+
);
122127
}, results);
123128

124129
await testFunction('validateEnvironment success', () => {

0 commit comments

Comments
 (0)