// R162: 189 admin-live UI E2E kick/read(String.raw 保反斜杠路径,修复 Location not found) const BASE = process.env.BASE_189 || 'http://192.168.16.189:4090'; const AUTH = 'Basic ' + Buffer.from('opencode:' + (process.env.OPENCODE_SERVER_PASSWORD || 'IwinTrue@123')).toString('base64'); const H = { Authorization: AUTH, 'Content-Type': 'application/json' }; const CWD = String.raw`C:\code\cfc`; const LOC = '?location%5Bdirectory%5D=' + encodeURIComponent(CWD); const LOG = String.raw`C:\code\cfc\_r190_admin-live_test.log`; const SLEEP = ms => new Promise(r => setTimeout(r, ms)); function buildKickCmd() { return [ 'cd ' + CWD, 'if (Test-Path ' + LOG + ') { Remove-Item ' + LOG + ' -Force }', `Start-Process -FilePath cmd.exe -ArgumentList '/c','cd /d ${CWD}\\tests && set PLAYWRIGHT_HTML_REPORT=false && npx playwright test -c playwright.config.live.js --project=admin-desktop --reporter=line > ${LOG} 2>&1' -WindowStyle Hidden -PassThru | Select-Object Id`, 'start-sleep -Seconds 3', 'echo KICKED' ].join('; '); } function buildReadCmd() { return [ 'echo READ_BEGIN', 'if (Test-Path ' + LOG + ') { Get-Content ' + LOG + ' -Tail 200 }', 'if (Test-Path ' + LOG + '.err) { echo ERR_BEGIN; Get-Content ' + LOG + '.err -Tail 30 }', 'echo READ_END' ].join('; '); } async function j(url, opt) { try { const r = await fetch(url, Object.assign({ headers: H }, opt)); const t = await r.text(); try { return { status: r.status, json: JSON.parse(t) }; } catch { return { status: r.status, text: t }; } } catch (e) { return { status: 0, err: e.message }; } } async function openShell(cmd, capMs = 30000) { const cr = await j(BASE + '/api/shell' + LOC, { method: 'POST', body: JSON.stringify({ command: cmd, cwd: CWD }) }); const cj = (cr.json) || {}; const id = (cj.data && cj.data.id) || cj.id; if (!id) return { err: 'create failed ' + cr.status + ' ' + JSON.stringify(cj).slice(0, 200), id: null }; const t0 = Date.now(); while (Date.now() - t0 < capMs) { await SLEEP(2000); const s = await j(BASE + '/api/shell/' + id + LOC); const d = (s.json && s.json.data) || null; if (d && d.status && d.status !== 'running') return { id, status: d.status }; if (!d) continue; } return { id, status: 'timeout' }; } (async () => { const mode = process.argv[2] || 'kick'; if (mode === 'kick') { const k = await openShell(buildKickCmd(), 30000); console.log('KICK', k.err || ('id=' + k.id + ' status=' + k.status)); return; } const r = await openShell(buildReadCmd(), 30000); if (r.err) { console.log('READ_ERR', r.err); process.exit(1); } const o = await j(BASE + '/api/shell/' + r.id + '/output' + LOC + '&limit=200000'); console.log('OUT_BEGIN'); console.log((o.json && o.json.data && o.json.data.output) || '(empty)'); console.log('OUT_END'); })().catch(e => { console.error('ERR', e.message); process.exit(1); });