@friggframework/core 2.0.0--canary.397.3862908.0 → 2.0.0--canary.397.155fecd.0

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.
Files changed (26) hide show
  1. package/README.md +933 -50
  2. package/core/create-handler.js +1 -0
  3. package/handlers/routers/auth.js +1 -15
  4. package/integrations/integration-router.js +11 -11
  5. package/integrations/tests/doubles/dummy-integration-class.js +80 -1
  6. package/integrations/tests/doubles/test-integration-repository.js +43 -6
  7. package/integrations/tests/use-cases/create-integration.test.js +108 -11
  8. package/integrations/tests/use-cases/delete-integration-for-user.test.js +132 -9
  9. package/integrations/tests/use-cases/get-integration-for-user.test.js +129 -15
  10. package/integrations/tests/use-cases/get-integration-instance.test.js +154 -11
  11. package/integrations/tests/use-cases/get-integrations-for-user.test.js +156 -17
  12. package/integrations/tests/use-cases/get-possible-integrations.test.js +183 -7
  13. package/integrations/tests/use-cases/update-integration-messages.test.js +135 -8
  14. package/integrations/tests/use-cases/update-integration-status.test.js +103 -0
  15. package/integrations/tests/use-cases/update-integration.test.js +122 -12
  16. package/integrations/use-cases/create-integration.js +19 -4
  17. package/integrations/use-cases/delete-integration-for-user.js +19 -0
  18. package/integrations/use-cases/get-integration-for-user.js +22 -6
  19. package/integrations/use-cases/get-integration-instance.js +14 -3
  20. package/integrations/use-cases/get-integrations-for-user.js +17 -3
  21. package/integrations/use-cases/get-possible-integrations.js +14 -0
  22. package/integrations/use-cases/update-integration-messages.js +17 -6
  23. package/integrations/use-cases/update-integration-status.js +16 -0
  24. package/integrations/use-cases/update-integration.js +17 -6
  25. package/package.json +5 -5
  26. package/integrations/test/integration-base.test.js +0 -144
