Files
homeclaw/tests/workshop-chat-stream.test.ts
T

58 lines
3.1 KiB
TypeScript

/**
* workshop-chat.js applyStreamEvent — 스트림 이벤트를 말풍선에 반영
*
* 사건(2026-09-30): 작업실 채팅에서 도구를 부른 턴의 응답이 두 번 보였다. 서버는 도구 호출 전 라운드의
* token도 그대로 흘려보내는데, 화면은 final 이벤트를 무시하고 이어 붙이기만 했다.
*/
import { test, describe } from 'node:test';
import assert from 'node:assert/strict';
import fs from 'fs';
import path from 'path';
const src = fs.readFileSync(path.join(__dirname, '../web-ui/js/app/workshop-chat.js'), 'utf8');
const m = /function applyStreamEvent\(st,ev\)\{[\s\S]*?\n\}\n/.exec(src);
if (!m) throw new Error('applyStreamEvent를 찾지 못했습니다');
// eslint-disable-next-line no-new-func
const applyStreamEvent: (st: any, ev: any) => boolean = new Function(`${m[0]}; return applyStreamEvent;`)();
const run = (events: any[]) => { const st = { textBuf: '', thinkingActive: false }; for (const e of events) applyStreamEvent(st, e); return st; };
describe('applyStreamEvent', () => {
test('토큰은 이어 붙인다(기본 스트리밍)', () => {
assert.equal(run([{ type: 'token', text: '안녕' }, { type: 'text_delta', text: '하세요' }]).textBuf, '안녕하세요');
});
test('도구 호출 전 문장과 최종 답이 겹쳐 나와도 final로 한 번만 보인다 — 사건의 재현', () => {
const st = run([
{ type: 'token', text: '주문 완료하셨군요! ' }, // 1라운드: 도구 호출 전 말
{ type: 'tool_call', name: 'workshop_project' },
{ type: 'token', text: '주문 완료하셨군요! ' }, // 2라운드: 최종 답
{ type: 'token', text: '수량을 업데이트했습니다.' },
{ type: 'final', text: '주문 완료하셨군요! 수량을 업데이트했습니다.' },
]);
assert.equal(st.textBuf, '주문 완료하셨군요! 수량을 업데이트했습니다.');
});
test('final이 없으면(옛 서버·중단) 스트리밍된 내용을 그대로 둔다', () => {
assert.equal(run([{ type: 'token', text: 'ab' }, { type: 'tool_call' }]).textBuf, 'ab');
});
test('빈 final은 무시한다 — 스트림으로 받은 답을 지우지 않는다', () => {
assert.equal(run([{ type: 'token', text: '내용' }, { type: 'final', text: ' ' }]).textBuf, '내용');
assert.equal(run([{ type: 'token', text: '내용' }, { type: 'final' }]).textBuf, '내용');
});
test('thinking_status는 생각 중 표시만 바꾸고 텍스트는 건드리지 않는다', () => {
const st = run([{ type: 'token', text: 'x' }, { type: 'thinking_status', active: true }]);
assert.equal(st.thinkingActive, true);
assert.equal(st.textBuf, 'x');
const done = run([{ type: 'thinking_status', active: true }, { type: 'final', text: '끝' }]);
assert.equal(done.thinkingActive, false);
});
test('알 수 없는 이벤트는 상태를 바꾸지 않는다', () => {
assert.equal(applyStreamEvent({ textBuf: '', thinkingActive: false }, { type: 'info' }), false);
assert.equal(applyStreamEvent({ textBuf: '', thinkingActive: false }, null), false);
});
});