UNPKG

@apistudio/apim-cli

Version:

CLI for API Management Products

86 lines (76 loc) 2.76 kB
/** * Copyright IBM Corp. 2024, 2025 */ import { VariableContext } from "../../../src/engine/variable-context-manager/variable-context.js"; describe("VariableContext", () => { let context: VariableContext; beforeEach(() => { context = new VariableContext(); }); it("should set and get a string", () => { context.setEnvVariable("key", "global Value"); expect(context.get("key")?.value).toBe("global Value"); expect(context.getValue("key")).toBe("global Value"); context.set("key", "value"); context.setVariable("key1", "value"); expect(context.get("key")?.value).toBe("value"); context.setEnvVariable("key", "global Value 2"); expect(context.get("key")?.value).toBe("value"); expect(context.getAll()).toEqual({ key: { isSecret: false, value: "value" }, key1: { isSecret: false, value: "value" }, }); expect(context.getVarStore()).toEqual({ key: { isSecret: false, value: "value" }, key1: { isSecret: false, value: "value" }, }); expect(context.getEnvStore()).toEqual({ key: { isSecret: false, value: "global Value 2" }, }); }); it("should handle numbers, booleans, objects, arrays", () => { context.setVariable("num", 42); context.setVariable("bool", true); context.setVariable("arr", [1, 2, 3]); context.setVariable("obj", { a: 1 }); expect(context.get("num")?.value).toBe(42); expect(context.get("bool")?.value).toBe(true); expect(context.get("arr")?.value).toEqual([1, 2, 3]); expect(context.get("obj")?.value).toEqual({ a: 1 }); expect(context.getValue("num")).toBe(42); expect(context.getValue("bool")).toBe(true); expect(context.getValue("arr")).toEqual([1, 2, 3]); expect(context.getValue("obj")).toEqual({ a: 1 }); }); it("should handle environment variable format", () => { const env = { key: "content-type", value: "application/json", isSecret: true, }; context.setVariable(env.key, env.value, env.isSecret); expect(context.get(env.key)?.value).toBe(env.value); expect(context.getValue(env.key)).toBe(env.value); expect(context.get(env.key)).toEqual({ value: env.value, isSecret: env.isSecret, }); }); it("should return all variables with getAll()", () => { context.setVariable("a", 1); context.setVariable("b", 2); expect(context.getAll()).toEqual({ a: { value: 1, isSecret: false }, b: { value: 2, isSecret: false }, }); context.delete("b"); expect(context.getAll()).toEqual({ a: { value: 1, isSecret: false }, }); }); it("should clear the context", () => { context.setVariable("temp", "data"); context.clear(); expect(context.getAll()).toEqual({}); }); });