feat(fleet): show instance resource metrics

This commit is contained in:
chick
2026-07-19 00:22:23 +08:00
parent 2411801240
commit 23f18963ef
12 changed files with 312 additions and 30 deletions
@@ -0,0 +1,48 @@
import { describe, expect, it } from 'vitest';
import { parseSim, parseStats } from './instance-resource-service.js';
const response = (body: unknown) => ({
status: 200,
headers: {},
body: JSON.stringify(body),
});
describe('instance resource allowlist parsing', () => {
it('extracts CPU, memory and only the highest valid temperature', () => {
expect(
parseStats(
response({
data: {
cpu_load: { load_percent: 23.4, secret: 'drop' },
memory: { used_percent: 67.8, total_bytes: 123 },
temperature: [
{ label: 'a', temperature: 41.2 },
{ label: 'b', temperature: 52.6 },
{ label: 'bad', temperature: 900 },
],
},
}),
),
).toEqual({ cpuPercent: 23.4, memoryPercent: 67.8, maxTemperatureCelsius: 52.6 });
});
it('extracts, validates, deduplicates and bounds phone numbers only', () => {
expect(
parseSim(
response({
data: {
phone_numbers: ['+86 138-0000-0000', '+86 138-0000-0000', 'bad<script>'],
imsi: 'drop',
iccid: 'drop',
},
}),
),
).toEqual({ phoneNumbers: ['+86 138-0000-0000'] });
});
it('fails closed for malformed and oversized payloads', () => {
expect(parseStats({ status: 200, headers: {}, body: '{' })).toEqual({});
expect(parseSim({ status: 200, headers: {}, body: 'x'.repeat(40_000) })).toEqual({});
});
});