-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrometheusMetricNameWithoutColon.test.ts
85 lines (71 loc) · 2.48 KB
/
PrometheusMetricNameWithoutColon.test.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
import { assertEquals, assertThrows } from '@std/assert';
import { PrometheusMetricNameWithoutColon } from './PrometheusMetricNameWithoutColon.ts';
Deno.test('PrometheusMetricNameWithoutColon - validates standard Prometheus metric names without colons', () =>
{
const validInputs = [
// Standard metrics
'http_requests_total',
'prometheus_http_requests_total',
'process_cpu_seconds_total',
// With numbers (but not leading)
'http_2xx_requests_total',
'tcp_port_8080_connections',
// With underscores
'_leading_underscore',
'trailing_underscore_',
'multiple__underscores',
// Single character
'a',
'_',
];
for (const input of validInputs)
{
const result = PrometheusMetricNameWithoutColon.try(input);
assertEquals(result, input);
}
});
Deno.test('PrometheusMetricNameWithoutColon - rejects invalid metric names', () =>
{
const invalidInputs = [
// With colons (not allowed in this version)
'node:http_requests_total',
':leading_colon',
'trailing_colon:',
'multiple::colons',
// Invalid starts
'1_metric', // starts with number
'123abc', // starts with number
// Invalid characters
'http-requests-total', // hyphens not allowed
'http.requests.total', // dots not allowed
'http requests total', // spaces not allowed
'http$requests', // special characters
'metric/sec', // slashes
'metric@host', // at symbol
// Empty
'',
// Unicode/emoji
'http_requests_😀',
'métric_name',
];
for (const input of invalidInputs)
{
const result = PrometheusMetricNameWithoutColon.try(input);
assertEquals(result, undefined);
}
});
Deno.test('PrometheusMetricNameWithoutColon - assert throws with descriptive message', () =>
{
assertThrows(
() => PrometheusMetricNameWithoutColon.assert('invalid:metric:name'),
Error,
'Invalid string did not pass validation: Supplied value "invalid:metric:name" is not valid for validator "PrometheusMetricNameWithoutColon" (must start with a letter or underscore, followed by letters, digits, or underscores).',
);
});
Deno.test('PrometheusMetricNameWithoutColon - type safety', () =>
{
// @ts-expect-error Type 'string' is not assignable to type 'PrometheusMetricNameWithoutColon'
const _invalid: PrometheusMetricNameWithoutColon = 'invalid:metric:name';
const valid: PrometheusMetricNameWithoutColon = PrometheusMetricNameWithoutColon.assert('valid_metric_name');
assertEquals(valid, 'valid_metric_name');
});