@friggframework/core 2.0.0--canary.397.4957a89.0 → 2.0.0--canary.398.bdb6d27.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 (97) hide show
  1. package/README.md +50 -931
  2. package/core/create-handler.js +0 -1
  3. package/database/models/WebsocketConnection.js +5 -0
  4. package/handlers/app-handler-helpers.js +3 -0
  5. package/handlers/backend-utils.js +34 -35
  6. package/handlers/routers/auth.js +14 -3
  7. package/handlers/routers/integration-defined-routers.js +5 -8
  8. package/handlers/routers/middleware/loadUser.js +15 -0
  9. package/handlers/routers/middleware/requireLoggedInUser.js +12 -0
  10. package/handlers/routers/user.js +5 -25
  11. package/handlers/workers/integration-defined-workers.js +3 -6
  12. package/index.js +16 -1
  13. package/integrations/create-frigg-backend.js +31 -0
  14. package/integrations/index.js +5 -0
  15. package/integrations/integration-base.js +44 -42
  16. package/integrations/integration-factory.js +251 -0
  17. package/integrations/integration-router.js +178 -301
  18. package/integrations/integration-user.js +144 -0
  19. package/integrations/options.js +1 -1
  20. package/integrations/test/integration-base.test.js +144 -0
  21. package/module-plugin/auther.js +393 -0
  22. package/module-plugin/entity-manager.js +70 -0
  23. package/{modules → module-plugin}/index.js +8 -0
  24. package/module-plugin/manager.js +169 -0
  25. package/module-plugin/module-factory.js +61 -0
  26. package/{modules → module-plugin}/test/mock-api/api.js +3 -8
  27. package/{modules → module-plugin}/test/mock-api/definition.js +8 -12
  28. package/package.json +5 -5
  29. package/syncs/sync.js +1 -0
  30. package/types/integrations/index.d.ts +6 -2
  31. package/types/module-plugin/index.d.ts +56 -4
  32. package/types/syncs/index.d.ts +2 -0
  33. package/credential/credential-repository.js +0 -42
  34. package/credential/use-cases/get-credential-for-user.js +0 -21
  35. package/credential/use-cases/update-authentication-status.js +0 -15
  36. package/handlers/app-definition-loader.js +0 -38
  37. package/integrations/integration-repository.js +0 -67
  38. package/integrations/integration.js +0 -233
  39. package/integrations/tests/doubles/dummy-integration-class.js +0 -90
  40. package/integrations/tests/doubles/test-integration-repository.js +0 -89
  41. package/integrations/tests/use-cases/create-integration.test.js +0 -124
  42. package/integrations/tests/use-cases/delete-integration-for-user.test.js +0 -143
  43. package/integrations/tests/use-cases/get-integration-for-user.test.js +0 -143
  44. package/integrations/tests/use-cases/get-integration-instance.test.js +0 -169
  45. package/integrations/tests/use-cases/get-integrations-for-user.test.js +0 -169
  46. package/integrations/tests/use-cases/get-possible-integrations.test.js +0 -188
  47. package/integrations/tests/use-cases/update-integration-messages.test.js +0 -142
  48. package/integrations/tests/use-cases/update-integration-status.test.js +0 -103
  49. package/integrations/tests/use-cases/update-integration.test.js +0 -134
  50. package/integrations/use-cases/create-integration.js +0 -72
  51. package/integrations/use-cases/delete-integration-for-user.js +0 -73
  52. package/integrations/use-cases/get-integration-for-user.js +0 -79
  53. package/integrations/use-cases/get-integration-instance.js +0 -84
  54. package/integrations/use-cases/get-integrations-for-user.js +0 -77
  55. package/integrations/use-cases/get-possible-integrations.js +0 -27
  56. package/integrations/use-cases/index.js +0 -11
  57. package/integrations/use-cases/update-integration-messages.js +0 -31
  58. package/integrations/use-cases/update-integration-status.js +0 -28
  59. package/integrations/use-cases/update-integration.js +0 -92
  60. package/integrations/utils/map-integration-dto.js +0 -36
  61. package/modules/module-factory.js +0 -54
  62. package/modules/module-repository.js +0 -107
  63. package/modules/module.js +0 -221
  64. package/modules/tests/doubles/test-module-factory.js +0 -16
  65. package/modules/tests/doubles/test-module-repository.js +0 -19
  66. package/modules/use-cases/get-entities-for-user.js +0 -32
  67. package/modules/use-cases/get-entity-options-by-id.js +0 -58
  68. package/modules/use-cases/get-entity-options-by-type.js +0 -34
  69. package/modules/use-cases/get-module-instance-from-type.js +0 -31
  70. package/modules/use-cases/get-module.js +0 -56
  71. package/modules/use-cases/process-authorization-callback.js +0 -114
  72. package/modules/use-cases/refresh-entity-options.js +0 -58
  73. package/modules/use-cases/test-module-auth.js +0 -54
  74. package/modules/utils/map-module-dto.js +0 -18
  75. package/user/tests/doubles/test-user-repository.js +0 -72
  76. package/user/tests/use-cases/create-individual-user.test.js +0 -24
  77. package/user/tests/use-cases/create-organization-user.test.js +0 -28
  78. package/user/tests/use-cases/create-token-for-user-id.test.js +0 -19
  79. package/user/tests/use-cases/get-user-from-bearer-token.test.js +0 -64
  80. package/user/tests/use-cases/login-user.test.js +0 -140
  81. package/user/use-cases/create-individual-user.js +0 -61
  82. package/user/use-cases/create-organization-user.js +0 -47
  83. package/user/use-cases/create-token-for-user-id.js +0 -30
  84. package/user/use-cases/get-user-from-bearer-token.js +0 -77
  85. package/user/use-cases/login-user.js +0 -122
  86. package/user/user-repository.js +0 -62
  87. package/user/user.js +0 -77
  88. /package/{modules → module-plugin}/ModuleConstants.js +0 -0
  89. /package/{modules → module-plugin}/credential.js +0 -0
  90. /package/{modules → module-plugin}/entity.js +0 -0
  91. /package/{modules → module-plugin}/requester/api-key.js +0 -0
  92. /package/{modules → module-plugin}/requester/basic.js +0 -0
  93. /package/{modules → module-plugin}/requester/oauth-2.js +0 -0
  94. /package/{modules → module-plugin}/requester/requester.js +0 -0
  95. /package/{modules → module-plugin}/requester/requester.test.js +0 -0
  96. /package/{modules → module-plugin}/test/auther.test.js +0 -0
  97. /package/{modules → module-plugin}/test/mock-api/mocks/hubspot.js +0 -0
