forked from stockstory/logtail-lambda-extension
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathenv.test.ts
More file actions
90 lines (75 loc) · 3.16 KB
/
Copy pathenv.test.ts
File metadata and controls
90 lines (75 loc) · 3.16 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
import { either as E } from 'fp-ts';
import { parseEnvironmentVariables } from '~/env';
import { ZodError } from 'zod';
describe('parseEnvironmentVariables', () => {
test('resolves new names and populates defaults', () => {
const env = {
AWS_LAMBDA_RUNTIME_API: 'http://127.0.0.1:9001/',
SOURCE_TOKEN: 'source_token',
INGESTING_HOST: 's123456.lu-trq-1.betterstackdata.com',
};
const result = parseEnvironmentVariables(env);
expect(result).toStrictEqual({
_tag: 'Right',
right: {
NODE_ENV: 'production',
AWS_LAMBDA_RUNTIME_API: 'http://127.0.0.1:9001/',
AWS_LAMBDA_RUNTIME_EXTENSION_API_VERSION: '2020-01-01',
AWS_LAMBDA_RUNTIME_TELEMETRY_API_VERSION: '2022-07-01',
EXTENSION_NAME: 'logtail-lambda-extension',
RECEIVER_ADDRESS: 'sandbox',
MAX_ITEMS: 10000,
MAX_BYTES: 262144,
TIMEOUT_MS: 1000,
RECEIVER_PORT: 4243,
sourceToken: 'source_token',
ingestionUrl: 'https://s123456.lu-trq-1.betterstackdata.com/',
},
});
});
test('falls back to legacy token and full URL used as-is', () => {
const result = parseEnvironmentVariables({
AWS_LAMBDA_RUNTIME_API: 'http://127.0.0.1:9001/',
LOGTAIL_TOKEN: 'legacy_token',
LOGTAIL_HTTP_API_URL: 'https://in.logtail.com/',
});
if (!E.isRight(result)) throw new Error('expected Right');
expect(result.right.sourceToken).toBe('legacy_token');
expect(result.right.ingestionUrl).toBe('https://in.logtail.com/');
});
test('defaults the ingestion host when no host or legacy URL is set', () => {
const result = parseEnvironmentVariables({
AWS_LAMBDA_RUNTIME_API: 'http://127.0.0.1:9001/',
SOURCE_TOKEN: 'source_token',
});
if (!E.isRight(result)) throw new Error('expected Right');
expect(result.right.ingestionUrl).toBe('https://in.logs.betterstack.com/');
});
test('SOURCE_TOKEN wins over LOGTAIL_TOKEN', () => {
const result = parseEnvironmentVariables({
AWS_LAMBDA_RUNTIME_API: 'http://127.0.0.1:9001/',
SOURCE_TOKEN: 'preferred',
LOGTAIL_TOKEN: 'legacy',
});
if (!E.isRight(result)) throw new Error('expected Right');
expect(result.right.sourceToken).toBe('preferred');
});
test('INGESTING_HOST wins over LOGTAIL_HTTP_API_URL', () => {
const result = parseEnvironmentVariables({
AWS_LAMBDA_RUNTIME_API: 'http://127.0.0.1:9001/',
SOURCE_TOKEN: 'source_token',
INGESTING_HOST: 's123456.lu-trq-1.betterstackdata.com',
LOGTAIL_HTTP_API_URL: 'https://in.logtail.com/',
});
if (!E.isRight(result)) throw new Error('expected Right');
expect(result.right.ingestionUrl).toBe('https://s123456.lu-trq-1.betterstackdata.com/');
});
test('fails with ZodError when a runtime API is set but no token', () => {
const result = parseEnvironmentVariables({ AWS_LAMBDA_RUNTIME_API: 'http://127.0.0.1:9001/' });
expect(E.isLeft(result) && result.left instanceof ZodError).toBe(true);
});
test('fails with ZodError on completely empty env', () => {
const result = parseEnvironmentVariables({});
expect(E.isLeft(result) && result.left instanceof ZodError).toBe(true);
});
});