From c95f3bb4e9325e60a4c3c52a1d61ddd1619a0500 Mon Sep 17 00:00:00 2001 From: agatha197 Date: Wed, 6 Nov 2024 11:48:43 +0900 Subject: [PATCH] test: add image util functions test --- react/src/hooks/index.test.tsx | 138 +++++++++++++++++++++++++++++++++ 1 file changed, 138 insertions(+) create mode 100644 react/src/hooks/index.test.tsx diff --git a/react/src/hooks/index.test.tsx b/react/src/hooks/index.test.tsx new file mode 100644 index 0000000000..5feb46d0be --- /dev/null +++ b/react/src/hooks/index.test.tsx @@ -0,0 +1,138 @@ +import _ from 'lodash'; + +describe('Image util functions tests', () => { + // TODO: Use test hooks to test the functions + const getBaseVersion = (IMAGE_NAME: string) => { + return ( + _.first(_.split(_.last(_.split(IMAGE_NAME, ':')), /[^a-zA-Z\d.]+/)) || '' + ); + }; + + const getBaseImage = (IMAGE_NAME: string) => { + const splitByColon = _.split(IMAGE_NAME, ':'); + const beforeLastColon = _.join(_.initial(splitByColon), ':'); + const lastItemAfterSplitBySlash = _.last(_.split(beforeLastColon, '/')); + return lastItemAfterSplitBySlash || ''; + }; + + const getTags = ( + tag: string, + labels: Array<{ key: string; value: string }>, + ) => { + // Remove the 'customized_' prefix and its following string from the tag + const cleanedTag = _.replace(tag, /customized_[a-zA-Z\d.]+/, ''); + // Split the remaining tag into segments based on alphanumeric and '.' characters, ignoring the first segment + const tags = _.tail(_.split(cleanedTag, /[^a-zA-Z\d.]+/)); + const result: Array<{ key: string; value: string }> = []; + + // Process not 'customized_' tags + _.forEach(tags, (currentTag) => { + // Separate the alphabetic prefix from the numeric and '.' suffix for each tag + const match = /^([a-zA-Z]+)(.*)$/.exec(currentTag); + if (match) { + const [, key, value] = match; + // Ensure the value is an empty string if it's undefined + result.push({ key, value: value || '' }); + } + }); + + // Handle the 'customized_' tag separately by finding the custom image name in labels + const customizedNameLabel = _.get( + _.find(labels, { key: 'ai.backend.customized-image.name' }), + 'value', + '', + ); + // If a custom image name exists, add it to the result with the key 'Customized' + if (customizedNameLabel) { + result.push({ key: 'Customized', value: customizedNameLabel }); + } + + // Remove duplicates and entries with an empty 'key' + return _.uniqWith( + _.filter(result, ({ key }) => !_.isEmpty(key)), + _.isEqual, + ); + }; + + describe('Test with underbar image tag', () => { + const IMAGE_NAME = 'abc-def.ghi.systems/llm/jkl/mno_pqr:0.0.0_stu'; + describe('getBaseVersion', () => { + it('should correctly parse the base version from an image name', () => { + const baseVersion = getBaseVersion(IMAGE_NAME); + expect(baseVersion).toBe('0.0.0'); + }); + }); + + describe('getBaseImage', () => { + it('should correctly parse the base image from an image name', () => { + const baseImage = getBaseImage(IMAGE_NAME); + expect(baseImage).toBe('mno_pqr'); + }); + }); + + describe('getTags', () => { + it('should correctly parse and process tags from a given tag string', () => { + const tag = '0.0.0_stu'; + const labels = [{ key: 'abc', value: 'def' }]; + const tags = getTags(tag, labels); + expect(tags).toEqual([{ key: 'stu', value: '' }]); + }); + }); + }); + describe('Test with dash image tag', () => { + const IMAGE_NAME = 'abc-def.ghi.systems/llm/jkl/mno_pqr:0.0.0-stu'; + describe('getBaseVersion', () => { + it('should correctly parse the base version from an image name', () => { + const baseVersion = getBaseVersion(IMAGE_NAME); + expect(baseVersion).toBe('0.0.0'); + }); + }); + + describe('getBaseImage', () => { + it('should correctly parse the base image from an image name', () => { + const baseImage = getBaseImage(IMAGE_NAME); + expect(baseImage).toBe('mno_pqr'); + }); + }); + + describe('getTags', () => { + it('should correctly parse and process tags from a given tag string', () => { + const tag = '0.0.0_stu'; + const labels = [{ key: 'abc', value: 'def' }]; + const tags = getTags(tag, labels); + expect(tags).toEqual([{ key: 'stu', value: '' }]); + }); + }); + }); + describe('Test with customized image tag', () => { + const IMAGE_NAME = + 'abc-def.ghi.systems/llm/jkl/mno_pqr:0.0.0-stu_customized_asdflkjnweri'; + describe('getBaseVersion', () => { + it('should correctly parse the base version from an image name', () => { + const baseVersion = getBaseVersion(IMAGE_NAME); + expect(baseVersion).toBe('0.0.0'); + }); + }); + + describe('getBaseImage', () => { + it('should correctly parse the base image from an image name', () => { + const baseImage = getBaseImage(IMAGE_NAME); + expect(baseImage).toBe('mno_pqr'); + }); + }); + + describe('getTags', () => { + it('should handle customized_ tags correctly', () => { + const tag = '0.0.0-stu_customized_asdflkjnweri'; + const labels = [ + { key: 'ai.backend.customized-image.name', value: 'CustomImage' }, + ]; + const tags = getTags(tag, labels); + expect(tags).toEqual([ + { key: 'stu', value: '' }, + { key: 'Customized', value: 'CustomImage' }, + ]); + }); + }); + }); +});