|
| 1 | +// © 2026 Adobe. MIT License. See /LICENSE for details. |
| 2 | + |
| 3 | +import { describe, it, expect } from "vitest"; |
| 4 | +import { createCoerceFunction } from "./create-coerce-function.js"; |
| 5 | +import { Schema } from "./index.js"; |
| 6 | +import type { Schema as SchemaType } from "./schema.js"; |
| 7 | + |
| 8 | +const num = { type: "number" } as const satisfies SchemaType; |
| 9 | +const f32 = { type: "number", precision: 1 } as const satisfies SchemaType; |
| 10 | +const bool = { type: "boolean" } as const satisfies SchemaType; |
| 11 | +const str = { type: "string" } as const satisfies SchemaType; |
| 12 | + |
| 13 | +// createCoerceFunction never returns null in the "possible" cases — narrow it. |
| 14 | +const coercer = (input: SchemaType, output: SchemaType) => { |
| 15 | + const fn = createCoerceFunction(input, output); |
| 16 | + expect(fn).not.toBeNull(); |
| 17 | + return fn!; |
| 18 | +}; |
| 19 | + |
| 20 | +describe("createCoerceFunction — numbers", () => { |
| 21 | + it("number → number with no bounds is identity (F64→F32 precision loss is the buffer's job)", () => { |
| 22 | + const fn = coercer(num, f32); |
| 23 | + expect(fn(3.14159)).toBe(3.14159); |
| 24 | + expect(fn(-100)).toBe(-100); |
| 25 | + }); |
| 26 | + |
| 27 | + it("clamps to the output maximum / minimum when declared", () => { |
| 28 | + expect(coercer(num, { type: "number", maximum: 100 })(150)).toBe(100); |
| 29 | + expect(coercer(num, { type: "number", maximum: 100 })(50)).toBe(50); |
| 30 | + expect(coercer(num, { type: "number", minimum: 0 })(-5)).toBe(0); |
| 31 | + const both = coercer(num, { type: "number", minimum: 0, maximum: 10 }); |
| 32 | + expect([both(-1), both(5), both(99)]).toEqual([0, 5, 10]); |
| 33 | + }); |
| 34 | + |
| 35 | + it("caps a wide integer down to a narrow integer range (the U32→'U16' case)", () => { |
| 36 | + const fn = coercer({ type: "integer" }, { type: "integer", minimum: 0, maximum: 65535 }); |
| 37 | + expect(fn(70000)).toBe(65535); |
| 38 | + expect(fn(-5)).toBe(0); |
| 39 | + expect(fn(40000)).toBe(40000); |
| 40 | + }); |
| 41 | +}); |
| 42 | + |
| 43 | +describe("createCoerceFunction — scalars & enums", () => { |
| 44 | + it("boolean → boolean and string → string are identity", () => { |
| 45 | + expect(coercer(bool, bool)(true)).toBe(true); |
| 46 | + expect(coercer(str, str)("hi")).toBe("hi"); |
| 47 | + }); |
| 48 | + |
| 49 | + it("enum → enum only when every input value is an output value", () => { |
| 50 | + const widen = coercer({ enum: ["a", "b"] }, { enum: ["a", "b", "c"] }); |
| 51 | + expect(widen("a")).toBe("a"); |
| 52 | + expect(createCoerceFunction({ enum: ["a", "z"] }, { enum: ["a", "b"] })).toBeNull(); |
| 53 | + }); |
| 54 | + |
| 55 | + it("→ const collapses any input to the constant", () => { |
| 56 | + const fn = coercer(num, { const: 42 }); |
| 57 | + expect(fn(1)).toBe(42); |
| 58 | + expect(fn(999)).toBe(42); |
| 59 | + }); |
| 60 | +}); |
| 61 | + |
| 62 | +describe("createCoerceFunction — objects", () => { |
| 63 | + it("reorders fields (order is irrelevant to the produced value)", () => { |
| 64 | + const fn = coercer( |
| 65 | + { type: "object", properties: { x: f32, y: f32 } }, |
| 66 | + { type: "object", properties: { y: f32, x: f32 } }, |
| 67 | + ); |
| 68 | + expect(fn({ x: 1, y: 2 })).toEqual({ x: 1, y: 2 }); |
| 69 | + }); |
| 70 | + |
| 71 | + it("fills a new field from its default", () => { |
| 72 | + const fn = coercer( |
| 73 | + { type: "object", properties: { x: f32, y: f32 } }, |
| 74 | + { type: "object", properties: { x: f32, y: f32, z: { type: "number", default: 9 } } }, |
| 75 | + ); |
| 76 | + expect(fn({ x: 1, y: 2 })).toEqual({ x: 1, y: 2, z: 9 }); |
| 77 | + }); |
| 78 | + |
| 79 | + it("requires a default for a new field of a numeric STRUCT (every field is packed)", () => { |
| 80 | + // f32 properties pack as a struct → a missing field would be NaN, so a |
| 81 | + // new field without a default makes the conversion impossible. |
| 82 | + expect(createCoerceFunction( |
| 83 | + { type: "object", properties: { x: f32 } }, |
| 84 | + { type: "object", properties: { x: f32, z: f32 } }, |
| 85 | + )).toBeNull(); |
| 86 | + }); |
| 87 | + |
| 88 | + it("omits a new NON-required field with no default on a plain object", () => { |
| 89 | + // f64 properties do not pack as a struct → a plain object may simply lack |
| 90 | + // an optional field, so this is convertible and the field is left off. |
| 91 | + const fn = coercer( |
| 92 | + { type: "object", properties: { a: num } }, |
| 93 | + { type: "object", properties: { a: num, b: num } }, |
| 94 | + ); |
| 95 | + expect(fn({ a: 1 })).toEqual({ a: 1 }); |
| 96 | + }); |
| 97 | + |
| 98 | + it("still requires a default for a new REQUIRED field on a plain object", () => { |
| 99 | + expect(createCoerceFunction( |
| 100 | + { type: "object", properties: { a: num } }, |
| 101 | + { type: "object", properties: { a: num, b: num }, required: ["b"] }, |
| 102 | + )).toBeNull(); |
| 103 | + }); |
| 104 | + |
| 105 | + it("drops fields the output does not declare", () => { |
| 106 | + const fn = coercer( |
| 107 | + { type: "object", properties: { x: f32, y: f32, z: f32 } }, |
| 108 | + { type: "object", properties: { x: f32, y: f32 } }, |
| 109 | + ); |
| 110 | + expect(fn({ x: 1, y: 2, z: 3 })).toEqual({ x: 1, y: 2 }); |
| 111 | + }); |
| 112 | + |
| 113 | + it("coerces (and clamps) a retyped field", () => { |
| 114 | + const fn = coercer( |
| 115 | + { type: "object", properties: { x: num } }, |
| 116 | + { type: "object", properties: { x: { type: "integer", minimum: 0, maximum: 10 } } }, |
| 117 | + ); |
| 118 | + expect(fn({ x: 50 })).toEqual({ x: 10 }); |
| 119 | + }); |
| 120 | + |
| 121 | + it("gives each element its own copy of an object default (no aliasing)", () => { |
| 122 | + const fn = coercer( |
| 123 | + { type: "object", properties: { x: f32 } }, |
| 124 | + { type: "object", properties: { x: f32, meta: { type: "object", properties: { n: { type: "number", default: 0 } } } } }, |
| 125 | + ); |
| 126 | + const a = fn({ x: 1 }) as { meta: object }; |
| 127 | + const b = fn({ x: 2 }) as { meta: object }; |
| 128 | + expect(a.meta).toEqual({ n: 0 }); |
| 129 | + expect(a.meta).not.toBe(b.meta); // distinct instances |
| 130 | + }); |
| 131 | + |
| 132 | + it("is not convertible when an existing sub-field cannot be converted", () => { |
| 133 | + // Nested numeric struct gains a defaultless field ⇒ the sub-conversion, |
| 134 | + // and therefore the whole conversion, is impossible. |
| 135 | + expect(createCoerceFunction( |
| 136 | + { type: "object", properties: { p: { type: "object", properties: { x: f32 } } } }, |
| 137 | + { type: "object", properties: { p: { type: "object", properties: { x: f32, y: f32 } } } }, |
| 138 | + )).toBeNull(); |
| 139 | + }); |
| 140 | +}); |
| 141 | + |
| 142 | +describe("createCoerceFunction — arrays", () => { |
| 143 | + it("extends a fixed vector, filling from the item default (vec3 → vec4)", () => { |
| 144 | + const fn = coercer( |
| 145 | + { type: "array", items: f32, minItems: 3, maxItems: 3 }, |
| 146 | + { type: "array", items: { type: "number", default: 0 }, minItems: 4, maxItems: 4 }, |
| 147 | + ); |
| 148 | + expect(fn([1, 2, 3])).toEqual([1, 2, 3, 0]); |
| 149 | + }); |
| 150 | + |
| 151 | + it("is not convertible when a longer fixed output has no item default", () => { |
| 152 | + expect(createCoerceFunction( |
| 153 | + { type: "array", items: f32, minItems: 3, maxItems: 3 }, |
| 154 | + { type: "array", items: f32, minItems: 4, maxItems: 4 }, |
| 155 | + )).toBeNull(); |
| 156 | + }); |
| 157 | + |
| 158 | + it("truncates a fixed vector (vec3 → vec2)", () => { |
| 159 | + const fn = coercer( |
| 160 | + { type: "array", items: f32, minItems: 3, maxItems: 3 }, |
| 161 | + { type: "array", items: f32, minItems: 2, maxItems: 2 }, |
| 162 | + ); |
| 163 | + expect(fn([1, 2, 3])).toEqual([1, 2]); |
| 164 | + }); |
| 165 | + |
| 166 | + it("maps every element of a variable-length array (with clamp)", () => { |
| 167 | + const fn = coercer( |
| 168 | + { type: "array", items: num }, |
| 169 | + { type: "array", items: { type: "integer", minimum: 0, maximum: 5 } }, |
| 170 | + ); |
| 171 | + expect(fn([1, 10, 3])).toEqual([1, 5, 3]); |
| 172 | + }); |
| 173 | +}); |
| 174 | + |
| 175 | +describe("createCoerceFunction — incompatible kinds return null", () => { |
| 176 | + it.each([ |
| 177 | + ["number → object", num, { type: "object", properties: { x: f32 } } as SchemaType], |
| 178 | + ["object → number", { type: "object", properties: { x: f32 } } as SchemaType, num], |
| 179 | + ["number → boolean", num, bool], |
| 180 | + ["number → enum", num, { enum: [1, 2, 3] } as SchemaType], |
| 181 | + ["array → object", { type: "array", items: num } as SchemaType, { type: "object", properties: { x: f32 } } as SchemaType], |
| 182 | + ])("%s", (_label, input, output) => { |
| 183 | + expect(createCoerceFunction(input, output)).toBeNull(); |
| 184 | + }); |
| 185 | +}); |
| 186 | + |
| 187 | +describe("createCoerceFunction — namespace exposure", () => { |
| 188 | + it("is reachable as Schema.createCoerceFunction", () => { |
| 189 | + expect(typeof Schema.createCoerceFunction).toBe("function"); |
| 190 | + expect(Schema.createCoerceFunction(num, f32)!(1.5)).toBe(1.5); |
| 191 | + }); |
| 192 | +}); |
0 commit comments