feat: 初始化 miot_x TypeScript 工作流构建器项目
This commit is contained in:
41
tests/validate.test.ts
Normal file
41
tests/validate.test.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { compileRule, readDeviceInventoryFile, validateBackup } from "../src/index.ts";
|
||||
import { eventTurnOn } from "../examples/rules/index.ts";
|
||||
|
||||
const now = 1_760_000_000_000;
|
||||
|
||||
describe("备份校验", () => {
|
||||
test("非法节点 ID 会失败", () => {
|
||||
const backup = backupFromRule();
|
||||
backup.rules[0]!.nodes[0]!.id = "bad-id";
|
||||
const codes = validateBackup(backup).map((diagnostic) => diagnostic.code);
|
||||
expect(codes).toContain("node-id-invalid");
|
||||
});
|
||||
|
||||
test("非法连接目标会失败", () => {
|
||||
const backup = backupFromRule();
|
||||
backup.rules[0]!.nodes[0]!.outputs.output = ["DO1.missing"];
|
||||
const codes = validateBackup(backup).map((diagnostic) => diagnostic.code);
|
||||
expect(codes).toContain("connection-port-missing");
|
||||
});
|
||||
|
||||
test("缺失设备会按严格模式报错", () => {
|
||||
const backup = backupFromRule();
|
||||
const diagnostics = validateBackup(backup, { devices: { devList: {} } });
|
||||
expect(diagnostics.filter((diagnostic) => diagnostic.code === "device-not-found")).toHaveLength(2);
|
||||
});
|
||||
|
||||
test("真实设备清单可通过设备校验", () => {
|
||||
const backup = backupFromRule();
|
||||
const diagnostics = validateBackup(backup, { devices: readDeviceInventoryFile("resources/devices.json") });
|
||||
expect(diagnostics.filter((diagnostic) => diagnostic.severity === "error")).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
function backupFromRule() {
|
||||
return {
|
||||
version: 2 as const,
|
||||
rules: [compileRule(eventTurnOn, { now }).rule],
|
||||
variables: { global: {} },
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user