-
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathavatar-snapshot.test.js
More file actions
116 lines (106 loc) · 3.54 KB
/
Copy pathavatar-snapshot.test.js
File metadata and controls
116 lines (106 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Tests for the avatar snapshot capture module.
//
// The actual WebGL → JPEG pipeline can't be exercised in jsdom (no GPU, no
// real canvas), so this suite covers the contract surface: precondition
// validation, threshold constants, error propagation. The end-to-end render
// path is exercised by the customizer in a real browser.
import { describe, it, expect } from 'vitest';
import {
captureSnapshotBlob,
uploadAvatarSnapshot,
SNAPSHOT_CONSTANTS,
} from '../src/voice/avatar-snapshot.js';
describe('captureSnapshotBlob — preconditions', () => {
it('rejects when scene is missing', () => {
expect(() => captureSnapshotBlob(null)).toThrow(/required/i);
expect(() => captureSnapshotBlob({})).toThrow(/required/i);
expect(() => captureSnapshotBlob({ renderer: {}, scene: {} })).toThrow(/required/i);
});
it('rejects when renderer.domElement is not a canvas', () => {
const fakeScene = {
renderer: {
render: () => {},
domElement: { /* no toBlob */ },
},
scene: {},
camera: {},
};
expect(() => captureSnapshotBlob(fakeScene)).toThrow(/HTMLCanvasElement/);
});
it('rejects blank frames via the MIN_BYTES floor', async () => {
const fakeScene = {
renderer: {
render: () => {},
domElement: {
toBlob: (cb) => cb(new Blob(['tiny'], { type: 'image/jpeg' })),
},
},
scene: {},
camera: {},
};
await expect(captureSnapshotBlob(fakeScene)).rejects.toThrow(/blank/i);
});
it('rejects oversize frames via the MAX_BYTES ceiling', async () => {
const huge = new Uint8Array(SNAPSHOT_CONSTANTS.MAX_BYTES + 10);
const fakeScene = {
renderer: {
render: () => {},
domElement: {
toBlob: (cb) => cb(new Blob([huge], { type: 'image/jpeg' })),
},
},
scene: {},
camera: {},
};
await expect(captureSnapshotBlob(fakeScene)).rejects.toThrow(/too large/i);
});
it('rejects when canvas.toBlob returns null', async () => {
const fakeScene = {
renderer: {
render: () => {},
domElement: {
toBlob: (cb) => cb(null),
},
},
scene: {},
camera: {},
};
await expect(captureSnapshotBlob(fakeScene)).rejects.toThrow(/null/i);
});
it('forces a render call before reading the buffer', async () => {
let rendered = false;
const valid = new Uint8Array(SNAPSHOT_CONSTANTS.MIN_BYTES + 100);
const fakeScene = {
renderer: {
render: () => { rendered = true; },
domElement: {
toBlob: (cb) => cb(new Blob([valid], { type: 'image/jpeg' })),
},
},
scene: {},
camera: {},
};
await captureSnapshotBlob(fakeScene);
expect(rendered).toBe(true);
});
});
describe('uploadAvatarSnapshot — preconditions', () => {
it('rejects when avatarId is missing', async () => {
await expect(uploadAvatarSnapshot({ scene: {} })).rejects.toThrow(/avatarId required/);
await expect(uploadAvatarSnapshot({ avatarId: '', scene: {} })).rejects.toThrow(/avatarId required/);
});
});
describe('SNAPSHOT_CONSTANTS', () => {
it('exposes the three guardrails', () => {
expect(SNAPSHOT_CONSTANTS.JPEG_QUALITY).toBeGreaterThan(0.5);
expect(SNAPSHOT_CONSTANTS.JPEG_QUALITY).toBeLessThanOrEqual(1);
expect(SNAPSHOT_CONSTANTS.MIN_BYTES).toBeGreaterThan(0);
expect(SNAPSHOT_CONSTANTS.MAX_BYTES).toBeGreaterThan(SNAPSHOT_CONSTANTS.MIN_BYTES);
});
it('MAX_BYTES matches the server-side cap (2 MB)', () => {
// thumbnailPresignSchema in api/avatars/_actions.js declares
// size_bytes.max(2 * 1024 * 1024). Keep these in sync — a mismatch
// produces confusing client-side success followed by server 400s.
expect(SNAPSHOT_CONSTANTS.MAX_BYTES).toBe(2 * 1024 * 1024);
});
});