feat(storage): add managed storage controls
Enforce a global log quota with configurable retention and observable degradation. Add authenticated storage reporting and safe cleanup controls to the settings UI. Protect active stream transfers and isolate parallel test resources.
This commit is contained in:
33 files changed
+5115
-136
No files matched your search
@@ -0,0 +1,108 @@
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||
import { createApiClient } from '../src/lib/api/client';
|
||||
|
||||
const tokenStore = {
|
||||
load: () => 'test-token',
|
||||
save: () => undefined,
|
||||
};
|
||||
|
||||
afterEach(() => {
|
||||
vi.unstubAllGlobals();
|
||||
});
|
||||
|
||||
describe('storage API client', () => {
|
||||
it('reads the authenticated storage overview', async () => {
|
||||
const payload = {
|
||||
settings: {
|
||||
saved: { logMaxTotalMb: 1024, logRetainDays: 7, logPerUin: false },
|
||||
effective: { logMaxTotalMb: 1024, logRetainDays: 7, logPerUin: false },
|
||||
envOverrides: [],
|
||||
},
|
||||
snapshot: {
|
||||
logs: {
|
||||
state: 'healthy',
|
||||
totalBytes: 0,
|
||||
maxTotalBytes: 1024 * 1024 * 1024,
|
||||
retainDays: 7,
|
||||
perUinEnabled: false,
|
||||
fileCount: 1,
|
||||
activeFileCount: 1,
|
||||
droppedLines: 0,
|
||||
},
|
||||
temporary: { totalBytes: 0, fileCount: 0, activeItemCount: 0 },
|
||||
accounts: [],
|
||||
totals: { logsBytes: 0, temporaryBytes: 0, accountDataBytes: 0, managedBytes: 0 },
|
||||
},
|
||||
lastCleanup: null,
|
||||
};
|
||||
const fetchMock = vi.fn(async () => new Response(JSON.stringify(payload), {
|
||||
status: 200,
|
||||
headers: { 'content-type': 'application/json' },
|
||||
}));
|
||||
vi.stubGlobal('fetch', fetchMock);
|
||||
|
||||
const client = createApiClient({ tokenStore });
|
||||
await expect(client.storage.get()).resolves.toEqual(payload);
|
||||
expect(fetchMock).toHaveBeenCalledWith('/api/system/storage', {
|
||||
headers: { Authorization: 'Bearer test-token' },
|
||||
});
|
||||
});
|
||||
|
||||
it('posts a partial log-settings patch', async () => {
|
||||
const fetchMock = vi.fn(async () => new Response(JSON.stringify({
|
||||
success: true,
|
||||
settings: {
|
||||
saved: { logMaxTotalMb: 1024, logRetainDays: 0, logPerUin: false },
|
||||
effective: { logMaxTotalMb: 1024, logRetainDays: 0, logPerUin: false },
|
||||
envOverrides: [],
|
||||
},
|
||||
status: { state: 'healthy' },
|
||||
snapshot: {},
|
||||
}), { status: 200, headers: { 'content-type': 'application/json' } }));
|
||||
vi.stubGlobal('fetch', fetchMock);
|
||||
|
||||
const client = createApiClient({ tokenStore });
|
||||
await client.storage.saveSettings({ logRetainDays: 0 });
|
||||
|
||||
expect(fetchMock).toHaveBeenCalledWith('/api/system/storage/settings', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({ logRetainDays: 0 }),
|
||||
headers: {
|
||||
Authorization: 'Bearer test-token',
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it('returns a structured partial cleanup result from an HTTP 500 response', async () => {
|
||||
const payload = {
|
||||
success: false,
|
||||
message: '部分文件清理失败,请检查失败明细',
|
||||
scope: 'temporary',
|
||||
cleanup: {
|
||||
deletedFiles: 1,
|
||||
freedBytes: 5,
|
||||
skippedActiveItems: 0,
|
||||
failures: [{ item: 'locked.bin', message: 'permission denied' }],
|
||||
},
|
||||
snapshot: {},
|
||||
lastCleanup: {
|
||||
at: '2026-07-24T04:00:00.000Z',
|
||||
scope: 'temporary',
|
||||
accountScope: 'global',
|
||||
deletedFiles: 1,
|
||||
freedBytes: 5,
|
||||
failureCount: 1,
|
||||
skippedActiveItems: 0,
|
||||
failures: [{ item: 'locked.bin', message: 'permission denied' }],
|
||||
},
|
||||
};
|
||||
vi.stubGlobal('fetch', vi.fn(async () => new Response(JSON.stringify(payload), {
|
||||
status: 500,
|
||||
headers: { 'content-type': 'application/json' },
|
||||
})));
|
||||
|
||||
const client = createApiClient({ tokenStore });
|
||||
await expect(client.storage.cleanup({ scope: 'temporary' })).resolves.toEqual(payload);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,50 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import {
|
||||
buildLogSettingsPatch,
|
||||
formatBytes,
|
||||
isAllAccountsConfirmation,
|
||||
logStoragePresentation,
|
||||
} from '../src/lib/storage-presentation';
|
||||
|
||||
describe('storage presentation helpers', () => {
|
||||
it('omits environment-locked and unchanged fields from a settings patch', () => {
|
||||
expect(buildLogSettingsPatch(
|
||||
{ logMaxTotalMb: 2048, logRetainDays: 0, logPerUin: true },
|
||||
{ logMaxTotalMb: 1024, logRetainDays: 7, logPerUin: false },
|
||||
['logMaxTotalMb'],
|
||||
)).toEqual({
|
||||
logRetainDays: 0,
|
||||
logPerUin: true,
|
||||
});
|
||||
});
|
||||
|
||||
it('prioritizes degraded state over capacity percentage and warns at 80 percent', () => {
|
||||
expect(logStoragePresentation({
|
||||
state: 'degraded',
|
||||
totalBytes: 10,
|
||||
maxTotalBytes: 100,
|
||||
})).toMatchObject({ label: '磁盘写入已暂停', tone: 'danger', percent: 10 });
|
||||
expect(logStoragePresentation({
|
||||
state: 'healthy',
|
||||
totalBytes: 80,
|
||||
maxTotalBytes: 100,
|
||||
})).toMatchObject({ label: '接近上限', tone: 'warning', percent: 80 });
|
||||
expect(logStoragePresentation({
|
||||
state: 'healthy',
|
||||
totalBytes: 20,
|
||||
maxTotalBytes: 100,
|
||||
})).toMatchObject({ label: '正常', tone: 'success', percent: 20 });
|
||||
});
|
||||
|
||||
it('formats byte counts without exposing implementation units', () => {
|
||||
expect(formatBytes(0)).toBe('0 B');
|
||||
expect(formatBytes(1536)).toBe('1.5 KB');
|
||||
expect(formatBytes(1024 * 1024)).toBe('1 MB');
|
||||
});
|
||||
|
||||
it('requires the exact all-account confirmation phrase', () => {
|
||||
expect(isAllAccountsConfirmation('清理全部账号')).toBe(true);
|
||||
expect(isAllAccountsConfirmation(' 清理全部账号 ')).toBe(false);
|
||||
expect(isAllAccountsConfirmation('确认')).toBe(false);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user