| 1 | import { execFileSync } from 'node:child_process'; |
| 2 | import http from 'node:http'; |
| 3 | import { createWorkbench } from './workbench-fixture.mjs'; |
| 4 | import { createPrivacyController } from '../../scripts/browser-privacy-fixture.mjs'; |
| 5 | export async function createSettingsWorkbench(options = {}) { |
| 6 | const clientContext = options.clientContext || { mode: 'local', connection_id: 'local', client_platform: 'darwin', server_platform: 'darwin', connection: { state: 'disconnected' } }; |
| 7 | const controller = await createPrivacyController(); |
| 8 | const fixtureState = { failSave: false, home: controller.origin + '/home' }; |
| 9 | const archive = execFileSync('uv', ['run', '--no-project', 'python', '-c', `import io,json,sys,zipfile |
| 10 | b=io.BytesIO() |
| 11 | with zipfile.ZipFile(b,'w') as z: |
| 12 | z.writestr('manifest.json',json.dumps({'manifest_version':3,'name':'阅读助手','version':'1.0','description':'调整网页阅读体验。','permissions':['storage'],'content_scripts':[{'matches':['http://localhost/*'],'js':['content.js']}]})) |
| 13 | z.writestr('content.js','document.documentElement.dataset.cageSettingsExtension="active";') |
| 14 | sys.stdout.buffer.write(b.getvalue())`], { stdio: ['ignore', 'pipe', 'ignore'], maxBuffer: 1 << 20 }); |
| 15 | const profileID = 'a'.repeat(32), extensionID = 'b'.repeat(32); |
| 16 | |
| 17 | |
| 18 | const proxy = http.createServer(async (request, response) => { |
| 19 | if (request.url === '/api/browser/extensions/upload' && request.method === 'POST') { |
| 20 | try { |
| 21 | const chunks = []; for await (const chunk of request) chunks.push(chunk); |
| 22 | const result = await controller.uploadExtension(Buffer.concat(chunks)); |
| 23 | response.setHeader('Content-Type', 'application/json'); response.end(JSON.stringify(result)); |
| 24 | } catch (error) { response.writeHead(400); response.end(error.message); } |
| 25 | return; |
| 26 | } |
| 27 | if (request.url.startsWith('/api/')) { response.writeHead(404); response.end(); return; } |
| 28 | const target = new URL(request.url, process.env.CAGE_UI_URL || 'http://127.0.0.1:5198'); |
| 29 | const upstream = http.request(target, { headers: { ...request.headers, host: target.host } }, source => { response.writeHead(source.statusCode, source.headers); source.pipe(response); }); |
| 30 | upstream.on('error', error => { response.writeHead(502); response.end(error.message); }); request.pipe(upstream); |
| 31 | }); |
| 32 | await new Promise(resolve => proxy.listen(0, '127.0.0.1', resolve)); |
| 33 | let workbench; |
| 34 | try { |
| 35 | workbench = await createWorkbench({ ...options, clientContext, url: `http://127.0.0.1:${proxy.address().port}`, |
| 36 | browserRequest: async (route, path, body) => { |
| 37 | if (path === '/browser/extensions/upload') return route.continue(); |
| 38 | if (path === '/browser/resize') return route.fulfill({ json: { ok: true } }); |
| 39 | if (path === '/browser/preferences') { |
| 40 | if (body) { |
| 41 | if (fixtureState.failSave) return route.fulfill({ status: 500, contentType: 'text/plain; charset=utf-8', body: '保存浏览器设置失败,请重试' }); |
| 42 | await controller.api('/homepage', { url: body.browser_home_url }); fixtureState.home = body.browser_home_url; |
| 43 | } |
| 44 | return route.fulfill({ json: { browser_home_url: fixtureState.home } }); |
| 45 | } |
| 46 | try { |
| 47 | const result = await controller.api(path.replace('/browser', ''), body); |
| 48 | return route.fulfill({ json: result }); |
| 49 | } catch (error) { return route.fulfill({ status: 400, contentType: 'text/plain; charset=utf-8', body: error.message }); } |
| 50 | }, |
| 51 | clientRequest: async (route, path, body) => { |
| 52 | if (path === '/client/context') return route.fulfill({ json: clientContext }); |
| 53 | if (path === '/client/browser-extensions') { |
| 54 | if (body) return route.fulfill({ contentType: 'application/zip', body: archive }); |
| 55 | return route.fulfill({ json: { profiles: [{ id: profileID, browser: 'Microsoft Edge', name: '个人配置', extensions: [ |
| 56 | { id: extensionID, name: '阅读助手', version: '1.0', enabled: false, state_known: true, compatible: true, identity_preserved: false, permissions: ['storage'], hostPermissions: ['http://localhost/*'] }, |
| 57 | { id: 'c'.repeat(32), name: '旧版扩展', version: '2.0', enabled: true, state_known: true, compatible: false, identity_preserved: false, permissions: [], hostPermissions: [] }, |
| 58 | ] }] } }); |
| 59 | } |
| 60 | return route.fulfill({ json: path === '/client/hosts' ? { hosts: [] } : {} }); |
| 61 | }, |
| 62 | }); |
| 63 | workbench.state.status.ready = workbench.state.status.vm_running = true; |
| 64 | workbench.state.settings.browser_home_url = fixtureState.home; |
| 65 | await workbench.page.reload(); |
| 66 | return { ...workbench, controller, fixtureState, archive, profileID, extensionID, close: async () => { await workbench.browser.close(); await new Promise(resolve => proxy.close(resolve)); await controller.close(); } }; |
| 67 | } catch (error) { await workbench?.browser.close(); await new Promise(resolve => proxy.close(resolve)); await controller.close(); throw error; } |
| 68 | } |