import { describe, it, beforeAll, afterAll, expect } from 'vitest'; import { DeviceDriver } from '../../drivers/types'; import { createDriver } from '../../drivers/factory'; import { TestReporter } from '../../utils/test-reporter'; import { sleep, addDeviceViaBLE, findDeviceNameOnHomepage, saveAddedDevice, enterPairingMode, configureHubWifi, setupResilientHooks, } from '../../utils/common'; import { getWifiCredentials } from '../../config/wifi.config'; import * as dotenv from 'dotenv'; import * as path from 'path'; dotenv.config({ path: path.resolve(__dirname, '../../.env') }); // 扫地机 S1 Plus:WiFi 产品。配对 = 同时长按 ch11+12 3s,**在添加向导第二步**按(非进门就按)。 // 流程:选品类 Robot Vacuum (可能再选 S1 Plus) → 第一步 Next → 第二步按继电器 → Connect/发现 → WiFi 配网。 // 入口名/型号 env 可覆盖。首版带诊断,按实测向导再校准。ONES 添加号待补,先 [P0]。 const CATEGORY = process.env.ROBOT_S1P_CATEGORY || 'Robot Vacuum Cleaner S1 Plus'; const MODEL = process.env.ROBOT_S1P_MODEL || 'S1 Plus'; // 二级型号(若品类下还需再选;入口已含S1 Plus则通常无需) const SCAN_KEYWORD = process.env.ROBOT_S1P_SCAN || 'S1 Plus'; const NAME_PATTERN = process.env.ROBOT_S1P_PATTERN || 'Robot Vacuum Cleaner S1 Plus[^"]*\\s*[0-9A-Za-z]{0,4}'; const ADD_ANCHOR = '[P0][ONES:15951]'; // 扫地机 S1 Plus 添加(Robot S1P,见 ones-anchor.helper) describe('Robot S1 Plus Connect - 添加扫地机S1 Plus(第二步两键同按 + WiFi配网)', () => { let driver: DeviceDriver; let reporter: TestReporter; setupResilientHooks(() => driver); beforeAll(async () => { driver = createDriver(); await driver.createSession(); reporter = new TestReporter('RobotS1P_Connect', driver.platform.toUpperCase()); }); afterAll(async () => { reporter.generate(); await driver.destroySession(); }); it(`${ADD_ANCHOR} 通过BLE+WiFi添加扫地机S1 Plus`, { timeout: 300000 }, async () => { const start = Date.now(); const wifi = getWifiCredentials(); try { const existing = await findDeviceNameOnHomepage(driver, NAME_PATTERN); if (existing) { console.log(`${existing}已在首页,跳过重新添加`); saveAddedDevice('robot', existing); reporter.record(`${ADD_ANCHOR} 添加扫地机S1 Plus`, 'SKIP', Date.now() - start, `${existing}已存在`); return; } const dump = async (tag: string) => { const src = await driver.getSource(); const re = driver.platform === 'ios' ? /(?:name|label)="([^"]+)"/g : /text="([^"]+)"/g; const t = [...new Set(Array.from(src.matchAll(re)).map((m) => m[1]).filter((x) => x.trim() && x.length < 30))].slice(0, 18); console.log(`[S1P诊断] ${tag}: ${JSON.stringify(t)}`); }; const result = await addDeviceViaBLE(driver, { categoryName: CATEGORY, deviceKeyword: SCAN_KEYWORD, scanTimeout: 30000, skipScanStep: true, // 第二步按继电器:① 选品类后(可能先二级选 S1 Plus)② 第一步 Next 推进 ③ 第二步按 ch11+12 3s ④ 继续 preSelectSteps: async (d) => { await dump('选品类后'); // 二级型号(若有) const sub = await d.findElementRaw('-android uiautomator', `new UiSelector().textContains("${MODEL}")`).catch(() => null); if (sub) { await d.tapElement(sub); await sleep(2000); await dump('选S1 Plus后'); } // 第一步:Next 推进到第二步 const n1 = await d.findElementRaw('-android uiautomator', 'new UiSelector().text("Next")').catch(() => null); if (n1) { await d.tapElement(n1); await sleep(2000); } // ★ 按 relay 前**轮询等到 "Second step" 页**再按(导航有时滞后,固定 sleep 会让 relay 在向导没到位时空按 → connection timeout) for (let i = 0; i < 10; i++) { if (/Second step|第二步|press the button|hold the button|按住/i.test(await d.getSource())) break; await sleep(1500); } await dump('第二步(按继电器前)'); // 第二步:按 ch11+12 5s await enterPairingMode('robot_s1p').catch((e) => console.log(`relay ch11+12 配对失败: ${e.message}`)); await sleep(2500); // 继续:Connect Device / Next for (const t of ['Connect device', 'Connect Device', 'Connect Devices', 'Connect', 'Next']) { const b = await d.findElementRaw('-android uiautomator', `new UiSelector().text("${t}")`).catch(() => null); if (b) { await d.tapElement(b); break; } } await sleep(3000); await dump('按继电器+继续后'); }, postConnectionSteps: async (d) => { // 搜到设备后是 "...found / Configure network" 页:点 Configure network 进 WiFi 配网 const cn = await d.findElementRaw('-android uiautomator', 'new UiSelector().textContains("Configure network")').catch(() => null); if (cn) { await d.tapElement(cn); await sleep(3000); } await configureHubWifi(d, wifi); }, wizardButtons: ['Start now', 'Return Home', 'Done', 'Use now', 'Start Using', 'Got it', 'OK', 'Skip', 'Next', 'Save'], connectionKeywords: [ 'found', 'Configure network', 'Enter Wi-Fi', 'Enter the password', 'Configure Wi-Fi', 'Wi-Fi Settings', 'Connecting', 'Pick a room', 'Done', 'added successfully', 'Added successfully', 'Initial Setup', 'Start now', 'Select a room', ], }); expect(result).toBe(true); const elapsed = ((Date.now() - start) / 1000).toFixed(1); reporter.record(`${ADD_ANCHOR} 添加扫地机S1 Plus`, 'PASS', Date.now() - start, `添加成功(WiFi:${wifi.ssid}), 耗时${elapsed}s`); } catch (e: any) { const ss = await driver.screenshot().catch(() => ''); reporter.record(`${ADD_ANCHOR} 添加扫地机S1 Plus`, 'FAIL', Date.now() - start, e.message, ss); throw e; } }); });