// Local or deployed browser smoke. Requires playwright-core in NODE_PATH and Chrome. // All hosted AI requests are stubbed in browser; never spends provider credits. const assert = require('node:assert/strict'); const { createHash } = require('node:crypto'); const { chromium } = require('playwright-core'); const url=process.env.LUMEN_TEST_URL || 'http://127.0.0.1:4391'; (async () => { const browser=await chromium.launch({headless:true,executablePath:process.env.CHROME||'/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'}); try { const context=await browser.newContext({acceptDownloads:true,viewport:{width:390,height:844}}); const p=await context.newPage(); p.setDefaultTimeout(15000); await p.goto(url+'/studio'); await p.getByRole('heading',{name:'Make it yours.'}).waitFor(); await p.getByRole('button',{name:'AI tools'}).click(); await p.getByText('AI generation isn’t available right now.',{exact:false}).waitFor(); assert.equal(await p.getByRole('button',{name:'Generate image'}).isDisabled(),true); await p.getByRole('button',{name:'Adjust',exact:true}).click(); const fixture=Buffer.from(await p.evaluate(()=>{ const c=document.createElement('canvas'); c.width=16;c.height=8; const ctx=c.getContext('2d');ctx.fillStyle='rgb(80,80,80)';ctx.fillRect(0,0,16,8); return c.toDataURL('image/png').split(',')[1]; }),'base64'); await p.locator('input[type=file]').setInputFiles({name:'synthetic.png',mimeType:'image/png',buffer:fixture}); await p.getByText('16 × 8',{exact:true}).waitFor(); const sourcePixel=()=>p.locator('canvas').evaluate(c=>c.getContext('2d').getImageData(1,1,1,1).data[0]); assert.equal(await sourcePixel(),80); await p.getByRole('slider').first().evaluate(el=>{Object.getOwnPropertyDescriptor(HTMLInputElement.prototype,'value').set.call(el,'150'); el.dispatchEvent(new Event('input',{bubbles:true})); el.dispatchEvent(new Event('change',{bubbles:true}));}); assert.ok(await sourcePixel()>80); await p.getByLabel('Crop').selectOption('square'); await p.getByText('8 × 8',{exact:true}).waitFor(); await p.getByRole('button',{name:'Rotate 90°'}).click(); const downloadPromise=p.waitForEvent('download'); await p.getByRole('button',{name:'Download PNG'}).click(); const download=await downloadPromise; assert.match(download.suggestedFilename(),/\.png$/); assert.equal(await p.evaluate(()=>document.documentElement.scrollWidth<=innerWidth),true); console.log('PASS: mobile-width upload, live color adjustment, crop, rotation, PNG download, no sign-in and fail-closed AI'); const ai=await browser.newPage(); ai.setDefaultTimeout(10000); const operations=[]; await ai.route('**/api/studio',async route=>{ const req=route.request(); if(req.method()==='GET') return route.fulfill({json:{ready:true,signedIn:true,signupAvailable:true,feedbackAvailable:true,maxUploadBytes:8000000,maxPixels:10000000,models:[{id:'gpt-image-2.6-sunburst',label:'OpenAI Image',available:true},{id:'gemini-3-pro-image',label:'Gemini Image',available:true}]}}); const payload=req.postDataBuffer().toString('utf8'); operations.push(payload); if(payload.includes('\r\n\r\nfeedback\r\n')) return route.fulfill({json:{feedback:'Try a slightly wider crop.'}}); return route.fulfill({status:200,contentType:'image/png',body:fixture}); }); await ai.goto(url+'/studio'); await ai.getByRole('button',{name:'AI tools'}).click(); await ai.getByRole('combobox').selectOption('gemini-3-pro-image'); await ai.getByLabel('Describe the image').fill('A synthetic monochrome test image'); await ai.getByRole('button',{name:'Generate image',exact:true}).click(); await ai.getByText('Image ready.',{exact:false}).waitFor(); await ai.getByLabel('What should change?').fill('Make the test image square'); await ai.getByRole('button',{name:'Revise image',exact:true}).click(); await ai.getByText('Image ready.',{exact:false}).waitFor(); await ai.getByRole('button',{name:'Get visual feedback'}).click(); await ai.getByText('Try a slightly wider crop.').waitFor(); assert.equal(operations.length,3); assert.ok(operations[0].includes('gemini-3-pro-image')); assert.ok(!operations[0].includes('studio-reference.jpg')); assert.ok(operations[1].includes('studio-reference.jpg')); console.log('PASS: selected Gemini model, generation, revision attachment, feedback rendered (provider calls mocked)'); if (process.env.TEST_SELF_SERVICE_PRINT==='true') { const print=await browser.newPage({viewport:{width:390,height:850}}); print.setDefaultTimeout(10000); let quoteHash; let checkoutCount=0; function multipart(req) { return new Request('https://smoke.invalid',{method:'POST',headers: req.headers(),body:req.postDataBuffer()}).formData(); } async function fileDigest(form) { return createHash('sha256').update(Buffer.from(await form.get('image').arrayBuffer())).digest('hex'); } await print.route('**/api/studio/print/quote',async route=>{ if(route.request().method()==='GET') return route.fulfill({json:{enabled:true,signedIn:true}}); const form=await multipart(route.request()); quoteHash=await fileDigest(form); return route.fulfill({json:{quoteToken:'synthetic-quote',amountCents:5800,currency:'USD',selection:JSON.parse(form.get('selection')), width:1200,height:1200,artworkSha256:quoteHash,expiresAt:Date.now()+10000}}); }); await print.route('**/api/studio/print/checkout',async route=>{ const form=await multipart(route.request()); checkoutCount++; assert.equal(await fileDigest(form),quoteHash); assert.equal(form.get('approved'),'true'); assert.equal(form.get('termsAccepted'),'true'); assert.equal(form.get('quoteToken'),'synthetic-quote'); // Fail-closed rather than sending the browser to even a fake payment site. return route.fulfill({json:{url:'https://checkout.example.invalid/never-pay'}}); }); await print.goto(url+'/studio'); await print.locator('input[type=file]').setInputFiles({name:'synthetic.png',mimeType:'image/png',buffer:fixture}); await print.getByText('16 × 8',{exact:true}).waitFor(); await print.getByLabel('Crop').selectOption('square'); await print.getByText('8 × 8',{exact:true}).waitFor(); await print.getByRole('button',{name:'Print',exact:true}).click(); await print.getByRole('button',{name:'Preview print and price'}).click(); await print.getByRole('img',{name:'Exact image to be sent to the printer'}).waitFor(); await print.getByText('$58.00', {exact:true}).waitFor(); const pay=print.getByRole('button',{name:'Continue to payment'}); assert.equal(await pay.isDisabled(),true); await print.getByLabel('I approve this exact image and print selection.').check(); assert.equal(await pay.isDisabled(),true); await print.getByRole('checkbox').nth(1).check(); await pay.click(); await print.getByText('Checkout returned an unexpected address.').waitFor(); assert.equal(checkoutCount,1); console.log('PASS: print proof and price, explicit approval plus terms, exact same artwork bytes at checkout and unexpected payment host blocked (mocked)'); } } finally { await browser.close(); } })().catch(err=>{console.error(err);process.exitCode=1;});