@declaro/data 2.0.0-beta.115 → 2.0.0-beta.117

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,6 +4,8 @@ import { MockMemoryRepository } from '../../test/mock/repositories/mock-memory-r
4
4
  import { MockBookSchema, type MockBookInput } from '../../test/mock/models/mock-book-models'
5
5
  import { EventManager } from '@declaro/core'
6
6
  import { mock } from 'bun:test'
7
+ import type { InferDetail } from '../../shared/utils/schema-inference'
8
+ import { ModelMutationAction } from '../events/event-types'
7
9
 
8
10
  describe('ModelService', () => {
9
11
  const namespace = 'books'
@@ -628,394 +630,4 @@ describe('ModelService', () => {
628
630
  expect(results).toEqual(inputs)
629
631
  })
630
632
  })
631
-
632
- describe('normalizeInput functionality', () => {
633
- class TestModelService extends ModelService<typeof mockSchema> {
634
- public testNormalizeInput(input: any) {
635
- return this.normalizeInput(input)
636
- }
637
-
638
- protected async normalizeInput(input: MockBookInput): Promise<MockBookInput> {
639
- return {
640
- ...input,
641
- title: input.title?.trim(),
642
- author: input.author?.trim(),
643
- publishedDate: new Date('2023-01-01'),
644
- }
645
- }
646
- }
647
-
648
- let testService: TestModelService
649
-
650
- beforeEach(() => {
651
- testService = new TestModelService({ repository, emitter, schema: mockSchema, namespace })
652
- })
653
-
654
- it('should use default normalizeInput method (no changes) when not overridden', async () => {
655
- const input = { title: ' Test Book ', author: ' Author Name ', publishedDate: new Date() }
656
- const normalized = await service['normalizeInput'](input)
657
-
658
- expect(normalized).toEqual(input)
659
- expect(normalized).toBe(input) // Should be the exact same reference
660
- })
661
-
662
- it('should use custom normalizeInput method for create operation', async () => {
663
- const input = { title: ' Test Book ', author: ' Author Name ', publishedDate: new Date() }
664
- const createdItem = await testService.create(input)
665
-
666
- expect(createdItem.title).toBe('Test Book')
667
- expect(createdItem.author).toBe('Author Name')
668
- expect(createdItem.publishedDate).toEqual(new Date('2023-01-01'))
669
- })
670
-
671
- it('should use custom normalizeInput method for update operation', async () => {
672
- const input = { id: 42, title: 'Original Book', author: 'Original Author', publishedDate: new Date() }
673
- const createdItem = await testService.create(input)
674
-
675
- const updateInput = { title: ' Updated Book ', author: ' Updated Author ', publishedDate: new Date() }
676
- const updatedItem = await testService.update({ id: createdItem.id }, updateInput)
677
-
678
- expect(updatedItem.title).toBe('Updated Book')
679
- expect(updatedItem.author).toBe('Updated Author')
680
- expect(updatedItem.publishedDate).toEqual(new Date('2023-01-01'))
681
- })
682
-
683
- it('should use custom normalizeInput method for upsert operation', async () => {
684
- const input = { id: 42, title: ' Test Book ', author: ' Author Name ', publishedDate: new Date() }
685
- const upsertedItem = await testService.upsert(input)
686
-
687
- expect(upsertedItem.title).toBe('Test Book')
688
- expect(upsertedItem.author).toBe('Author Name')
689
- expect(upsertedItem.publishedDate).toEqual(new Date('2023-01-01'))
690
-
691
- // Upsert again with different data
692
- const updateInput = {
693
- id: 42,
694
- title: ' Updated Book ',
695
- author: ' Updated Author ',
696
- publishedDate: new Date(),
697
- }
698
- const updatedItem = await testService.upsert(updateInput)
699
-
700
- expect(updatedItem.title).toBe('Updated Book')
701
- expect(updatedItem.author).toBe('Updated Author')
702
- expect(updatedItem.publishedDate).toEqual(new Date('2023-01-01'))
703
- })
704
-
705
- it('should use custom normalizeInput method for bulkUpsert operation', async () => {
706
- const inputs = [
707
- { id: 1, title: ' Book One ', author: ' Author One ', publishedDate: new Date() },
708
- { id: 2, title: ' Book Two ', author: ' Author Two ', publishedDate: new Date() },
709
- { title: ' Book Three ', author: ' Author Three ', publishedDate: new Date() }, // No ID - will be created
710
- ]
711
-
712
- const results = await testService.bulkUpsert(inputs)
713
-
714
- expect(results).toHaveLength(3)
715
- expect(results[0].title).toBe('Book One')
716
- expect(results[0].author).toBe('Author One')
717
- expect(results[0].publishedDate).toEqual(new Date('2023-01-01'))
718
-
719
- expect(results[1].title).toBe('Book Two')
720
- expect(results[1].author).toBe('Author Two')
721
- expect(results[1].publishedDate).toEqual(new Date('2023-01-01'))
722
-
723
- expect(results[2].title).toBe('Book Three')
724
- expect(results[2].author).toBe('Author Three')
725
- expect(results[2].publishedDate).toEqual(new Date('2023-01-01'))
726
- })
727
-
728
- it('should preserve events order with normalized input in create operation', async () => {
729
- const input = { title: ' Test Book ', author: ' Author Name ', publishedDate: new Date() }
730
- await testService.create(input)
731
-
732
- // Debug: Let's see what was actually called
733
- const beforeCreateCall = beforeCreateSpy.mock.calls[0][0]
734
- const afterCreateCall = afterCreateSpy.mock.calls[0][0]
735
-
736
- expect(beforeCreateCall.meta.input.title).toBe('Test Book')
737
- expect(beforeCreateCall.meta.input.author).toBe('Author Name')
738
- expect(beforeCreateCall.meta.input.publishedDate).toEqual(new Date('2023-01-01'))
739
-
740
- expect(afterCreateCall.meta.input.title).toBe('Test Book')
741
- expect(afterCreateCall.meta.input.author).toBe('Author Name')
742
- expect(afterCreateCall.meta.input.publishedDate).toEqual(new Date('2023-01-01'))
743
- })
744
-
745
- it('should call normalizeInput method exactly once per input during bulkUpsert with Promise.all', async () => {
746
- const normalizeInputSpy = mock(async (input: any) => ({ ...input, normalized: true }))
747
-
748
- class SpyService extends ModelService<typeof mockSchema> {
749
- protected async normalizeInput(input: any) {
750
- return normalizeInputSpy(input)
751
- }
752
- }
753
-
754
- const spyService = new SpyService({ repository, emitter, schema: mockSchema, namespace })
755
-
756
- const inputs = [
757
- { id: 1, title: 'Book One', author: 'Author One', publishedDate: new Date() },
758
- { id: 2, title: 'Book Two', author: 'Author Two', publishedDate: new Date() },
759
- ]
760
-
761
- await spyService.bulkUpsert(inputs)
762
-
763
- expect(normalizeInputSpy).toHaveBeenCalledTimes(2)
764
- expect(normalizeInputSpy).toHaveBeenNthCalledWith(1, inputs[0])
765
- expect(normalizeInputSpy).toHaveBeenNthCalledWith(2, inputs[1])
766
- })
767
-
768
- it('should handle async normalization errors gracefully', async () => {
769
- class ErrorNormalizationService extends ModelService<typeof mockSchema> {
770
- protected async normalizeInput(input: any) {
771
- if (input.title === 'ERROR') {
772
- throw new Error('Normalization failed')
773
- }
774
- return input
775
- }
776
- }
777
-
778
- const errorService = new ErrorNormalizationService({
779
- repository,
780
- emitter,
781
- schema: mockSchema,
782
- namespace,
783
- })
784
-
785
- const input = { title: 'ERROR', author: 'Author Name', publishedDate: new Date() }
786
-
787
- await expect(errorService.create(input)).rejects.toThrow('Normalization failed')
788
- })
789
-
790
- it('should process bulk normalization in parallel for performance', async () => {
791
- const processingTimes: number[] = []
792
-
793
- class TimingNormalizationService extends ModelService<typeof mockSchema> {
794
- protected async normalizeInput(input: any) {
795
- const start = Date.now()
796
- // Simulate some async work
797
- await new Promise((resolve) => setTimeout(resolve, 50))
798
- processingTimes.push(Date.now() - start)
799
- return input
800
- }
801
- }
802
-
803
- const timingService = new TimingNormalizationService({
804
- repository,
805
- emitter,
806
- schema: mockSchema,
807
- namespace,
808
- })
809
-
810
- const inputs = [
811
- { id: 1, title: 'Book One', author: 'Author One', publishedDate: new Date() },
812
- { id: 2, title: 'Book Two', author: 'Author Two', publishedDate: new Date() },
813
- { id: 3, title: 'Book Three', author: 'Author Three', publishedDate: new Date() },
814
- ]
815
-
816
- const start = Date.now()
817
- await timingService.bulkUpsert(inputs)
818
- const totalTime = Date.now() - start
819
-
820
- // With Promise.all, total time should be closer to single operation time rather than sum of all
821
- // Allow some variance for test stability
822
- expect(totalTime).toBeLessThan(150) // Much less than 3 * 50ms = 150ms
823
- expect(processingTimes).toHaveLength(3)
824
- })
825
- })
826
-
827
- describe('Response Normalization', () => {
828
- class TestRepository extends MockMemoryRepository<typeof mockSchema> {
829
- constructor() {
830
- super({
831
- schema: mockSchema,
832
- })
833
- }
834
-
835
- async create(input: MockBookInput): Promise<any> {
836
- const record = await super.create(input)
837
- // Return with publishedDate as string to test normalization
838
- record.publishedDate = '2024-01-01' as any
839
- return record
840
- }
841
-
842
- async update(lookup: any, input: MockBookInput): Promise<any> {
843
- const record = await super.update(lookup, input)
844
- // Return with publishedDate as string to test normalization
845
- record.publishedDate = '2024-01-01' as any
846
- return record
847
- }
848
-
849
- async upsert(input: MockBookInput): Promise<any> {
850
- const record = await super.upsert(input)
851
- // Return with publishedDate as string to test normalization
852
- record.publishedDate = '2024-01-01' as any
853
- return record
854
- }
855
-
856
- async bulkUpsert(inputs: MockBookInput[]): Promise<any[]> {
857
- const records = await super.bulkUpsert(inputs)
858
- // Return with publishedDate as string to test normalization
859
- for (const record of records) {
860
- record.publishedDate = '2024-01-01' as any
861
- }
862
- return records
863
- }
864
-
865
- async remove(lookup: any): Promise<any> {
866
- const record = await super.remove(lookup)
867
- // Return with publishedDate as string to test normalization
868
- if (record) {
869
- record.publishedDate = '2024-01-01' as any
870
- }
871
- return record
872
- }
873
-
874
- async restore(lookup: any): Promise<any> {
875
- const record = await super.restore(lookup)
876
- // Return with publishedDate as string to test normalization
877
- if (record) {
878
- record.publishedDate = '2024-01-01' as any
879
- }
880
- return record
881
- }
882
- }
883
-
884
- class TestService extends ModelService<typeof mockSchema> {}
885
-
886
- let testRepository: TestRepository
887
- let testService: TestService
888
-
889
- beforeEach(() => {
890
- testRepository = new TestRepository()
891
- emitter = new EventManager()
892
-
893
- testService = new TestService({
894
- repository: testRepository,
895
- emitter,
896
- schema: mockSchema,
897
- namespace,
898
- })
899
- })
900
-
901
- it('should normalize details in the create response', async () => {
902
- const input = { id: 200, title: 'Create Test', author: 'Creator', publishedDate: new Date() }
903
- const record = await testService.create(input)
904
-
905
- const expectedDate = new Date('2024-01-01')
906
- const actualDate = record.publishedDate
907
-
908
- expect(actualDate).toEqual(expectedDate)
909
- expect(actualDate).toBeInstanceOf(Date)
910
- })
911
-
912
- it('should normalize details in the update response', async () => {
913
- const input = { id: 201, title: 'Update Test', author: 'Updater', publishedDate: new Date() }
914
- await testRepository.create(input)
915
-
916
- const updatedInput = { title: 'Updated Test', author: 'Updated Author', publishedDate: new Date() }
917
- const record = await testService.update({ id: 201 }, updatedInput)
918
-
919
- const expectedDate = new Date('2024-01-01')
920
- const actualDate = record.publishedDate
921
-
922
- expect(actualDate).toEqual(expectedDate)
923
- expect(actualDate).toBeInstanceOf(Date)
924
- })
925
-
926
- it('should normalize details in the upsert response when creating', async () => {
927
- const input = { id: 202, title: 'Upsert Create Test', author: 'Upserter', publishedDate: new Date() }
928
- const record = await testService.upsert(input)
929
-
930
- const expectedDate = new Date('2024-01-01')
931
- const actualDate = record.publishedDate
932
-
933
- expect(actualDate).toEqual(expectedDate)
934
- expect(actualDate).toBeInstanceOf(Date)
935
- })
936
-
937
- it('should normalize details in the upsert response when updating', async () => {
938
- const input = { id: 203, title: 'Upsert Update Test', author: 'Upserter', publishedDate: new Date() }
939
- await testRepository.create(input)
940
-
941
- const updatedInput = {
942
- id: 203,
943
- title: 'Updated Upsert Test',
944
- author: 'Updated Upserter',
945
- publishedDate: new Date(),
946
- }
947
- const record = await testService.upsert(updatedInput)
948
-
949
- const expectedDate = new Date('2024-01-01')
950
- const actualDate = record.publishedDate
951
-
952
- expect(actualDate).toEqual(expectedDate)
953
- expect(actualDate).toBeInstanceOf(Date)
954
- })
955
-
956
- it('should normalize details in the bulkUpsert response', async () => {
957
- const input1 = { id: 204, title: 'Bulk Test 1', author: 'Bulk Author 1', publishedDate: new Date() }
958
- const input2 = { id: 205, title: 'Bulk Test 2', author: 'Bulk Author 2', publishedDate: new Date() }
959
-
960
- const records = await testService.bulkUpsert([input1, input2])
961
-
962
- const expectedDate = new Date('2024-01-01')
963
-
964
- for (const record of records) {
965
- const actualDate = record.publishedDate
966
- expect(actualDate).toEqual(expectedDate)
967
- expect(actualDate).toBeInstanceOf(Date)
968
- }
969
- })
970
-
971
- it('should normalize details in the bulkUpsert response with mixed create and update', async () => {
972
- const existingInput = { id: 206, title: 'Existing', author: 'Existing Author', publishedDate: new Date() }
973
- await testRepository.create(existingInput)
974
-
975
- const updateInput = {
976
- id: 206,
977
- title: 'Updated Existing',
978
- author: 'Updated Author',
979
- publishedDate: new Date(),
980
- }
981
- const createInput = { id: 207, title: 'New Record', author: 'New Author', publishedDate: new Date() }
982
-
983
- const records = await testService.bulkUpsert([updateInput, createInput])
984
-
985
- const expectedDate = new Date('2024-01-01')
986
-
987
- for (const record of records) {
988
- const actualDate = record.publishedDate
989
- expect(actualDate).toEqual(expectedDate)
990
- expect(actualDate).toBeInstanceOf(Date)
991
- }
992
- })
993
-
994
- it('should normalize summaries in the remove response', async () => {
995
- const input = { id: 208, title: 'Remove Test', author: 'Remover', publishedDate: new Date() }
996
- await testRepository.create(input)
997
-
998
- const record = await testService.remove({ id: 208 })
999
-
1000
- const expectedDate = new Date('2024-01-01')
1001
- const actualDate = record.publishedDate
1002
-
1003
- expect(actualDate).toEqual(expectedDate)
1004
- expect(actualDate).toBeInstanceOf(Date)
1005
- })
1006
-
1007
- it('should normalize summaries in the restore response', async () => {
1008
- const input = { id: 209, title: 'Restore Test', author: 'Restorer', publishedDate: new Date() }
1009
- await testRepository.create(input)
1010
- await testRepository.remove({ id: 209 })
1011
-
1012
- const record = await testService.restore({ id: 209 })
1013
-
1014
- const expectedDate = new Date('2024-01-01')
1015
- const actualDate = record.publishedDate
1016
-
1017
- expect(actualDate).toEqual(expectedDate)
1018
- expect(actualDate).toBeInstanceOf(Date)
1019
- })
1020
- })
1021
633
  })