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 { addDeviceWithSerialPairing, findDeviceNameOnHomepage, saveAddedDevice, onesAdd, } from '../../utils/common'; import { getDeviceName, getModelName } from '../../config/device.config'; import * as dotenv from 'dotenv'; import * as path from 'path'; dotenv.config({ path: path.resolve(__dirname, '../../.env') }); const deviceName = getDeviceName('sensor', 'CONTACT_DEVICE'); // 默认 'Contact Sensor 5O' // 首页实际名形如 "Contact Sensor 5O"(型号后缀两位字母/数字)。可用 env 覆盖 const CONTACT_PATTERN = process.env.CONTACT_PATTERN || 'Contact Sensor\\s+[0-9A-Za-z]{2}'; // ONES 锚点:Contact Sensor 归在 'sensor' 品类(add: 15962) const ADD_ANCHOR = onesAdd('sensor', getModelName('sensor', 'CONTACT_DEVICE')); describe('Contact Sensor Connect - 添加Contact Sensor设备', () => { let driver: DeviceDriver; let reporter: TestReporter; beforeAll(async () => { driver = createDriver(); await driver.createSession(); reporter = new TestReporter('ContactSensor_Connect', driver.platform.toUpperCase()); }); afterAll(async () => { reporter.generate(); await driver.destroySession(); }); it(`${ADD_ANCHOR} 通过BLE添加Contact Sensor设备`, { timeout: 180000 }, async () => { const start = Date.now(); try { // 已添加则按模式拿到首页实际名并回写;否则走 ch44 继电器配对添加 const existing = await findDeviceNameOnHomepage(driver, CONTACT_PATTERN); if (existing) { console.log(`${existing}已在首页,跳过重新添加`); saveAddedDevice('sensor', existing); reporter.record(`${ADD_ANCHOR} 添加Contact Sensor设备`, 'SKIP', Date.now() - start, `${existing}已存在, 无需重新添加`); return; } const result = await addDeviceWithSerialPairing(driver, { categoryName: 'Contact Sensor', categoryScrollHint: 4, // 实测滑4次到,边滑边找(首跑打印实际次数后再提速) deviceKeyword: deviceName, relayDevice: 'contact', // config/relay.config: ch44, 长按 2s(待实测确认) registryCategory: 'sensor', namePattern: CONTACT_PATTERN, // 首页实际名(如 "Contact Sensor 5O") // contact 向导:命名页=Save;安装指引=Next;安装门页底部按钮文字="Test the installation"(实为继续键 tvInstallDoor3Next); // 测试页有 "Done" 完成(无需真实开合门即可结束);可能有 OK 确认弹窗。完成=Done。 wizardButtons: ['Save', 'Test the installation', 'Done', 'Next', 'Use now', 'Start Using', 'Got it', 'OK', 'Skip'], connectionKeywords: [ 'Pick a room', 'Save', 'Initial Setup', 'added successfully', 'Added successfully', 'Got it', 'Done', 'How to install', 'Test the installation', 'Test the Installation', ], }); expect(result).toBe(true); const elapsed = ((Date.now() - start) / 1000).toFixed(1); reporter.record(`${ADD_ANCHOR} 添加Contact Sensor设备`, 'PASS', Date.now() - start, `${deviceName}添加成功, 耗时${elapsed}s`); } catch (e: any) { const ss = await driver.screenshot().catch(() => ''); reporter.record(`${ADD_ANCHOR} 添加Contact Sensor设备`, 'FAIL', Date.now() - start, e.message, ss); throw e; } }); });