From 773baea857a4caaa1676ce7e51cacd137a2d951a Mon Sep 17 00:00:00 2001 From: RJ <27843274+rjchow@users.noreply.github.com> Date: Wed, 11 Oct 2023 13:41:34 +0800 Subject: [PATCH] fix: cys design with ai duplicate color validation (#40670) --- .../design-with-ai/prompts/colorChoices.ts | 17 +++++-- .../prompts/test/colorChoices.test.ts | 47 ++++++++++++++----- ...with-ai-duplicate-color-palette-validation | 4 ++ 3 files changed, 53 insertions(+), 15 deletions(-) create mode 100644 plugins/woocommerce/changelog/fix-cys-design-with-ai-duplicate-color-palette-validation diff --git a/plugins/woocommerce-admin/client/customize-store/design-with-ai/prompts/colorChoices.ts b/plugins/woocommerce-admin/client/customize-store/design-with-ai/prompts/colorChoices.ts index efae680778a..2e7d4f4a692 100644 --- a/plugins/woocommerce-admin/client/customize-store/design-with-ai/prompts/colorChoices.ts +++ b/plugins/woocommerce-admin/client/customize-store/design-with-ai/prompts/colorChoices.ts @@ -222,10 +222,19 @@ export const colorPaletteValidator = z.object( { .regex( hexColorRegex, { message: 'Invalid background color' } ), } ); -export const colorPaletteResponseValidator = z.object( { - default: colorPaletteNameValidator, - bestColors: z.array( colorPaletteNameValidator ).length( 8 ), -} ); +export const colorPaletteResponseValidator = z + .object( { + default: colorPaletteNameValidator, + bestColors: z.array( colorPaletteNameValidator ).length( 8 ), + } ) + .refine( + ( response ) => { + const allColors = [ response.default, ...response.bestColors ]; + const uniqueColors = new Set( allColors ); + return uniqueColors.size === allColors.length; + }, + { message: 'Color palette names must be unique' } + ); export const defaultColorPalette = { queryId: 'default_color_palette', diff --git a/plugins/woocommerce-admin/client/customize-store/design-with-ai/prompts/test/colorChoices.test.ts b/plugins/woocommerce-admin/client/customize-store/design-with-ai/prompts/test/colorChoices.test.ts index d1aae496b45..28cbbe646d7 100644 --- a/plugins/woocommerce-admin/client/customize-store/design-with-ai/prompts/test/colorChoices.test.ts +++ b/plugins/woocommerce-admin/client/customize-store/design-with-ai/prompts/test/colorChoices.test.ts @@ -141,12 +141,20 @@ describe( 'colorPaletteValidator', () => { } ); describe( 'colorPaletteResponseValidator', () => { + const validPalette = { + default: 'Ancient Bronze', + bestColors: [ + 'Canary', + 'Cinder', + 'Rustic Rosewood', + 'Lightning', + 'Midnight Citrus', + 'Purple Twilight', + 'Crimson Tide', + 'Ice', + ], + }; it( 'should validate a correct color palette response', () => { - const validPalette = { - default: 'Ancient Bronze', - bestColors: Array( 8 ).fill( 'Ancient Bronze' ), - }; - const parsedResult = defaultColorPalette.responseValidation( validPalette ); expect( parsedResult ).toEqual( validPalette ); @@ -154,10 +162,10 @@ describe( 'colorPaletteResponseValidator', () => { it( 'should fail if array contains invalid color', () => { const invalidPalette = { - default: 'Ancient Bronze', - bestColors: Array( 7 ) - .fill( 'Ancient Bronze' ) - .concat( [ 'Invalid Color' ] ), + default: validPalette.default, + bestColors: validPalette.bestColors + .slice( 0, 7 ) + .concat( 'Invalid Color' ), }; expect( () => defaultColorPalette.responseValidation( invalidPalette ) ) .toThrowErrorMatchingInlineSnapshot( ` @@ -195,7 +203,7 @@ describe( 'colorPaletteResponseValidator', () => { } ); it( 'should fail if default property is missing', () => { const invalidPalette = { - bestColors: Array( 8 ).fill( 'Ancient Bronze' ), + bestColors: validPalette.bestColors, }; expect( () => defaultColorPalette.responseValidation( invalidPalette ) ) .toThrowErrorMatchingInlineSnapshot( ` @@ -216,7 +224,7 @@ describe( 'colorPaletteResponseValidator', () => { it( 'should fail if bestColors array is not of length 8', () => { const invalidPalette = { default: 'Ancient Bronze', - bestColors: Array( 7 ).fill( 'Ancient Bronze' ), + bestColors: validPalette.bestColors.slice( 0, 7 ), }; expect( () => defaultColorPalette.responseValidation( invalidPalette ) ) .toThrowErrorMatchingInlineSnapshot( ` @@ -235,4 +243,21 @@ describe( 'colorPaletteResponseValidator', () => { ]" ` ); } ); + + it( 'should fail if there are duplicate colors', () => { + const invalidPalette = { + default: 'Ancient Bronze', + bestColors: Array( 8 ).fill( 'Ancient Bronze' ), + }; + expect( () => defaultColorPalette.responseValidation( invalidPalette ) ) + .toThrowErrorMatchingInlineSnapshot( ` + "[ + { + \\"code\\": \\"custom\\", + \\"message\\": \\"Color palette names must be unique\\", + \\"path\\": [] + } + ]" + ` ); + } ); } ); diff --git a/plugins/woocommerce/changelog/fix-cys-design-with-ai-duplicate-color-palette-validation b/plugins/woocommerce/changelog/fix-cys-design-with-ai-duplicate-color-palette-validation new file mode 100644 index 00000000000..7ab3a9302dd --- /dev/null +++ b/plugins/woocommerce/changelog/fix-cys-design-with-ai-duplicate-color-palette-validation @@ -0,0 +1,4 @@ +Significance: patch +Type: fix + +CYS: Fix the bug where sometimes switching from user defined color palettes to a pre-defined color palette won't set some colors.