Skip to content

Commit ecb2d3f

Browse files
committed
fix: normalize discovered source URLs
1 parent e34bc26 commit ecb2d3f

2 files changed

Lines changed: 9 additions & 6 deletions

File tree

‎src/lib/ai/client.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,10 @@ function parseDiscoveredSources(value: unknown): DiscoveredSource[] {
113113

114114
const title = typeof candidate.title === 'string' ? candidate.title.trim() : '';
115115
const url = typeof candidate.url === 'string' ? candidate.url.trim() : '';
116-
if (!title || !validateUrl(url)) return [];
116+
const normalizedUrl = validateUrl(url);
117+
if (!title || !normalizedUrl) return [];
117118

118-
const source: DiscoveredSource = { title, url };
119+
const source: DiscoveredSource = { title, url: normalizedUrl };
119120
if (typeof candidate.content === 'string') source.content = candidate.content;
120121

121122
if (typeof candidate.sourceType === 'string' && candidate.sourceType.trim()) {

‎src/test/ai/client-branches.test.ts‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -259,10 +259,10 @@ describe('AI client branch coverage', () => {
259259
title: 'Source A',
260260
content: 'A content',
261261
type: 'url',
262-
origin: 'https://a.com',
262+
origin: 'https://a.com/',
263263
folder: 'Auto-saved',
264264
isActive: true,
265-
metadata: expect.objectContaining({ capturedVia: 'generate', url: 'https://a.com' }),
265+
metadata: expect.objectContaining({ capturedVia: 'generate', url: 'https://a.com/' }),
266266
});
267267
});
268268

@@ -311,6 +311,7 @@ describe('AI client branch coverage', () => {
311311
qualityLabel: 'unknown',
312312
query: 42,
313313
},
314+
{ title: 'Protocol-less source', url: 'example.com/source-two' },
314315
{ title: '', url: 'https://example.com/rejected' },
315316
{ title: 'Unsafe protocol', url: 'httpx://example.com/rejected' },
316317
],
@@ -322,6 +323,7 @@ describe('AI client branch coverage', () => {
322323

323324
expect(outcome.discoveredSources).toEqual([
324325
{ title: 'Source with malformed metadata', url: 'https://example.com/source' },
326+
{ title: 'Protocol-less source', url: 'https://example.com/source-two' },
325327
]);
326328
expect(addSourceMock).toHaveBeenCalledWith(expect.objectContaining({
327329
title: 'Source with malformed metadata',
@@ -428,10 +430,10 @@ describe('AI client branch coverage', () => {
428430
title: 'Source C',
429431
content: 'C content',
430432
type: 'url',
431-
origin: 'https://c.com',
433+
origin: 'https://c.com/',
432434
folder: 'Auto-saved',
433435
isActive: true,
434-
metadata: expect.objectContaining({ capturedVia: 'brainstorm', url: 'https://c.com' }),
436+
metadata: expect.objectContaining({ capturedVia: 'brainstorm', url: 'https://c.com/' }),
435437
});
436438
});
437439

0 commit comments

Comments
 (0)