feat(support-chat): infer intent with model and remove scope tabs

This commit is contained in:
devops committed 2026-10-14 19:20:41 +00:00
1 parent e0581f22aa
commit 33afd7f45a
10 files changed
+110 -68

No files matched your search

@@ -5,13 +5,14 @@ import { render, screen, fireEvent, waitFor, cleanup } from '@testing-library/re
import SupportChat from './SupportChat';
afterEach(() => { cleanup(); vi.restoreAllMocks(); localStorage.clear(); });
describe('support widget', () => {
it('submits only message and explicit scope, renders text safely', async () => {
it('submits only message without scope tabs, renders text safely', async () => {
const fetcher=vi.spyOn(globalThis,'fetch').mockResolvedValue({ok:true,json:async()=>({answer:'<script>bad</script>'})} as Response);
render(<SupportChat/>); fireEvent.click(screen.getByRole('button',{name:/گفتگو با پشتیبان/}));
fireEvent.change(screen.getByLabelText('پیام شما'),{target:{value:'مکمل سگ'}}); fireEvent.click(screen.getByRole('button',{name:'ارسال'}));
await waitFor(()=>expect(screen.getByText('<script>bad</script>')).toBeTruthy());
expect(JSON.parse(fetcher.mock.calls[0][1]!.body as string)).toEqual({message:'مکمل سگ',scope:'public'});
expect(JSON.parse(fetcher.mock.calls[0][1]!.body as string)).toEqual({message:'مکمل سگ'});
expect(document.querySelector('script')).toBeNull();
for (const label of ['محصولات و عمومی','سفارش‌های من','کیف پول']) expect(screen.queryByRole('button',{name:label})).toBeNull();
});
it('does not expose upstream errors', async()=> {
vi.spyOn(globalThis,'fetch').mockResolvedValue({ok:false,status:503,json:async()=>({message:'secret-provider'})} as Response);
@@ -13,7 +13,6 @@ const token = () => {
export default function SupportChat() {
const [open, setOpen] = useState(false);
const [message, setMessage] = useState('');
const [scope, setScope] = useState<'public' | 'orders' | 'wallet'>('public');
const [busy, setBusy] = useState(false);
const [rows, setRows] = useState<{ question: string; answer: string }[]>([]);
const identity = useRef('');
@@ -28,7 +27,6 @@ export default function SupportChat() {
pending.current?.abort();
setRows([]);
setMessage('');
setScope('public');
setBusy(false);
}
};
@@ -72,7 +70,7 @@ export default function SupportChat() {
...(current ? { Authorization: `Bearer ${current}` } : {})
},
credentials: 'include',
body: JSON.stringify({ message: question, scope }),
body: JSON.stringify({ message: question }),
signal: AbortSignal.any([controller.signal, AbortSignal.timeout(50000)])
});
if (response.status === 401) {
@@ -151,43 +149,6 @@ export default function SupportChat() {
</button>
</div>
{/* Scope Selector Bar */}
<div className="bg-medical-gray-100 p-2 border-b border-medical-gray-200 flex items-center gap-1">
<button
type="button"
onClick={() => setScope('public')}
className={`flex-1 py-1.5 text-xs font-medium rounded-lg transition-all ${
scope === 'public'
? 'bg-white text-canina-blue shadow-sm font-bold border border-medical-gray-300'
: 'text-medical-gray-600 hover:text-medical-gray-900 hover:bg-white/50'
}`}
>
محصولات و عمومی
</button>
<button
type="button"
onClick={() => setScope('orders')}
className={`flex-1 py-1.5 text-xs font-medium rounded-lg transition-all ${
scope === 'orders'
? 'bg-white text-canina-blue shadow-sm font-bold border border-medical-gray-300'
: 'text-medical-gray-600 hover:text-medical-gray-900 hover:bg-white/50'
}`}
>
سفارش‌های من
</button>
<button
type="button"
onClick={() => setScope('wallet')}
className={`flex-1 py-1.5 text-xs font-medium rounded-lg transition-all ${
scope === 'wallet'
? 'bg-white text-canina-blue shadow-sm font-bold border border-medical-gray-300'
: 'text-medical-gray-600 hover:text-medical-gray-900 hover:bg-white/50'
}`}
>
کیف پول
</button>
</div>
{/* Message Area */}
<div
ref={chatScrollRef}