@powersync/service-core 1.17.0 → 1.18.1

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.
@@ -0,0 +1,102 @@
1
+ import { AbstractModule, loadModules, ServiceContextContainer, TearDownOptions } from '@/index.js';
2
+ import { describe, expect, it, vi } from 'vitest';
3
+
4
+ interface MockConfig {
5
+ connections?: { type: string }[];
6
+ storage: { type: string };
7
+ }
8
+
9
+ class MockMySQLModule extends AbstractModule {
10
+ constructor() {
11
+ super({ name: 'MySQLModule' });
12
+ }
13
+ async initialize(context: ServiceContextContainer): Promise<void> {}
14
+ async teardown(options: TearDownOptions): Promise<void> {}
15
+ }
16
+ class MockPostgresModule extends AbstractModule {
17
+ constructor() {
18
+ super({ name: 'PostgresModule' });
19
+ }
20
+ async initialize(context: ServiceContextContainer): Promise<void> {}
21
+ async teardown(options: TearDownOptions): Promise<void> {}
22
+ }
23
+ class MockPostgresStorageModule extends AbstractModule {
24
+ constructor() {
25
+ super({ name: 'PostgresStorageModule' });
26
+ }
27
+ async initialize(context: ServiceContextContainer): Promise<void> {}
28
+ async teardown(options: TearDownOptions): Promise<void> {}
29
+ }
30
+ const mockLoaders = {
31
+ connection: {
32
+ mysql: async () => {
33
+ return new MockMySQLModule();
34
+ },
35
+ postgresql: async () => {
36
+ return new MockPostgresModule();
37
+ }
38
+ },
39
+ storage: {
40
+ postgresql: async () => {
41
+ return new MockPostgresStorageModule();
42
+ }
43
+ }
44
+ };
45
+
46
+ describe('module loader', () => {
47
+ it('should load all modules defined in connections and storage', async () => {
48
+ const config: MockConfig = {
49
+ connections: [{ type: 'mysql' }, { type: 'postgresql' }],
50
+ storage: { type: 'postgresql' }
51
+ };
52
+
53
+ const modules = await loadModules(config as any, mockLoaders);
54
+
55
+ expect(modules.length).toBe(3);
56
+ expect(modules[0]).toBeInstanceOf(MockMySQLModule);
57
+ expect(modules[1]).toBeInstanceOf(MockPostgresModule);
58
+ expect(modules[2]).toBeInstanceOf(MockPostgresStorageModule);
59
+ });
60
+
61
+ it('should handle duplicate connection types (e.g., mysql used twice)', async () => {
62
+ const config: MockConfig = {
63
+ connections: [{ type: 'mysql' }, { type: 'postgresql' }, { type: 'mysql' }], // mysql duplicated
64
+ storage: { type: 'postgresql' }
65
+ };
66
+
67
+ const modules = await loadModules(config as any, mockLoaders);
68
+
69
+ // Expect 3 modules: mysql, postgresql, postgresql-storage
70
+ expect(modules.length).toBe(3);
71
+ expect(modules.filter((m) => m instanceof MockMySQLModule).length).toBe(1);
72
+ expect(modules.filter((m) => m instanceof MockPostgresModule).length).toBe(1);
73
+ expect(modules.filter((m) => m instanceof MockPostgresStorageModule).length).toBe(1);
74
+ });
75
+
76
+ it('should throw an error if any modules are not found in ModuleMap', async () => {
77
+ const config: MockConfig = {
78
+ connections: [{ type: 'mysql' }, { type: 'redis' }],
79
+ storage: { type: 'postgresql' }
80
+ };
81
+
82
+ await expect(loadModules(config as any, mockLoaders)).rejects.toThrowError();
83
+ });
84
+
85
+ it('should throw an error if one dynamic connection module import fails', async () => {
86
+ const config: MockConfig = {
87
+ connections: [{ type: 'mysql' }],
88
+ storage: { type: 'postgresql' }
89
+ };
90
+
91
+ const loaders = {
92
+ connection: {
93
+ mysql: async () => {
94
+ throw new Error('Failed to load MySQL module');
95
+ }
96
+ },
97
+ storage: mockLoaders.storage
98
+ };
99
+
100
+ await expect(loadModules(config as any, loaders)).rejects.toThrowError('Failed to load MySQL module');
101
+ });
102
+ });