70 lines
2.5 KiB
TypeScript
70 lines
2.5 KiB
TypeScript
import { describe, expect, it } from "@suaveplan/testing/runner";
|
|
import type { OperatorDefinition } from "../types/index.js";
|
|
import { OperatorRegistry } from "./registry.js";
|
|
|
|
const dummy: OperatorDefinition = {
|
|
name: "dummy",
|
|
description: "test",
|
|
evaluate: () => true,
|
|
};
|
|
|
|
describe("OperatorRegistry", () => {
|
|
it("registers and retrieves an operator", { timeout: 5000 }, () => {
|
|
const r = new OperatorRegistry();
|
|
r.register("dummy", dummy);
|
|
expect(r.has("dummy")).toBe(true);
|
|
expect(r.get("dummy")).toBe(dummy);
|
|
expect(r.names()).toContain("dummy");
|
|
});
|
|
|
|
it("get returns undefined for unknown names", { timeout: 5000 }, () => {
|
|
const r = new OperatorRegistry();
|
|
expect(r.get("missing")).toBeUndefined();
|
|
expect(r.has("missing")).toBe(false);
|
|
});
|
|
|
|
it("mustGet raises RULES.OPERATOR_NOT_FOUND", { timeout: 5000 }, () => {
|
|
const r = new OperatorRegistry();
|
|
expect(() => r.mustGet("missing")).toThrow(/RULES\.OPERATOR_NOT_FOUND/);
|
|
});
|
|
|
|
it("rejects empty name with RULES.OPERATOR_INVALID", {
|
|
timeout: 5000,
|
|
}, () => {
|
|
const r = new OperatorRegistry();
|
|
expect(() => r.register("", dummy)).toThrow(/RULES\.OPERATOR_INVALID/);
|
|
});
|
|
|
|
it("re-register replaces the prior entry", { timeout: 5000 }, () => {
|
|
const r = new OperatorRegistry();
|
|
const a: OperatorDefinition = { ...dummy, description: "a" };
|
|
const b: OperatorDefinition = { ...dummy, description: "b" };
|
|
r.register("dummy", a);
|
|
r.register("dummy", b);
|
|
expect(r.get("dummy")).toBe(b);
|
|
});
|
|
|
|
it("registers aliases", { timeout: 5000 }, () => {
|
|
const r = new OperatorRegistry();
|
|
r.register("dummy", { ...dummy, aliases: ["d", "dum"] });
|
|
expect(r.has("d")).toBe(true);
|
|
expect(r.has("dum")).toBe(true);
|
|
expect(r.get("d")?.name).toBe("dummy");
|
|
});
|
|
|
|
it("ignores empty alias strings", { timeout: 5000 }, () => {
|
|
const r = new OperatorRegistry();
|
|
r.register("dummy", { ...dummy, aliases: ["", "alias"] });
|
|
expect(r.has("")).toBe(false);
|
|
expect(r.has("alias")).toBe(true);
|
|
});
|
|
|
|
it("unregister removes and reports", { timeout: 5000 }, () => {
|
|
const r = new OperatorRegistry();
|
|
r.register("dummy", dummy);
|
|
expect(r.unregister("dummy")).toBe(true);
|
|
expect(r.unregister("dummy")).toBe(false);
|
|
expect(r.has("dummy")).toBe(false);
|
|
});
|
|
});
|