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, } 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') }); // 天气站(Weather Station):继电器 ch13,WiFi 产品,添加流程同水浸(Water Leak Detector)。 // 入口品类名 "Weather Station";自包含(env 可覆盖),不依赖 device.config/ones-anchor。 const CATEGORY = process.env.WEATHER_CATEGORY || 'Weather Station'; const deviceName = process.env.WEATHER_DEVICE || 'Weather Station'; const NAME_PATTERN = process.env.WEATHER_PATTERN || 'Weather Station\\s*\\w*'; const ADD_ANCHOR = process.env.WEATHER_ONES ? `[P0][ONES:${process.env.WEATHER_ONES}]` : '[P0] 添加天气站'; describe('Weather Station Connect - 添加天气站(串口配对 + WiFi配网)', () => { let driver: DeviceDriver; let reporter: TestReporter; beforeAll(async () => { driver = createDriver(); await driver.createSession(); reporter = new TestReporter('WeatherStation_Connect', driver.platform.toUpperCase()); }); afterAll(async () => { reporter.generate(); await driver.destroySession(); }); it(`${ADD_ANCHOR} 通过BLE+WiFi添加天气站`, { timeout: 240000 }, async () => { const start = Date.now(); const wifi = getWifiCredentials(); try { const existing = await findDeviceNameOnHomepage(driver, NAME_PATTERN); if (existing) { console.log(`${existing}已在首页,跳过重新添加`); saveAddedDevice('weatherStation', existing); reporter.record(`${ADD_ANCHOR} 添加天气站`, 'SKIP', Date.now() - start, `${existing}已存在, 无需重新添加`); return; } const result = await addDeviceWithSerialPairing(driver, { categoryName: CATEGORY, categoryScrollHint: 4, deviceKeyword: deviceName, relayDevice: 'weather_station', // 继电器 ch13 长按 2s(config/relay.config) registryCategory: 'weatherStation', namePattern: NAME_PATTERN, // 连接后流程同水浸:① 点 "Connect Device" 启动连接 ② 进 Configure Wi-Fi 页填 SSID/密码 postConnectSteps: async (d) => { const btn = await d.findElementRaw('-android uiautomator', 'new UiSelector().text("Connect Device")') || await d.findElementRaw('-android uiautomator', 'new UiSelector().text("Connect device")'); if (btn) { await d.tapElement(btn); await sleep(2500); } await configureHubWifi(d, wifi); }, connectionKeywords: [ 'Connect Device', 'Configure Wi-Fi', 'Connecting', 'Discover device', 'Pick a room', 'Done', 'added successfully', 'Initial Setup', ], }); expect(result).toBe(true); const elapsed = ((Date.now() - start) / 1000).toFixed(1); reporter.record(`${ADD_ANCHOR} 添加天气站`, 'PASS', Date.now() - start, `${deviceName}添加成功(WiFi:${wifi.ssid}), 耗时${elapsed}s`); } catch (e: any) { const ss = await driver.screenshot().catch(() => ''); reporter.record(`${ADD_ANCHOR} 添加天气站`, 'FAIL', Date.now() - start, e.message, ss); throw e; } }); });