98 lines
4.9 KiB
TypeScript
98 lines
4.9 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,
|
|
addDeviceWithSerialPairing,
|
|
findDeviceNameOnHomepage,
|
|
saveAddedDevice,
|
|
configureHubWifi,
|
|
ensurePlugOn,
|
|
onesAdd,
|
|
} 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') });
|
|
|
|
// Plug Mini 是 WiFi 产品:relay 按键配对 + WiFi 配网。ch1=JP(默认)/ch2=US。
|
|
const PLUG_MINI_CATEGORY = process.env.PLUG_MINI_CATEGORY || 'Plug Mini (JP)';
|
|
const RELAY_DEVICE = process.env.PLUG_MINI_RELAY || 'plug_mini'; // JP=plug_mini(ch1) / US=plug_mini_us(ch2)
|
|
// iOS 插座首页名**无区域后缀**(实测 "Plug Mini 8A"),JP/US/EU 无法区分 → 同 curtain:添加时按类型内联改名为 "Plug JP/US/EU",
|
|
// skip/校验/注册都用改名后的名(互不串台)。可用 PLUG_MINI_RENAME 覆盖。
|
|
const PLUG_REGION = (PLUG_MINI_CATEGORY.match(/\(([^)]+)\)/)?.[1] || 'JP').trim(); // JP / US / EU
|
|
const RENAME_TO = process.env.PLUG_MINI_RENAME || `Plug ${PLUG_REGION}`;
|
|
const deviceName = process.env.PLUG_MINI_DEVICE || RENAME_TO; // 校验关键字 = 改名后的名
|
|
const PLUG_MINI_PATTERN = process.env.PLUG_MINI_PATTERN || RENAME_TO;
|
|
const ADD_ANCHOR = process.env.PLUG_MINI_ONES ? `[P0][ONES:${process.env.PLUG_MINI_ONES}]` : onesAdd('plug', 'PlugMini 5E'); // EU 传 PLUG_MINI_ONES=332918
|
|
|
|
describe('Plug Mini Connect - 添加Plug Mini设备(串口配对 + WiFi配网)', () => {
|
|
let driver: DeviceDriver;
|
|
let reporter: TestReporter;
|
|
|
|
beforeAll(async () => {
|
|
driver = createDriver();
|
|
await driver.createSession();
|
|
reporter = new TestReporter(`PlugMini_Connect_${PLUG_REGION}`, driver.platform.toUpperCase());
|
|
});
|
|
|
|
afterAll(async () => {
|
|
reporter.generate();
|
|
await driver.destroySession();
|
|
});
|
|
|
|
it(`${ADD_ANCHOR} 通过BLE+WiFi添加Plug Mini(${PLUG_MINI_CATEGORY})`, { timeout: 360000 }, async () => {
|
|
const start = Date.now();
|
|
const wifi = getWifiCredentials();
|
|
try {
|
|
const existing = await findDeviceNameOnHomepage(driver, PLUG_MINI_PATTERN);
|
|
if (existing) {
|
|
console.log(`${existing}已在首页,跳过重新添加`);
|
|
saveAddedDevice('plug', existing);
|
|
// 供电前提:灯/灯泡插在该插座上,确保插座为 ON(下游灯才有电配对/控制)。失败不影响添加结果。
|
|
await ensurePlugOn(driver, existing).catch((e) => console.log(`ensurePlugOn 跳过(非致命): ${e.message}`));
|
|
reporter.record(`${ADD_ANCHOR} 添加${PLUG_MINI_CATEGORY}`, 'SKIP', Date.now() - start, `${existing}已存在`);
|
|
return;
|
|
}
|
|
|
|
const result = await addDeviceWithSerialPairing(driver, {
|
|
categoryName: PLUG_MINI_CATEGORY,
|
|
categoryScrollHint: 5,
|
|
deviceKeyword: deviceName,
|
|
relayDevice: RELAY_DEVICE, // ch1(JP)/ch2(US),长按 2s
|
|
registryCategory: 'plug',
|
|
namePattern: PLUG_MINI_PATTERN,
|
|
// plug 第一次按键+Next 即连上去 WiFi 页;重按会把它弄出连接 → 不重按,只延长等待
|
|
repressOnRetry: false,
|
|
tapOtherPairing: process.env.PLUG_OTHER_PAIRING === '1', // JP 自动配对不行,需点"其他配对方式"(PLUG_OTHER_PAIRING=1 启用)
|
|
pairRetries: 2,
|
|
pairPollIters: 16,
|
|
// 选品类后是 "Before Using" 免责页(非 Disclaimer 关键字),beforePairing 里点 Next 过掉 → 到"长按按钮"引导页
|
|
beforePairing: async (d) => {
|
|
const n = await d.findElementRaw('-android uiautomator', 'new UiSelector().text("Next")');
|
|
if (n) { await d.tapElement(n); await sleep(2000); }
|
|
},
|
|
// 连接后:进 Wi-Fi Settings 页填 SSID/密码(configureHubWifi 已兼容标题/输入框检测)
|
|
postConnectSteps: async (d) => { await configureHubWifi(d, wifi, { renameTo: RENAME_TO }); },
|
|
connectionKeywords: [
|
|
'Configure Wi-Fi', 'Connecting', 'Pick a room', 'Done',
|
|
'added successfully', 'Added successfully', 'Initial Setup',
|
|
],
|
|
});
|
|
expect(result).toBe(true);
|
|
|
|
// 供电前提:灯/灯泡插在该插座上,确保插座为 ON(下游灯才有电配对/控制)。失败不影响添加结果。
|
|
await ensurePlugOn(driver, deviceName).catch((e) => console.log(`ensurePlugOn 跳过(非致命): ${e.message}`));
|
|
|
|
const elapsed = ((Date.now() - start) / 1000).toFixed(1);
|
|
reporter.record(`${ADD_ANCHOR} 添加${PLUG_MINI_CATEGORY}`, 'PASS', Date.now() - start, `${deviceName}添加成功(WiFi:${wifi.ssid}), 耗时${elapsed}s`);
|
|
} catch (e: any) {
|
|
const ss = await driver.screenshot().catch(() => '');
|
|
reporter.record(`${ADD_ANCHOR} 添加${PLUG_MINI_CATEGORY}`, 'FAIL', Date.now() - start, e.message, ss);
|
|
throw e;
|
|
}
|
|
});
|
|
});
|