支持免登录游客聊天并隔离历史与附件
This commit is contained in:
@@ -80,7 +80,7 @@ async function customerPage(pageOptions = {}){
|
||||
fetch: async (url, options) => {
|
||||
calls.push({url, body:options && options.body && (typeof options.body === 'string' ? JSON.parse(options.body) : options.body)});
|
||||
if(hooks[url]) return hooks[url](options);
|
||||
if(url.startsWith('/api/human/poll') && hooks.poll) return hooks.poll(url);
|
||||
if(url.startsWith('/api/visitor/human/poll') && hooks.poll) return hooks.poll(url);
|
||||
if(url.startsWith('/api/get_messages') && hooks.messages) return hooks.messages(url);
|
||||
if(url === '/stream_chat'){
|
||||
let read = false;
|
||||
@@ -91,14 +91,15 @@ async function customerPage(pageOptions = {}){
|
||||
}})}};
|
||||
}
|
||||
const data = url === '/api/new_chat' ? {id:nextConvId++, welcome:'welcome'}
|
||||
: url === '/api/human/request' ? {ok:true, session_id:7}
|
||||
: url.startsWith('/api/human/poll') ? state
|
||||
: ['/api/rate', '/api/human/send', '/api/human/close', '/api/ticket'].includes(url) || url.startsWith('/api/del_history') ? {ok:true}
|
||||
: url === '/api/visitor/session' ? {ok:true,key:'1'.repeat(64)}
|
||||
: url === '/api/visitor/human/request' ? {ok:true, session_id:7}
|
||||
: url.startsWith('/api/visitor/human/poll') ? state
|
||||
: ['/api/rate', '/api/visitor/human/send', '/api/visitor/human/close', '/api/ticket'].includes(url) || url.startsWith('/api/del_history') ? {ok:true}
|
||||
: url === '/api/get_history' ? (pageOptions.history || []) : [];
|
||||
return {ok:true, json:async () => data};
|
||||
},
|
||||
});
|
||||
for(const file of ['support.js', 'voice.js', 'chat.js']){
|
||||
for(const file of ['support.js', 'visitor.js', 'voice.js', 'chat.js']){
|
||||
vm.runInContext(fs.readFileSync(path.join(__dirname, '../../static/js', file), 'utf8'), context, {filename:file});
|
||||
if(file === 'voice.js'){
|
||||
const createVoice=context.ChatVoice.create;
|
||||
@@ -362,7 +363,7 @@ test('all three footer questions leave the human queue and send to separate AI c
|
||||
assert.equal(requests.length, 3);
|
||||
assert.equal(new Set(requests.map(call => call.body.conv_id)).size, 3);
|
||||
assert.deepEqual(requests.map(call => call.body.msg), ['查询订单状态', '欧洲海运专线报价', '包裹异常处理']);
|
||||
assert.equal(app.calls.filter(call => call.url === '/api/human/send').length, 0);
|
||||
assert.equal(app.calls.filter(call => call.url === '/api/visitor/human/send').length, 0);
|
||||
assert.equal(app.context.window.humanSid, 0);
|
||||
assert.equal(app.element('human-session').hidden, true);
|
||||
});
|
||||
@@ -377,7 +378,7 @@ test('welcome and hot question buttons use the same AI routing as the footer', a
|
||||
}
|
||||
const requests = app.calls.filter(call => call.url === '/stream_chat');
|
||||
assert.deepEqual(requests.map(call => call.body.msg), ['欧洲海运专线报价', '如何申请理赔?']);
|
||||
assert.equal(app.calls.filter(call => call.url === '/api/human/send').length, 0);
|
||||
assert.equal(app.calls.filter(call => call.url === '/api/visitor/human/send').length, 0);
|
||||
});
|
||||
|
||||
test('the welcome contact button opens the human queue and does not send an AI question', async () => {
|
||||
@@ -386,7 +387,7 @@ test('the welcome contact button opens the human queue and does not send an AI q
|
||||
button.setAttribute('data-human-support', '');
|
||||
app.element('chatDom').listeners.click({target:button});
|
||||
await flush();
|
||||
assert.equal(app.calls.filter(call => call.url === '/api/human/request').length, 1);
|
||||
assert.equal(app.calls.filter(call => call.url === '/api/visitor/human/request').length, 1);
|
||||
assert.equal(app.calls.filter(call => call.url === '/stream_chat').length, 0);
|
||||
assert.equal(app.element('human-session').hidden, false);
|
||||
});
|
||||
@@ -397,7 +398,7 @@ test('new chat clears human routing and the next quote is sent to AI', async ()
|
||||
await app.context.window.fillInput('请报价,30KG发往美国FBA仓');
|
||||
await flush();
|
||||
assert.equal(app.calls.find(call => call.url === '/stream_chat').body.msg, '请报价,30KG发往美国FBA仓');
|
||||
assert.equal(app.calls.filter(call => call.url === '/api/human/send').length, 0);
|
||||
assert.equal(app.calls.filter(call => call.url === '/api/visitor/human/send').length, 0);
|
||||
assert.equal(app.context.window.__busy, false);
|
||||
});
|
||||
|
||||
@@ -454,7 +455,7 @@ test('a late history response cannot overwrite the latest quick conversation', a
|
||||
test('a delayed human connection cannot capture a newly selected AI topic', async () => {
|
||||
const app = await customerPage({human:false});
|
||||
let resolve;
|
||||
app.hooks['/api/human/request'] = () => new Promise(done => { resolve=done; });
|
||||
app.hooks['/api/visitor/human/request'] = () => new Promise(done => { resolve=done; });
|
||||
const connecting = app.context.window.showHuman();
|
||||
await flush();
|
||||
await app.click(app.quickButtons[1]);
|
||||
@@ -468,7 +469,7 @@ test('a delayed human connection cannot capture a newly selected AI topic', asyn
|
||||
test('repeated contact clicks create one human request and show a visible queue state', async () => {
|
||||
const app = await customerPage({human:false});
|
||||
await Promise.all([app.context.window.showHuman(), app.context.window.showHuman()]);
|
||||
assert.equal(app.calls.filter(call => call.url === '/api/human/request').length, 1);
|
||||
assert.equal(app.calls.filter(call => call.url === '/api/visitor/human/request').length, 1);
|
||||
assert.equal(app.element('human-session').hidden, false);
|
||||
assert.match(app.element('human-session-label').textContent, /正在排队/);
|
||||
});
|
||||
@@ -476,7 +477,7 @@ test('repeated contact clicks create one human request and show a visible queue
|
||||
test('the contact shortcut outside service hours submits a ticket without entering human mode', async () => {
|
||||
const app = await customerPage({human:false, hour:23, offlineMessage:'请明天跟进报价'});
|
||||
await app.context.window.showHuman();
|
||||
assert.equal(app.calls.filter(call => call.url === '/api/human/request').length, 0);
|
||||
assert.equal(app.calls.filter(call => call.url === '/api/visitor/human/request').length, 0);
|
||||
assert.equal(app.calls.find(call => call.url === '/api/ticket').body.content, '请明天跟进报价');
|
||||
assert.match(app.element('chatDom').textContent, /留言已提交/);
|
||||
assert.equal(app.context.window.humanSid, 0);
|
||||
@@ -484,7 +485,7 @@ test('the contact shortcut outside service hours submits a ticket without enteri
|
||||
|
||||
test('a failed human send preserves the text and makes retry possible', async () => {
|
||||
const app = await customerPage();
|
||||
app.hooks['/api/human/send'] = () => response({ok:false}, 503);
|
||||
app.hooks['/api/visitor/human/send'] = () => response({ok:false}, 503);
|
||||
await app.context.window.fillInput('请确认这批货的价格');
|
||||
assert.equal(app.element('input-text').value, '请确认这批货的价格');
|
||||
assert.match(app.element('composer-status').textContent, /内容已保留/);
|
||||
@@ -494,10 +495,10 @@ test('a failed human send preserves the text and makes retry possible', async ()
|
||||
test('a duplicate human send cannot submit an in-flight message twice', async () => {
|
||||
const app = await customerPage();
|
||||
let resolve;
|
||||
app.hooks['/api/human/send'] = () => new Promise(done => { resolve=done; });
|
||||
app.hooks['/api/visitor/human/send'] = () => new Promise(done => { resolve=done; });
|
||||
const first = app.context.window.fillInput('请确认时效');
|
||||
await app.context.window.fillInput('请确认时效');
|
||||
assert.equal(app.calls.filter(call => call.url === '/api/human/send').length, 1);
|
||||
assert.equal(app.calls.filter(call => call.url === '/api/visitor/human/send').length, 1);
|
||||
resolve(response({ok:true}));
|
||||
await first;
|
||||
assert.equal(app.element('input-text').value, '');
|
||||
@@ -508,7 +509,7 @@ test('returning to AI explicitly closes the human session and preserves the conv
|
||||
await app.click(app.element('return-ai'));
|
||||
await app.context.window.fillInput('请继续解答');
|
||||
await flush();
|
||||
assert.equal(app.calls.filter(call => call.url === '/api/human/close').length, 1);
|
||||
assert.equal(app.calls.filter(call => call.url === '/api/visitor/human/close').length, 1);
|
||||
assert.equal(app.calls.find(call => call.url === '/stream_chat').body.conv_id, 1);
|
||||
assert.equal(app.context.window.humanSid, 0);
|
||||
});
|
||||
@@ -639,7 +640,7 @@ test('recognized voice uses the normal AI and human send routes with a visible u
|
||||
const app=await customerPage({human,width:390});
|
||||
await app.voiceOptions.onText('语音识别的物流问题');
|
||||
await flush();
|
||||
const call=app.calls.find(call=>call.url===(human ? '/api/human/send' : '/stream_chat'));
|
||||
const call=app.calls.find(call=>call.url===(human ? '/api/visitor/human/send' : '/stream_chat'));
|
||||
assert.ok(call);
|
||||
assert.equal(human ? call.body.content : call.body.msg,'语音识别的物流问题');
|
||||
assert.match(app.element('chatDom').querySelector('.user-message').textContent,/语音识别的物流问题/);
|
||||
@@ -682,7 +683,7 @@ test('an AI regeneration is not forwarded to a human session', async () => {
|
||||
await app.context.window.fillInput('之前的问题');await flush();
|
||||
const retry=app.element('chatDom').querySelectorAll('.msg-action-btn').find(button=>button.dataset.action==='retry');
|
||||
await app.context.window.showHuman();await app.click(retry);
|
||||
assert.equal(app.calls.filter(call=>call.url==='/api/human/send').length,0);
|
||||
assert.equal(app.calls.filter(call=>call.url==='/api/visitor/human/send').length,0);
|
||||
assert.match(app.element('chatDom').textContent,/测试 AI 回复/);
|
||||
assert.match(app.element('composer-status').textContent,/先返回 AI/);
|
||||
});
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
const test=require('node:test');
|
||||
const assert=require('node:assert/strict');
|
||||
const fs=require('node:fs');
|
||||
const path=require('node:path');
|
||||
const vm=require('node:vm');
|
||||
const {webcrypto}=require('node:crypto');
|
||||
const flush=()=>new Promise(setImmediate);
|
||||
|
||||
function sessionPage(options={}){
|
||||
const storage=options.storage || new Map(),calls=[],timers=new Map();let timerId=0;
|
||||
const response=(data,status=200)=>({ok:status>=200&&status<300,status,json:async()=>data});
|
||||
const context=vm.createContext({console,AbortController,Uint8Array,
|
||||
window:{crypto:webcrypto},
|
||||
localStorage:{getItem:key=>{if(options.storageBlocked) throw Error();return storage.get(key);},
|
||||
setItem:(key,value)=>{if(options.storageBlocked) throw Error();storage.set(key,value);}},
|
||||
setTimeout:(callback,delay)=>{const id=++timerId;timers.set(id,{callback,delay});return id;},clearTimeout:id=>timers.delete(id),
|
||||
fetch:async(url,init)=>{calls.push({url,init});
|
||||
if(options.fetch) return options.fetch(url,init,response);
|
||||
return url==='/api/visitor/session' ? response({ok:true,key:JSON.parse(init.body).key || 'f'.repeat(64)}) : response([]);
|
||||
},
|
||||
});
|
||||
vm.runInContext(fs.readFileSync(path.join(__dirname,'../../static/js/visitor.js'),'utf8'),context);
|
||||
return {fetch:context.VisitorSession.fetch,calls,storage,
|
||||
expire:async()=>{for(const timer of [...timers.values()])timer.callback();await flush();}};
|
||||
}
|
||||
|
||||
test('a local random key is restored across reloads and differs in another browser',async()=>{
|
||||
const a=sessionPage();await a.fetch('/api/get_history');
|
||||
const key=JSON.parse(a.calls[0].init.body).key;
|
||||
assert.match(key,/^[0-9a-f]{64}$/);
|
||||
const restored=sessionPage({storage:a.storage});await restored.fetch('/api/get_history');
|
||||
assert.equal(JSON.parse(restored.calls[0].init.body).key,key);
|
||||
const b=sessionPage();await b.fetch('/api/get_history');
|
||||
assert.notEqual(JSON.parse(b.calls[0].init.body).key,key);
|
||||
assert.equal(a.calls[1].init.credentials,'same-origin');
|
||||
assert.equal(a.calls.some(call=>call.url.includes(key)),false);
|
||||
});
|
||||
|
||||
test('concurrent requests wait for one bootstrap before sending customer data',async()=>{
|
||||
let release;
|
||||
const app=sessionPage({fetch:(url,init,response)=>url==='/api/visitor/session'
|
||||
? new Promise(resolve=>{release=()=>resolve(response({ok:true,key:JSON.parse(init.body).key}));}) : response([])});
|
||||
const a=app.fetch('/api/get_history'),b=app.fetch('/api/new_chat',{method:'POST'});
|
||||
assert.equal(app.calls.length,1);
|
||||
release();await Promise.all([a,b]);
|
||||
assert.equal(app.calls.filter(call=>call.url==='/api/visitor/session').length,1);
|
||||
assert.equal(app.calls.length,3);
|
||||
});
|
||||
|
||||
test('blocked local storage uses a server cookie without falling back to global history',async()=>{
|
||||
const app=sessionPage({storageBlocked:true});await app.fetch('/api/get_history');
|
||||
assert.equal(JSON.parse(app.calls[0].init.body).key,null);
|
||||
assert.equal(app.calls[1].init.credentials,'same-origin');
|
||||
const failed=sessionPage({fetch:(url,init,response)=>response({ok:false},503)});
|
||||
await assert.rejects(failed.fetch('/api/get_history'));
|
||||
assert.equal(failed.calls.length,1);
|
||||
});
|
||||
|
||||
test('expired sessions rebootstrap once and retry the original request once',async()=>{
|
||||
let attempts=0;
|
||||
const app=sessionPage({fetch:(url,init,response)=>url==='/api/visitor/session'
|
||||
? response({ok:true,key:JSON.parse(init.body).key}) : response([],++attempts===1 ? 401 : 200)});
|
||||
const result=await app.fetch('/api/get_history');
|
||||
assert.equal(result.status,200);
|
||||
assert.equal(app.calls.filter(call=>call.url==='/api/visitor/session').length,2);
|
||||
assert.equal(attempts,2);
|
||||
});
|
||||
|
||||
test('a cancelled request waiting for bootstrap does not upload its body',async()=>{
|
||||
let release;
|
||||
const app=sessionPage({fetch:(url,init,response)=>new Promise(resolve=>{
|
||||
release=()=>resolve(response({ok:true,key:JSON.parse(init.body).key}));
|
||||
})});
|
||||
const controller=new AbortController();
|
||||
const pending=app.fetch('/api/upload',{method:'POST',body:'private file',signal:controller.signal});
|
||||
controller.abort();release();
|
||||
await assert.rejects(pending,{name:'AbortError'});
|
||||
assert.equal(app.calls.length,1);
|
||||
});
|
||||
Reference in New Issue
Block a user