File size: 7,090 Bytes
c92aa92 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | import axios from 'axios';
import { fetchWebContent } from '../engines/web/index.js';
type TestCase = {
name: string;
run: () => Promise<void>;
};
const originalAxiosGet = axios.get.bind(axios);
const originalAxiosHead = axios.head.bind(axios);
function installAxiosMock(): void {
(axios as any).head = async (url: string) => {
if (url.endsWith('/too-large.md')) {
return {
headers: { 'content-length': String(5 * 1024 * 1024) },
request: { res: { responseUrl: url } }
};
}
if (url.endsWith('/long.md')) {
return {
headers: { 'content-length': String(1024) },
request: { res: { responseUrl: url } }
};
}
return {
headers: {},
request: { res: { responseUrl: url } }
};
};
(axios as any).get = async (url: string) => {
if (url.endsWith('/skill.md')) {
return {
headers: { 'content-type': 'text/plain; charset=utf-8' },
data: '# Skill Title\n\nThis is a markdown test document.',
request: { res: { responseUrl: url } }
};
}
if (url.endsWith('/page')) {
return {
headers: { 'content-type': 'text/html; charset=utf-8' },
data: `
<html>
<head><title>Skill Page</title></head>
<body>
<main>
<h1>Skill Page</h1>
<p>${'Skill body content '.repeat(12)}</p>
</main>
</body>
</html>
`,
request: { res: { responseUrl: `${url}?from=test` } }
};
}
if (url.endsWith('/long.md')) {
return {
headers: { 'content-type': 'text/markdown; charset=utf-8' },
data: `# Long\n\n${'x'.repeat(6000)}`,
request: { res: { responseUrl: url } }
};
}
if (url.endsWith('/too-large.md')) {
throw new Error('GET should not be called when HEAD indicates oversized response');
}
if (url.endsWith('/spa')) {
return {
headers: { 'content-type': 'text/html; charset=utf-8' },
data: `
<html>
<head>
<title>SPA Site</title>
<meta name="description" content="Rendered by JS runtime">
</head>
<body>
<div id="root"></div>
</body>
</html>
`,
request: { res: { responseUrl: url } }
};
}
throw new Error(`Unexpected mocked URL: ${url}`);
};
}
function restoreAxiosMock(): void {
(axios as any).get = originalAxiosGet;
(axios as any).head = originalAxiosHead;
}
function assert(condition: unknown, message: string): void {
if (!condition) {
throw new Error(message);
}
}
async function runCase(testCase: TestCase): Promise<boolean> {
try {
await testCase.run();
console.log(`✅ ${testCase.name}`);
return true;
} catch (error) {
console.error(`❌ ${testCase.name}:`, error);
return false;
}
}
async function main(): Promise<void> {
installAxiosMock();
const testCases: TestCase[] = [
{
name: 'should parse markdown content by .md URL',
run: async () => {
const result = await fetchWebContent('https://example.com/skill.md', 5000);
assert(result.title === '', 'markdown title should be empty');
assert(result.content.includes('Skill Title'), 'markdown content should keep source text');
assert(result.truncated === false, 'markdown should not be truncated');
}
},
{
name: 'should extract text and title from html page',
run: async () => {
const result = await fetchWebContent('https://example.com/page', 5000);
assert(result.title === 'Skill Page', 'html title should be extracted');
assert(result.finalUrl.endsWith('/page?from=test'), 'finalUrl should follow redirect target');
assert(result.content.includes('Skill body content'), 'html content should be extracted');
}
},
{
name: 'should truncate long content when maxChars is small',
run: async () => {
const result = await fetchWebContent('https://example.com/long.md', 1200);
assert(result.truncated === true, 'long content should be truncated');
assert(result.content.includes('[...truncated '), 'truncation marker should exist');
}
},
{
name: 'should fallback to metadata for js-rendered html pages',
run: async () => {
const result = await fetchWebContent('https://example.com/spa', 5000);
assert(result.title === 'SPA Site', 'title should be extracted from html');
assert(result.content.includes('Rendered by JS runtime'), 'meta description fallback should be used');
}
},
{
name: 'should reject non-http protocol',
run: async () => {
let failed = false;
try {
await fetchWebContent('file:///tmp/skill.md', 5000);
} catch {
failed = true;
}
assert(failed, 'file protocol should be rejected');
}
},
{
name: 'should reject private/local network targets',
run: async () => {
let failed = false;
try {
await fetchWebContent('http://127.0.0.1/private', 5000);
} catch {
failed = true;
}
assert(failed, 'private network target should be rejected');
}
},
{
name: 'should reject oversized response by content-length',
run: async () => {
let failed = false;
try {
await fetchWebContent('https://example.com/too-large.md', 5000);
} catch {
failed = true;
}
assert(failed, 'oversized response should be rejected');
}
}
];
let passed = 0;
for (const testCase of testCases) {
if (await runCase(testCase)) {
passed += 1;
}
}
restoreAxiosMock();
const total = testCases.length;
console.log(`\nResult: ${passed}/${total} passed`);
if (passed !== total) {
process.exit(1);
}
}
main().catch((error) => {
restoreAxiosMock();
console.error('❌ test-web-content failed:', error);
process.exit(1);
});
|