Files
HiveGrid-Wireframes/hivegrid-cloud/packages/email-sequences/src/conditions.test.ts
T

63 lines
2.6 KiB
TypeScript

import { describe, it, expect } from "vitest";
import { evaluateAllConditions, evaluateCondition, resolvePath } from "./conditions.js";
describe("resolvePath", () => {
it("walks nested objects", () => {
expect(resolvePath({ a: { b: { c: 1 } } }, "a.b.c")).toBe(1);
});
it("walks array indices", () => {
expect(resolvePath({ items: [{ name: "x" }, { name: "y" }] }, "items.1.name")).toBe("y");
});
it("returns undefined for missing path", () => {
expect(resolvePath({ a: 1 }, "a.b.c")).toBeUndefined();
});
it("returns undefined on empty path", () => {
expect(resolvePath({}, "")).toBeUndefined();
});
});
describe("evaluateCondition", () => {
const payload = { lead: { status: "qualified", tags: ["alpha", "beta"] }, count: 5 };
it("supports eq / neq", () => {
expect(evaluateCondition(payload, { path: "lead.status", op: "eq", value: "qualified" })).toBe(true);
expect(evaluateCondition(payload, { path: "lead.status", op: "neq", value: "qualified" })).toBe(false);
});
it("supports in / not_in with arrays", () => {
expect(evaluateCondition(payload, { path: "lead.status", op: "in", value: ["new", "qualified"] })).toBe(true);
expect(evaluateCondition(payload, { path: "lead.status", op: "not_in", value: ["new"] })).toBe(true);
expect(evaluateCondition(payload, { path: "lead.status", op: "not_in", value: ["qualified"] })).toBe(false);
});
it("supports exists / not_exists", () => {
expect(evaluateCondition(payload, { path: "lead.status", op: "exists" })).toBe(true);
expect(evaluateCondition(payload, { path: "lead.missing", op: "not_exists" })).toBe(true);
expect(evaluateCondition(payload, { path: "lead.status", op: "not_exists" })).toBe(false);
});
it("returns false for unknown op", () => {
expect(
evaluateCondition(payload, { path: "lead.status", op: "unknown" as unknown as "eq", value: "x" })
).toBe(false);
});
});
describe("evaluateAllConditions", () => {
it("returns true for undefined / empty list", () => {
expect(evaluateAllConditions({}, undefined)).toBe(true);
expect(evaluateAllConditions({}, [])).toBe(true);
});
it("AND-combines multiple conditions", () => {
const payload = { status: "qualified", plan: "pro" };
expect(
evaluateAllConditions(payload, [
{ path: "status", op: "eq", value: "qualified" },
{ path: "plan", op: "eq", value: "pro" },
])
).toBe(true);
expect(
evaluateAllConditions(payload, [
{ path: "status", op: "eq", value: "qualified" },
{ path: "plan", op: "eq", value: "starter" },
])
).toBe(false);
});
});