107 lines
4.8 KiB
TypeScript
107 lines
4.8 KiB
TypeScript
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') });
|
|
|
|
// Hub 3:WiFi 产品。**发现页流程**(同 URC/屋檐灯,非 Hub Mini 流程):选品类 "Hub 3" → 推进到发现页(Discover/Connect device)
|
|
// → 同时长按 off(ch9)+on(ch10) 3s → 点 Connect device → Connecting → Configure Wi-Fi 配网。
|
|
// 实测:在连接页上按 ch9+10 3s + 点 Connect Device,约20s后到配网页。ONES 添加号待补,先 [P0]。
|
|
const CATEGORY = process.env.HUB3_CATEGORY || 'Hub 3';
|
|
// ⚠️ 'Hub 3' 会被 "AI Hub 37" 串台(AI **Hub 3**7)→ 校验/skip 用带尾空格的 "Hub 3 "(首页名形如 "Hub 3 XX",AI Hub 是 "AI Hub 37" 无空格)。
|
|
const SCAN_KEYWORD = process.env.HUB3_SCAN || 'Hub 3 ';
|
|
const NAME_PATTERN = process.env.HUB3_PATTERN || 'Hub ?3 [0-9A-Za-z]{1,4}';
|
|
const ADD_ANCHOR = '[P0]'; // TODO: 待补 Hub 3 添加 ONES 编号
|
|
|
|
describe('Hub3 Connect - 添加Hub 3(发现页流程: 两键同按配对 + WiFi配网)', () => {
|
|
let driver: DeviceDriver;
|
|
let reporter: TestReporter;
|
|
setupResilientHooks(() => driver);
|
|
|
|
beforeAll(async () => {
|
|
driver = createDriver();
|
|
await driver.createSession();
|
|
reporter = new TestReporter('Hub3_Connect', driver.platform.toUpperCase());
|
|
});
|
|
|
|
afterAll(async () => {
|
|
reporter.generate();
|
|
await driver.destroySession();
|
|
});
|
|
|
|
it(`${ADD_ANCHOR} 通过BLE+WiFi添加Hub 3`, { 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 3`, 'SKIP', Date.now() - start, `${existing}已存在`);
|
|
return;
|
|
}
|
|
|
|
const result = await addDeviceViaBLE(driver, {
|
|
categoryName: CATEGORY,
|
|
deviceKeyword: SCAN_KEYWORD,
|
|
scanTimeout: 30000,
|
|
skipScanStep: true,
|
|
// 推进到发现页 → 长按 ch9+10 3s 进配对 → 点 Connect device 触发发现
|
|
preSelectSteps: async (d) => {
|
|
// 跨平台按文案点击(iOS: name/predicate;Android: uiautomator)
|
|
const tapByLabel = async (labels: string[]): Promise<boolean> => {
|
|
for (const t of labels) {
|
|
let el: string | null = null;
|
|
if (d.platform === 'ios') {
|
|
el = await d.findElementRaw('name', t).catch(() => null)
|
|
|| await d.findElementRaw('predicate string', `name CONTAINS "${t}" OR label CONTAINS "${t}"`).catch(() => null);
|
|
} else {
|
|
el = await d.findElementRaw('-android uiautomator', `new UiSelector().text("${t}")`).catch(() => null);
|
|
}
|
|
if (el) { await d.tapElement(el); return true; }
|
|
}
|
|
return false;
|
|
};
|
|
for (let i = 0; i < 4; i++) {
|
|
const src = await d.getSource();
|
|
if (/Discover device|Connect device/i.test(src)) break;
|
|
if (!(await tapByLabel(['Next', 'Connect']))) break;
|
|
await sleep(2000);
|
|
}
|
|
await enterPairingMode('hub3').catch((e) => console.log(`relay ch9+10 两键同按配对失败: ${e.message}`));
|
|
await sleep(2500);
|
|
await tapByLabel(['Connect device', 'Connect Device', 'Connect Devices', 'Connect', 'Next']);
|
|
await sleep(3000);
|
|
},
|
|
postConnectionSteps: async (d) => { await configureHubWifi(d, wifi); },
|
|
wizardButtons: ['Start now', 'Return Home', 'Done', 'Use now', 'Start Using', 'Got it', 'OK', 'Skip', 'Next', 'Save'],
|
|
connectionKeywords: [
|
|
'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} 添加Hub 3`, 'PASS', Date.now() - start, `添加成功(WiFi:${wifi.ssid}), 耗时${elapsed}s`);
|
|
} catch (e: any) {
|
|
const ss = await driver.screenshot().catch(() => '');
|
|
reporter.record(`${ADD_ANCHOR} 添加Hub 3`, 'FAIL', Date.now() - start, e.message, ss);
|
|
throw e;
|
|
}
|
|
});
|
|
});
|