-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema.test.js
More file actions
247 lines (232 loc) · 8.92 KB
/
Copy pathschema.test.js
File metadata and controls
247 lines (232 loc) · 8.92 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// Contract tests for the published JSON Schemas (public/*.schema.json) against
// the documents the probe runner shapes (buildStatus / buildHealth / a synthetic
// history). These guard the agent-facing machine contract: if the schemas drift
// from what the code emits (or vice-versa), one of these fails. To stay true to
// this zero-dependency project, a small JSON-Schema validator covering exactly
// the keywords the schemas use is embedded here rather than pulling in ajv.
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { readFileSync } from 'node:fs';
import { dirname, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import {
buildStatus,
buildHealth,
appendHistory,
shapeResult,
STATUS,
SCHEMA_VERSION,
} from '../lib/probe.js';
const __dirname = dirname(fileURLToPath(import.meta.url));
const PUBLIC = resolve(__dirname, '..', 'public');
const loadSchema = (f) => JSON.parse(readFileSync(resolve(PUBLIC, f), 'utf8'));
// --- minimal JSON-Schema (draft-2020-12 subset) validator -------------------
// Supports: type, enum, const, required, properties, additionalProperties
// (boolean OR sub-schema), items, minimum, $ref (local "#/..."), $defs. Returns
// an array of human-readable error strings (empty = valid).
function validate(schema, data, root = schema, path = '$') {
const errs = [];
if (schema.$ref) {
const target = resolveRef(root, schema.$ref);
return validate(target, data, root, path);
}
if (schema.const !== undefined && data !== schema.const) {
errs.push(
`${path}: expected const ${JSON.stringify(schema.const)}, got ${JSON.stringify(data)}`,
);
}
if (schema.enum && !schema.enum.includes(data)) {
errs.push(`${path}: ${JSON.stringify(data)} not in enum ${JSON.stringify(schema.enum)}`);
}
if (schema.type && !typeMatches(schema.type, data)) {
errs.push(`${path}: expected type ${JSON.stringify(schema.type)}, got ${jsType(data)}`);
return errs; // type mismatch — skip deeper checks
}
if (typeof data === 'number' && schema.minimum !== undefined && data < schema.minimum) {
errs.push(`${path}: ${data} < minimum ${schema.minimum}`);
}
if (
jsType(data) === 'object' &&
(schema.properties || schema.required || schema.additionalProperties !== undefined)
) {
for (const key of schema.required || []) {
if (!(key in data)) errs.push(`${path}: missing required '${key}'`);
}
const props = schema.properties || {};
for (const [key, val] of Object.entries(data)) {
if (props[key]) {
errs.push(...validate(props[key], val, root, `${path}.${key}`));
} else if (schema.additionalProperties === false) {
errs.push(`${path}: unexpected property '${key}'`);
} else if (schema.additionalProperties && typeof schema.additionalProperties === 'object') {
errs.push(...validate(schema.additionalProperties, val, root, `${path}.${key}`));
}
}
}
if (jsType(data) === 'array' && schema.items) {
data.forEach((item, i) => errs.push(...validate(schema.items, item, root, `${path}[${i}]`)));
}
return errs;
}
function resolveRef(root, ref) {
const parts = ref.replace(/^#\//, '').split('/');
let node = root;
for (const p of parts) node = node[p];
if (!node) throw new Error(`unresolvable $ref ${ref}`);
return node;
}
function jsType(v) {
if (v === null) return 'null';
if (Array.isArray(v)) return 'array';
if (Number.isInteger(v)) return 'integer';
return typeof v; // string | number | boolean | object
}
function typeMatches(type, v) {
const types = Array.isArray(type) ? type : [type];
const t = jsType(v);
// an integer also satisfies "number"
return types.some((x) => x === t || (x === 'number' && t === 'integer'));
}
// --- the actual documents the runner shapes ---------------------------------
function sampleSystems() {
return [
shapeResult({
id: 'rpc',
name: 'rpc.dig.net',
category: 'Read path',
status: STATUS.UP,
latencyMs: 120,
checkedAt: '2026-06-28T00:00:00.000Z',
detail: { kind: 'jsonrpc', method: 'dig.methods' },
url: 'https://rpc.dig.net/',
description: 'RPC',
}),
{
...shapeResult({
id: 'cdn',
name: 'cdn.dig.net',
category: 'Read path',
status: STATUS.DEGRADED,
latencyMs: 9000,
checkedAt: '2026-06-28T00:00:00.000Z',
detail: { kind: 'http', httpStatus: null, errorCode: 'TRANSPORT' },
error: 'fetch failed',
}),
excludeFromOverall: true,
},
shapeResult({
id: 'relay',
name: 'relay.dig.net',
category: 'Network',
status: STATUS.UP,
latencyMs: 70,
checkedAt: '2026-06-28T00:00:00.000Z',
detail: { kind: 'tls', host: 'relay.dig.net', port: 443 },
url: 'wss://relay.dig.net:443',
description: 'NAT-traversal relay',
}),
shapeResult({
id: 'coinset',
name: 'coinset.org ChainView',
category: 'Chia',
status: STATUS.UP,
latencyMs: 200,
checkedAt: '2026-06-28T00:00:00.000Z',
detail: { kind: 'chainview', peakHeight: 8933589, synced: true },
}),
shapeResult({
id: 'chia-mainnet',
name: 'Chia mainnet',
category: 'Chia',
status: STATUS.UP,
latencyMs: 0,
checkedAt: '2026-06-28T00:00:00.000Z',
detail: { kind: 'derived', peakHeight: 8933589, advancedBy: 5, secondsSincePrev: 90 },
}),
];
}
test('status.schema.json: a buildStatus document validates', () => {
const schema = loadSchema('status.schema.json');
const doc = buildStatus({ generatedAt: '2026-06-28T00:00:00.000Z', systems: sampleSystems() });
const errs = validate(schema, doc);
assert.deepEqual(errs, [], errs.join('\n'));
});
test('status.schema.json: a document WITH the injected $schema key still validates', () => {
const schema = loadSchema('status.schema.json');
const doc = {
$schema: 'https://status.dig.net/status.schema.json',
...buildStatus({ generatedAt: '2026-06-28T00:00:00.000Z', systems: sampleSystems() }),
};
assert.deepEqual(validate(schema, doc), []);
});
test('status.schema.json: a tls-kind record (incl. TLS_ERROR) validates', () => {
const schema = loadSchema('status.schema.json');
const doc = buildStatus({
generatedAt: '2026-06-28T00:00:00.000Z',
systems: [
shapeResult({
id: 'relay',
name: 'relay.dig.net',
category: 'Network',
status: STATUS.UP,
latencyMs: 70,
checkedAt: '2026-06-28T00:00:00.000Z',
detail: { kind: 'tls', host: 'relay.dig.net', port: 443 },
url: 'wss://relay.dig.net:443',
}),
shapeResult({
id: 'relay2',
name: 'relay2',
category: 'Network',
status: STATUS.DOWN,
latencyMs: 12000,
checkedAt: '2026-06-28T00:00:00.000Z',
detail: { kind: 'tls', host: 'relay.dig.net', port: 443, errorCode: 'TLS_ERROR' },
error: 'cert invalid',
}),
],
});
assert.deepEqual(validate(schema, doc), [], validate(schema, doc).join('\n'));
});
test('status.schema.json: pins schemaVersion to SCHEMA_VERSION', () => {
const schema = loadSchema('status.schema.json');
assert.equal(schema.properties.schemaVersion.const, SCHEMA_VERSION);
});
test('health.schema.json: a buildHealth document validates', () => {
const schema = loadSchema('health.schema.json');
const doc = buildHealth(
buildStatus({ generatedAt: '2026-06-28T00:00:00.000Z', systems: sampleSystems() }),
);
assert.deepEqual(validate(schema, doc), []);
});
test('health.schema.json: a document WITH the injected $schema key still validates', () => {
const schema = loadSchema('health.schema.json');
const doc = {
$schema: 'https://status.dig.net/health.schema.json',
...buildHealth(buildStatus({ generatedAt: 't', systems: sampleSystems() })),
};
assert.deepEqual(validate(schema, doc), []);
});
test('history.schema.json: an appendHistory document validates', () => {
const schema = loadSchema('history.schema.json');
const doc = buildStatus({ generatedAt: '2026-06-28T00:00:00.000Z', systems: sampleSystems() });
let history = appendHistory({}, doc);
// simulate the runner stamping peakHeight onto the coinset point
history.coinset[history.coinset.length - 1].peakHeight = 8933589;
assert.deepEqual(validate(schema, history), []);
});
test('schemas: reject an out-of-enum status (validator + schema actually bite)', () => {
const schema = loadSchema('health.schema.json');
const bad = {
schemaVersion: SCHEMA_VERSION,
overall: 'sideways',
generatedAt: 't',
systems: { rpc: 'up' },
};
assert.ok(validate(schema, bad).length > 0);
});
test('schemas: all three are valid JSON with the expected $id', () => {
assert.equal(loadSchema('status.schema.json').$id, 'https://status.dig.net/status.schema.json');
assert.equal(loadSchema('health.schema.json').$id, 'https://status.dig.net/health.schema.json');
assert.equal(loadSchema('history.schema.json').$id, 'https://status.dig.net/history.schema.json');
});