feat: 驱动按 process.platform 自动选择,非 win32 抛 platform_unsupported

迁移前置项(指南五件小事 #2):
- RpaEngine.driver 改惰性 getter:win32 惰性创建 WindowsDriver,其他平台访问时
  抛 RuntimeError(platform_unsupported),import 不再隐式绑定 Windows
- 错误码 platform_unsupported 登记 RUNTIME_ERROR_CODES
- engine-driver.test.ts 覆盖两条路径
This commit is contained in:
yu 2026-11-05 17:56:34 +08:00
parent 8414c4f355
commit 51a6178bfc
3 changed files with 41 additions and 2 deletions

View File

@ -0,0 +1,22 @@
import { describe, expect, it } from "vitest";
import { createEngine } from "./engine";
import { WindowsDriver } from "../driver/windows/windows-driver";
describe("driver 平台自动选择(迁移指南五件小事 #2)", () => {
it("win32 上惰性创建 WindowsDriver 且实例缓存", () => {
const engine = createEngine();
expect(engine.driver).toBeInstanceOf(WindowsDriver);
expect(engine.driver).toBe(engine.driver);
});
it("非 win32 访问 driver 抛 platform_unsupported", () => {
const engine = createEngine();
const original = process.platform;
Object.defineProperty(process, "platform", { value: "darwin", configurable: true });
try {
expect(() => engine.driver).toThrowError(/暂不支持桌面自动化驱动/);
} finally {
Object.defineProperty(process, "platform", { value: original, configurable: true });
}
});
});

View File

@ -2,6 +2,7 @@ import type { Client } from "basic-ftp";
import type { Workbook as ExcelJSWorkbook } from "exceljs";
import { WindowsDriver } from "../driver/windows/windows-driver";
import type { AutomationDriver } from "../driver/types";
import { RuntimeError } from "./types";
/** 人为拟真操作的开关与随机参数(desktop 能力的进程级设置)。 */
export interface ManualOperationSettings {
@ -41,8 +42,23 @@ export class RpaEngine {
minMs: 1000,
maxMs: 3000
};
/** 桌面自动化驱动(任务 3.4 注入点,默认 PowerShell 版 WindowsDriver) */
readonly driver: AutomationDriver = new WindowsDriver();
/** 桌面自动化驱动:按系统自动选(迁移指南五件小事 #2)。win32 用 PowerShell 版 WindowsDriver;
* 其他平台尚未实现,访问时抛 platform_unsupported 明确报错(惰性创建,避免 import 即炸) */
private _driver?: AutomationDriver;
get driver(): AutomationDriver {
if (!this._driver) {
if (process.platform === "win32") {
this._driver = new WindowsDriver();
} else {
throw new RuntimeError(
"platform_unsupported",
`该平台暂不支持桌面自动化驱动(当前:${process.platform})`,
{ recommendation: "桌面/键鼠/窗口能力目前仅支持 Windows;macOS/Linux 驱动在迁移指南三端计划中规划" }
);
}
}
return this._driver;
}
}
export function createEngine(): RpaEngine {

View File

@ -40,6 +40,7 @@ export const RUNTIME_ERROR_CODES = [
"word_text_not_found",
"outlook_unavailable",
// 其他
"platform_unsupported",
"decryption_failed",
"host_unavailable"
] as const;