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
@@ -3,25 +3,34 @@ import { describe, expect, it, vi } from 'vitest';
import { createFleetApiDataSource } from './fleet-api-data-source.js';
describe('Fleet API data source', () => {
it('parses the frozen InstancePage envelope and maps origin to the Fleet URL', async () => {
it('loads instances and their allowlisted resource summaries', async () => {
const fetcher = vi.fn(
async () =>
async (input: RequestInfo | URL) =>
new Response(
JSON.stringify({
items: [
{
id: 'alpha',
name: 'Alpha',
origin: 'https://alpha.example/admin',
tags: ['lab'],
revision: 1,
capabilityStatus: 'unknown',
freshness: 'unknown',
credentialConfigured: false,
},
],
page: { page: 1, pageSize: 20, total: 1 },
}),
JSON.stringify(
String(input).endsWith('/resources')
? {
cpuPercent: 23.4,
memoryPercent: 67.8,
maxTemperatureCelsius: 52.6,
phoneNumbers: ['13800000000'],
}
: {
items: [
{
id: 'alpha',
name: 'Alpha',
origin: 'https://alpha.example/admin',
tags: ['lab'],
revision: 1,
capabilityStatus: 'unknown',
freshness: 'unknown',
credentialConfigured: false,
},
],
page: { page: 1, pageSize: 20, total: 1 },
},
),
{ status: 200, headers: { 'content-type': 'application/json' } },
),
);
@@ -29,10 +38,20 @@ describe('Fleet API data source', () => {
expect(snapshot.instances).toEqual([
{ id: 'alpha', name: 'Alpha', url: 'https://alpha.example/admin', tags: ['lab'] },
]);
expect(snapshot.statuses.get('alpha')?.summary?.resources).toEqual({
cpuPercent: 23.4,
memoryPercent: 67.8,
maxTemperatureCelsius: 52.6,
phoneNumbers: ['13800000000'],
});
expect(fetcher).toHaveBeenCalledWith(
'/api/v1/instances',
expect.objectContaining({ credentials: 'same-origin' }),
);
expect(fetcher).toHaveBeenCalledWith(
'/api/v1/instances/alpha/resources',
expect.objectContaining({ credentials: 'same-origin' }),
);
});
it('rejects legacy or malformed envelopes instead of rendering a false empty fleet', async () => {