1+ import { BadRequestException } from '@nestjs/common'
2+ import { plainToInstance } from 'class-transformer'
3+ import { booruQueryValuesPostsDTO } from './booru-queries.dto'
4+
5+ describe ( 'booruQueryValuesPostsDTO' , ( ) => {
6+ describe ( 'tags transform' , ( ) => {
7+ it ( 'should decode URL-encoded ampersands in tags' , ( ) => {
8+ const dto = plainToInstance ( booruQueryValuesPostsDTO , {
9+ tags : 'panty_%26_stocking_with_garterbelt'
10+ } )
11+
12+ expect ( dto . tags ) . toEqual ( [ 'panty_&_stocking_with_garterbelt' ] )
13+ } )
14+
15+ it ( 'should split pipe-separated tags and decode each one' , ( ) => {
16+ const dto = plainToInstance ( booruQueryValuesPostsDTO , {
17+ tags : 'panty_%26_stocking_with_garterbelt|rating%3Asafe'
18+ } )
19+
20+ expect ( dto . tags ) . toEqual ( [ 'panty_&_stocking_with_garterbelt' , 'rating:safe' ] )
21+ } )
22+
23+ it ( 'should normalize array tag inputs and keep tag array shape' , ( ) => {
24+ const dto = plainToInstance ( booruQueryValuesPostsDTO , {
25+ tags : [ 'panty_%26_stocking_with_garterbelt|rating%3Asafe' , 'score%3A%3E100' ]
26+ } )
27+
28+ expect ( dto . tags ) . toEqual ( [
29+ 'panty_&_stocking_with_garterbelt' ,
30+ 'rating:safe' ,
31+ 'score:>100'
32+ ] )
33+ } )
34+
35+ it ( 'should normalize non-string tag input without throwing' , ( ) => {
36+ const dto = plainToInstance ( booruQueryValuesPostsDTO , {
37+ tags : 123
38+ } )
39+
40+ expect ( dto . tags ) . toEqual ( [ '123' ] )
41+ } )
42+
43+ it ( 'should keep non-encoded percent tags unchanged' , ( ) => {
44+ const dto = plainToInstance ( booruQueryValuesPostsDTO , {
45+ tags : '100%_real'
46+ } )
47+
48+ expect ( dto . tags ) . toEqual ( [ '100%_real' ] )
49+ } )
50+
51+ it ( 'should throw BadRequestException when encoded tag decoding fails' , ( ) => {
52+ expect ( ( ) =>
53+ plainToInstance ( booruQueryValuesPostsDTO , {
54+ tags : 'bad%25%'
55+ } )
56+ ) . toThrow ( BadRequestException )
57+
58+ expect ( ( ) =>
59+ plainToInstance ( booruQueryValuesPostsDTO , {
60+ tags : 'bad%25%'
61+ } )
62+ ) . toThrow ( 'Invalid tag encoding' )
63+ } )
64+
65+ it ( 'should return undefined when tags is undefined' , ( ) => {
66+ const dto = plainToInstance ( booruQueryValuesPostsDTO , { } )
67+
68+ expect ( dto . tags ) . toBeUndefined ( )
69+ } )
70+
71+ it ( 'should return null when tags is null' , ( ) => {
72+ const dto = plainToInstance ( booruQueryValuesPostsDTO , {
73+ tags : null
74+ } )
75+
76+ expect ( dto . tags ) . toBeNull ( )
77+ } )
78+ } )
79+ } )
0 commit comments