79 行
2.8 KiB
TypeScript
79 行
2.8 KiB
TypeScript
import {
|
|
negotiateOptimalMediaUrl,
|
|
MediaNegotiationContext,
|
|
SupportedImageFormat,
|
|
} from '../../../src/modules/card/service/media-negotiator';
|
|
|
|
describe('Point 48: 移动微官网现代图片自适应协商与弱网资源压缩', () => {
|
|
const originalUrl = 'https://cdn.omnicard.ai/uploads/cards/vip-avatar.png';
|
|
|
|
describe('1. 格式协商 (Image Format Negotiation: AVIF > WebP > JPEG/PNG)', () => {
|
|
it('客户端声明支持 AVIF 时应优先协商为 AVIF 格式', () => {
|
|
const ctx: MediaNegotiationContext = {
|
|
acceptHeader: 'image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8',
|
|
networkType: '4g',
|
|
};
|
|
|
|
const result = negotiateOptimalMediaUrl(originalUrl, ctx);
|
|
expect(result.format).toBe('avif');
|
|
expect(result.optimizedUrl).toContain('format=avif');
|
|
});
|
|
|
|
it('客户端不支持 AVIF 但支持 WebP 时应协商为 WebP', () => {
|
|
const ctx: MediaNegotiationContext = {
|
|
acceptHeader: 'image/webp,*/*',
|
|
networkType: 'wifi',
|
|
};
|
|
|
|
const result = negotiateOptimalMediaUrl(originalUrl, ctx);
|
|
expect(result.format).toBe('webp');
|
|
expect(result.optimizedUrl).toContain('format=webp');
|
|
});
|
|
|
|
it('旧版客户端不包含新现代格式时保留原有格式或转为 jpg', () => {
|
|
const ctx: MediaNegotiationContext = {
|
|
acceptHeader: 'image/png,image/*,*/*',
|
|
};
|
|
|
|
const result = negotiateOptimalMediaUrl(originalUrl, ctx);
|
|
expect(result.format).toBe('png');
|
|
expect(result.optimizedUrl).toContain('format=png');
|
|
});
|
|
});
|
|
|
|
describe('2. 网络环境与屏幕自适应 (Network & DPR Adaptive Scaling)', () => {
|
|
it('弱网 (2g/3g) 环境应自动降低图片质量与宽度以保障极致加载速度', () => {
|
|
const ctx: MediaNegotiationContext = {
|
|
acceptHeader: 'image/webp,*/*',
|
|
networkType: '3g',
|
|
targetWidth: 800,
|
|
};
|
|
|
|
const result = negotiateOptimalMediaUrl(originalUrl, ctx);
|
|
expect(result.quality).toBeLessThanOrEqual(70);
|
|
expect(result.width).toBeLessThanOrEqual(600);
|
|
expect(result.optimizedUrl).toContain(`w=${result.width}`);
|
|
expect(result.optimizedUrl).toContain(`q=${result.quality}`);
|
|
});
|
|
|
|
it('高速 WiFi 且高 DPR (dpr >= 2) 时应保障高清晰度', () => {
|
|
const ctx: MediaNegotiationContext = {
|
|
acceptHeader: 'image/avif,image/webp',
|
|
networkType: 'wifi',
|
|
targetWidth: 400,
|
|
dpr: 2,
|
|
};
|
|
|
|
const result = negotiateOptimalMediaUrl(originalUrl, ctx);
|
|
expect(result.width).toBe(800); // 400 * 2 = 800
|
|
expect(result.quality).toBeGreaterThanOrEqual(85);
|
|
expect(result.optimizedUrl).toContain('w=800');
|
|
});
|
|
|
|
it('无效或空 URL 时应安全回退', () => {
|
|
const res = negotiateOptimalMediaUrl('', {});
|
|
expect(res.optimizedUrl).toBe('');
|
|
});
|
|
});
|
|
});
|