Files
homeclaw/tests/prompt-gates.test.ts
T
kimandClaude Opus 5 2ba39bca76 v4.3.21: 테스트 인프라 복구 — npm test 실행 가능하게 + 회귀 테스트 30개
tests/ 디렉터리가 비어 있었고 package.json의 두 스크립트가 삭제된 파일을
가리킨 채 방치돼 있었음:
- test    → tests/test-v2.ts (존재하지 않음)
- gateway → src/gateway/server-v2.ts (v4.3.5에서 삭제됨)

즉 npm test가 몇 달간 실행 자체가 불가능한 상태였고, 그 대가로 프롬프트 게이트
정규식을 고칠 때마다 node -e 임시 스크립트를 손으로 짜고 버리는 일이 반복됐음
(2026-07-29 하루에만 5회). 회귀 방지는 하나도 남지 않았음.

- Node 22 내장 러너(node:test) + tsx 사용, 테스트 프레임워크 의존성 없음
- tests/prompt-gates.test.ts (25) — 모델 동작을 강제/억제하는 정규식 게이트 전반
- tests/usage-log.test.ts (5)  — Ollama 외 provider의 토큰 집계 단일 지점
- tests/README.md — 무엇을 왜 테스트하는지, 케이스 작성 규칙, 다음 확장 대상
- 전체 30개 통과, 0.6초

부수 성과 — 테스트가 실제 버그를 찾음:
케이스를 쓰면서 프로덕션 원문을 한 단어(GPU) 줄여 썼더니 실패했고, 그게 진짜
구멍이었음. "토큰 처리 속도 손실이 약 15%~25%"처럼 하드웨어 명사가 없는 처리량
날조는 SPEC_CONTEXT_KEYWORD에 걸리지 않아 그대로 통과하고 있었음 — 원문에
우연히 들어있던 "GPU" 덕에 잡히던 것. 토큰/대역폭/추론/처리속도를 키워드에
추가해 막고, 그 케이스를 테스트로 고정함(강수확률·할인율 오탐 없음 확인).

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-29 15:29:02 +09:00

211 lines
9.4 KiB
TypeScript

