-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cluster.service.spec.ts
116 lines (105 loc) · 3.03 KB
/
cluster.service.spec.ts
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
113
114
115
116
const mockBrowser = {
on: jest.fn(),
start: jest.fn()
};
const mockAdvertisement = {
start: jest.fn()
};
const mockMdns = {
udp: jest.fn().mockImplementation((name: string) => {
return { name };
}),
createBrowser: jest.fn().mockReturnValue(mockBrowser),
createAdvertisement: jest.fn().mockReturnValue(mockAdvertisement),
rst: {
DNSServiceResolve: jest.fn(),
DNSServiceGetAddrInfo: jest.fn(),
getaddrinfo: jest.fn(),
makeAddressesUnique: jest.fn()
},
// eslint-disable-next-line @typescript-eslint/camelcase
dns_sd: []
};
import { networkInterfaces } from 'os';
import Democracy from 'democracy';
import { Test, TestingModule } from '@nestjs/testing';
import { ClusterService } from './cluster.service';
import { ConfigModule } from '../config/config.module';
import { ClusterConfig } from './cluster.config';
import { ConfigService } from '../config/config.service';
import c from 'config';
jest.mock('os');
jest.mock('democracy');
jest.mock('mdns', () => mockMdns, { virtual: true });
describe('ClusterService', () => {
let service: ClusterService;
const mockConfig = new ClusterConfig();
const configService = {
get: jest.fn().mockImplementation((key: string) => {
return key === 'cluster' ? mockConfig : c.get(key);
})
};
beforeAll(async () => {
(networkInterfaces as jest.Mock).mockReturnValue({
lo: [
{
address: '127.0.0.1',
family: 'IPv4',
internal: true
}
],
eth0: [
{
address: '192.168.1.108',
family: 'IPv4',
internal: false
},
{
address: 'fe80::a00:27ff:fe4e:66a1',
family: 'IPv6',
internal: false
}
]
});
});
beforeEach(async () => {
jest.clearAllMocks();
const module: TestingModule = await Test.createTestingModule({
imports: [ConfigModule],
providers: [ClusterService]
})
.overrideProvider(ConfigService)
.useValue(configService)
.compile();
service = module.get<ClusterService>(ClusterService);
});
it('should determine the local IP', () => {
expect(Democracy).toHaveBeenCalledWith({
source: '192.168.1.108:6425',
peers: [],
timeout: 60000
});
});
it('should start advertising room-assistant via Bonjour', () => {
service.onApplicationBootstrap();
expect(mockMdns.createAdvertisement).toHaveBeenCalledWith(
{ name: 'room-assistant' },
6425,
{
networkInterface: undefined
}
);
expect(mockMdns.createBrowser).toHaveBeenCalledWith(
{ name: 'room-assistant' },
expect.anything()
);
expect(mockBrowser.start).toHaveBeenCalled();
expect(mockAdvertisement.start).toHaveBeenCalled();
});
it('should not advertise the instance if auto discovery has been turned off', () => {
mockConfig.autoDiscovery = false;
service.onApplicationBootstrap();
expect(mockMdns.createAdvertisement).not.toHaveBeenCalled();
expect(mockMdns.createBrowser).not.toHaveBeenCalled();
});
});