Branchwell

Lumen & Gesso / website / scripts / test-studio-browser.cjs

1// Local or deployed browser smoke. Requires playwright-core in NODE_PATH and Chrome.
2// All hosted AI requests are stubbed in browser; never spends provider credits.
3const assert = require('node:assert/strict');
4const { createHash } = require('node:crypto');
5const { chromium } = require('playwright-core');
6const url=process.env.LUMEN_TEST_URL || 'http://127.0.0.1:4391';
7(async () => {
8 const browser=await chromium.launch({headless:true,executablePath:process.env.CHROME||'/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'});
9 try {
10 const context=await browser.newContext({acceptDownloads:true,viewport:{width:390,height:844}});
11 const p=await context.newPage(); p.setDefaultTimeout(15000);
12 await p.goto(url+'/studio');
13 await p.getByRole('heading',{name:'Make it yours.'}).waitFor();
14 await p.getByRole('button',{name:'AI tools'}).click();
15 await p.getByText('AI generation isn’t available right now.',{exact:false}).waitFor();
16 assert.equal(await p.getByRole('button',{name:'Generate image'}).isDisabled(),true);
17 await p.getByRole('button',{name:'Adjust',exact:true}).click();
18 const fixture=Buffer.from(await p.evaluate(()=>{
19 const c=document.createElement('canvas'); c.width=16;c.height=8;
20 const ctx=c.getContext('2d');ctx.fillStyle='rgb(80,80,80)';ctx.fillRect(0,0,16,8);
21 return c.toDataURL('image/png').split(',')[1];
22 }),'base64');
23 await p.locator('input[type=file]').setInputFiles({name:'synthetic.png',mimeType:'image/png',buffer:fixture});
24 await p.getByText('16 × 8',{exact:true}).waitFor();
25 const sourcePixel=()=>p.locator('canvas').evaluate(c=>c.getContext('2d').getImageData(1,1,1,1).data[0]);
26 assert.equal(await sourcePixel(),80);
27 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}));});
28 assert.ok(await sourcePixel()>80);
29 await p.getByLabel('Crop').selectOption('square');
30 await p.getByText('8 × 8',{exact:true}).waitFor();
31 await p.getByRole('button',{name:'Rotate 90°'}).click();
32 const downloadPromise=p.waitForEvent('download');
33 await p.getByRole('button',{name:'Download PNG'}).click();
34 const download=await downloadPromise;
35 assert.match(download.suggestedFilename(),/\.png$/);
36 assert.equal(await p.evaluate(()=>document.documentElement.scrollWidth<=innerWidth),true);
37 console.log('PASS: mobile-width upload, live color adjustment, crop, rotation, PNG download, no sign-in and fail-closed AI');
38 const ai=await browser.newPage(); ai.setDefaultTimeout(10000);
39 const operations=[];
40 await ai.route('**/api/studio',async route=>{
41 const req=route.request();
42 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}]}});
43 const payload=req.postDataBuffer().toString('utf8');
44 operations.push(payload);
45 if(payload.includes('\r\n\r\nfeedback\r\n')) return route.fulfill({json:{feedback:'Try a slightly wider crop.'}});
46 return route.fulfill({status:200,contentType:'image/png',body:fixture});
47 });
48 await ai.goto(url+'/studio');
49 await ai.getByRole('button',{name:'AI tools'}).click();
50 await ai.getByRole('combobox').selectOption('gemini-3-pro-image');
51 await ai.getByLabel('Describe the image').fill('A synthetic monochrome test image');
52 await ai.getByRole('button',{name:'Generate image',exact:true}).click();
53 await ai.getByText('Image ready.',{exact:false}).waitFor();
54 await ai.getByLabel('What should change?').fill('Make the test image square');
55 await ai.getByRole('button',{name:'Revise image',exact:true}).click();
56 await ai.getByText('Image ready.',{exact:false}).waitFor();
57 await ai.getByRole('button',{name:'Get visual feedback'}).click();
58 await ai.getByText('Try a slightly wider crop.').waitFor();
59 assert.equal(operations.length,3);
60 assert.ok(operations[0].includes('gemini-3-pro-image'));
61 assert.ok(!operations[0].includes('studio-reference.jpg'));
62 assert.ok(operations[1].includes('studio-reference.jpg'));
63 console.log('PASS: selected Gemini model, generation, revision attachment, feedback rendered (provider calls mocked)');
64 if (process.env.TEST_SELF_SERVICE_PRINT==='true') {
65 const print=await browser.newPage({viewport:{width:390,height:850}});
66 print.setDefaultTimeout(10000);
67 let quoteHash;
68 let checkoutCount=0;
69 function multipart(req) { return new Request('https://smoke.invalid',{method:'POST',headers: req.headers(),body:req.postDataBuffer()}).formData(); }
70 async function fileDigest(form) { return createHash('sha256').update(Buffer.from(await form.get('image').arrayBuffer())).digest('hex'); }
71 await print.route('**/api/studio/print/quote',async route=>{
72 if(route.request().method()==='GET') return route.fulfill({json:{enabled:true,signedIn:true}});
73 const form=await multipart(route.request());
74 quoteHash=await fileDigest(form);
75 return route.fulfill({json:{quoteToken:'synthetic-quote',amountCents:5800,currency:'USD',selection:JSON.parse(form.get('selection')),
76 width:1200,height:1200,artworkSha256:quoteHash,expiresAt:Date.now()+10000}});
77 });
78 await print.route('**/api/studio/print/checkout',async route=>{
79 const form=await multipart(route.request()); checkoutCount++;
80 assert.equal(await fileDigest(form),quoteHash);
81 assert.equal(form.get('approved'),'true'); assert.equal(form.get('termsAccepted'),'true');
82 assert.equal(form.get('quoteToken'),'synthetic-quote');
83 // Fail-closed rather than sending the browser to even a fake payment site.
84 return route.fulfill({json:{url:'https://checkout.example.invalid/never-pay'}});
85 });
86 await print.goto(url+'/studio');
87 await print.locator('input[type=file]').setInputFiles({name:'synthetic.png',mimeType:'image/png',buffer:fixture});
88 await print.getByText('16 × 8',{exact:true}).waitFor();
89 await print.getByLabel('Crop').selectOption('square');
90 await print.getByText('8 × 8',{exact:true}).waitFor();
91 await print.getByRole('button',{name:'Print',exact:true}).click();
92 await print.getByRole('button',{name:'Preview print and price'}).click();
93 await print.getByRole('img',{name:'Exact image to be sent to the printer'}).waitFor();
94 await print.getByText('$58.00', {exact:true}).waitFor();
95 const pay=print.getByRole('button',{name:'Continue to payment'});
96 assert.equal(await pay.isDisabled(),true);
97 await print.getByLabel('I approve this exact image and print selection.').check();
98 assert.equal(await pay.isDisabled(),true);
99 await print.getByRole('checkbox').nth(1).check();
100 await pay.click();
101 await print.getByText('Checkout returned an unexpected address.').waitFor();
102 assert.equal(checkoutCount,1);
103 console.log('PASS: print proof and price, explicit approval plus terms, exact same artwork bytes at checkout and unexpected payment host blocked (mocked)');
104 }
105 } finally { await browser.close(); }
106})().catch(err=>{console.error(err);process.exitCode=1;});