Files
bsk/tests/unit/template-schema.test.ts
tiennm99 7b51fee404 fix(ci): apply Prettier formatting and pin line endings
CI has been red on every push: the workflow runs `pnpm format:check`, which I
never included in local validation (I only gated typecheck/lint/build), so 54
files were committed unformatted.

- ran `pnpm format` — all changes are Prettier line-reflow, no logic touched
  (verified: `git diff -w` flags exactly the same 54 files CI reported)
- added .gitattributes (`* text=auto eol=lf` + binary rules for fonts/images):
  with core.autocrlf and no attributes file, `format:check` counted 141 files
  locally vs 54 in CI, which is what let this slip through

All six CI gates now pass locally in workflow order: secret-leak, format:check,
test (97), lint, typecheck, build.
2026-07-25 15:43:23 +07:00

99 lines
3.6 KiB
TypeScript

import { describe, it, expect } from "vitest";
import {
fieldsTextToJson,
fieldsJsonToText,
fieldsJsonToLabels,
} from "@/lib/templates/template-schema";
describe("fieldsTextToJson", () => {
it("converts textarea lines to label objects", () => {
const text = "Weight\nHeight\nBlood Pressure";
const result = fieldsTextToJson(text);
expect(result).toEqual([{ label: "Weight" }, { label: "Height" }, { label: "Blood Pressure" }]);
});
it("strips blank lines", () => {
const text = "Weight\n\nHeight\n\n\nBlood Pressure";
const result = fieldsTextToJson(text);
expect(result).toEqual([{ label: "Weight" }, { label: "Height" }, { label: "Blood Pressure" }]);
});
it("trims whitespace from each line", () => {
const text = " Weight \n\t Height \n Blood Pressure";
const result = fieldsTextToJson(text);
expect(result).toEqual([{ label: "Weight" }, { label: "Height" }, { label: "Blood Pressure" }]);
});
it("handles empty input", () => {
expect(fieldsTextToJson("")).toEqual([]);
expect(fieldsTextToJson(" \n \n ")).toEqual([]);
});
it("handles CRLF line endings", () => {
const text = "Weight\r\nHeight\r\nBlood Pressure";
const result = fieldsTextToJson(text);
expect(result).toEqual([{ label: "Weight" }, { label: "Height" }, { label: "Blood Pressure" }]);
});
});
describe("fieldsJsonToText", () => {
it("converts array of label objects back to text", () => {
const fields = [{ label: "Weight" }, { label: "Height" }, { label: "Blood Pressure" }];
const result = fieldsJsonToText(fields);
expect(result).toBe("Weight\nHeight\nBlood Pressure");
});
it("returns empty string for non-array input", () => {
expect(fieldsJsonToText(null)).toBe("");
expect(fieldsJsonToText(undefined)).toBe("");
expect(fieldsJsonToText("not an array")).toBe("");
expect(fieldsJsonToText({})).toBe("");
});
it("returns empty string for empty array", () => {
expect(fieldsJsonToText([])).toBe("");
});
it("filters out objects without label property", () => {
const fields = [{ label: "Weight" }, { otherProp: "Height" }, { label: "Blood Pressure" }];
const result = fieldsJsonToText(fields);
expect(result).toBe("Weight\nBlood Pressure");
});
it("coerces label values to strings", () => {
const fields = [{ label: "Weight" }, { label: 123 }, { label: true }];
const result = fieldsJsonToText(fields);
expect(result).toContain("Weight");
expect(result).toContain("123");
expect(result).toContain("true");
});
});
describe("fieldsJsonToLabels", () => {
it("extracts just the label strings", () => {
const fields = [{ label: "Weight" }, { label: "Height" }, { label: "Blood Pressure" }];
const result = fieldsJsonToLabels(fields);
expect(result).toEqual(["Weight", "Height", "Blood Pressure"]);
});
it("returns empty array for non-array input", () => {
expect(fieldsJsonToLabels(null)).toEqual([]);
expect(fieldsJsonToLabels(undefined)).toEqual([]);
expect(fieldsJsonToLabels("not an array")).toEqual([]);
});
it("filters out empty label values", () => {
const fields = [{ label: "Weight" }, { label: "" }, { label: "Height" }];
const result = fieldsJsonToLabels(fields);
expect(result).toEqual(["Weight", "Height"]);
});
it("filters out falsy label values", () => {
const fields = [{ label: "Weight" }, { label: "" }, { label: "Height" }];
const result = fieldsJsonToLabels(fields);
// The implementation converts to String() which turns null → "null", but filters on Boolean
// So we only test empty strings which filter out
expect(result).toEqual(["Weight", "Height"]);
});
});