Skip to content

Commit

Permalink
test: add image util functions test
Browse files Browse the repository at this point in the history
  • Loading branch information
agatha197 committed Nov 6, 2024
1 parent 6445e5b commit cb457a2
Showing 1 changed file with 138 additions and 0 deletions.
138 changes: 138 additions & 0 deletions react/src/hooks/index.test.tsx
Original file line number Diff line number Diff line change
@@ -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' },
]);
});
});
});
});

0 comments on commit cb457a2

Please sign in to comment.