gongxiangp2/lib/__tests__/system-performance.test.ts
chenpei e864fc3249 fix: 系统性能面板改回浅色,并纠正内存和队列口径
内存改读应用容器上限,不再把整机 90% 当成素材服务快满了。
转码队列改看素材状态和租约,上传停住的会话单独标出。
2026-08-17 13:35:09 +08:00

109 lines
3.1 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import {
classifyTranscodeMaterial,
classifyUploadSession,
resolveProcessMemory,
} from '../system-performance'
describe('resolveProcessMemory', () => {
test('优先使用小于主机内存的容器上限,避免把 TrueNAS 整机 90% 当成应用快满了', () => {
const stats = resolveProcessMemory({
containerUsedBytes: 162_533_376,
containerLimitBytes: 6_442_450_944,
hostTotalBytes: 16_281_152_000,
hostFreeBytes: 1_702_132_000,
})
expect(stats.source).toBe('container')
expect(stats.totalBytes).toBe(6_442_450_944)
expect(stats.usedBytes).toBe(162_533_376)
expect(stats.percentage).toBe(3)
expect(stats.hostPercentage).toBe(90)
})
test('没有容器上限时回退到主机口径', () => {
const stats = resolveProcessMemory({
containerUsedBytes: null,
containerLimitBytes: null,
hostTotalBytes: 16_000_000_000,
hostFreeBytes: 1_600_000_000,
})
expect(stats.source).toBe('host')
expect(stats.usedBytes).toBe(14_400_000_000)
expect(stats.percentage).toBe(90)
})
})
describe('classifyUploadSession', () => {
test('未完成且最近有活动记为处理中', () => {
expect(
classifyUploadSession({
progress: 40,
inactiveMs: 20_000,
activeThresholdMs: 5 * 60 * 1000,
maxShowThresholdMs: 60 * 60 * 1000,
}),
).toBe('processing')
})
test('未完成但已停一会的会话记为等待,不再假装还有一条服务器排队', () => {
expect(
classifyUploadSession({
progress: 40,
inactiveMs: 12 * 60 * 1000,
activeThresholdMs: 5 * 60 * 1000,
maxShowThresholdMs: 60 * 60 * 1000,
}),
).toBe('waiting')
})
test('完成或过久无活动的会话不展示', () => {
expect(
classifyUploadSession({
progress: 100,
inactiveMs: 1000,
activeThresholdMs: 5 * 60 * 1000,
maxShowThresholdMs: 60 * 60 * 1000,
}),
).toBe('hidden')
expect(
classifyUploadSession({
progress: 10,
inactiveMs: 2 * 60 * 60 * 1000,
activeThresholdMs: 5 * 60 * 1000,
maxShowThresholdMs: 60 * 60 * 1000,
}),
).toBe('hidden')
})
})
describe('classifyTranscodeMaterial', () => {
const now = new Date('2026-08-17T05:00:00.000Z')
test('素材仍是 processing 且租约有效才算正在转码', () => {
expect(
classifyTranscodeMaterial({
status: 'processing',
leaseExpiresAt: new Date('2026-08-17T05:04:00.000Z'),
now,
}),
).toBe('processing')
})
test('不读阶段表里的陈旧 processing;素材已 pending 或租约过期都算等待', () => {
expect(classifyTranscodeMaterial({ status: 'pending', now })).toBe('waiting')
expect(
classifyTranscodeMaterial({
status: 'processing',
leaseExpiresAt: new Date('2026-08-17T04:50:00.000Z'),
now,
}),
).toBe('waiting')
})
test('已完成或空状态不进面板', () => {
expect(classifyTranscodeMaterial({ status: 'done', now })).toBe('hidden')
expect(classifyTranscodeMaterial({ status: null, now })).toBe('hidden')
})
})