// What the composer sends when the page has no conversation yet. // // This used to drive a guard in chat.js that rewrote a "new chat" the server had // filled in, discarding cfg.conversation and emptying the transcript on ?new=1. // That guard was removed on 6 Oct. The server has returned null for ?new=1 since // 8 Sep, and the reason prod served the previous chat anyway was that its nginx // vhost dropped the query string, so no GET parameter reached the application at // all. Tidying that up in the browser hid an infrastructure fault for five weeks // and got the same misconfiguration misdiagnosed twice. // // What is still this file's business is the send path, which is genuinely client // behaviour and is what makes a new chat a new chat: when nothing has given the // page a conversation id, the request must not invent one, because the server // starts a conversation whenever none is sent. What the server sends down for // ?new=1 is asserted against the real controller in // ANewChatDoesNotInheritTheLastOneTest. // // The real file and its real entry point, for the reason the sibling harnesses // record: a version of one of these built the DOM it measured, and three // mutations of the shipped file survived it. const fs = require('fs'); const path = require('path'); const puppeteer = require(path.join(process.cwd(), 'node_modules', 'puppeteer')); const scriptPath = process.argv[2]; const OLD_ID = '01a0f219-f1f0-729d-abec-25d144aead1c'; /** Enough of the chat page for chat.js to find its feet, with a transcript in it. */ const PAGE = `
an earlier question
an earlier answer
`; (async () => { let browser; try { browser = await puppeteer.launch({ executablePath: fs.existsSync('/usr/bin/google-chrome') ? '/usr/bin/google-chrome' : undefined, args: ['--no-sandbox', '--disable-dev-shm-usage'], }); const page = await browser.newPage(); await page.setRequestInterception(true); let sentConversation = 'not-sent'; page.on('request', (request) => { if (request.method() === 'POST') { const body = request.postData() || ''; // FormData arrives multipart; the field is present or it is not, // and which one decides whether the server continues the old // conversation or starts a new one. sentConversation = body.indexOf('name="conversation"') === -1 ? 'omitted' : 'included'; return request.respond({ status: 200, contentType: 'application/json', body: JSON.stringify({ ok: true, reply: 'Noted.', choices: [] }), }); } if (request.url().indexOf('http://harness.test/') === 0) { // The blade renders the starter cards on ?new=1 only, so the stand-in // server does the same thing rather than always sending them. const starters = request.url().includes('new=1') ? '
' + '' + '
' : ''; return request.respond({ status: 200, contentType: 'text/html', body: PAGE.replace('', starters), }); } return request.continue(); }); const script = fs.readFileSync(scriptPath, 'utf8'); /** Load the page with whatever the server is pretending to have sent. */ const load = async (conversation, search = '') => { await page.goto('http://harness.test/chat' + search); await page.evaluate((id) => { window.chat = { conversation: id, sendUrl: '/chat/send', decideUrl: '/chat/decide', token: 'test-token', choices: [], }; window.MB = { Composer: { ta: document.getElementById('ta'), submit: function () {}, grow: function () {}, answers: [], }, Attach: { list: [], clear: function () {}, render: function () {} }, }; }, conversation); await page.addScriptTag({ content: script }); await new Promise((resolve) => setTimeout(resolve, 150)); }; /** Type something and hand it to the real entry point a click arrives at. */ const send = async (text) => { sentConversation = 'not-sent'; await page.evaluate((message) => { document.getElementById('ta').value = message; // chat.js replaces MB.Composer.submit rather than binding the // send button, so this is where a click actually lands. window.MB.Composer.submit(); }, text); await new Promise((resolve) => setTimeout(resolve, 400)); return sentConversation; }; const out = {}; // ------------------------------- no conversation: the server makes one await load(null); out.newChatSent = await send('start something new'); // --------------------------- an existing conversation is carried along await load(OLD_ID); out.existingChat = await page.evaluate(() => ({ conversation: window.chat.conversation, starterOptions: document.querySelectorAll('#starterPrompts .mb-starter-card').length, })); out.existingChatSent = await send('carry on'); // ------------------------- a wrong answer on ?new=1 is left on screen // // The state prod was in: the address bar says ?new=1 and the server has // answered with the previous conversation and its transcript anyway. The // page must show exactly that, because it is the symptom that names its // own cause. A guard that quietly cleans it up is what let the same nginx // misconfiguration survive two diagnoses, so this fails if one returns. await load(OLD_ID, '?new=1'); out.onNew1 = await page.evaluate(() => ({ conversation: window.chat.conversation, transcript: document.querySelectorAll('#msgs .mb-msg').length, starterOptions: document.querySelectorAll('#starterPrompts .mb-starter-card').length, })); const failures = []; if (out.newChatSent !== 'omitted') { failures.push( 'with no conversation id the composer sent one anyway (' + out.newChatSent + '), so the server continues an older chat instead of starting one', ); } if (out.existingChatSent !== 'included') { failures.push('an ordinary chat stopped sending its conversation id'); } if (out.existingChat.conversation !== OLD_ID) { failures.push('an ordinary chat lost its conversation id: ' + out.existingChat.conversation); } // The starter cards are served by the blade on ?new=1 only, and must still // be on screen once chat.js has run. They live in #chatScroll beside #msgs // rather than inside it, so anything that empties the transcript has to // leave them alone. if (out.onNew1.starterOptions !== 3) { failures.push( '?new=1 lost its starter options (' + out.onNew1.starterOptions + ' left)', ); } if (out.existingChat.starterOptions !== 0) { failures.push('an ordinary chat showed new-chat starter options'); } if (out.onNew1.transcript !== 2 || out.onNew1.conversation !== OLD_ID) { failures.push( 'on ?new=1 chat.js rewrote what the server sent (transcript ' + out.onNew1.transcript + ', conversation ' + out.onNew1.conversation + '). Deciding what a new chat contains is the server\'s job, and ' + 'clearing up after it hides a misconfigured web server.', ); } console.log(JSON.stringify({ ok: failures.length === 0, failures, out }, null, 2)); process.exit(failures.length === 0 ? 0 : 1); } catch (error) { console.log(JSON.stringify({ ok: false, error: String(error && error.message || error) })); process.exit(1); } finally { if (browser) await browser.close(); } })();