SPB Git forge

spb/polyllm

Public
15commits 1branches 0releases
2.2 MBsize
maindefault branch
13 days agolast push
TypeScript 97.4% SQL 1% JavaScript 0.9% CSS 0.6%
2.2 KB · 47 lines typescript
Raw Blame History
1import { describe, it, expect } from "vitest";2import { parseVariables, mergeVariables, renderTemplate, labelFor, variablesToAsk } from "@/lib/prompts/variables";34describe("parseVariables", () => {5  it("returns unique names in order of first appearance", () => {6    expect(parseVariables("Translate {{text}} into {{lang}}. Keep {{text}} tone.")).toEqual(["text", "lang"]);7  });8  it("tolerates whitespace and allows _ - . in names", () => {9    expect(parseVariables("{{ customer_name }} / {{ order-id }} / {{ meta.locale }}")).toEqual(["customer_name", "order-id", "meta.locale"]);10  });11  it("ignores JSON examples, single braces and invalid names", () => {12    expect(parseVariables('{"a": 1} {b} {{1bad}} {{ }} {{ok}}')).toEqual(["ok"]);13    expect(parseVariables("")).toEqual([]);14  });15});1617describe("mergeVariables", () => {18  it("keeps labels/defaults for surviving names, appends new, drops removed", () => {19    const merged = mergeVariables("Hi {{name}}, {{tone}} please", [20      { name: "name", label: "Customer", default: "there" },21      { name: "removed", default: "x" },22    ]);23    expect(merged).toEqual([{ name: "name", label: "Customer", default: "there" }, { name: "tone" }]);24  });25});2627describe("renderTemplate", () => {28  it("fills values, then defaults, then empty strings and reports both", () => {29    const r = renderTemplate("{{a}}-{{b}}-{{c}}", { a: "A" }, [{ name: "b", default: "B" }, { name: "c" }]);30    expect(r.text).toBe("A-B-");31    expect(r.defaulted).toEqual(["b"]);32    expect(r.missing).toEqual(["c"]);33  });34  it("replaces every occurrence and leaves non-variables intact", () => {35    expect(renderTemplate("{{x}} {{ x }} {y} {{1}}", { x: "ok" }).text).toBe("ok ok {y} {{1}}");36  });37});3839describe("labelFor / variablesToAsk", () => {40  it("humanizes names and asks only for variables without defaults", () => {41    expect(labelFor({ name: "customer_name" })).toBe("Customer name");42    expect(labelFor({ name: "targetLang" })).toBe("Target lang");43    expect(labelFor({ name: "x", label: " Custom " })).toBe("Custom");44    expect(variablesToAsk([{ name: "a", default: "1" }, { name: "b" }, { name: "c", default: "2", required: true }]).map((v) => v.name)).toEqual(["b", "c"]);45  });46});47