84 lines
4.1 KiB
TypeScript
84 lines
4.1 KiB
TypeScript
/**
|
|
* search-merge — Ollama(1순위) + SearXNG 병행 결과 합치기
|
|
*
|
|
* 표본은 2026-09-30 실측: Ollama는 3건에 본문 통째, SearXNG는 건수 많고 본문 짧음.
|
|
* 고정하는 것: Ollama가 앞, 중복 URL 제거, 본문 상한(34만자 사고 방지), SearXNG 건수 상한.
|
|
*/
|
|
import { test, describe } from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { mergeSearchResults, normalizeUrlKey, renderMergedStdout } from '../src/tools/search-merge';
|
|
|
|
const OPTS = { maxSecondary: 5, primaryCharCap: 6000, secondaryCharCap: 400 };
|
|
const it = (url: string, snippet = 'x'.repeat(50), title = 't') => ({ title, url, snippet });
|
|
|
|
describe('normalizeUrlKey', () => {
|
|
test('프로토콜·www·끝 슬래시·해시·추적 파라미터를 무시한다', () => {
|
|
const a = normalizeUrlKey('https://www.news1.kr/a/b/?utm_source=x&id=3#top');
|
|
const b = normalizeUrlKey('http://news1.kr/a/b?id=3');
|
|
assert.equal(a, b);
|
|
});
|
|
test('id 같은 실제 파라미터는 구분한다', () => {
|
|
assert.notEqual(normalizeUrlKey('https://a.com/p?id=1'), normalizeUrlKey('https://a.com/p?id=2'));
|
|
});
|
|
});
|
|
|
|
describe('mergeSearchResults', () => {
|
|
test('Ollama 결과가 앞에 온다', () => {
|
|
const m = mergeSearchResults([it('https://o.com/1')], [it('https://s.com/1'), it('https://s.com/2')], OPTS);
|
|
assert.deepEqual(m.map((r) => r.url), ['https://o.com/1', 'https://s.com/1', 'https://s.com/2']);
|
|
});
|
|
|
|
test('겹치는 URL은 한 번만 나오고, SearXNG 쪽 본문이 더 길면 채워 넣는다', () => {
|
|
const m = mergeSearchResults([it('https://www.a.com/x', 'short')], [it('http://a.com/x/', 'a much longer snippet from the other engine')], OPTS);
|
|
assert.equal(m.length, 1);
|
|
assert.equal(m[0].snippet, 'a much longer snippet from the other engine');
|
|
});
|
|
|
|
test('본문 상한: 34만자 결과가 상한에서 잘린다', () => {
|
|
const m = mergeSearchResults([it('https://st.com/p', 'y'.repeat(340_000))], [], OPTS);
|
|
assert.equal(m[0].snippet.length, 6000);
|
|
});
|
|
|
|
test('SearXNG는 건수 상한과 본문 상한을 지킨다', () => {
|
|
const many = Array.from({ length: 30 }, (_, i) => it(`https://s.com/${i}`, 'z'.repeat(900)));
|
|
const m = mergeSearchResults([], many, OPTS);
|
|
assert.equal(m.length, 5);
|
|
assert.equal(m[0].snippet.length, 400);
|
|
});
|
|
|
|
test('한쪽이 비어도 다른 쪽 결과만으로 돌려준다', () => {
|
|
assert.equal(mergeSearchResults([], [it('https://s.com/1')], OPTS).length, 1);
|
|
assert.equal(mergeSearchResults([it('https://o.com/1')], [], OPTS).length, 1);
|
|
assert.equal(mergeSearchResults([], [], OPTS).length, 0);
|
|
});
|
|
});
|
|
|
|
describe('renderMergedStdout', () => {
|
|
test('[N] 제목 / URL / 본문 형식을 유지한다 — 다른 가드가 이 형식을 파싱한다', () => {
|
|
const out = renderMergedStdout([it('https://a.com', 'body text that is long enough to count', 'T1'), it('https://b.com', 'second body text that is long enough', 'T2')]);
|
|
assert.match(out, /^\[1\] T1\n https:\/\/a\.com\n body text/);
|
|
assert.match(out, /\n\n\[2\] T2\n/);
|
|
});
|
|
});
|
|
|
|
import { routesThroughWebTool, WEB_TOOL_ROUTED_PROVIDERS } from '../src/tools/search-merge';
|
|
describe('routesThroughWebTool — 채팅 검색 라우터', () => {
|
|
test('ollama_cloud가 web.ts로 라우팅된다 — 빠지면 Tavily 폴백으로 새 401 (2026-09-30 사고)', () => {
|
|
assert.equal(routesThroughWebTool('ollama_cloud'), true);
|
|
assert.deepEqual([...WEB_TOOL_ROUTED_PROVIDERS].sort(), ['ollama', 'ollama_cloud', 'searxng']);
|
|
});
|
|
test('직접 API를 쓰는 provider는 라우팅하지 않는다', () => {
|
|
assert.equal(routesThroughWebTool('tavily'), false);
|
|
assert.equal(routesThroughWebTool('google'), false);
|
|
});
|
|
});
|
|
|
|
import { normalizePreferredProvider } from '../src/tools/search-merge';
|
|
describe('normalizePreferredProvider', () => {
|
|
test("설정 화면의 'ollama'는 ollama_cloud로 — 안 그러면 허용 목록 밖이라 ddg로 떨어진다", () => {
|
|
assert.equal(normalizePreferredProvider('ollama'), 'ollama_cloud');
|
|
assert.equal(normalizePreferredProvider('ollama_cloud'), 'ollama_cloud');
|
|
assert.equal(normalizePreferredProvider('searxng'), 'searxng');
|
|
});
|
|
});
|