immich/server/src/utils/misc.spec.ts

70 lines
2.1 KiB
TypeScript
Raw Normal View History

import { getKeysDeep, globToSqlPattern, unsetDeep } from 'src/utils/misc';
import { describe, expect, it } from 'vitest';
describe('getKeysDeep', () => {
it('should handle an empty object', () => {
expect(getKeysDeep({})).toEqual([]);
});
it('should list properties', () => {
expect(
getKeysDeep({
foo: 'bar',
flag: true,
count: 42,
date: new Date(),
}),
).toEqual(['foo', 'flag', 'count', 'date']);
});
it('should skip undefined properties', () => {
expect(getKeysDeep({ foo: 'bar', hello: undefined })).toEqual(['foo']);
});
it('should skip array indices', () => {
expect(getKeysDeep({ foo: 'bar', hello: ['foo', 'bar'] })).toEqual(['foo', 'hello']);
expect(getKeysDeep({ foo: 'bar', nested: { hello: ['foo', 'bar'] } })).toEqual(['foo', 'nested.hello']);
});
it('should list nested properties', () => {
expect(getKeysDeep({ foo: 'bar', hello: { world: true } })).toEqual(['foo', 'hello.world']);
});
});
describe('unsetDeep', () => {
it('should remove a property', () => {
expect(unsetDeep({ hello: 'world', foo: 'bar' }, 'foo')).toEqual({ hello: 'world' });
});
it('should remove the last property', () => {
expect(unsetDeep({ foo: 'bar' }, 'foo')).toBeUndefined();
});
it('should remove a nested property', () => {
expect(unsetDeep({ foo: 'bar', nested: { enabled: true, count: 42 } }, 'nested.enabled')).toEqual({
foo: 'bar',
nested: { count: 42 },
});
});
it('should clean up an empty property', () => {
expect(unsetDeep({ foo: 'bar', nested: { enabled: true } }, 'nested.enabled')).toEqual({ foo: 'bar' });
});
});
describe('globToSqlPattern', () => {
const testCases = [
['**/Raw/**', '%/Raw/%'],
['**/abc/*.tif', '%/abc/%.tif'],
['**/*.tif', '%/%.tif'],
['**/*.jp?', '%/%.jp_'],
['**/@eaDir/**', '%/@eaDir/%'],
['**/._*', `%/._%`],
['/absolute/path/**', `/absolute/path/%`],
];
it.each(testCases)('should convert %s to %s', (input, expected) => {
expect(globToSqlPattern(input)).toEqual(expected);
});
});