125 lines
4.9 KiB
TypeScript
125 lines
4.9 KiB
TypeScript
import { describe, expect, it } from 'vitest';
|
|
import {
|
|
classifyArtifact,
|
|
evaluateBudgetTotals,
|
|
evaluateMediaAssetBudget,
|
|
evaluateRuntimeBudgetEvidence,
|
|
PERFORMANCE_BUDGETS,
|
|
} from '../../scripts/lib/bundle-budgets.mjs';
|
|
|
|
describe('release budgets', () => {
|
|
it('pins every exact section 14 threshold', () => {
|
|
expect(PERFORMANCE_BUDGETS).toMatchObject({
|
|
initialHtmlCriticalCss: 100 * 1024,
|
|
initialApplicationJs: 120 * 1024,
|
|
heroPoster: 220 * 1024,
|
|
initialFonts: 120 * 1024,
|
|
firstLiveScene: 650 * 1024,
|
|
projectVideo: 2.5 * 1024 * 1024,
|
|
projectVideoDurationSeconds: 10,
|
|
projectVideoWidth: 1280,
|
|
projectVideoHeight: 720,
|
|
projectStill: 400 * 1024,
|
|
chart: 100 * 1024,
|
|
deferredProjectVisuals: 2.5 * 1024 * 1024,
|
|
highTierExtras: 750 * 1024,
|
|
deferredNonVideo: 3.5 * 1024 * 1024,
|
|
imageSceneConcurrency: 2,
|
|
videoConcurrency: 1,
|
|
mobileTextureDimension: 2048,
|
|
lowTierGpuEstimate: 96 * 1024 * 1024,
|
|
highTierGpuEstimate: 192 * 1024 * 1024,
|
|
});
|
|
});
|
|
|
|
it('classifies every artifact once and keeps the scene chunk deferred', () => {
|
|
expect(classifyArtifact('index.html', '<script src="/assets/index-a.js"></script>')).toBe('initial-html');
|
|
expect(classifyArtifact('assets/index-a.css', '')).toBe('critical-css');
|
|
expect(classifyArtifact('assets/index-a.js', 'import("./environment-b.js")')).toBe('initial-application-js');
|
|
expect(classifyArtifact('assets/environment-b.js', 'three scene')).toBe('first-live-scene');
|
|
expect(classifyArtifact('assets/high-tier-b.js', 'optional bloom')).toBe('high-tier-extras');
|
|
expect(() => classifyArtifact('assets/mystery.bin', '')).toThrow(/unclassified/u);
|
|
});
|
|
|
|
it('fails exact byte boundaries and accepts equality', () => {
|
|
expect(evaluateBudgetTotals({ initialHtmlCriticalCss: 100 * 1024 }).violations).toEqual([]);
|
|
expect(evaluateBudgetTotals({ initialHtmlCriticalCss: 100 * 1024 + 1 }).violations).toContainEqual(expect.objectContaining({ budget: 'initialHtmlCriticalCss' }));
|
|
});
|
|
|
|
it('rejects an undersized transfer when its video delivery rendition is 20s at 1920x1080', () => {
|
|
const result = evaluateMediaAssetBudget({
|
|
id: 'project-demo',
|
|
mediaType: 'video/webm',
|
|
width: 1_920,
|
|
height: 1_080,
|
|
durationMs: 20_000,
|
|
}, {
|
|
mediaType: 'video/webm',
|
|
width: 1_920,
|
|
height: 1_080,
|
|
durationMs: 20_000,
|
|
}, 1_024);
|
|
|
|
expect(result.violations.map((violation) => violation.budget)).toEqual(expect.arrayContaining([
|
|
'projectVideoDurationSeconds',
|
|
'projectVideoWidth',
|
|
'projectVideoHeight',
|
|
]));
|
|
});
|
|
|
|
it('fails closed when non-empty media dimensions cannot be deterministically probed', () => {
|
|
expect(() => evaluateMediaAssetBudget({
|
|
id: 'project-still',
|
|
mediaType: 'image/webp',
|
|
width: 1_280,
|
|
height: 720,
|
|
}, null, 1_024)).toThrow(/probe evidence/u);
|
|
});
|
|
|
|
it('rejects excessive high-tier extras and request concurrency', () => {
|
|
expect(evaluateBudgetTotals({ highTierExtras: PERFORMANCE_BUDGETS.highTierExtras! + 1 }).violations)
|
|
.toContainEqual(expect.objectContaining({ budget: 'highTierExtras' }));
|
|
expect(evaluateRuntimeBudgetEvidence({
|
|
schema: 'portfolio-runtime-budget-evidence/v1',
|
|
peakRequests: { imageScene: 3, video: 2 },
|
|
tiers: [
|
|
{ tier: 'low', textures: [], geometryBytes: 0, renderTargets: [] },
|
|
{ tier: 'high', textures: [], geometryBytes: 0, renderTargets: [] },
|
|
],
|
|
}).violations.map((violation) => violation.budget)).toEqual(expect.arrayContaining([
|
|
'imageSceneConcurrency',
|
|
'videoConcurrency',
|
|
]));
|
|
});
|
|
|
|
it('derives decoded texture and GPU bytes from allocation formulas', () => {
|
|
const result = evaluateRuntimeBudgetEvidence({
|
|
schema: 'portfolio-runtime-budget-evidence/v1',
|
|
peakRequests: { imageScene: 0, video: 0 },
|
|
tiers: [
|
|
{
|
|
tier: 'low',
|
|
textures: [{ id: 'oversized', width: 4_096, height: 4_096, bytesPerPixel: 4, layers: 1, mipmapped: true }],
|
|
geometryBytes: 40 * 1024 * 1024,
|
|
renderTargets: [{ id: 'framebuffer', width: 2_048, height: 2_048, bytesPerPixel: 8, attachments: 2, samples: 1 }],
|
|
},
|
|
{ tier: 'high', textures: [], geometryBytes: 0, renderTargets: [] },
|
|
],
|
|
});
|
|
|
|
expect(result.tiers[0]?.textureBytes).toBe(Math.ceil(4_096 * 4_096 * 4 * 4 / 3));
|
|
expect(result.violations.map((violation) => violation.budget)).toEqual(expect.arrayContaining([
|
|
'mobileTextureDimension',
|
|
'lowTierGpuEstimate',
|
|
]));
|
|
});
|
|
|
|
it('fails closed when either named tier allocation ledger is absent', () => {
|
|
expect(() => evaluateRuntimeBudgetEvidence({
|
|
schema: 'portfolio-runtime-budget-evidence/v1',
|
|
peakRequests: { imageScene: 0, video: 0 },
|
|
tiers: [{ tier: 'low', textures: [], geometryBytes: 0, renderTargets: [] }],
|
|
})).toThrow(/exactly one Low and High/u);
|
|
});
|
|
});
|