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-07-28 21:32:47 +08:00
it ( 'converts select fields with enums 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 ]
expect ( selectSchema . type ). toBe ( 'string' )
expect ( selectSchema . enum ). toEqual ([ 'opt1' , 'opt2' ])
expect ( selectSchema . description ). toBe (
2026-07-28 21:44:56 +08:00
"The field 'Species' represents Species of animal.\nSelect one option and return the option ID as the value.\n\nAvailable options:\n- Option 1 => opt1\n- Option 2 => opt2" ,
2026-07-28 21:32:47 +08:00
)
2026-08-13 20:06:09 +08:00
// Only the declared option IDs are valid (enforced by the typebox validator)
expect ( Value . Check ( schema , { f1 : 'opt1' })). toBe ( true )
expect ( Value . Check ( schema , { f1 : null })). toBe ( true )
expect ( Value . Check ( schema , { f1 : 'not-an-option' })). toBe ( false )
2026-07-28 21:32:47 +08:00
})
it ( 'converts selectMulti fields with array enums and option descriptions' , () => {
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 . items ! . enum ). toEqual ([ 'opt1' , 'opt2' ])
expect ( selectMultiSchema . description ). toBe (
2026-07-28 21:44:56 +08:00
"The field 'Tags' represents Tags.\nSelect applicable options and return the option IDs as the value.\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 )
expect ( Value . Check ( schema , { f1 : null })). toBe ( true )
expect ( Value . Check ( schema , { f1 : [ 'opt1' , 'bad' ] })). 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
})
})