/**
* prompt-gates.test.ts
*
* Regression tests for the hardcoded regex gates that force/suppress model behavior.
* These are pure string->boolean functions with no I/O, which makes them the highest-value
* thing in the codebase to pin down: every one of them exists because a real production
* failure got through, and a silent regex regression re-opens that exact hole with no
* crash and no stack trace to notice it by.
*
* Cases marked [2026-07-29] come from a production log audit of a GPU/PCIe hardware chat
* where the model fabricated tok/s figures and benchmark percentages. They were verified by
* hand against the real transcript before being written here — do not "simplify" them into
* synthetic strings, the awkward phrasing is the point.
*/
import { test, describe } from 'node:test';
import assert from 'node:assert/strict';
import {
looksLikeUnverifiedSpecClaim,
isExemptFromVerification,
isFactualInfoRequest,
isLiveDataRequest,
isNewsRequest,
isExecutionLikeRequest,
isGreetingLikeMessage,
isUsableGroundingResult,
claimsMessageSent,
isMessagingRequest,
isCancelIntent,
isRerunIntent,
} from '../src/gateway/guards/prompt-gates';
describe('looksLikeUnverifiedSpecClaim — 출력에서 검증 안 된 수치 감지', () => {
test('명시적 스펙 단위는 단독으로 걸린다', () => {
assert.equal(looksLikeUnverifiedSpecClaim('약 70~80GB 정도 필요합니다.'), true);
assert.equal(looksLikeUnverifiedSpecClaim('31 tok/s가 나옵니다.'), true);
assert.equal(looksLikeUnverifiedSpecClaim('약 15 TFLOPS 수준입니다.'), true);
});
test('[2026-07-29] 단위 없는 "N토큰"도 걸린다 — /초 접미사 없는 실제 사례', () => {
assert.equal(
looksLikeUnverifiedSpecClaim(
'PCIe 3.0 슬롯의 대역폭 한계로 인해 GPU 간 통신 병목이 발생하여, 70B나 31B 같은 대형 모델 구동 시 15토큰 안팎의 속도가 나오는 것이 맞습니다.',
),
true,
);
});
test('[2026-07-29] 퍼센트 벤치마크는 스펙 문맥과 함께일 때 걸린다', () => {
assert.equal(
looksLikeUnverifiedSpecClaim(
'RTX 5060 Ti는 PCIe 5.0 x8 규격이라 PCIe 3.0 환경에서 구동할 때 성능 손실은 약 5% 안팎 수준입니다.',
),
true,
);
// 프로덕션 원문 그대로 — "GPU"가 들어있어서 스펙 문맥으로 걸린다.
assert.equal(
looksLikeUnverifiedSpecClaim(
'RTX 5060 Ti 2장을 묶어 텐서 병렬(Tensor Parallelism)로 LLM을 돌릴 때, PCIe 3.0 환경은 4.0에 비해 GPU 간 통신 병목으로 인해 토큰 처리 속도 손실이 약 15%~25% 정도까지 벌어집니다.',
),
true,
);
});
test('[2026-07-29-2] 하드웨어 명사 없는 처리량 주장도 걸린다 — 이 테스트가 찾아낸 구멍', () => {
// 위 원문을 한 단어(GPU) 빼고 바꿔 쓰면 통과해버리던 케이스.
// SPEC_CONTEXT_KEYWORD에 토큰/대역폭/추론/처리속도를 추가해 막았다.
assert.equal(
looksLikeUnverifiedSpecClaim(
'PCIe 3.0은 4.0 대비 토큰 처리 속도 손실이 약 15%~25%까지 벌어집니다.',
),
true,
);
assert.equal(
looksLikeUnverifiedSpecClaim('메모리 대역폭이 약 40% 낮습니다.'),
true,
);
});
test('퍼센트가 스펙 문맥 없이 쓰이면 걸리지 않는다 (오탐 방지)', () => {
// % 를 STRICT tier에 넣었다면 이 셋이 전부 헛검색을 유발했을 것 — 이 테스트가 그 회귀를 막는다.
assert.equal(looksLikeUnverifiedSpecClaim('오늘 강수확률은 20%입니다.'), false);
assert.equal(looksLikeUnverifiedSpecClaim('이 상품은 지금 30% 할인 중이에요.'), false);
assert.equal(looksLikeUnverifiedSpecClaim('배터리가 15% 남았습니다.'), false);
});
test('일반 대화/숫자 없는 설명은 걸리지 않는다', () => {
assert.equal(looksLikeUnverifiedSpecClaim('저는 GLM 모델이며, 1+1은 2입니다.'), false);
assert.equal(looksLikeUnverifiedSpecClaim('오늘 날씨는 20도 정도입니다.'), false);
assert.equal(looksLikeUnverifiedSpecClaim('이 노트북은 90만원 정도 합니다.'), false);
assert.equal(looksLikeUnverifiedSpecClaim('트랜스포머는 어텐션 기반 구조입니다.'), false);
});
test('한국어 헤지 표현은 숫자 없이도 걸린다', () => {
assert.equal(looksLikeUnverifiedSpecClaim('대략 그 정도일 것으로 예상됩니다.'), true);
assert.equal(looksLikeUnverifiedSpecClaim('정확하진 않지만 그렇게 추정됩니다.'), true);
});
test('코드블록 안의 숫자는 무시한다', () => {
assert.equal(looksLikeUnverifiedSpecClaim('아래 코드입니다:\n```py\nSIZE_GB = 80\n```'), false);
});
test('빈 입력/널은 안전하게 false', () => {
assert.equal(looksLikeUnverifiedSpecClaim(''), false);
assert.equal(looksLikeUnverifiedSpecClaim(null as any), false);
assert.equal(looksLikeUnverifiedSpecClaim(undefined as any), false);
});
});
describe('isExemptFromVerification — 개인 인프라 별칭', () => {
test('사용자 개인 서버 별칭은 검색 강제에서 면제', () => {
assert.equal(isExemptFromVerification('지서버는 PCIe 3.0인가?'), true);
assert.equal(isExemptFromVerification('클로서버 사양 알려줘'), true);
});
test('일반 제품 질문은 면제 아님', () => {
assert.equal(isExemptFromVerification('RTX 5090 스펙 알려줘'), false);
});
});
describe('isFactualInfoRequest — 검증이 필요한 질문 분류', () => {
test('비교/스펙/가격 질문을 잡는다', () => {
assert.equal(isFactualInfoRequest('A100 vs V100 비교'), true);
assert.equal(isFactualInfoRequest('RTX 5090 가격은?'), true);
assert.equal(isFactualInfoRequest('실제 속도는 얼마나 되나 비교 도표'), true);
});
test('코딩/실행 요청은 제외 (검색이 아니라 실행할 일)', () => {
assert.equal(isFactualInfoRequest('로그인 페이지 만들어줘'), false);
assert.equal(isFactualInfoRequest('이 버그 고쳐줘'), false);
});
test('개인 인프라 질문은 제외 — 검색해도 나올 수 없음', () => {
assert.equal(isFactualInfoRequest('지서버 사양 비교해줘'), false);
});
test('인사말은 제외', () => {
assert.equal(isFactualInfoRequest('hello'), false);
assert.equal(isFactualInfoRequest(''), false);
});
});
describe('isNewsRequest / isLiveDataRequest', () => {
test('뉴스 요청을 잡는다', () => {
assert.equal(isNewsRequest('오늘 미국 주요 뉴스'), true);
assert.equal(isNewsRequest('속보 있어?'), true);
});
test('"무슨 소식 있어?"류 안부 인사는 뉴스가 아니다', () => {
// 이 관용구 예외가 없으면 잡담이 news_search 강제로 이어진다.
assert.equal(isNewsRequest('무슨 좋은 소식 있어?'), false);
assert.equal(isNewsRequest('별 소식 없지?'), false);
});
test('안부 관용구 + 실제 뉴스 키워드가 섞이면 뉴스로 본다', () => {
assert.equal(isNewsRequest('오늘 무슨 뉴스 있어?'), true);
});
test('날씨/환율 등 라이브 데이터', () => {
assert.equal(isLiveDataRequest('내일 구미 날씨'), true);
assert.equal(isLiveDataRequest('환율 알려줘'), true);
assert.equal(isLiveDataRequest('파이썬 리스트 정렬법'), false);
});
});
describe('isUsableGroundingResult — 빈 검색결과 판별', () => {
test('실제 내용이 있으면 사용 가능', () => {
assert.equal(isUsableGroundingResult({ result: '[1] 어떤 기사 제목' }), true);
});
test('에러/빈문자/센티넬은 사용 불가 — 이게 false여야 정직한 재답변이 강제된다', () => {
assert.equal(isUsableGroundingResult({ error: true, result: 'x' }), false);
assert.equal(isUsableGroundingResult({ result: '' }), false);
assert.equal(isUsableGroundingResult({ result: ' ' }), false);
assert.equal(isUsableGroundingResult({ result: '(no articles found)' }), false);
assert.equal(isUsableGroundingResult({ result: '에 대한 검색 결과가 없습니다.' }), false);
});
});
describe('메시징 완료 주장 검증', () => {
test('전송 요청을 잡는다', () => {
assert.equal(isMessagingRequest('이메일 보내줘'), true);
assert.equal(isMessagingRequest('카톡으로 전송해줘'), true);
assert.equal(isMessagingRequest('이메일 읽어줘'), false);
});
test('보냈다는 주장을 잡는다 — 도구 호출 없이 이러면 재확인이 강제된다', () => {
assert.equal(claimsMessageSent('메일 보냈습니다.'), true);
assert.equal(claimsMessageSent('전송 완료했어요.'), true);
assert.equal(claimsMessageSent('보낼까요?'), false);
});
});
describe('의도 분류', () => {
test('취소/재실행 의도', () => {
assert.equal(isCancelIntent('취소해'), true);
assert.equal(isCancelIntent('그만해'), true);
assert.equal(isRerunIntent('다시 실행해줘'), true);
assert.equal(isRerunIntent('안녕'), false);
});
});
describe('보조 분류기', () => {
test('실행형 요청 / 인사말', () => {
assert.equal(isExecutionLikeRequest('파일 만들어줘'), true);
assert.equal(isExecutionLikeRequest('오늘 날씨'), false);
assert.equal(isGreetingLikeMessage('hello'), true);
assert.equal(isGreetingLikeMessage('hello, search the web for X'), false);
});
});