2026-05-19 16:05:09 +08:00
import { describe , it , expect } from 'vitest'
2026-08-13 20:06:09 +08:00
import { type TSchema } from 'typebox'
import { Value } from 'typebox/value'
2026-05-20 22:43:31 +08:00
import { fieldsToTypeBoxSchema , type AutofillField } from './index'
2026-05-19 16:05:09 +08:00
2026-08-13 20:06:09 +08:00
interface AnyOfFieldSchema {
type ?: string
enum ?: string []
const ?: string
description? : string
items ?: { enum ?: string []; type ?: string }
anyOf? : AnyOfFieldSchema []
}
function propsOf ( schema : TSchema ) : Record < string , { anyOf ?: AnyOfFieldSchema [] }> {
return ( schema as unknown as { properties : Record < string , { anyOf ?: AnyOfFieldSchema [] }> })
. properties
}
2026-05-19 16:05:09 +08:00
describe ( 'fieldsToTypeBoxSchema' , () => {
it ( 'converts text fields' , () => {
const fields : AutofillField [] = [
{ id : 'f1' , config : { name : 'Title' , type : 'text' }, description : 'The title' },
{ id : 'f2' , config : { name : 'Desc' , type : 'longText' } },
]
const schema = fieldsToTypeBoxSchema ( fields )
2026-08-13 20:06:09 +08:00
const props = propsOf ( schema )
expect ( props . f1 . anyOf ! [ 0 ]. type ). toBe ( 'string' )
expect ( props . f1 . anyOf ! [ 1 ]. type ). toBe ( 'null' )
expect ( props . f1 . anyOf ! [ 0 ]. description ). toBe ( "The field 'Title' represents The title." )
expect ( props . f2 . anyOf ! [ 0 ]. type ). toBe ( 'string' )
expect ( props . f2 . anyOf ! [ 0 ]. description ). toBe ( "The field 'Desc' represents Desc." )
2026-05-19 16:05:09 +08:00
})
it ( 'converts numeric fields' , () => {
const fields : AutofillField [] = [
{ id : 'f1' , config : { name : 'Price' , type : 'number' } },
{ id : 'f2' , config : { name : 'Rating' , type : 'rating' } },
]
const schema = fieldsToTypeBoxSchema ( fields )
2026-08-13 20:06:09 +08:00
const props = propsOf ( schema )
expect ( props . f1 . anyOf ! [ 0 ]. type ). toBe ( 'number' )
expect ( props . f2 . anyOf ! [ 0 ]. type ). toBe ( 'number' )
2026-05-19 16:05:09 +08:00
})
it ( 'converts toggle fields' , () => {
const fields : AutofillField [] = [{ id : 'f1' , config : { name : 'Active' , type : 'toggle' } }]
const schema = fieldsToTypeBoxSchema ( fields )
2026-08-13 20:06:09 +08:00
expect ( propsOf ( schema ). f1 . anyOf ! [ 0 ]. type ). toBe ( 'boolean' )
2026-05-19 16:05:09 +08:00
})
2026-08-14 10:56:18 +08:00
it ( 'converts select fields with enums, newOption support and option descriptions' , () => {
2026-05-19 16:05:09 +08:00
const fields : AutofillField [] = [
{
id : 'f1' ,
2026-07-28 21:32:47 +08:00
description : 'Species of animal' ,
2026-05-19 16:05:09 +08:00
config : {
2026-07-28 21:32:47 +08:00
name : 'Species' ,
2026-05-19 16:05:09 +08:00
type : 'select' ,
select : {
options : [
{ id : 'opt1' , displayName : 'Option 1' , color : 'red' },
{ id : 'opt2' , displayName : 'Option 2' , color : 'blue' },
],
},
},
},
]
const schema = fieldsToTypeBoxSchema ( fields )
2026-08-13 20:06:09 +08:00
const selectSchema = propsOf ( schema ). f1 . anyOf ! [ 0 ]
2026-08-14 10:56:18 +08:00
expect ( selectSchema . anyOf ! [ 0 ]. type ). toBe ( 'string' )
expect ( selectSchema . anyOf ! [ 0 ]. enum ). toEqual ([ 'opt1' , 'opt2' ])
expect ( selectSchema . anyOf ! [ 1 ]. type ). toBe ( 'object' )
2026-08-13 20:06:09 +08:00
expect ( selectSchema . description ). toBe (
2026-08-14 10:56:18 +08:00
'The field \'Species\' represents Species of animal.\nSelect one existing option ID or provide {"newOption": {"value": "..."}} to create a new option.\n\nAvailable options:\n- Option 1 => opt1\n- Option 2 => opt2' ,
2026-07-28 21:32:47 +08:00
)
2026-08-14 10:56:18 +08:00
// Declared option IDs, newOption objects, and null are valid
2026-08-13 20:06:09 +08:00
expect ( Value . Check ( schema , { f1 : 'opt1' })). toBe ( true )
2026-08-14 10:56:18 +08:00
expect ( Value . Check ( schema , { f1 : 'opt2' })). toBe ( true )
expect ( Value . Check ( schema , { f1 : { newOption : { value : 'kling' } } })). toBe ( true )
2026-08-13 20:06:09 +08:00
expect ( Value . Check ( schema , { f1 : null })). toBe ( true )
2026-08-14 10:56:18 +08:00
// Arbitrary strings or invalid objects are rejected
2026-08-13 20:06:09 +08:00
expect ( Value . Check ( schema , { f1 : 'not-an-option' })). toBe ( false )
2026-08-14 10:56:18 +08:00
expect ( Value . Check ( schema , { f1 : { newOption : { foo : 'bar' } } })). toBe ( false )
2026-07-28 21:32:47 +08:00
})
2026-08-14 10:56:18 +08:00
it ( 'converts select fields with empty options to newOption schema only' , () => {
const fields : AutofillField [] = [
{
id : 'f1' ,
config : {
name : 'Provider' ,
type : 'select' ,
select : { options : [] },
},
},
]
const schema = fieldsToTypeBoxSchema ( fields )
expect ( Value . Check ( schema , { f1 : { newOption : { value : 'kling' } } })). toBe ( true )
expect ( Value . Check ( schema , { f1 : null })). toBe ( true )
expect ( Value . Check ( schema , { f1 : 'any-string' })). toBe ( false )
})
it ( 'converts selectMulti fields with array enums, newOption support and option descriptions' , () => {
2026-07-28 21:32:47 +08:00
const fields : AutofillField [] = [
{
id : 'f1' ,
config : {
name : 'Tags' ,
type : 'selectMulti' ,
selectMulti : {
options : [
{ id : 'opt1' , displayName : 'Tag 1' , color : 'red' },
{ id : 'opt2' , displayName : 'Tag 2' , color : 'blue' },
],
},
},
},
]
const schema = fieldsToTypeBoxSchema ( fields )
2026-08-13 20:06:09 +08:00
const selectMultiSchema = propsOf ( schema ). f1 . anyOf ! [ 0 ]
expect ( selectMultiSchema . type ). toBe ( 'array' )
expect ( selectMultiSchema . description ). toBe (
2026-08-14 10:56:18 +08:00
'The field \'Tags\' represents Tags.\nSelect applicable option IDs or provide {"newOption": {"value": "..."}} objects for new options (e.g. ["opt1", {"newOption": {"value": "new_name"}}]).\n\nAvailable options:\n- Tag 1 => opt1\n- Tag 2 => opt2' ,
2026-07-28 21:32:47 +08:00
)
2026-08-13 20:06:09 +08:00
expect ( Value . Check ( schema , { f1 : [ 'opt1' , 'opt2' ] })). toBe ( true )
2026-08-14 10:56:18 +08:00
expect ( Value . Check ( schema , { f1 : [{ newOption : { value : 'Tag 3' } }] })). toBe ( true )
expect ( Value . Check ( schema , { f1 : [ 'opt1' , { newOption : { value : 'Tag 3' } }] })). toBe ( true )
2026-08-13 20:06:09 +08:00
expect ( Value . Check ( schema , { f1 : null })). toBe ( true )
expect ( Value . Check ( schema , { f1 : [ 'opt1' , 'bad' ] })). toBe ( false )
2026-08-14 10:56:18 +08:00
expect ( Value . Check ( schema , { f1 : [ 'opt1' , { newOption : { bad : 'value' } }] })). toBe ( false )
})
it ( 'converts selectMulti fields with empty options to array of newOption schema' , () => {
const fields : AutofillField [] = [
{
id : 'f1' ,
config : {
name : 'Tags' ,
type : 'selectMulti' ,
selectMulti : { options : [] },
},
},
]
const schema = fieldsToTypeBoxSchema ( fields )
expect ( Value . Check ( schema , { f1 : [{ newOption : { value : 'Tag 1' } }] })). toBe ( true )
expect ( Value . Check ( schema , { f1 : [] })). toBe ( true )
expect ( Value . Check ( schema , { f1 : null })). toBe ( true )
expect ( Value . Check ( schema , { f1 : [ 'tag1' ] })). toBe ( false )
2026-05-19 16:05:09 +08:00
})
it ( 'defaults to string for unknown types' , () => {
const fields : AutofillField [] = [
// @ts-ignore
{ id : 'f1' , config : { name : 'Unknown' , type : 'invalid' } },
]
const schema = fieldsToTypeBoxSchema ( fields )
2026-08-13 20:06:09 +08:00
expect ( propsOf ( schema ). f1 . anyOf ! [ 0 ]. type ). toBe ( 'string' )
})
it ( 'marks every property required (nullable) and rejects unknown keys' , () => {
const fields : AutofillField [] = [
{ id : 'f1' , config : { name : 'Title' , type : 'text' } },
{ id : 'f2' , config : { name : 'Rating' , type : 'rating' } },
]
const schema = fieldsToTypeBoxSchema ( fields )
const props = propsOf ( schema )
// typebox v1 objects are strict by default: unknown keys are rejected
expect ( props . f1 . anyOf ! [ 0 ]. type ). toBe ( 'string' )
expect ( props . f1 . anyOf ! [ 1 ]. type ). toBe ( 'null' )
expect ( Value . Check ( schema , { f1 : 'x' , f2 : null })). toBe ( true )
expect ( Value . Check ( schema , { f1 : null , f2 : 3 })). toBe ( true )
expect ( Value . Check ( schema , { f1 : 'x' })). toBe ( false )
expect ( Value . Check ( schema , { f1 : 'x' , f2 : null , extra : 1 })). toBe ( false )
2026-05-19 16:05:09 +08:00
})
})