fix(диалоги): в чужой диалог не пишем — человеку выдаётся свой новый

Идентификатор диалога мог остаться в памяти браузера от прежней учётки, и
запись снова склеила бы две переписки. Теперь сервер отбивает такую отправку,
выдаёт новый идентификатор и пишет след в журнал, а страница молча переезжает
на него и повторяет вопрос один раз.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SrxPccQVeKJc7dTH9ejt7M
This commit is contained in:
anor
2026-09-11 03:05:46 +10:00
co-authored by Claude Opus 5
parent e77d2cbb9c
commit 4c23c90554
5 changed files with 174 additions and 9 deletions
+38
View File
@@ -226,3 +226,41 @@ def test_banner_fits_narrow_screen(ctx, site):
pg.wait_for_selector("#chat-screen", state="visible")
pg.wait_for_timeout(700)
assert pg.evaluate("document.body.scrollWidth <= document.body.clientWidth + 1")
def test_page_moves_to_new_tab_on_conflict(ctx, site):
"""Сервер сказал «диалог чужой, вот новый» — страница переезжает и повторяет вопрос.
Порча: не разбирать отказ 409 → человек упрётся в ошибку и потеряет вопрос."""
pg = ctx.new_page()
pg.set_default_timeout(8000)
pg.goto(site + "a.html")
pg.wait_for_selector("#chat-screen", state="visible")
pg.wait_for_timeout(400)
pg.evaluate("""() => {
window._posted = [];
const orig = window.fetch;
window.fetch = async (url, opts={}) => {
if (String(url).includes('/api/chat')) {
const tab = (opts.headers||{})['X-Tab-Id'] || (opts.headers||{})['x-tab-id'];
window._posted.push(tab);
if (window._posted.length === 1) {
return new Response(JSON.stringify({error:'чужой', new_tab:'svezhii-dialog'}),
{status:409, headers:{'Content-Type':'application/json'}});
}
const enc = new TextEncoder();
return new Response(new ReadableStream({start(c){
c.enqueue(enc.encode('event: text\\ndata: {"text":"ответ на своё"}\\n\\n'));
c.enqueue(enc.encode('event: done\\ndata: {"result":"ответ на своё"}\\n\\n'));
c.close(); }}), {status:200, headers:{'Content-Type':'text/event-stream'}});
}
return orig(url, opts);
};
document.getElementById('msg-input').value = 'вопрос в чужой диалог';
document.getElementById('send-btn').click();
}""")
pg.wait_for_function("window._posted && window._posted.length === 2", timeout=10000)
posted = pg.evaluate("window._posted")
assert posted[1] == "svezhii-dialog", posted
assert pg.evaluate("tabId") == "svezhii-dialog"
assert pg.evaluate("localStorage.getItem('tabId:a@x')") == "svezhii-dialog"
assert "ответ на своё" in pg.inner_text("#messages")