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, addDeviceWithSerialPairing, findDeviceNameOnHomepage, saveAddedDevice, } 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') }); // Hub Plus:WiFi 产品。配对 = 同时长按 ch14+15(relay 'hub_plus')。入口品类「Hub Plus」(env 可覆盖;"hub/hub plus")。 // 流程含 WiFi 配网。首版照 Hub Mini(addDeviceWithSerialPairing),按实测再调。ONES 添加号待补,先 [P0]。 const CATEGORY = process.env.HUB_PLUS_CATEGORY || 'Hub/Hub Plus'; // 入口字面名(带斜杠的单条目) const DEVICE_KEYWORD = process.env.HUB_PLUS_SCAN || 'Hub Plus'; const NAME_PATTERN = process.env.HUB_PLUS_PATTERN || 'Hub ?Plus[^"]*\\s*[0-9A-Za-z]{0,4}'; const ADD_ANCHOR = '[P0]'; // TODO: 待补 Hub Plus 添加 ONES 编号 describe('HubPlus Connect - 添加Hub Plus(两键同按 + WiFi配网)', () => { let driver: DeviceDriver; let reporter: TestReporter; beforeAll(async () => { driver = createDriver(); await driver.createSession(); reporter = new TestReporter('HubPlus_Connect', driver.platform.toUpperCase()); }); afterAll(async () => { reporter.generate(); await driver.destroySession(); }); it(`${ADD_ANCHOR} 通过BLE+WiFi添加Hub 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('hub', existing); reporter.record(`${ADD_ANCHOR} 添加Hub Plus`, 'SKIP', Date.now() - start, `${existing}已存在`); return; } const result = await addDeviceWithSerialPairing(driver, { categoryName: CATEGORY, categoryScrollHint: 1, deviceKeyword: DEVICE_KEYWORD, relayDevice: 'hub_plus', // ch14+15 同按(config/relay.config) registryCategory: 'hub', namePattern: NAME_PATTERN, // 选品类后 Disclaimer/Before Using → Next(beforePairing 过掉);连上进 Configure Wi-Fi beforePairing: async (d) => { const n = await d.findElementRaw('-android uiautomator', 'new UiSelector().text("Next")').catch(() => null); if (n) { await d.tapElement(n); await sleep(2000); } }, postConnectSteps: async (d) => { // Hub Plus 的 Wi-Fi 配置页是 4 框:[0]设备名(勿改) [1]Select Room [2]Wi-Fi SSID [3]Wi-Fi 密码。 // 预填的 SSID 可能是旧网络(连不上会卡)→ 必须显式覆盖 SSID + 密码为配置值(ASUS_30/woan123456)。 const EDIT = 'new UiSelector().className("android.widget.EditText")'; for (let i = 0; i < 20; i++) { const eds = await d.findElementsRaw('-android uiautomator', EDIT).catch(() => [] as any[]); if ((eds as any[]).length >= 3) break; await sleep(2000); } // 每次重新 find(goBack 收键盘后元素引用会失效),按索引填值。 const typeIdx = async (idx: number, val: string) => { const eds = await d.findElementsRaw('-android uiautomator', EDIT).catch(() => [] as any[]) as any[]; const i = idx < 0 ? eds.length + idx : idx; if (!eds[i]) { console.log(`[HubPlus] Wi-Fi 框${idx}不存在(共${eds.length})`); return; } await d.clearText(eds[i]).catch(() => {}); await d.typeText(eds[i], val); await sleep(700); try { await d.goBack(); } catch { /* 收键盘 */ } await sleep(600); }; await typeIdx(2, wifi.ssid); // SSID 框(第3个)= ASUS_30 await typeIdx(-1, wifi.password); // 密码框(最后一个)= woan123456 console.log(`[HubPlus] 已填 SSID=${wifi.ssid} 密码=${wifi.password}`); // Next(可能需滚到底) let n = await d.findElementRaw('-android uiautomator', 'new UiSelector().text("Next")').catch(() => null); if (!n) { await d.scrollDown(400).catch(() => {}); await sleep(800); n = await d.findElementRaw('-android uiautomator', 'new UiSelector().text("Next")').catch(() => null); } if (n) { await d.tapElement(n); await sleep(3000); } // "要连接至设备吗" 弹框 → 连接 for (const t of ['连接', 'Connect']) { const b = await d.findElementRaw('-android uiautomator', `new UiSelector().text("${t}")`).catch(() => null); if (b) { await d.tapElement(b); break; } } // 等连接/初始化完成 → 点 Done(WiFi 配好后点 Done 即添加成功) for (let i = 0; i < 20; i++) { await sleep(4000); const src = await d.getSource().catch(() => ''); if (/successfully|Pick a room|Connected to the network|完成/i.test(src) || src.includes('Done')) { for (const t of ['Done', '完成']) { const b = await d.findElementRaw('-android uiautomator', `new UiSelector().text("${t}")`).catch(() => null); if (b) { await d.tapElement(b); await sleep(2500); break; } } break; } } }, connectionKeywords: [ 'Configure Wi-Fi', 'Your device is connected', 'Connect Device', 'Connecting', 'Initial Setup', 'Pick a room', 'Done', 'Next', 'found', 'Configure network', 'added successfully', ], }); expect(result).toBe(true); const elapsed = ((Date.now() - start) / 1000).toFixed(1); reporter.record(`${ADD_ANCHOR} 添加Hub Plus`, 'PASS', Date.now() - start, `添加成功(WiFi:${wifi.ssid}), 耗时${elapsed}s`); } catch (e: any) { const ss = await driver.screenshot().catch(() => ''); reporter.record(`${ADD_ANCHOR} 添加Hub Plus`, 'FAIL', Date.now() - start, e.message, ss); throw e; } }); });