@thzero/library_server_service_grpc 0.19.3 → 0.19.5

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.
@@ -1,236 +1,236 @@
1
- import assert from 'node:assert/strict';
2
- import { beforeEach, describe, it } from 'node:test';
3
-
4
- import '@thzero/library_common/utility/string.js';
5
- import LibraryMomentUtility from '@thzero/library_common/utility/moment.js';
6
- import BaseClientGrpcService from '../client.js';
7
-
8
- // dayjs.utc() only exists once the plugins are registered; the app does this at
9
- // boot, so anything calling getTimestamp() outside a booted app must do it too.
10
- LibraryMomentUtility.initDateTime();
11
-
12
- const inject = (target, name, value) => {
13
- Object.defineProperty(target, name, { value, writable: true, configurable: true });
14
- return target;
15
- };
16
-
17
- const newLogger = () => ({ debug() {}, info() {}, info2() {}, warn() {}, error() {}, exception() {}, fatal() {}, trace() {} });
18
-
19
- let service;
20
-
21
- beforeEach(() => {
22
- service = new BaseClientGrpcService();
23
- inject(service, '_logger', newLogger());
24
- inject(service, '_config', { getBackend: () => ({ baseUrl: 'http://backend:1' }) });
25
- });
26
-
27
- describe('_host', () => {
28
- it('requires a key', async () => {
29
- await assert.rejects(() => service._host('cid', null, null), /key is empty/);
30
- });
31
-
32
- // Regression: the guard read `!String.url(opts.url)`, and String.url does not
33
- // exist - library_common/utility/string.js declares capitalize, isNullOrEmpty,
34
- // isString and trim. Every call carrying a url threw
35
- // `TypeError: String.url is not a function`.
36
- it('takes the url straight from opts', async () => {
37
- const host = await service._host('cid', 'key', { url: 'http://given:1', secure: true });
38
- assert.deepEqual(host, { url: 'http://given:1', secure: true });
39
- });
40
-
41
- it('prefers a resource in opts over a url', async () => {
42
- const host = await service._host('cid', 'key', {
43
- url: 'http://ignored:1',
44
- resource: { address: 'from-resource', grpc: { port: 9000 } }
45
- });
46
- assert.equal(host.url, 'from-resource:9000');
47
- });
48
-
49
- it('falls back to config when there are no opts at all', async () => {
50
- const host = await service._host('cid', 'key', null);
51
- assert.equal(host.url, 'http://backend:1');
52
- });
53
-
54
- // Known gap: opts that carry neither a resource nor a url do NOT fall back to
55
- // config - the host comes back empty. Pinning current behaviour.
56
- it('returns an empty host for opts with neither a resource nor a url', async () => {
57
- assert.deepEqual(await service._host('cid', 'key', {}), { url: null, secure: false });
58
- });
59
- });
60
-
61
- describe('_hostFromConfig', () => {
62
- const host = () => ({ url: null, secure: false });
63
-
64
- it('uses the configured base url when discovery is off', async () => {
65
- const result = await service._hostFromConfig('cid', host(), 'key', null);
66
- assert.equal(result.url, 'http://backend:1');
67
- });
68
-
69
- it('uses the configured base url when no discovery service is wired up', async () => {
70
- inject(service, '_config', { getBackend: () => ({ baseUrl: 'http://backend:1', discoverable: { name: 'svc' } }) });
71
- const result = await service._hostFromConfig('cid', host(), 'key', null);
72
- assert.equal(result.url, 'http://backend:1');
73
- });
74
-
75
- it('uses the configured base url when discovery is explicitly disabled', async () => {
76
- inject(service, '_config', {
77
- getBackend: () => ({ baseUrl: 'http://backend:1', discoverable: { name: 'svc', enabled: false } })
78
- });
79
- service._serviceDiscoveryResources = { getService: async () => { throw new Error('must not be called'); } };
80
- const result = await service._hostFromConfig('cid', host(), 'key', null);
81
- assert.equal(result.url, 'http://backend:1');
82
- });
83
-
84
- it('resolves and caches a discovered host', async () => {
85
- let lookups = 0;
86
- inject(service, '_config', { getBackend: () => ({ baseUrl: 'http://backend:1', discoverable: { name: 'svc' } }) });
87
- service._serviceDiscoveryResources = {
88
- getService: async () => { lookups++; return { success: true, results: { address: 'discovered', grpc: { port: 9000 } } }; }
89
- };
90
- const first = await service._hostFromConfig('cid', host(), 'key', null);
91
- const second = await service._hostFromConfig('cid', host(), 'key', null);
92
- assert.equal(first.url, 'discovered:9000');
93
- assert.equal(lookups, 1, 'the second call came from the cache');
94
- assert.equal(second, first);
95
- });
96
-
97
- it('returns null when discovery fails', async () => {
98
- inject(service, '_config', { getBackend: () => ({ baseUrl: 'http://backend:1', discoverable: { name: 'svc' } }) });
99
- service._serviceDiscoveryResources = { getService: async () => ({ success: false }) };
100
- assert.equal(await service._hostFromConfig('cid', host(), 'key', null), null);
101
- });
102
-
103
- // Regression: a failure cached nothing, so every call after it took the mutex
104
- // and asked discovery again, one at a time.
105
- it('does not ask discovery again for a failed key within the retry window', async () => {
106
- let lookups = 0;
107
- inject(service, '_config', { getBackend: () => ({ baseUrl: 'http://backend:1', discoverable: { name: 'svc' } }) });
108
- service._serviceDiscoveryResources = { getService: async () => { lookups++; return { success: false }; } };
109
- assert.equal(await service._hostFromConfig('cid', host(), 'key', null), null);
110
- assert.equal(await service._hostFromConfig('cid', host(), 'key', null), null);
111
- assert.equal(await service._hostFromConfig('cid', host(), 'key', null), null);
112
- assert.equal(lookups, 1);
113
- });
114
-
115
- it('asks again once the retry window has passed, and forgets the failure on success', async () => {
116
- let lookups = 0;
117
- let succeed = false;
118
- inject(service, '_config', { getBackend: () => ({ baseUrl: 'http://backend:1', discoverable: { name: 'svc' } }) });
119
- service._serviceDiscoveryResources = {
120
- getService: async () => { lookups++; return succeed ? { success: true, results: { address: 'up', grpc: { port: 1 } } } : { success: false }; }
121
- };
122
- service._hostsRetryMs = 0;
123
- assert.equal(await service._hostFromConfig('cid', host(), 'key', null), null);
124
- succeed = true;
125
- const result = await service._hostFromConfig('cid', host(), 'key', null);
126
- assert.equal(result.url, 'up:1');
127
- assert.equal(lookups, 2);
128
- assert.equal(service._hostsFailed.has('key'), false);
129
- });
130
-
131
- it('shares one discovery between concurrent calls for the same key', async () => {
132
- let lookups = 0;
133
- let release;
134
- const gate = new Promise((resolve) => { release = resolve; });
135
- inject(service, '_config', { getBackend: () => ({ baseUrl: 'http://backend:1', discoverable: { name: 'svc' } }) });
136
- service._serviceDiscoveryResources = {
137
- getService: async () => { lookups++; await gate; return { success: true, results: { address: 'discovered', grpc: { port: 9000 } } }; }
138
- };
139
- const pending = Promise.all([ service._hostFromConfig('a', host(), 'key', null), service._hostFromConfig('b', host(), 'key', null) ]);
140
- release();
141
- const [ first, second ] = await pending;
142
- assert.equal(lookups, 1);
143
- assert.equal(second, first);
144
- assert.equal(service._hostsPending.size, 0);
145
- });
146
-
147
- // The single mutex serialized every key behind whichever was in flight.
148
- it('does not hold one key\'s discovery behind another\'s', async () => {
149
- let release;
150
- const gate = new Promise((resolve) => { release = resolve; });
151
- inject(service, '_config', { getBackend: (correlationId, key) => ({ baseUrl: 'http://backend:1', discoverable: { name: key } }) });
152
- service._serviceDiscoveryResources = {
153
- getService: async (correlationId, name) => {
154
- if (name === 'slow')
155
- await gate;
156
- return { success: true, results: { address: name, grpc: { port: 1 } } };
157
- }
158
- };
159
- const slow = service._hostFromConfig('cid', host(), 'slow', null);
160
- const fast = await service._hostFromConfig('cid', host(), 'fast', null);
161
- assert.equal(fast.url, 'fast:1', 'resolved while the slow one is still in flight');
162
- release();
163
- assert.equal((await slow).url, 'slow:1');
164
- });
165
-
166
- it('rejects a config without a base url', async () => {
167
- inject(service, '_config', { getBackend: () => ({}) });
168
- await assert.rejects(() => service._hostFromConfig('cid', host(), 'key', null), /config.baseUrl is empty/);
169
- });
170
- });
171
-
172
- describe('_hostFromResource', () => {
173
- const host = () => ({ url: null, secure: false });
174
-
175
- it('joins address and port', async () => {
176
- const result = await service._hostFromResource('cid', host(), { address: 'a', grpc: { port: 1 } });
177
- assert.equal(result.url, 'a:1');
178
- });
179
-
180
- it('omits a missing port', async () => {
181
- const result = await service._hostFromResource('cid', host(), { address: 'a', grpc: {} });
182
- assert.equal(result.url, 'a');
183
- });
184
-
185
- it('carries the secure flag off the grpc block', async () => {
186
- assert.equal((await service._hostFromResource('cid', host(), { address: 'a', grpc: { secure: true } })).secure, true);
187
- assert.equal((await service._hostFromResource('cid', host(), { address: 'a', grpc: {} })).secure, false);
188
- });
189
-
190
- it('assembles a dns name from label, namespace and local', async () => {
191
- const result = await service._hostFromResource('cid', host(),
192
- { dns: { label: 'svc', namespace: 'ns', local: true }, grpc: { port: 1 } });
193
- assert.equal(result.url, 'svc.ns.local:1');
194
- });
195
-
196
- it('assembles a dns name that is neither namespaced nor local', async () => {
197
- const result = await service._hostFromResource('cid', host(), { dns: { label: 'svc' }, grpc: {} });
198
- assert.equal(result.url, 'svc');
199
- });
200
-
201
- it('rejects a resource with no grpc block and one with no address', async () => {
202
- await assert.rejects(() => service._hostFromResource('cid', host(), { address: 'a' }), /resource.grpc is null/);
203
- await assert.rejects(() => service._hostFromResource('cid', host(), { grpc: {} }), /address is null/);
204
- });
205
- });
206
-
207
- describe('_execute', () => {
208
- it('requires a function', async () => {
209
- await assert.rejects(() => service._execute('cid', null, {}, {}), /func is null/);
210
- });
211
-
212
- it('resolves with the response the callback is given', async () => {
213
- const func = function (request, meta, callback) { callback(null, { echoed: request.a }); };
214
- assert.deepEqual(await service._execute('cid', func, {}, { a: 1 }), { echoed: 1 });
215
- });
216
-
217
- it('rejects with the error the callback is given', async () => {
218
- const func = function (request, meta, callback) { callback(new Error('boom')); };
219
- await assert.rejects(() => service._execute('cid', func, {}, {}), /boom/);
220
- });
221
-
222
- it('sends the correlationId as metadata', async () => {
223
- let seen = null;
224
- const func = function (request, meta, callback) { seen = meta.get('correlationId'); callback(null, {}); };
225
- await service._execute('cid-123', func, {}, {});
226
- assert.deepEqual(seen, [ 'cid-123' ]);
227
- });
228
-
229
- it('calls the function with the client as its receiver', async () => {
230
- const client = { marker: 'me' };
231
- let receiver = null;
232
- const func = function (request, meta, callback) { receiver = this; callback(null, {}); };
233
- await service._execute('cid', func, client, {});
234
- assert.equal(receiver, client);
235
- });
236
- });
1
+ import assert from 'node:assert/strict';
2
+ import { beforeEach, describe, it } from 'node:test';
3
+
4
+ import '@thzero/library_common/utility/string.js';
5
+ import LibraryMomentUtility from '@thzero/library_common/utility/moment.js';
6
+ import BaseClientGrpcService from '../client.js';
7
+
8
+ // dayjs.utc() only exists once the plugins are registered; the app does this at
9
+ // boot, so anything calling getTimestamp() outside a booted app must do it too.
10
+ LibraryMomentUtility.initDateTime();
11
+
12
+ const inject = (target, name, value) => {
13
+ Object.defineProperty(target, name, { value, writable: true, configurable: true });
14
+ return target;
15
+ };
16
+
17
+ const newLogger = () => ({ debug() {}, info() {}, info2() {}, warn() {}, error() {}, exception() {}, fatal() {}, trace() {} });
18
+
19
+ let service;
20
+
21
+ beforeEach(() => {
22
+ service = new BaseClientGrpcService();
23
+ inject(service, '_logger', newLogger());
24
+ inject(service, '_config', { getBackend: () => ({ baseUrl: 'http://backend:1' }) });
25
+ });
26
+
27
+ describe('_host', () => {
28
+ it('requires a key', async () => {
29
+ await assert.rejects(() => service._host('cid', null, null), /key is empty/);
30
+ });
31
+
32
+ // Regression: the guard read `!String.url(opts.url)`, and String.url does not
33
+ // exist - library_common/utility/string.js declares capitalize, isNullOrEmpty,
34
+ // isString and trim. Every call carrying a url threw
35
+ // `TypeError: String.url is not a function`.
36
+ it('takes the url straight from opts', async () => {
37
+ const host = await service._host('cid', 'key', { url: 'http://given:1', secure: true });
38
+ assert.deepEqual(host, { url: 'http://given:1', secure: true });
39
+ });
40
+
41
+ it('prefers a resource in opts over a url', async () => {
42
+ const host = await service._host('cid', 'key', {
43
+ url: 'http://ignored:1',
44
+ resource: { address: 'from-resource', grpc: { port: 9000 } }
45
+ });
46
+ assert.equal(host.url, 'from-resource:9000');
47
+ });
48
+
49
+ it('falls back to config when there are no opts at all', async () => {
50
+ const host = await service._host('cid', 'key', null);
51
+ assert.equal(host.url, 'http://backend:1');
52
+ });
53
+
54
+ // Known gap: opts that carry neither a resource nor a url do NOT fall back to
55
+ // config - the host comes back empty. Pinning current behaviour.
56
+ it('returns an empty host for opts with neither a resource nor a url', async () => {
57
+ assert.deepEqual(await service._host('cid', 'key', {}), { url: null, secure: false });
58
+ });
59
+ });
60
+
61
+ describe('_hostFromConfig', () => {
62
+ const host = () => ({ url: null, secure: false });
63
+
64
+ it('uses the configured base url when discovery is off', async () => {
65
+ const result = await service._hostFromConfig('cid', host(), 'key', null);
66
+ assert.equal(result.url, 'http://backend:1');
67
+ });
68
+
69
+ it('uses the configured base url when no discovery service is wired up', async () => {
70
+ inject(service, '_config', { getBackend: () => ({ baseUrl: 'http://backend:1', discoverable: { name: 'svc' } }) });
71
+ const result = await service._hostFromConfig('cid', host(), 'key', null);
72
+ assert.equal(result.url, 'http://backend:1');
73
+ });
74
+
75
+ it('uses the configured base url when discovery is explicitly disabled', async () => {
76
+ inject(service, '_config', {
77
+ getBackend: () => ({ baseUrl: 'http://backend:1', discoverable: { name: 'svc', enabled: false } })
78
+ });
79
+ service._serviceDiscoveryResources = { getService: async () => { throw new Error('must not be called'); } };
80
+ const result = await service._hostFromConfig('cid', host(), 'key', null);
81
+ assert.equal(result.url, 'http://backend:1');
82
+ });
83
+
84
+ it('resolves and caches a discovered host', async () => {
85
+ let lookups = 0;
86
+ inject(service, '_config', { getBackend: () => ({ baseUrl: 'http://backend:1', discoverable: { name: 'svc' } }) });
87
+ service._serviceDiscoveryResources = {
88
+ getService: async () => { lookups++; return { success: true, results: { address: 'discovered', grpc: { port: 9000 } } }; }
89
+ };
90
+ const first = await service._hostFromConfig('cid', host(), 'key', null);
91
+ const second = await service._hostFromConfig('cid', host(), 'key', null);
92
+ assert.equal(first.url, 'discovered:9000');
93
+ assert.equal(lookups, 1, 'the second call came from the cache');
94
+ assert.equal(second, first);
95
+ });
96
+
97
+ it('returns null when discovery fails', async () => {
98
+ inject(service, '_config', { getBackend: () => ({ baseUrl: 'http://backend:1', discoverable: { name: 'svc' } }) });
99
+ service._serviceDiscoveryResources = { getService: async () => ({ success: false }) };
100
+ assert.equal(await service._hostFromConfig('cid', host(), 'key', null), null);
101
+ });
102
+
103
+ // Regression: a failure cached nothing, so every call after it took the mutex
104
+ // and asked discovery again, one at a time.
105
+ it('does not ask discovery again for a failed key within the retry window', async () => {
106
+ let lookups = 0;
107
+ inject(service, '_config', { getBackend: () => ({ baseUrl: 'http://backend:1', discoverable: { name: 'svc' } }) });
108
+ service._serviceDiscoveryResources = { getService: async () => { lookups++; return { success: false }; } };
109
+ assert.equal(await service._hostFromConfig('cid', host(), 'key', null), null);
110
+ assert.equal(await service._hostFromConfig('cid', host(), 'key', null), null);
111
+ assert.equal(await service._hostFromConfig('cid', host(), 'key', null), null);
112
+ assert.equal(lookups, 1);
113
+ });
114
+
115
+ it('asks again once the retry window has passed, and forgets the failure on success', async () => {
116
+ let lookups = 0;
117
+ let succeed = false;
118
+ inject(service, '_config', { getBackend: () => ({ baseUrl: 'http://backend:1', discoverable: { name: 'svc' } }) });
119
+ service._serviceDiscoveryResources = {
120
+ getService: async () => { lookups++; return succeed ? { success: true, results: { address: 'up', grpc: { port: 1 } } } : { success: false }; }
121
+ };
122
+ service._hostsRetryMs = 0;
123
+ assert.equal(await service._hostFromConfig('cid', host(), 'key', null), null);
124
+ succeed = true;
125
+ const result = await service._hostFromConfig('cid', host(), 'key', null);
126
+ assert.equal(result.url, 'up:1');
127
+ assert.equal(lookups, 2);
128
+ assert.equal(service._hostsFailed.has('key'), false);
129
+ });
130
+
131
+ it('shares one discovery between concurrent calls for the same key', async () => {
132
+ let lookups = 0;
133
+ let release;
134
+ const gate = new Promise((resolve) => { release = resolve; });
135
+ inject(service, '_config', { getBackend: () => ({ baseUrl: 'http://backend:1', discoverable: { name: 'svc' } }) });
136
+ service._serviceDiscoveryResources = {
137
+ getService: async () => { lookups++; await gate; return { success: true, results: { address: 'discovered', grpc: { port: 9000 } } }; }
138
+ };
139
+ const pending = Promise.all([ service._hostFromConfig('a', host(), 'key', null), service._hostFromConfig('b', host(), 'key', null) ]);
140
+ release();
141
+ const [ first, second ] = await pending;
142
+ assert.equal(lookups, 1);
143
+ assert.equal(second, first);
144
+ assert.equal(service._hostsPending.size, 0);
145
+ });
146
+
147
+ // The single mutex serialized every key behind whichever was in flight.
148
+ it('does not hold one key\'s discovery behind another\'s', async () => {
149
+ let release;
150
+ const gate = new Promise((resolve) => { release = resolve; });
151
+ inject(service, '_config', { getBackend: (correlationId, key) => ({ baseUrl: 'http://backend:1', discoverable: { name: key } }) });
152
+ service._serviceDiscoveryResources = {
153
+ getService: async (correlationId, name) => {
154
+ if (name === 'slow')
155
+ await gate;
156
+ return { success: true, results: { address: name, grpc: { port: 1 } } };
157
+ }
158
+ };
159
+ const slow = service._hostFromConfig('cid', host(), 'slow', null);
160
+ const fast = await service._hostFromConfig('cid', host(), 'fast', null);
161
+ assert.equal(fast.url, 'fast:1', 'resolved while the slow one is still in flight');
162
+ release();
163
+ assert.equal((await slow).url, 'slow:1');
164
+ });
165
+
166
+ it('rejects a config without a base url', async () => {
167
+ inject(service, '_config', { getBackend: () => ({}) });
168
+ await assert.rejects(() => service._hostFromConfig('cid', host(), 'key', null), /config.baseUrl is empty/);
169
+ });
170
+ });
171
+
172
+ describe('_hostFromResource', () => {
173
+ const host = () => ({ url: null, secure: false });
174
+
175
+ it('joins address and port', async () => {
176
+ const result = await service._hostFromResource('cid', host(), { address: 'a', grpc: { port: 1 } });
177
+ assert.equal(result.url, 'a:1');
178
+ });
179
+
180
+ it('omits a missing port', async () => {
181
+ const result = await service._hostFromResource('cid', host(), { address: 'a', grpc: {} });
182
+ assert.equal(result.url, 'a');
183
+ });
184
+
185
+ it('carries the secure flag off the grpc block', async () => {
186
+ assert.equal((await service._hostFromResource('cid', host(), { address: 'a', grpc: { secure: true } })).secure, true);
187
+ assert.equal((await service._hostFromResource('cid', host(), { address: 'a', grpc: {} })).secure, false);
188
+ });
189
+
190
+ it('assembles a dns name from label, namespace and local', async () => {
191
+ const result = await service._hostFromResource('cid', host(),
192
+ { dns: { label: 'svc', namespace: 'ns', local: true }, grpc: { port: 1 } });
193
+ assert.equal(result.url, 'svc.ns.local:1');
194
+ });
195
+
196
+ it('assembles a dns name that is neither namespaced nor local', async () => {
197
+ const result = await service._hostFromResource('cid', host(), { dns: { label: 'svc' }, grpc: {} });
198
+ assert.equal(result.url, 'svc');
199
+ });
200
+
201
+ it('rejects a resource with no grpc block and one with no address', async () => {
202
+ await assert.rejects(() => service._hostFromResource('cid', host(), { address: 'a' }), /resource.grpc is null/);
203
+ await assert.rejects(() => service._hostFromResource('cid', host(), { grpc: {} }), /address is null/);
204
+ });
205
+ });
206
+
207
+ describe('_execute', () => {
208
+ it('requires a function', async () => {
209
+ await assert.rejects(() => service._execute('cid', null, {}, {}), /func is null/);
210
+ });
211
+
212
+ it('resolves with the response the callback is given', async () => {
213
+ const func = function (request, meta, callback) { callback(null, { echoed: request.a }); };
214
+ assert.deepEqual(await service._execute('cid', func, {}, { a: 1 }), { echoed: 1 });
215
+ });
216
+
217
+ it('rejects with the error the callback is given', async () => {
218
+ const func = function (request, meta, callback) { callback(new Error('boom')); };
219
+ await assert.rejects(() => service._execute('cid', func, {}, {}), /boom/);
220
+ });
221
+
222
+ it('sends the correlationId as metadata', async () => {
223
+ let seen = null;
224
+ const func = function (request, meta, callback) { seen = meta.get('correlationId'); callback(null, {}); };
225
+ await service._execute('cid-123', func, {}, {});
226
+ assert.deepEqual(seen, [ 'cid-123' ]);
227
+ });
228
+
229
+ it('calls the function with the client as its receiver', async () => {
230
+ const client = { marker: 'me' };
231
+ let receiver = null;
232
+ const func = function (request, meta, callback) { receiver = this; callback(null, {}); };
233
+ await service._execute('cid', func, client, {});
234
+ assert.equal(receiver, client);
235
+ });
236
+ });
@@ -1,85 +1,85 @@
1
- import assert from 'node:assert/strict';
2
- import { beforeEach, describe, it } from 'node:test';
3
-
4
- import '@thzero/library_common/utility/string.js';
5
- import BaseServerGrpcService from '../server.js';
6
-
7
- const inject = (target, name, value) => {
8
- Object.defineProperty(target, name, { value, writable: true, configurable: true });
9
- return target;
10
- };
11
-
12
- const newLogger = () => ({ debug() {}, info() {}, info2() {}, warn() {}, error() {}, exception() {}, fatal() {}, trace() {} });
13
-
14
- // grpc metadata exposes get(key) -> array of values
15
- const newCall = (metadata = {}) => ({ metadata: { get: (key) => metadata[key] ?? [] } });
16
-
17
- let service;
18
-
19
- beforeEach(() => {
20
- service = new BaseServerGrpcService();
21
- inject(service, '_logger', newLogger());
22
- inject(service, '_config', { get: () => null });
23
- });
24
-
25
- describe('_correlationId', () => {
26
- it('reads the first correlationId from the call metadata', () => {
27
- assert.equal(service._correlationId(newCall({ correlationId: [ 'cid-123' ] })), 'cid-123');
28
- });
29
-
30
- it('returns null when the metadata carries none', () => {
31
- assert.equal(service._correlationId(newCall()), null);
32
- });
33
-
34
- it('throws on a missing call', () => {
35
- assert.throws(() => service._correlationId(null), /call is null/);
36
- });
37
- });
38
-
39
- describe('_authenticate', () => {
40
- it('returns the correlationId off the call', () => {
41
- assert.equal(service._authenticate(newCall({ correlationId: [ 'cid-123' ] })), 'cid-123');
42
- });
43
-
44
- // Known gap: `const valid = true; // TODO` - the authorization metadata is read
45
- // and logged but never checked, so nothing is ever rejected here. Pinning that
46
- // so it is visible rather than assumed.
47
- it('does not currently reject anything (TODO in the source)', () => {
48
- assert.doesNotThrow(() => service._authenticate(newCall({ authorization: [ 'Bearer nonsense' ] })));
49
- });
50
- });
51
-
52
- describe('_handleError', () => {
53
- it('requires a callback', () => {
54
- assert.throws(() => service._handleError('cid', new Error('boom'), null), /callback is null/);
55
- });
56
-
57
- it('does nothing when there is no error', () => {
58
- let called = false;
59
- service._handleError('cid', null, () => { called = true; });
60
- assert.equal(called, false);
61
- });
62
-
63
- it('reports the name and message together', () => {
64
- let payload = null;
65
- service._handleError('cid', new Error('boom'), (err) => { payload = err; });
66
- assert.deepEqual(payload, { message: 'Error boom' });
67
- });
68
-
69
- it('trims when only one of name and message is present', () => {
70
- let payload = null;
71
- service._handleError('cid', { message: 'boom' }, (err) => { payload = err; });
72
- assert.deepEqual(payload, { message: 'boom' });
73
-
74
- service._handleError('cid', { name: 'Bad' }, (err) => { payload = err; });
75
- assert.deepEqual(payload, { message: 'Bad' });
76
- });
77
- });
78
-
79
- describe('_start', () => {
80
- it('requires a grpc config with a port', async () => {
81
- await assert.rejects(() => service._start('cid'), /config.grpc is null/);
82
- service._configGrpc = {};
83
- await assert.rejects(() => service._start('cid'), /config.grpc.port is null/);
84
- });
85
- });
1
+ import assert from 'node:assert/strict';
2
+ import { beforeEach, describe, it } from 'node:test';
3
+
4
+ import '@thzero/library_common/utility/string.js';
5
+ import BaseServerGrpcService from '../server.js';
6
+
7
+ const inject = (target, name, value) => {
8
+ Object.defineProperty(target, name, { value, writable: true, configurable: true });
9
+ return target;
10
+ };
11
+
12
+ const newLogger = () => ({ debug() {}, info() {}, info2() {}, warn() {}, error() {}, exception() {}, fatal() {}, trace() {} });
13
+
14
+ // grpc metadata exposes get(key) -> array of values
15
+ const newCall = (metadata = {}) => ({ metadata: { get: (key) => metadata[key] ?? [] } });
16
+
17
+ let service;
18
+
19
+ beforeEach(() => {
20
+ service = new BaseServerGrpcService();
21
+ inject(service, '_logger', newLogger());
22
+ inject(service, '_config', { get: () => null });
23
+ });
24
+
25
+ describe('_correlationId', () => {
26
+ it('reads the first correlationId from the call metadata', () => {
27
+ assert.equal(service._correlationId(newCall({ correlationId: [ 'cid-123' ] })), 'cid-123');
28
+ });
29
+
30
+ it('returns null when the metadata carries none', () => {
31
+ assert.equal(service._correlationId(newCall()), null);
32
+ });
33
+
34
+ it('throws on a missing call', () => {
35
+ assert.throws(() => service._correlationId(null), /call is null/);
36
+ });
37
+ });
38
+
39
+ describe('_authenticate', () => {
40
+ it('returns the correlationId off the call', () => {
41
+ assert.equal(service._authenticate(newCall({ correlationId: [ 'cid-123' ] })), 'cid-123');
42
+ });
43
+
44
+ // Known gap: `const valid = true; // TODO` - the authorization metadata is read
45
+ // and logged but never checked, so nothing is ever rejected here. Pinning that
46
+ // so it is visible rather than assumed.
47
+ it('does not currently reject anything (TODO in the source)', () => {
48
+ assert.doesNotThrow(() => service._authenticate(newCall({ authorization: [ 'Bearer nonsense' ] })));
49
+ });
50
+ });
51
+
52
+ describe('_handleError', () => {
53
+ it('requires a callback', () => {
54
+ assert.throws(() => service._handleError('cid', new Error('boom'), null), /callback is null/);
55
+ });
56
+
57
+ it('does nothing when there is no error', () => {
58
+ let called = false;
59
+ service._handleError('cid', null, () => { called = true; });
60
+ assert.equal(called, false);
61
+ });
62
+
63
+ it('reports the name and message together', () => {
64
+ let payload = null;
65
+ service._handleError('cid', new Error('boom'), (err) => { payload = err; });
66
+ assert.deepEqual(payload, { message: 'Error boom' });
67
+ });
68
+
69
+ it('trims when only one of name and message is present', () => {
70
+ let payload = null;
71
+ service._handleError('cid', { message: 'boom' }, (err) => { payload = err; });
72
+ assert.deepEqual(payload, { message: 'boom' });
73
+
74
+ service._handleError('cid', { name: 'Bad' }, (err) => { payload = err; });
75
+ assert.deepEqual(payload, { message: 'Bad' });
76
+ });
77
+ });
78
+
79
+ describe('_start', () => {
80
+ it('requires a grpc config with a port', async () => {
81
+ await assert.rejects(() => service._start('cid'), /config.grpc is null/);
82
+ service._configGrpc = {};
83
+ await assert.rejects(() => service._start('cid'), /config.grpc.port is null/);
84
+ });
85
+ });