feat(documents): selected-custom-fields encode/decode helpers
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
38
src/__tests__/selected-custom-fields.test.ts
Normal file
38
src/__tests__/selected-custom-fields.test.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import { describe, it, expect } from "vitest";
|
||||
import {
|
||||
encodeSelectedCustomFields,
|
||||
parseSelectedCustomFields,
|
||||
} from "../utils/custom-fields";
|
||||
|
||||
describe("selected custom fields encode/decode", () => {
|
||||
it("encodes a non-empty index array to a JSON string", () => {
|
||||
expect(encodeSelectedCustomFields([0, 2])).toBe("[0,2]");
|
||||
});
|
||||
|
||||
it("encodes empty / non-array to null", () => {
|
||||
expect(encodeSelectedCustomFields([])).toBeNull();
|
||||
expect(encodeSelectedCustomFields(undefined)).toBeNull();
|
||||
expect(encodeSelectedCustomFields("nope")).toBeNull();
|
||||
});
|
||||
|
||||
it("dedupes, sorts, and drops invalid entries before encoding", () => {
|
||||
expect(encodeSelectedCustomFields([2, 0, 2, -1, 1.5, 3])).toBe("[0,2,3]");
|
||||
});
|
||||
|
||||
it("parses a stored string back to a number array", () => {
|
||||
expect(parseSelectedCustomFields("[0,2]")).toEqual([0, 2]);
|
||||
});
|
||||
|
||||
it("parses null / malformed to an empty array", () => {
|
||||
expect(parseSelectedCustomFields(null)).toEqual([]);
|
||||
expect(parseSelectedCustomFields("")).toEqual([]);
|
||||
expect(parseSelectedCustomFields("{garbage")).toEqual([]);
|
||||
expect(parseSelectedCustomFields('"x"')).toEqual([]);
|
||||
});
|
||||
|
||||
it("parses already-array input (defensive) and filters invalid", () => {
|
||||
expect(parseSelectedCustomFields([0, "1", 2, -3] as unknown)).toEqual([
|
||||
0, 2,
|
||||
]);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user