import { ResourceId, ResourceIdOf, ResourceTypeId, isResourceId, isResourceIdOf, parseResourceId, parseResourceIdOf, } from '@cloudpss/resource-types'; const xxType = ResourceTypeId('xx'); const yyType = ResourceTypeId('yy'); it('ResourceTypeId', () => { expect(xxType).toEqual('xx'); expect(yyType).toEqual('yy'); expect(ResourceTypeId('x'.repeat(240))).toEqual('x'.repeat(240)); expect(ResourceTypeId('x-')).toEqual('x-'); expect(ResourceTypeId('x_')).toEqual('x_'); expect(ResourceTypeId('x-123yz')).toEqual('x-123yz'); expect(ResourceTypeId('-x-123yz')).toEqual('-x-123yz'); expect(ResourceTypeId('_x-123yz')).toEqual('_x-123yz'); for (const char of '!@()[]{}~-._') { expect(ResourceTypeId(char)).toEqual(char); } expect(() => ResourceTypeId(' xx')).toThrow(`Invalid type " xx", should not contains spaces or special characters`); for (const char of '\0\u0001\u001F\f\n\r\t\v\u0020\u00A0\u1680\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u2028\u2029\u202F\u205F\u3000\uFEFF \\/`^$%&*;:\'"|+?,<=>\u007F') { expect(() => ResourceTypeId(char)).toThrow( `Invalid type ${JSON.stringify(char)}, should not contains spaces or special characters`, ); } expect(() => ResourceTypeId('x x')).toThrow(`Invalid type "x x", should not contains spaces or special characters`); expect(() => ResourceTypeId('x\nx')).toThrow( String.raw`Invalid type "x\nx", should not contains spaces or special characters`, ); expect(() => ResourceTypeId('x/x')).toThrow(`Invalid type "x/x", should not contains spaces or special characters`); expect(() => ResourceTypeId('')).toThrow('Invalid type "", should not be shorter than 1 characters'); expect(() => ResourceTypeId('x'.repeat(241))).toThrow( 'Invalid type "xxxxxxxxxxxxxxxxxxx..., should not be longer than 240 characters', ); // @ts-expect-error test invalid type expect(() => ResourceTypeId({})).toThrow('Invalid type [object Object], should be string'); // @ts-expect-error test invalid type expect(() => ResourceTypeId(null)).toThrow('Invalid type null, should be string'); // @ts-expect-error test invalid type expect(() => ResourceTypeId(undefined)).toThrow('Invalid type undefined, should be string'); // @ts-expect-error test invalid type expect(() => ResourceTypeId(true)).toThrow('Invalid type [object Boolean], should be string'); // @ts-expect-error test invalid type expect(() => ResourceTypeId(1)).toThrow('Invalid type [object Number], should be string'); }); it('ResourceId', () => { expect(ResourceId(xxType, 'o', 'k')).toEqual('xx/o/k'); expect(ResourceId(yyType, 'o', 'k')).toEqual('yy/o/k'); expect(ResourceId('xx/o/k')).toEqual('xx/o/k'); expect(ResourceId('_xx_/__o__/__k__')).toEqual('_xx_/__o__/__k__'); expect( ResourceId({ type: xxType, owner: 'o', key: 'k', }), ).toEqual('xx/o/k'); expect(() => ResourceId(xxType, ' xx', '')).toThrow( `Invalid owner " xx", should not contains spaces or special characters`, ); expect(() => ResourceId(xxType, 'y', 'x x')).toThrow( `Invalid key "x x", should not contains spaces or special characters`, ); expect(() => ResourceId(xxType, 'x', 'x\nx')).toThrow( String.raw`Invalid key "x\nx", should not contains spaces or special characters`, ); // @ts-expect-error test invalid type expect(() => ResourceId('x/x', '', '')).toThrow( `Invalid type "x/x", should not contains spaces or special characters`, ); expect(() => ResourceId('')).toThrow('"" is not an valid resource id'); expect(() => ResourceId({ type: xxType, owner: 'o', key: 'x'.repeat(241), }), ).toThrow('Invalid key "xxxxxxxxxxxxxxxxxxx..., should not be longer than 240 characters'); // @ts-expect-error test invalid type expect(() => ResourceId({})).toThrow('[object Object] is not an valid resource id'); expect(() => ResourceId(null)).toThrow('Required an non-null value'); expect(() => ResourceId(undefined)).toThrow('Required an non-null value'); // @ts-expect-error test invalid type expect(() => ResourceId(true)).toThrow('[object Boolean] is not an valid resource id'); // @ts-expect-error test invalid type expect(() => ResourceId(1)).toThrow('[object Number] is not an valid resource id'); }); it('ResourceIdOf', () => { expect(ResourceIdOf(xxType, 'o', 'k')).toEqual('xx/o/k'); expect(ResourceIdOf(yyType, 'o', 'k')).toEqual('yy/o/k'); expect(ResourceIdOf(xxType, 'xx/o/k')).toEqual('xx/o/k'); expect( ResourceIdOf(xxType, { type: xxType, owner: 'o', key: 'k', }), ).toEqual('xx/o/k'); expect(() => ResourceIdOf(xxType, ' xx', '')).toThrow( `Invalid owner " xx", should not contains spaces or special characters`, ); expect(() => ResourceIdOf(xxType, 'y', 'x x')).toThrow( `Invalid key "x x", should not contains spaces or special characters`, ); expect(() => ResourceIdOf(xxType, 'x', 'x\nx')).toThrow( String.raw`Invalid key "x\nx", should not contains spaces or special characters`, ); // @ts-expect-error test invalid type expect(() => ResourceIdOf('x/x', '', '')).toThrow( `Invalid type "x/x", should not contains spaces or special characters`, ); expect(() => ResourceIdOf(xxType, '')).toThrow('"" is not an valid resource id of "xx"'); expect(() => ResourceIdOf(xxType, { type: xxType, owner: 'o', key: 'x'.repeat(241), }), ).toThrow('Invalid key "xxxxxxxxxxxxxxxxxxx..., should not be longer than 240 characters'); expect(() => ResourceIdOf(xxType, { type: yyType, owner: 'o', key: 'k', }), ).toThrow('Invalid type "yy", should be "xx"'); // @ts-expect-error test invalid type expect(() => ResourceIdOf(xxType, {})).toThrow('[object Object] is not an valid resource id'); expect(() => ResourceIdOf(xxType, null)).toThrow('Required an non-null value'); expect(() => ResourceIdOf(xxType, undefined)).toThrow('Required an non-null value'); // @ts-expect-error test invalid type expect(() => ResourceIdOf(xxType, true)).toThrow('[object Boolean] is not an valid resource id'); // @ts-expect-error test invalid type expect(() => ResourceIdOf(xxType, 1)).toThrow('[object Number] is not an valid resource id'); }); it('parseResourceId', () => { expect(parseResourceId('xx/yy/zz')).toEqual({ type: xxType, owner: 'yy', key: 'zz' }); expect(parseResourceId('x x/yy/zz')).toBeUndefined(); // @ts-expect-error test invalid type expect(parseResourceId({})).toBeUndefined(); expect(parseResourceId(null)).toBeUndefined(); expect(parseResourceId(undefined)).toBeUndefined(); // @ts-expect-error test invalid type expect(parseResourceId(false)).toBeUndefined(); // @ts-expect-error test invalid type expect(parseResourceId(true)).toBeUndefined(); // @ts-expect-error test invalid type expect(parseResourceId(1)).toBeUndefined(); }); it('parseResourceIdOf', () => { expect(parseResourceIdOf(xxType, 'xxx/yy/zz')).toBeUndefined(); expect(parseResourceIdOf(xxType, 'xx/yy/zz')).toEqual({ type: xxType, owner: 'yy', key: 'zz' }); }); it('isResourceId', () => { expect(isResourceId('xx/yy/zz')).toBe(true); expect(isResourceId('xx/yy/z z')).toBe(false); expect(isResourceId('')).toBe(false); expect(isResourceId({ type: xxType, owner: 'yy', key: 'zz' })).toBe(false); }); test('isResourceIdOf', () => { // @ts-expect-error test invalid type expect(() => isResourceIdOf({}, 'xx/yy/zz')).toThrow('Invalid type [object Object], should be string'); expect(isResourceIdOf(xxType, 'xx/yy/zz')).toBe(true); expect(isResourceIdOf(xxType, 'xxx/yy/zz')).toBe(false); expect(isResourceIdOf(xxType, '')).toBe(false); });