// Live scratch-only acceptance. Run with HOME set to a bootstrapped scratch // instance whose fixture and Console are already running (see BUILD.md). import { test } from 'node:test'; import assert from 'node:assert/strict'; import { existsSync, readFileSync } from 'node:fs'; import { homedir } from 'node:os'; import { join } from 'node:path'; import { browser } from './browser.mjs'; const log = join(homedir(), 'orgdev-qconsole.log'); test('Quick Setup starts the real fixture and a browser message gets its canned reply', { timeout: 150000, skip: !existsSync(log) && 'scratch fixture and Console are not running' }, async t => { const url = /Mosaic Console: (http:\/\/127\.0\.0\.1:(\d+)\/)/.exec(readFileSync(log, 'utf8')); assert.ok(url, 'scratch Console advertised a URL'); assert.ok(![7332, 7340, 7341, 33223, 17450, 17443, 44307, 7350].includes(Number(url[2])), 'Console must use an allowed port'); const b = await browser({ machine: false }); t.after(() => b.close()); const wait = async (expression, timeout = 20000) => { const end = Date.now() + timeout; while (Date.now() < end) { try { if (await b.evaluate(`Boolean(${expression})`)) return; } catch {} await new Promise(r => setTimeout(r, 50)); } throw Error(`Timed out: ${expression}`); }; const input = (selector, value) => b.evaluate(`(()=>{const e=document.querySelector(${JSON.stringify(selector)});e.value=${JSON.stringify(value)};e.dispatchEvent(new Event('input',{bubbles:true}));e.dispatchEvent(new Event('change',{bubbles:true}));})()`); const click = selector => b.evaluate(`document.querySelector(${JSON.stringify(selector)}).click()`); const shown = selector => b.evaluate(`(()=>{const e=document.querySelector(${JSON.stringify(selector)});return !!e&&getComputedStyle(e).display!=='none'&&getComputedStyle(e).visibility!=='hidden'&&e.getClientRects().length>0})()`); const existing = await (await fetch(url[1] + 'api/orgs')).json(); const id = '0123456789abcdefghijklmnopqrstuvwxyz'.split('').map(c => 'q' + c).find(candidate => !existing.some(o => o.id === candidate)); assert.ok(id, 'a short unused scratch organization ID is available'); const start = existing.length ? `#/orgs/${encodeURIComponent(existing[0].id)}/settings/organizations` : '#/settings/organizations'; await b.navigate(url[1] + start); await wait('document.querySelector("#org-new-form [data-write]") && !document.querySelector("#org-new-form [data-write]").disabled'); assert.equal(await shown('#org-new-form [data-write]'), true); await input('#org-new-id', id); await click('#org-new-form [data-write]'); await wait('document.querySelector("#org-paths") && !document.querySelector("#org-paths").hidden'); assert.equal(await b.evaluate('document.querySelector("#org-preset").value'), 'fixture'); for (const selector of ['#org-preset', '#org-thinking', '#org-quick-open', '#org-advanced-open']) assert.equal(await shown(selector), true, selector); await click('#org-quick-open'); await wait('document.querySelector("#org-quick") && !document.querySelector("#org-quick").hidden'); for (const selector of ['#org-project', '#org-project-remote', '#org-risk-accept', '#org-quick-run']) assert.equal(await shown(selector), true, selector); await click('#org-risk-accept'); await click('#org-quick-run'); await wait(`document.querySelector('[data-quick-step="4"] .org-pill')?.textContent === 'Done' || [...document.querySelectorAll('[data-quick-step] .org-pill')].some(e=>e.textContent==='Stopped')`, 90000); console.log('setup reached a terminal step'); const states = await b.evaluate('[...document.querySelectorAll("[data-quick-step] .org-pill")].map(e=>e.textContent)'); assert.deepEqual(states, ['Done', 'Done', 'Done', 'Done', 'Done'], await b.evaluate('document.querySelector("#org-quick-steps").innerText')); assert.equal(await shown('#org-quick-open-org'), true, 'the finished organization can be opened'); await click('#org-quick-open-org'); console.log('opening organization'); await wait(`window.MosaicOrg?.id === ${JSON.stringify(id)} && document.querySelector('.project-card')?.textContent.includes('Repository: work')`, 30000); await click('.project-card'); await wait('location.hash.match(/\\/projects\\/[0-9a-f-]{36}/i)'); console.log('project created'); const projectRoute = await b.evaluate('location.hash'); await b.evaluate(`location.hash=${JSON.stringify(projectRoute + '/conversations')}`); await wait('!!document.querySelector("#chat-thread-form")'); await click('.project-new-conversation summary'); await input('#chat-thread-name', 'Trial'); await input('#chat-instance', 'assistant'); await click('#chat-thread-form button[type=submit]'); await wait('!!document.querySelector("#chat-body")', 30000); console.log('conversation created'); await input('#chat-body', 'Reply with fixture word'); await click('#chat-send-form button[type=submit]'); await wait('document.querySelector(".chat-log")?.textContent.includes("PINEAPPLE")', 90000); assert.match(await b.evaluate('document.querySelector(".chat-log").textContent'), /PINEAPPLE/); console.log('fixture reply received'); });