import { describe, expect, it, test } from '@jest/globals'; import { EntityConfiguration } from '../EntityConfiguration.ts'; import { RESERVED_ENTITY_COUNT_QUERY_ALIAS } from '../EntityDatabaseAdapter.ts'; import { StringField, UUIDField } from '../EntityFields.ts'; import { CompositeFieldHolder } from '../internal/CompositeFieldHolder.ts'; describe(EntityConfiguration, () => { describe('when valid', () => { type BlahT = { id: string; cacheable: string; uniqueButNotCacheable: string; }; type Blah2T = { id: string; }; const blahEntityConfiguration = new EntityConfiguration({ idField: 'id', tableName: 'blah_table', schema: { id: new UUIDField({ columnName: 'id', cache: false, }), cacheable: new StringField({ columnName: 'cacheable', cache: true, }), uniqueButNotCacheable: new StringField({ columnName: 'unique_but_not_cacheable', }), }, databaseAdapterFlavor: 'postgres', cacheAdapterFlavor: 'redis', compositeFieldDefinitions: [ { compositeField: ['id', 'cacheable'], cache: true }, { compositeField: ['id', 'uniqueButNotCacheable'], cache: false }, ], }); it('returns correct fields', () => { expect(blahEntityConfiguration.idField).toEqual('id'); expect(blahEntityConfiguration.tableName).toEqual('blah_table'); expect(blahEntityConfiguration.databaseAdapterFlavor).toEqual('postgres'); expect(blahEntityConfiguration.cacheAdapterFlavor).toEqual('redis'); expect(blahEntityConfiguration.compositeFieldInfo.getAllCompositeFieldHolders()).toEqual([ new CompositeFieldHolder(['id', 'cacheable']), new CompositeFieldHolder(['id', 'uniqueButNotCacheable']), ]); }); it('filters cacheable fields', () => { expect(blahEntityConfiguration.cacheableKeys).toEqual(new Set(['cacheable'])); }); it('correctly returns cacheable composite fields', () => { expect( blahEntityConfiguration.compositeFieldInfo.canCacheCompositeField(['id', 'cacheable']), ).toBe(true); expect( blahEntityConfiguration.compositeFieldInfo.canCacheCompositeField([ 'id', 'uniqueButNotCacheable', ]), ).toBe(false); expect(() => blahEntityConfiguration.compositeFieldInfo.canCacheCompositeField(['id']), ).toThrow('Composite field (id) not found in entity configuration'); }); it('validates composite fields', () => { expect( () => new EntityConfiguration({ idField: 'id', tableName: 'blah_table', schema: { id: new UUIDField({ columnName: 'id', cache: false, }), cacheable: new StringField({ columnName: 'cacheable', cache: true, }), uniqueButNotCacheable: new StringField({ columnName: 'unique_but_not_cacheable', }), }, databaseAdapterFlavor: 'postgres', cacheAdapterFlavor: 'redis', compositeFieldDefinitions: [{ compositeField: ['id', 'id'], cache: true }], }), ).toThrow('Composite field must have unique sub-fields'); expect( () => new EntityConfiguration({ idField: 'id', tableName: 'blah_table', schema: { id: new UUIDField({ columnName: 'id', cache: false, }), cacheable: new StringField({ columnName: 'cacheable', cache: true, }), uniqueButNotCacheable: new StringField({ columnName: 'unique_but_not_cacheable', }), }, databaseAdapterFlavor: 'postgres', cacheAdapterFlavor: 'redis', compositeFieldDefinitions: [{ compositeField: ['id'], cache: true }], }), ).toThrow('Composite field must have at least two sub-fields'); }); describe('cache key version', () => { it('defaults to 0', () => { const entityConfiguration = new EntityConfiguration({ idField: 'id', tableName: 'blah', schema: { id: new UUIDField({ columnName: 'id', cache: false, }), }, databaseAdapterFlavor: 'postgres', cacheAdapterFlavor: 'redis', }); expect(entityConfiguration.cacheKeyVersion).toEqual(0); }); it('sets to custom version', () => { const entityConfiguration = new EntityConfiguration({ idField: 'id', tableName: 'blah', schema: { id: new UUIDField({ columnName: 'id', cache: false, }), }, databaseAdapterFlavor: 'postgres', cacheAdapterFlavor: 'redis', cacheKeyVersion: 100, }); expect(entityConfiguration.cacheKeyVersion).toEqual(100); }); }); }); describe('validation', () => { describe('disallows keys of JS Object prototype for safety', () => { test.each([ 'constructor', '__defineGetter__', '__defineSetter__', 'hasOwnProperty', '__lookupGetter__', '__lookupSetter__', 'isPrototypeOf', 'propertyIsEnumerable', 'toString', 'valueOf', '__proto__', 'toLocaleString', ])('disallows %p as field key', (keyName) => { expect( () => new EntityConfiguration<{ id: string }, 'id'>({ idField: 'id', tableName: 'blah_table', schema: { id: new UUIDField({ columnName: 'id', cache: false, }), [keyName]: new StringField({ columnName: 'any', }), }, databaseAdapterFlavor: 'postgres', cacheAdapterFlavor: 'redis', }), ).toThrow( `Entity field name not allowed to prevent conflicts with standard Object prototype fields: ${keyName}`, ); }); }); it('disallows RESERVED_ENTITY_COUNT_QUERY_ALIAS as a column name', () => { expect( () => new EntityConfiguration<{ id: string; count: string }, 'id'>({ idField: 'id', tableName: 'blah_table', schema: { id: new UUIDField({ columnName: 'id', cache: false, }), count: new StringField({ columnName: RESERVED_ENTITY_COUNT_QUERY_ALIAS, }), }, databaseAdapterFlavor: 'postgres', cacheAdapterFlavor: 'redis', }), ).toThrow( `Entity field "count" has disallowed column name "${RESERVED_ENTITY_COUNT_QUERY_ALIAS}" which is reserved for count queries. Choose a different column name.`, ); }); }); });