-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtestContext.ts
More file actions
112 lines (94 loc) · 3.25 KB
/
testContext.ts
File metadata and controls
112 lines (94 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import {
type CommonLogger,
type ErrorReporter,
globalLogger,
type TransactionObservabilityManager,
} from '@lokalise/node-core'
import { enrichMessageSchemaWithBase } from '@message-queue-toolkit/schemas'
import { asClass, asFunction, createContainer, Lifetime, type Resolver } from 'awilix'
import { AwilixManager } from 'awilix-manager'
import { z } from 'zod/v4'
import { DomainEventEmitter } from '../lib/events/DomainEventEmitter.ts'
import { EventRegistry } from '../lib/events/EventRegistry.ts'
import type { CommonEventDefinition } from '../lib/events/eventTypes.ts'
import type { MetadataFiller } from '../lib/messages/MetadataFiller.ts'
import { CommonMetadataFiller } from '../lib/messages/MetadataFiller.ts'
import { FakeTransactionObservabilityManager } from './fakes/FakeTransactionObservabilityManager.ts'
export const SINGLETON_CONFIG = { lifetime: Lifetime.SINGLETON }
export type DependencyOverrides = Partial<DiConfig>
const TestLogger: CommonLogger = globalLogger
export const TestEvents = {
created: {
...enrichMessageSchemaWithBase(
'entity.created',
z.object({
message: z.string(),
}),
),
},
updated: {
...enrichMessageSchemaWithBase(
'entity.updated',
z.object({
message: z.string(),
}),
),
},
} as const satisfies Record<string, CommonEventDefinition>
export type TestEventsType = (typeof TestEvents)[keyof typeof TestEvents][]
export async function registerDependencies(dependencyOverrides: DependencyOverrides = {}) {
const diContainer = createContainer<Dependencies>({
injectionMode: 'PROXY',
})
const awilixManager = new AwilixManager({
diContainer,
asyncDispose: true,
asyncInit: true,
eagerInject: true,
})
const diConfig: DiConfig = {
logger: asFunction(() => {
return TestLogger
}, SINGLETON_CONFIG),
awilixManager: asFunction(() => {
return awilixManager
}, SINGLETON_CONFIG),
eventRegistry: asFunction(() => {
return new EventRegistry(Object.values(TestEvents))
}, SINGLETON_CONFIG),
eventEmitter: asFunction((deps: Dependencies) => {
return new DomainEventEmitter(deps, {
handlerSpy: true,
})
}, SINGLETON_CONFIG),
metadataFiller: asFunction(() => {
return new CommonMetadataFiller({
serviceId: 'test',
})
}, SINGLETON_CONFIG),
// vendor-specific dependencies
transactionObservabilityManager: asClass(FakeTransactionObservabilityManager, SINGLETON_CONFIG),
errorReporter: asFunction(() => {
return {
report: () => {},
} satisfies ErrorReporter
}, SINGLETON_CONFIG),
}
diContainer.register(diConfig)
for (const [dependencyKey, dependencyValue] of Object.entries(dependencyOverrides)) {
diContainer.register(dependencyKey, dependencyValue)
}
await awilixManager.executeInit()
return diContainer
}
type DiConfig = Record<keyof Dependencies, Resolver<any>>
export interface Dependencies {
logger: CommonLogger
awilixManager: AwilixManager
// vendor-specific dependencies
transactionObservabilityManager: TransactionObservabilityManager
errorReporter: ErrorReporter
eventRegistry: EventRegistry<TestEventsType>
eventEmitter: DomainEventEmitter<TestEventsType>
metadataFiller: MetadataFiller
}