@@ -4,26 +4,140 @@ const { TestModuleFactory } = require('../../../modules/tests/doubles/test-modul
4
4
  const { TestModuleRepository } = require('../../../modules/tests/doubles/test-module-repository');
5
5
  const { DummyIntegration } = require('../doubles/dummy-integration-class');
6
6
 
7
+ describe('GetIntegrationForUser Use-Case', () => {
8
+ let integrationRepository;
9
+ let moduleRepository;
10
+ let moduleFactory;
11
+ let useCase;
7
12
 
8
- describe('GetIntegrationForUser', () => {
9
- it('returns integration dto', async () => {
10
- const iRepo = new TestIntegrationRepository();
11
- const mRepo = new TestModuleRepository();
12
- const mFactory = new TestModuleFactory();
13
+ beforeEach(() => {
14
+ integrationRepository = new TestIntegrationRepository();
15
+ moduleRepository = new TestModuleRepository();
16
+ moduleFactory = new TestModuleFactory();
17
+ useCase = new GetIntegrationForUser({
18
+ integrationRepository,
19
+ integrationClasses: [DummyIntegration],
20
+ moduleFactory,
21
+ moduleRepository,
22
+ });
23
+ });
13
24
 
14
- const entity = { id: 'entity-1' };
15
- mRepo.addEntity(entity);
25
+ describe('happy path', () => {
26
+ it('returns integration dto', async () => {
27
+ const entity = { id: 'entity-1', _id: 'entity-1' };
28
+ moduleRepository.addEntity(entity);
16
29
 
17
- const record = await iRepo.createIntegration([entity.id], 'user-1', { type: 'dummy' });
30
+ const record = await integrationRepository.createIntegration([entity.id], 'user-1', { type: 'dummy' });
18
31
 
19
- const useCase = new GetIntegrationForUser({
20
- integrationRepository: iRepo,
21
- integrationClasses: [DummyIntegration],
22
- moduleFactory: mFactory,
23
- moduleRepository: mRepo,
32
+ const dto = await useCase.execute(record.id, 'user-1');
33
+ expect(dto.id).toBe(record.id);
34
+ expect(dto.userId).toBe('user-1');
35
+ expect(dto.config.type).toBe('dummy');
36
+ });
37
+
38
+ it('returns integration with multiple entities', async () => {
39
+ const entity1 = { id: 'entity-1', _id: 'entity-1' };
40
+ const entity2 = { id: 'entity-2', _id: 'entity-2' };
41
+ moduleRepository.addEntity(entity1);
42
+ moduleRepository.addEntity(entity2);
43
+
44
+ const record = await integrationRepository.createIntegration([entity1.id, entity2.id], 'user-1', { type: 'dummy' });
45
+
46
+ const dto = await useCase.execute(record.id, 'user-1');
47
+ expect(dto.entities).toEqual([entity1, entity2]);
48
+ });
49
+
50
+ it('returns integration with complex config', async () => {
51
+ const entity = { id: 'entity-1', _id: 'entity-1' };
52
+ moduleRepository.addEntity(entity);
53
+
54
+ const complexConfig = {
55
+ type: 'dummy',
56
+ settings: { api: { timeout: 5000 }, debug: true },
57
+ features: ['webhooks', 'sync']
58
+ };
59
+
60
+ const record = await integrationRepository.createIntegration([entity.id], 'user-1', complexConfig);
61
+
62
+ const dto = await useCase.execute(record.id, 'user-1');
63
+ expect(dto.config).toEqual(complexConfig);
64
+ });
65
+ });
66
+
67
+ describe('error cases', () => {
68
+ it('throws error when integration not found', async () => {
69
+ const nonExistentId = 'non-existent-id';
70
+
71
+ await expect(useCase.execute(nonExistentId, 'user-1'))
72
+ .rejects
73
+ .toThrow();
24
74
  });
25
75
 
26
- const dto = await useCase.execute(record.id, 'user-1');
27
- expect(dto.id).toBe(record.id);
76
+ it('throws error when user does not own integration', async () => {
77
+ const entity = { id: 'entity-1', _id: 'entity-1' };
78
+ moduleRepository.addEntity(entity);
79
+
80
+ const record = await integrationRepository.createIntegration([entity.id], 'user-1', { type: 'dummy' });
81
+
82
+ await expect(useCase.execute(record.id, 'different-user'))
83
+ .rejects
84
+ .toThrow();
85
+ });
86
+
87
+ it('throws error when integration class not found', async () => {
88
+ const useCaseWithoutClasses = new GetIntegrationForUser({
89
+ integrationRepository,
90
+ integrationClasses: [],
91
+ moduleFactory,
92
+ moduleRepository,
93
+ });
94
+
95
+ const entity = { id: 'entity-1', _id: 'entity-1' };
96
+ moduleRepository.addEntity(entity);
97
+
98
+ const record = await integrationRepository.createIntegration([entity.id], 'user-1', { type: 'dummy' });
99
+
100
+ await expect(useCaseWithoutClasses.execute(record.id, 'user-1'))
101
+ .rejects
102
+ .toThrow();
103
+ });
104
+
105
+ it('handles missing entities gracefully', async () => {
106
+ const record = await integrationRepository.createIntegration(['missing-entity'], 'user-1', { type: 'dummy' });
107
+
108
+ await expect(useCase.execute(record.id, 'user-1'))
109
+ .rejects
110
+ .toThrow();
111
+ });
112
+ });
113
+
114
+ describe('edge cases', () => {
115
+ it('handles userId as string vs number comparison', async () => {
116
+ const entity = { id: 'entity-1', _id: 'entity-1' };
117
+ moduleRepository.addEntity(entity);
118
+
119
+ const record = await integrationRepository.createIntegration([entity.id], 'user-1', { type: 'dummy' });
120
+
121
+ const dto1 = await useCase.execute(record.id, 'user-1');
122
+ const dto2 = await useCase.execute(record.id, 'user-1');
123
+
124
+ expect(dto1.userId).toBe(dto2.userId);
125
+ });
126
+
127
+ it('returns all integration properties', async () => {
128
+ const entity = { id: 'entity-1', _id: 'entity-1' };
129
+ moduleRepository.addEntity(entity);
130
+
131
+ const record = await integrationRepository.createIntegration([entity.id], 'user-1', { type: 'dummy' });
132
+
133
+ record.status = 'ACTIVE';
134
+ record.version = '1.0.0';
135
+ record.messages = { info: [{ title: 'Test', message: 'Message' }] };
136
+
137
+ const dto = await useCase.execute(record.id, 'user-1');
138
+ expect(dto.status).toBe('ACTIVE');
139
+ expect(dto.version).toBe('1.0.0');
140
+ expect(dto.messages).toEqual({ info: [{ title: 'Test', message: 'Message' }] });
141
+ });
28
142
  });
29
143
  });
@@ -4,23 +4,166 @@ const { TestModuleFactory } = require('../../../modules/tests/doubles/test-modul
4
4
  const { DummyIntegration } = require('../doubles/dummy-integration-class');
5
5
 
6
6
  describe('GetIntegrationInstance Use-Case', () => {
7
- it('returns hydrated integration instance', async () => {
8
- const integrationRepository = new TestIntegrationRepository();
9
- const moduleFactory = new TestModuleFactory();
7
+ let integrationRepository;
8
+ let moduleFactory;
9
+ let useCase;
10
10
 
11
- // first create record directly via repo
12
- const record = await integrationRepository.createIntegration(['entity-1'], 'user-1', { type: 'dummy' });
13
-
14
- const useCase = new GetIntegrationInstance({
11
+ beforeEach(() => {
12
+ integrationRepository = new TestIntegrationRepository();
13
+ moduleFactory = new TestModuleFactory();
14
+ useCase = new GetIntegrationInstance({
15
15
  integrationRepository,
16
16
  integrationClasses: [DummyIntegration],
17
17
  moduleFactory,
18
18
  });
19
+ });
20
+
21
+ describe('happy path', () => {
22
+ it('returns hydrated integration instance', async () => {
23
+ const record = await integrationRepository.createIntegration(['entity-1'], 'user-1', { type: 'dummy' });
24
+
25
+ const instance = await useCase.execute(record.id, 'user-1');
26
+
27
+ expect(instance.id).toBe(record.id);
28
+ expect(instance.getConfig().type).toBe('dummy');
29
+ expect(instance.entities).toEqual(record.entitiesIds);
30
+ expect(instance.userId).toBe('user-1');
31
+ });
32
+
33
+ it('returns instance with multiple modules', async () => {
34
+ const record = await integrationRepository.createIntegration(['entity-1', 'entity-2'], 'user-1', { type: 'dummy' });
35
+
36
+ const instance = await useCase.execute(record.id, 'user-1');
37
+
38
+ expect(instance.entities).toEqual(['entity-1', 'entity-2']);
39
+ expect(Object.keys(instance.modules)).toHaveLength(1);
40
+ expect(instance.modules['stubModule']).toBeDefined();
41
+ });
42
+
43
+ it('initializes integration instance properly', async () => {
44
+ const record = await integrationRepository.createIntegration(['entity-1'], 'user-1', { type: 'dummy' });
45
+
46
+ const instance = await useCase.execute(record.id, 'user-1');
47
+
48
+ expect(typeof instance.send).toBe('function');
49
+ expect(typeof instance.getConfig).toBe('function');
50
+ expect(typeof instance.initialize).toBe('function');
51
+ });
52
+
53
+ it('preserves all integration properties', async () => {
54
+ const record = await integrationRepository.createIntegration(['entity-1'], 'user-1', { type: 'dummy', custom: 'value' });
55
+
56
+ record.status = 'ACTIVE';
57
+ record.version = '2.0.0';
58
+ record.messages = { logs: [{ title: 'Test', message: 'Log entry' }] };
59
+
60
+ const instance = await useCase.execute(record.id, 'user-1');
61
+
62
+ expect(instance.status).toBe('ACTIVE');
63
+ expect(instance.version).toBe('2.0.0');
64
+ expect(instance.messages).toEqual({ logs: [{ title: 'Test', message: 'Log entry' }] });
65
+ expect(instance.getConfig().custom).toBe('value');
66
+ });
67
+ });
68
+
69
+ describe('error cases', () => {
70
+ it('throws error when integration not found', async () => {
71
+ const nonExistentId = 'non-existent-id';
72
+
73
+ await expect(useCase.execute(nonExistentId, 'user-1'))
74
+ .rejects
75
+ .toThrow(`No integration found by the ID of ${nonExistentId}`);
76
+ });
77
+
78
+ it('throws error when user does not own integration', async () => {
79
+ const record = await integrationRepository.createIntegration(['entity-1'], 'user-1', { type: 'dummy' });
80
+
81
+ await expect(useCase.execute(record.id, 'different-user'))
82
+ .rejects
83
+ .toThrow(`Integration ${record.id} does not belong to User different-user`);
84
+ });
85
+
86
+ it('throws error when integration class not found', async () => {
87
+ const useCaseWithoutClasses = new GetIntegrationInstance({
88
+ integrationRepository,
89
+ integrationClasses: [],
90
+ moduleFactory,
91
+ });
19
92
 
20
- const instance = await useCase.execute(record.id, 'user-1');
93
+ const record = await integrationRepository.createIntegration(['entity-1'], 'user-1', { type: 'dummy' });
21
94
 
22
- expect(instance.id).toBe(record.id);
23
- expect(instance.getConfig().type).toBe('dummy');
24
- expect(instance.entities).toEqual(record.entitiesIds);
95
+ await expect(useCaseWithoutClasses.execute(record.id, 'user-1'))
96
+ .rejects
97
+ .toThrow('No integration class found for type: dummy');
98
+ });
99
+
100
+ it('throws error when integration has unknown type', async () => {
101
+ const record = await integrationRepository.createIntegration(['entity-1'], 'user-1', { type: 'unknown-type' });
102
+
103
+ await expect(useCase.execute(record.id, 'user-1'))
104
+ .rejects
105
+ .toThrow('No integration class found for type: unknown-type');
106
+ });
107
+ });
108
+
109
+ describe('edge cases', () => {
110
+ it('handles integration with no entities', async () => {
111
+ const record = await integrationRepository.createIntegration([], 'user-1', { type: 'dummy' });
112
+
113
+ const instance = await useCase.execute(record.id, 'user-1');
114
+
115
+ expect(instance.entities).toEqual([]);
116
+ expect(Object.keys(instance.modules)).toHaveLength(0);
117
+ });
118
+
119
+ it('handles integration with null config values', async () => {
120
+ const record = await integrationRepository.createIntegration(['entity-1'], 'user-1', { type: 'dummy', nullValue: null });
121
+
122
+ const instance = await useCase.execute(record.id, 'user-1');
123
+
124
+ expect(instance.getConfig().nullValue).toBeNull();
125
+ });
126
+
127
+ it('handles userId comparison edge cases', async () => {
128
+ const record = await integrationRepository.createIntegration(['entity-1'], 'user-1', { type: 'dummy' });
129
+
130
+ const instance1 = await useCase.execute(record.id, 'user-1');
131
+ const instance2 = await useCase.execute(record.id, 'user-1');
132
+
133
+ expect(instance1.userId).toBe(instance2.userId);
134
+ });
135
+
136
+ it('returns fresh instance on each call', async () => {
137
+ const record = await integrationRepository.createIntegration(['entity-1'], 'user-1', { type: 'dummy' });
138
+
139
+ const instance1 = await useCase.execute(record.id, 'user-1');
140
+ const instance2 = await useCase.execute(record.id, 'user-1');
141
+
142
+ expect(instance1).not.toBe(instance2);
143
+ expect(instance1.id).toBe(instance2.id);
144
+ });
145
+
146
+ it('handles complex nested config structures', async () => {
147
+ const complexConfig = {
148
+ type: 'dummy',
149
+ settings: {
150
+ api: {
151
+ timeout: 5000,
152
+ retries: 3,
153
+ endpoints: ['users', 'orders']
154
+ },
155
+ features: {
156
+ webhooks: true,
157
+ sync: { interval: 300 }
158
+ }
159
+ }
160
+ };
161
+
162
+ const record = await integrationRepository.createIntegration(['entity-1'], 'user-1', complexConfig);
163
+
164
+ const instance = await useCase.execute(record.id, 'user-1');
165
+
166
+ expect(instance.getConfig()).toEqual(complexConfig);
167
+ });
25
168
  });
26
169
  });
@@ -4,27 +4,166 @@ const { TestModuleFactory } = require('../../../modules/tests/doubles/test-modul
4
4
  const { TestModuleRepository } = require('../../../modules/tests/doubles/test-module-repository');
5
5
  const { DummyIntegration } = require('../doubles/dummy-integration-class');
6
6
 
7
- describe('GetIntegrationsForUser', () => {
8
- it('returns integrations dto list', async () => {
9
- const integrationRepo = new TestIntegrationRepository();
10
- const moduleRepo = new TestModuleRepository();
11
- const moduleFactory = new TestModuleFactory();
7
+ describe('GetIntegrationsForUser Use-Case', () => {
8
+ let integrationRepository;
9
+ let moduleRepository;
10
+ let moduleFactory;
11
+ let useCase;
12
12
 
13
- // setup entity and module repo
14
- const entity = { id: 'entity-1' };
15
- moduleRepo.addEntity(entity);
16
-
17
- await integrationRepo.createIntegration([entity.id], 'user-1', { type: 'dummy' });
18
-
19
- const useCase = new GetIntegrationsForUser({
20
- integrationRepository: integrationRepo,
13
+ beforeEach(() => {
14
+ integrationRepository = new TestIntegrationRepository();
15
+ moduleRepository = new TestModuleRepository();
16
+ moduleFactory = new TestModuleFactory();
17
+ useCase = new GetIntegrationsForUser({
18
+ integrationRepository,
21
19
  integrationClasses: [DummyIntegration],
22
20
  moduleFactory,
23
- moduleRepository: moduleRepo,
21
+ moduleRepository,
22
+ });
23
+ });
24
+
25
+ describe('happy path', () => {
26
+ it('returns integrations dto list for single user', async () => {
27
+ const entity = { id: 'entity-1' };
28
+ moduleRepository.addEntity(entity);
29
+
30
+ await integrationRepository.createIntegration([entity.id], 'user-1', { type: 'dummy' });
31
+
32
+ const list = await useCase.execute('user-1');
33
+ expect(list.length).toBe(1);
34
+ expect(list[0].config.type).toBe('dummy');
35
+ expect(list[0].userId).toBe('user-1');
36
+ });
37
+
38
+ it('returns multiple integrations for same user', async () => {
39
+ const entity1 = { id: 'entity-1' };
40
+ const entity2 = { id: 'entity-2' };
41
+ moduleRepository.addEntity(entity1);
42
+ moduleRepository.addEntity(entity2);
43
+
44
+ await integrationRepository.createIntegration([entity1.id], 'user-1', { type: 'dummy', name: 'first' });
45
+ await integrationRepository.createIntegration([entity2.id], 'user-1', { type: 'dummy', name: 'second' });
46
+
47
+ const list = await useCase.execute('user-1');
48
+ expect(list.length).toBe(2);
49
+ expect(list[0].config.name).toBe('first');
50
+ expect(list[1].config.name).toBe('second');
51
+ });
52
+
53
+ it('filters integrations by user correctly', async () => {
54
+ const entity1 = { id: 'entity-1' };
55
+ const entity2 = { id: 'entity-2' };
56
+ moduleRepository.addEntity(entity1);
57
+ moduleRepository.addEntity(entity2);
58
+
59
+ await integrationRepository.createIntegration([entity1.id], 'user-1', { type: 'dummy', owner: 'user1' });
60
+ await integrationRepository.createIntegration([entity2.id], 'user-2', { type: 'dummy', owner: 'user2' });
61
+
62
+ const user1List = await useCase.execute('user-1');
63
+ const user2List = await useCase.execute('user-2');
64
+
65
+ expect(user1List.length).toBe(1);
66
+ expect(user2List.length).toBe(1);
67
+ expect(user1List[0].config.owner).toBe('user1');
68
+ expect(user2List[0].config.owner).toBe('user2');
69
+ });
70
+
71
+ it('returns empty array when user has no integrations', async () => {
72
+ const entity = { id: 'entity-1' };
73
+ moduleRepository.addEntity(entity);
74
+
75
+ await integrationRepository.createIntegration([entity.id], 'user-1', { type: 'dummy' });
76
+
77
+ const list = await useCase.execute('user-2');
78
+ expect(list).toEqual([]);
79
+ });
80
+
81
+ it('tracks repository operations', async () => {
82
+ const entity = { id: 'entity-1' };
83
+ moduleRepository.addEntity(entity);
84
+ await integrationRepository.createIntegration([entity.id], 'user-1', { type: 'dummy' });
85
+ integrationRepository.clearHistory();
86
+
87
+ await useCase.execute('user-1');
88
+
89
+ const history = integrationRepository.getOperationHistory();
90
+ const findOperation = history.find(op => op.operation === 'findByUserId');
91
+ expect(findOperation).toEqual({
92
+ operation: 'findByUserId',
93
+ userId: 'user-1',
94
+ count: 1
95
+ });
96
+ });
97
+ });
98
+
99
+ describe('error cases', () => {
100
+ it('throws error when integration class not found', async () => {
101
+ const useCaseWithoutClasses = new GetIntegrationsForUser({
102
+ integrationRepository,
103
+ integrationClasses: [],
104
+ moduleFactory,
105
+ moduleRepository,
106
+ });
107
+
108
+ const entity = { id: 'entity-1' };
109
+ moduleRepository.addEntity(entity);
110
+ await integrationRepository.createIntegration([entity.id], 'user-1', { type: 'dummy' });
111
+
112
+ await expect(useCaseWithoutClasses.execute('user-1'))
113
+ .rejects
114
+ .toThrow();
115
+ });
116
+
117
+ it('handles missing entities gracefully', async () => {
118
+ await integrationRepository.createIntegration(['missing-entity'], 'user-1', { type: 'dummy' });
119
+
120
+ await expect(useCase.execute('user-1'))
121
+ .rejects
122
+ .toThrow();
123
+ });
124
+ });
125
+
126
+ describe('edge cases', () => {
127
+ it('handles user with null/undefined userId', async () => {
128
+ const list1 = await useCase.execute(null);
129
+ const list2 = await useCase.execute(undefined);
130
+
131
+ expect(list1).toEqual([]);
132
+ expect(list2).toEqual([]);
24
133
  });
25
134
 
26
- const list = await useCase.execute('user-1');
27
- expect(list.length).toBe(1);
28
- expect(list[0].config.type).toBe('dummy');
135
+ it('handles integrations with complex configs', async () => {
136
+ const entity = { id: 'entity-1' };
137
+ moduleRepository.addEntity(entity);
138
+
139
+ const complexConfig = {
140
+ type: 'dummy',
141
+ settings: {
142
+ nested: { deep: 'value' },
143
+ array: [1, 2, 3],
144
+ boolean: true,
145
+ nullValue: null
146
+ }
147
+ };
148
+
149
+ await integrationRepository.createIntegration([entity.id], 'user-1', complexConfig);
150
+
151
+ const list = await useCase.execute('user-1');
152
+ expect(list[0].config).toEqual(complexConfig);
153
+ });
154
+
155
+ it('handles integrations with multiple entities', async () => {
156
+ const entity1 = { id: 'entity-1' };
157
+ const entity2 = { id: 'entity-2' };
158
+ const entity3 = { id: 'entity-3' };
159
+ moduleRepository.addEntity(entity1);
160
+ moduleRepository.addEntity(entity2);
161
+ moduleRepository.addEntity(entity3);
162
+
163
+ await integrationRepository.createIntegration([entity1.id, entity2.id, entity3.id], 'user-1', { type: 'dummy' });
164
+
165
+ const list = await useCase.execute('user-1');
166
+ expect(list[0].entities).toEqual([entity1, entity2, entity3]);
167
+ });
29
168
  });
30
169
  });