dispatch_189_shell_v2.cjs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. // 189 admin-desktop E2E via opencode SHELL channel v2 (POST /api/shell direct, PowerShell 5.1 compatible)
  2. // Auth: opencode:IwinTrue@123
  3. const BASE = process.env.OPENCODE_189_BASE || 'http://192.168.16.189:4090';
  4. const AUTH = 'Basic ' + Buffer.from('opencode:' + (process.env.OPENCODE_SERVER_PASSWORD || 'IwinTrue@123')).toString('base64');
  5. const capMs = parseInt(process.env.DISPATCH_CAP_MS || '540000', 10);
  6. // PowerShell 5.1: use ; as separator, $LASTEXITCODE for exit code
  7. const CMD = 'cd tests; git fetch origin 2>&1 | Select-Object -Last 2; git reset --hard origin/cfclub 2>&1 | Select-Object -Last 2; npx playwright test -c playwright.config.live.js --project=admin-desktop --reporter=list; echo "EXIT=$LASTEXITCODE"';
  8. async function j(url, opt) {
  9. const r = await fetch(url, Object.assign({ headers: { 'Authorization': AUTH, 'Content-Type': 'application/json' } }, opt));
  10. const t = await r.text();
  11. try { return { status: r.status, json: JSON.parse(t) }; } catch { return { status: r.status, text: t }; }
  12. }
  13. async function main() {
  14. const cr = await j(BASE + '/api/shell', { method: 'POST', body: JSON.stringify({ command: CMD }) });
  15. const shellId = (cr.json.data && cr.json.data.id) || cr.json.id;
  16. if (!shellId) throw new Error('no shell id on create: ' + JSON.stringify(cr.json).slice(0, 200));
  17. console.log('SHELL_ID', shellId, 'status', cr.json.data && cr.json.data.status);
  18. const start = Date.now();
  19. let last = '';
  20. while (Date.now() - start < capMs) {
  21. await new Promise(r => setTimeout(r, 6000));
  22. const st = await j(BASE + '/api/shell/' + shellId);
  23. const status = (st.json && ((st.json.data && st.json.data.status) || st.json.status)) || '?';
  24. if (status !== last) { console.log('SHELL_STATUS', status, '@', Math.round((Date.now() - start) / 1000) + 's'); last = status; }
  25. if (status && status !== 'running' && status !== 'pending' && status !== 'queued') {
  26. const out = await j(BASE + '/api/shell/' + shellId + '/output');
  27. const o = (out.json && out.json.data && out.json.data.output) || '';
  28. console.log('OUTPUT_BEGIN');
  29. console.log(o.slice(-6000));
  30. console.log('OUTPUT_END');
  31. return;
  32. }
  33. }
  34. console.log('SHELL_TIMEOUT after ' + capMs + 'ms');
  35. }
  36. main().catch(e => { console.error('ERR', e.message); process.exit(1); });