@@ -1,143 +0,0 @@
1
- const { DeleteIntegrationForUser } = require('../../use-cases/delete-integration-for-user');
2
- const { TestIntegrationRepository } = require('../doubles/test-integration-repository');
3
- const { DummyIntegration } = require('../doubles/dummy-integration-class');
4
-
5
- describe('DeleteIntegrationForUser Use-Case', () => {
6
- let integrationRepository;
7
- let useCase;
8
-
9
- beforeEach(() => {
10
- integrationRepository = new TestIntegrationRepository();
11
- useCase = new DeleteIntegrationForUser({
12
- integrationRepository,
13
- integrationClasses: [DummyIntegration],
14
- });
15
- });
16
-
17
- describe('happy path', () => {
18
- it('deletes integration successfully', async () => {
19
- const record = await integrationRepository.createIntegration(['e1'], 'user-1', { type: 'dummy' });
20
-
21
- await useCase.execute(record.id, 'user-1');
22
-
23
- const found = await integrationRepository.findIntegrationById(record.id);
24
- expect(found).toBeNull();
25
- });
26
-
27
- it('tracks delete operation', async () => {
28
- const record = await integrationRepository.createIntegration(['e1'], 'user-1', { type: 'dummy' });
29
- integrationRepository.clearHistory();
30
-
31
- await useCase.execute(record.id, 'user-1');
32
-
33
- const history = integrationRepository.getOperationHistory();
34
- const deleteOperation = history.find(op => op.operation === 'delete');
35
- expect(deleteOperation).toEqual({
36
- operation: 'delete',
37
- id: record.id,
38
- existed: true,
39
- success: true
40
- });
41
- });
42
-
43
- it('deletes integration with multiple entities', async () => {
44
- const record = await integrationRepository.createIntegration(['e1', 'e2', 'e3'], 'user-1', { type: 'dummy' });
45
-
46
- await useCase.execute(record.id, 'user-1');
47
-
48
- const found = await integrationRepository.findIntegrationById(record.id);
49
- expect(found).toBeNull();
50
- });
51
- });
52
-
53
- describe('error cases', () => {
54
- it('throws error when integration not found', async () => {
55
- const nonExistentId = 'non-existent-id';
56
-
57
- await expect(useCase.execute(nonExistentId, 'user-1'))
58
- .rejects
59
- .toThrow(`Integration with id of ${nonExistentId} does not exist`);
60
- });
61
-
62
- it('throws error when user does not own integration', async () => {
63
- const record = await integrationRepository.createIntegration(['e1'], 'user-1', { type: 'dummy' });
64
-
65
- await expect(useCase.execute(record.id, 'different-user'))
66
- .rejects
67
- .toThrow(`Integration ${record.id} does not belong to User different-user`);
68
- });
69
-
70
- it('throws error when integration class not found', async () => {
71
- const useCaseWithoutClasses = new DeleteIntegrationForUser({
72
- integrationRepository,
73
- integrationClasses: [],
74
- });
75
-
76
- const record = await integrationRepository.createIntegration(['e1'], 'user-1', { type: 'dummy' });
77
-
78
- await expect(useCaseWithoutClasses.execute(record.id, 'user-1'))
79
- .rejects
80
- .toThrow();
81
- });
82
-
83
- it('tracks failed delete operation for non-existent integration', async () => {
84
- const nonExistentId = 'non-existent-id';
85
- integrationRepository.clearHistory();
86
-
87
- try {
88
- await useCase.execute(nonExistentId, 'user-1');
89
- } catch (error) {
90
- const history = integrationRepository.getOperationHistory();
91
- const findOperation = history.find(op => op.operation === 'findById');
92
- expect(findOperation).toEqual({
93
- operation: 'findById',
94
- id: nonExistentId,
95
- found: false
96
- });
97
- }
98
- });
99
- });
100
-
101
- describe('edge cases', () => {
102
- it('handles deletion of already deleted integration', async () => {
103
- const record = await integrationRepository.createIntegration(['e1'], 'user-1', { type: 'dummy' });
104
-
105
- await useCase.execute(record.id, 'user-1');
106
-
107
- await expect(useCase.execute(record.id, 'user-1'))
108
- .rejects
109
- .toThrow(`Integration with id of ${record.id} does not exist`);
110
- });
111
-
112
- it('handles integration with complex config during deletion', async () => {
113
- const complexConfig = {
114
- type: 'dummy',
115
- settings: { nested: { deep: 'value' } },
116
- credentials: { encrypted: true }
117
- };
118
-
119
- const record = await integrationRepository.createIntegration(['e1'], 'user-1', complexConfig);
120
-
121
- await useCase.execute(record.id, 'user-1');
122
-
123
- const found = await integrationRepository.findIntegrationById(record.id);
124
- expect(found).toBeNull();
125
- });
126
-
127
- it('handles null userId gracefully', async () => {
128
- const record = await integrationRepository.createIntegration(['e1'], 'user-1', { type: 'dummy' });
129
-
130
- await expect(useCase.execute(record.id, null))
131
- .rejects
132
- .toThrow(`Integration ${record.id} does not belong to User null`);
133
- });
134
-
135
- it('handles undefined userId gracefully', async () => {
136
- const record = await integrationRepository.createIntegration(['e1'], 'user-1', { type: 'dummy' });
137
-
138
- await expect(useCase.execute(record.id, undefined))
139
- .rejects
140
- .toThrow(`Integration ${record.id} does not belong to User undefined`);
141
- });
142
- });
143
- });
@@ -1,143 +0,0 @@
1
- const { GetIntegrationForUser } = require('../../use-cases/get-integration-for-user');
2
- const { TestIntegrationRepository } = require('../doubles/test-integration-repository');
3
- const { TestModuleFactory } = require('../../../modules/tests/doubles/test-module-factory');
4
- const { TestModuleRepository } = require('../../../modules/tests/doubles/test-module-repository');
5
- const { DummyIntegration } = require('../doubles/dummy-integration-class');
6
-
7
- describe('GetIntegrationForUser Use-Case', () => {
8
- let integrationRepository;
9
- let moduleRepository;
10
- let moduleFactory;
11
- let useCase;
12
-
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
- });
24
-
25
- describe('happy path', () => {
26
- it('returns integration dto', async () => {
27
- const entity = { id: 'entity-1', _id: 'entity-1' };
28
- moduleRepository.addEntity(entity);
29
-
30
- const record = await integrationRepository.createIntegration([entity.id], 'user-1', { type: 'dummy' });
31
-
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();
74
- });
75
-
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
- });
142
- });
143
- });
@@ -1,169 +0,0 @@
1
- const { GetIntegrationInstance } = require('../../use-cases/get-integration-instance');
2
- const { TestIntegrationRepository } = require('../doubles/test-integration-repository');
3
- const { TestModuleFactory } = require('../../../modules/tests/doubles/test-module-factory');
4
- const { DummyIntegration } = require('../doubles/dummy-integration-class');
5
-
6
- describe('GetIntegrationInstance Use-Case', () => {
7
- let integrationRepository;
8
- let moduleFactory;
9
- let useCase;
10
-
11
- beforeEach(() => {
12
- integrationRepository = new TestIntegrationRepository();
13
- moduleFactory = new TestModuleFactory();
14
- useCase = new GetIntegrationInstance({
15
- integrationRepository,
16
- integrationClasses: [DummyIntegration],
17
- moduleFactory,
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
- });
92
-
93
- const record = await integrationRepository.createIntegration(['entity-1'], 'user-1', { type: 'dummy' });
94
-
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
- });
168
- });
169
- });
@@ -1,169 +0,0 @@
1
- const { GetIntegrationsForUser } = require('../../use-cases/get-integrations-for-user');
2
- const { TestIntegrationRepository } = require('../doubles/test-integration-repository');
3
- const { TestModuleFactory } = require('../../../modules/tests/doubles/test-module-factory');
4
- const { TestModuleRepository } = require('../../../modules/tests/doubles/test-module-repository');
5
- const { DummyIntegration } = require('../doubles/dummy-integration-class');
6
-
7
- describe('GetIntegrationsForUser Use-Case', () => {
8
- let integrationRepository;
9
- let moduleRepository;
10
- let moduleFactory;
11
- let useCase;
12
-
13
- beforeEach(() => {
14
- integrationRepository = new TestIntegrationRepository();
15
- moduleRepository = new TestModuleRepository();
16
- moduleFactory = new TestModuleFactory();
17
- useCase = new GetIntegrationsForUser({
18
- integrationRepository,
19
- integrationClasses: [DummyIntegration],
20
- moduleFactory,
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([]);
133
- });
134
-
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
- });
168
- });
169
- });