teleop / tests /browser.cjs
ebubar's picture
fix: expose controller selection and live joystick input
828a888
Raw History Blame Contribute Delete
9.69 kB
// Run against Vite with Playwright available via PLAYWRIGHT_MODULE or NODE_PATH.
// All robot and host lifecycle calls are mocked; this never contacts hardware.
const { chromium } = require(process.env.PLAYWRIGHT_MODULE || 'playwright');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const mock = `
class Robot extends EventTarget {
state = 'streaming';
robotState = {head:[1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1],antennas:[-.1745,.1745],body_yaw:0,is_move_running:false};
targets = []; homes = []; modes = [];
setTarget(target) { this.targets.push(structuredClone(target)); this.robotState = {...this.robotState,...target}; return true; }
setMotorMode(mode) { this.modes.push(mode); return true; }
gotoTarget(target) { this.homes.push(structuredClone(target)); this.robotState = {...this.robotState,...target}; return true; }
requestState() { this.dispatchEvent(new CustomEvent('state',{detail:this.robotState})); return true; }
}
window.__robot = new Robot();
window.__pad = {index:0,id:'Mock standard controller',connected:true,mapping:'standard',axes:[0,0,0,0],buttons:Array.from({length:16},()=>({pressed:false,value:0,touched:false}))};
Object.defineProperty(navigator,'getGamepads',{value:()=>window.__pads || [window.__pad]});
window.__stateTimer = setInterval(()=>window.__robot.requestState(),100);
export async function connectToHost(){return {reachy:window.__robot,theme:'dark',config:null,
onThemeChange:()=>()=>{},onLeave:fn=>{window.__leave=fn;return ()=>{};}};}
`;
(async () => {
const browser = await chromium.launch({channel:'chrome',headless:true});
fs.mkdirSync('.test-artifacts',{recursive:true});
try {
for (const [name,width,height] of [['desktop',1280,1000],['phone',390,844]]) {
const context = await browser.newContext({viewport:{width,height},isMobile:name==='phone',hasTouch:name==='phone'});
const page = await context.newPage();
const errors = []; page.on('pageerror',e=>errors.push(e.message));
await page.route(/reachy-mini-sdk.*host.*embed.*\.js/,route=>route.fulfill({contentType:'application/javascript',body:mock}));
await page.goto('http://127.0.0.1:5176/?embedded=1');
await page.getByRole('button',{name:'Start controls',exact:true}).waitFor();
await page.waitForTimeout(250);
assert.equal(await page.evaluate(()=>document.documentElement.scrollWidth > innerWidth),false);
await page.screenshot({path:'.test-artifacts/'+name+'.png',fullPage:true});
// A detected physical gamepad takes priority even on a touchscreen device.
assert.equal(await page.locator('#source').inputValue(),'gamepad');
await page.evaluate(()=>window.__pad.axes[0]=.6);
await page.waitForTimeout(80);
assert.ok((await page.locator('#left-input').innerText()).includes('0.60'));
assert.equal(await page.evaluate(()=>window.__robot.targets.length),0,'paused input test never moves robot');
await page.evaluate(()=>window.__pad.axes[0]=0);
await page.locator('#source').selectOption('gamepad');
// Gamepad is retained; antenna and independent body commands reach atomic targets.
await page.getByRole('button',{name:'Start controls',exact:true}).click();
await page.waitForTimeout(100);
await page.evaluate(()=>{window.__pad.buttons[6].value=1;window.__pad.axes[2]=.6;});
await page.waitForTimeout(150);
const target = await page.evaluate(()=>window.__robot.targets.at(-1));
assert.ok(target.antennas[1]>.1745,'left trigger moves left antenna');
assert.ok(target.body_yaw<0,'right stick controls body');
await page.evaluate(()=>window.__pad.buttons[12].pressed=true);
await page.waitForTimeout(60);
assert.equal(await page.locator('#turbo').isChecked(),true);
await page.evaluate(()=>{window.__pad.connected=false;});
await page.waitForTimeout(100);
assert.ok((await page.locator('#phase').innerText()).includes('disconnected'));
const count = await page.evaluate(()=>window.__robot.targets.length);
await page.waitForTimeout(100);
assert.equal(await page.evaluate(()=>window.__robot.targets.length),count);
assert.equal(await page.evaluate(()=>window.__robot.homes.length),1);
// Leave cleanup must terminate the worker and prevent further motion.
await page.evaluate(()=>window.__leave());
assert.equal(await page.evaluate(()=>window.__robot.homes.length),2);
await page.waitForTimeout(100);
assert.equal(await page.evaluate(()=>window.__robot.targets.length),count);
assert.deepEqual(errors,[]);
await context.close();
console.log(name+': layout, gamepad, antennas, Turbo, disconnect and Leave passed');
}
// Independent touch path and normal Stop.
const page = await browser.newPage({viewport:{width:390,height:844}});
await page.route(/reachy-mini-sdk.*host.*embed.*\.js/,route=>route.fulfill({contentType:'application/javascript',body:mock}));
await page.goto('http://127.0.0.1:5176/?embedded=1');
await page.locator('#source').selectOption('touch');
await page.evaluate(()=>{window.__pad.axes[2]=1;});
await page.waitForTimeout(200);
await page.locator('#start').click();
const button = page.locator('[data-hold="right-out"]');
await button.scrollIntoViewIfNeeded();
const box = await button.boundingBox();
await page.mouse.move(box.x+box.width/2,box.y+box.height/2);
await page.mouse.down();await page.waitForTimeout(150);await page.mouse.up();
assert.ok((await page.evaluate(()=>window.__robot.targets.at(-1))).antennas[0]<-.1745);
assert.equal((await page.evaluate(()=>window.__robot.targets.at(-1))).body_yaw,0,'touch mode ignores gamepad');
for (const id of ['look','body']) {
const stick = page.locator('#'+id);
await stick.evaluate(el=>el.scrollIntoView({block:'center'}));
const rect = await stick.boundingBox();
const x=rect.x+rect.width/2,y=rect.y+rect.height/2;
await page.mouse.move(x,y);await page.mouse.down();await page.mouse.move(x+25,y-25);
await page.waitForTimeout(120);await page.mouse.up();
}
const touchTarget = await page.evaluate(()=>window.__robot.targets.at(-1));
assert.ok(touchTarget.body_yaw<0,'touch body yaw; phase='+await page.locator('#phase').innerText());
assert.ok(touchTarget.head[11]>0,'touch height');
assert.ok(Math.atan2(touchTarget.head[4],touchTarget.head[0])<touchTarget.body_yaw,'touch head yaw is independent');
await page.locator('#stop').click();
assert.equal(await page.evaluate(()=>window.__robot.homes.length),1);
console.log('touch head/body/height/antennas, input arbitration and Stop passed');
await page.close();
const focus = await browser.newPage();
await focus.route(/reachy-mini-sdk.*host.*embed.*\.js/,route=>route.fulfill({contentType:'application/javascript',body:mock}));
await focus.goto('http://127.0.0.1:5176/?embedded=1');
await focus.locator('#source').selectOption('touch');
await focus.waitForTimeout(200);await focus.locator('#start').click();
await focus.waitForTimeout(80);
await focus.evaluate(()=>{Object.defineProperty(document,'hidden',{configurable:true,value:true});document.dispatchEvent(new Event('visibilitychange'));});
await focus.waitForTimeout(100);
const pausedCount = await focus.evaluate(()=>window.__robot.targets.length);
await focus.evaluate(()=>{Object.defineProperty(document,'hidden',{configurable:true,value:false});document.dispatchEvent(new Event('visibilitychange'));});
await focus.waitForTimeout(100);
assert.equal(await focus.evaluate(()=>window.__robot.targets.length),pausedCount);
assert.equal(await focus.evaluate(()=>window.__robot.homes.length),1);
assert.ok((await focus.locator('#phase').innerText()).includes('Tab hidden'));
console.log('visibility loss pauses, centers and requires explicit resume');
await focus.close();
const multi = await browser.newPage();
await multi.route(/reachy-mini-sdk.*host.*embed.*\.js/,route=>route.fulfill({contentType:'application/javascript',body:mock}));
await multi.goto('http://127.0.0.1:5176/?embedded=1');
await multi.locator('#controller').waitFor();
await multi.evaluate(()=>{
const other=structuredClone(window.__pad);other.index=1;other.id='Actual DualSense';
window.__pads=[window.__pad,other];
});
await multi.waitForTimeout(100);
await multi.locator('#controller').selectOption('1');
await multi.evaluate(()=>{window.__pads[1].axes[2]=.7;});
await multi.waitForTimeout(80);
assert.ok((await multi.locator('#right-input').innerText()).includes('0.70'));
assert.equal(await multi.evaluate(()=>window.__robot.targets.length),0);
await multi.evaluate(()=>{window.__pads[1].axes[2]=0;});
await multi.locator('#start').click();
await multi.evaluate(()=>{window.__pads[1].axes[2]=.7;});
await multi.waitForTimeout(100);
assert.ok((await multi.evaluate(()=>window.__robot.targets.at(-1))).body_yaw<0);
console.log('explicit controller selection and paused input indicators passed');
await multi.close();
// Standalone host should mount sign-in, not the old static landing page.
const host = await browser.newPage();
await host.goto('http://127.0.0.1:5176/');
await host.waitForTimeout(1500);
await host.screenshot({path:'.test-artifacts/host.png',fullPage:true});
assert.ok((await host.locator('body').innerText()).toLowerCase().includes('sign'));
console.log('standalone host sign-in mounted');
} finally { await browser.close(); }
})().catch(error=>{console.error(error);process.exitCode=1;});