AI_UIAutomation/tests/ceiling_light/ceiling_light_pro_connect.t...

98 lines
4.6 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,
pressButton,
powerCycle,
configureHubWifi,
setupResilientHooks,
resetBluetooth,
} 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') });
// 吸顶灯 Pro:WiFi 产品(relay ch1)。添加流程同 Color Bulb:**点一次→等2s→再点一次** 进配对 + WiFi 配网;仅品类入口不同。
const CATEGORY = process.env.CEILING_PRO_CATEGORY || 'Ceiling Light Pro';
const SCAN_KEYWORD = process.env.CEILING_PRO_SCAN || 'Ceiling Light Pro'; // skipScanStep=true → 仅首页校验,用可区分全名(勿用前缀 'Ceiling Light')
const NAME_PATTERN = process.env.CEILING_PRO_PATTERN || 'Ceiling Light Pro\\s*[0-9A-Za-z]{0,4}';
describe('CeilingLight Pro Connect - 添加吸顶灯Pro(两次点按配对 + WiFi配网)', () => {
let driver: DeviceDriver;
let reporter: TestReporter;
setupResilientHooks(() => driver);
beforeAll(async () => {
driver = createDriver();
await driver.createSession();
reporter = new TestReporter('CeilingLightPro_Connect', driver.platform.toUpperCase());
});
afterAll(async () => {
reporter.generate();
await driver.destroySession();
});
it('[P0][ONES:15950] 通过BLE+WiFi添加吸顶灯Pro', { timeout: 240000 }, async () => {
const start = Date.now();
const wifi = getWifiCredentials();
try {
const existing = await findDeviceNameOnHomepage(driver, NAME_PATTERN);
if (existing) {
console.log(`${existing}已在首页,跳过重新添加`);
saveAddedDevice('ceilingLight', existing);
reporter.record('[P0][ONES:15950] 添加吸顶灯Pro', 'SKIP', Date.now() - start, `${existing}已存在`);
return;
}
// BLE 配对前复位手机蓝牙(关→开),根治偶发 "FAIL: connection timeout"(重跑才过 = 蓝牙栈陈旧)
await resetBluetooth(driver);
const result = await addDeviceViaBLE(driver, {
categoryName: CATEGORY,
deviceKeyword: SCAN_KEYWORD,
registryCategory: 'ceilingLight', // 独立 key,灯控制按注册名找卡片(方案A)
namePattern: NAME_PATTERN,
scanTimeout: 30000,
skipScanStep: true, // 配对后点 Next 直接连接(无扫描列表)
// 配对:点一次→等2s→再点一次(ch1 按钮,实测能进配对;App 指引的"电源循环"文案对此 relay 接法无效)→ 点 Next(引导页完成按钮)
preSelectSteps: async (d) => {
await pressButton('ceiling_light_pro', 'main', 400).catch((e) => console.log(`relay ch1 按键失败: ${e.message}`));
await sleep(2000);
await pressButton('ceiling_light_pro', 'main', 400).catch((e) => console.log(`relay ch1 第二次按键失败: ${e.message}`));
await sleep(2000);
const next = await d.findElementRaw('-android uiautomator', 'new UiSelector().text("Next")');
if (next) { await d.tapElement(next); await sleep(3000); }
else console.log('引导页未找到 Next');
},
postConnectionSteps: async (d) => { await configureHubWifi(d, wifi); },
// 命名页完成按钮是 "Start now"(默认向导没有)→ 必须加,否则设备不落账号。Return Home 兜底。
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);
// 添加完成后吸顶灯通常为打开状态 → 按一次按钮关灯(避免长亮)
await pressButton('ceiling_light_pro', 'main', 400).catch((e) => console.log(`关灯按键失败: ${e.message}`));
await sleep(1000);
const elapsed = ((Date.now() - start) / 1000).toFixed(1);
reporter.record('[P0][ONES:15950] 添加吸顶灯Pro', 'PASS', Date.now() - start, `添加成功(WiFi:${wifi.ssid}), 耗时${elapsed}s`);
} catch (e: any) {
const ss = await driver.screenshot().catch(() => '');
reporter.record('[P0][ONES:15950] 添加吸顶灯Pro', 'FAIL', Date.now() - start, e.message, ss);
throw e;
}
});
});