filters.test.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { describe, expect, it } from "vitest";
  2. import type { ShaderDef } from "@nengine/core";
  3. import {
  4. filterParams,
  5. BLUR_DEFAULT_STRENGTH,
  6. GRAYSCALE_DEFAULT_FACTOR,
  7. BRIGHTNESS_DEFAULT_FACTOR,
  8. HUE_DEFAULT_DEGREES,
  9. } from "../src/filters.js";
  10. const def = (overrides: Partial<ShaderDef> = {}): ShaderDef => ({ id: "fx", filter: "blur", ...overrides });
  11. describe("filterParams (pure shader-def → numeric/color parameters)", () => {
  12. it("maps blur with a strength param", () => {
  13. expect(filterParams(def({ filter: "blur", params: { strength: 4 } }))).toEqual({ kind: "blur", strength: 4 });
  14. });
  15. it("defaults blur strength when absent", () => {
  16. expect(filterParams(def({ filter: "blur" }))).toEqual({ kind: "blur", strength: BLUR_DEFAULT_STRENGTH });
  17. });
  18. it("maps tint from a #rrggbb color string", () => {
  19. expect(filterParams(def({ filter: "tint", params: { color: "#2f3b4c" } }))).toEqual({ kind: "tint", color: "#2f3b4c" });
  20. });
  21. it("maps tint from r/g/b components (clamped to 0-255)", () => {
  22. expect(filterParams(def({ filter: "tint", params: { r: 47, g: 59, b: 76 } }))).toEqual({ kind: "tint", color: "#2f3b4c" });
  23. expect(filterParams(def({ filter: "tint", params: { r: 300, g: -5, b: 0 } }))).toEqual({ kind: "tint", color: "#ff0000" });
  24. });
  25. it("defaults tint to white when no color params are given", () => {
  26. expect(filterParams(def({ filter: "tint" }))).toEqual({ kind: "tint", color: "#ffffff" });
  27. });
  28. it("maps grayscale with a factor", () => {
  29. expect(filterParams(def({ filter: "grayscale", params: { factor: 0.7 } }))).toEqual({ kind: "grayscale", factor: 0.7 });
  30. expect(filterParams(def({ filter: "grayscale" }))).toEqual({ kind: "grayscale", factor: GRAYSCALE_DEFAULT_FACTOR });
  31. });
  32. it("maps sepia without params", () => {
  33. expect(filterParams(def({ filter: "sepia" }))).toEqual({ kind: "sepia" });
  34. });
  35. it("maps hue with degrees", () => {
  36. expect(filterParams(def({ filter: "hue", params: { degrees: 120 } }))).toEqual({ kind: "hue", degrees: 120 });
  37. expect(filterParams(def({ filter: "hue" }))).toEqual({ kind: "hue", degrees: HUE_DEFAULT_DEGREES });
  38. });
  39. it("maps brightness with a factor", () => {
  40. expect(filterParams(def({ filter: "brightness", params: { factor: 1.2 } }))).toEqual({ kind: "brightness", factor: 1.2 });
  41. expect(filterParams(def({ filter: "brightness" }))).toEqual({ kind: "brightness", factor: BRIGHTNESS_DEFAULT_FACTOR });
  42. });
  43. it("ignores non-numeric params (falls back to defaults)", () => {
  44. expect(filterParams(def({ filter: "blur", params: { strength: "high" } }))).toEqual({ kind: "blur", strength: BLUR_DEFAULT_STRENGTH });
  45. });
  46. });