|
1 | | -import { describe, expect, it, vi } from 'vitest' |
| 1 | +import { BufferSource } from 'mediabunny' |
| 2 | +import { readFileSync } from 'node:fs' |
| 3 | +import { join } from 'node:path' |
| 4 | +import { afterEach, describe, expect, it, vi } from 'vitest' |
2 | 5 |
|
3 | | -import { api } from '@/scripts/api' |
4 | | -import { fetchVideoMetadata } from '@/utils/videoMetadataUtil' |
| 6 | +import { |
| 7 | + extractVideoMetadata, |
| 8 | + fetchVideoMetadata, |
| 9 | + snapToStandardFrameRate |
| 10 | +} from '@/utils/videoMetadataUtil' |
5 | 11 |
|
6 | 12 | vi.mock('@/scripts/api', () => ({ |
7 | 13 | api: { |
8 | | - fetchApi: vi.fn(), |
9 | 14 | apiURL: (path: string) => `http://localhost:8188/api${path}` |
10 | 15 | } |
11 | 16 | })) |
12 | 17 |
|
13 | | -const metadata = { |
14 | | - fps: 30, |
15 | | - duration: 4.2, |
16 | | - frame_count: 126, |
17 | | - width: 1920, |
18 | | - height: 1080, |
19 | | - size: 1024 |
| 18 | +function bufferSource(bytes: Uint8Array) { |
| 19 | + return new BufferSource(bytes) |
20 | 20 | } |
21 | 21 |
|
22 | | -function mockResponse(ok: boolean, body?: unknown) { |
23 | | - vi.mocked(api.fetchApi).mockResolvedValueOnce({ |
24 | | - ok, |
25 | | - json: async () => body |
26 | | - } as Response) |
| 22 | +function readFixture(name: string): Uint8Array { |
| 23 | + return new Uint8Array( |
| 24 | + readFileSync(join(process.cwd(), 'src', 'utils', '__fixtures__', name)) |
| 25 | + ) |
27 | 26 | } |
28 | 27 |
|
29 | | -describe('fetchVideoMetadata', () => { |
30 | | - it('queries the backend with the view resource params', async () => { |
31 | | - mockResponse(true, metadata) |
| 28 | +describe('extractVideoMetadata', () => { |
| 29 | + it('reads dimensions, duration, fps and size from an mp4', async () => { |
| 30 | + const bytes = readFixture('tiny.mp4') |
32 | 31 |
|
33 | | - const result = await fetchVideoMetadata( |
34 | | - 'http://localhost:8188/api/view?filename=a.mp4&subfolder=clips&type=input&rand=0.1' |
35 | | - ) |
| 32 | + const result = await extractVideoMetadata(bufferSource(bytes)) |
36 | 33 |
|
37 | | - expect(api.fetchApi).toHaveBeenCalledWith( |
38 | | - '/video_metadata?filename=a.mp4&subfolder=clips&type=input', |
39 | | - { signal: undefined } |
40 | | - ) |
41 | | - expect(result).toEqual(metadata) |
| 34 | + expect(result).toBeDefined() |
| 35 | + expect(result?.width).toBe(64) |
| 36 | + expect(result?.height).toBe(48) |
| 37 | + expect(result?.duration).toBeCloseTo(12 / 8, 1) |
| 38 | + expect(result?.fps).toBeCloseTo(8, 1) |
| 39 | + expect(result?.size).toBe(bytes.byteLength) |
| 40 | + }) |
| 41 | + |
| 42 | + it('reads a webm container', async () => { |
| 43 | + const bytes = readFixture('tiny.webm') |
| 44 | + |
| 45 | + const result = await extractVideoMetadata(bufferSource(bytes)) |
| 46 | + |
| 47 | + expect(result).toBeDefined() |
| 48 | + expect(result?.width).toBe(64) |
| 49 | + expect(result?.height).toBe(48) |
| 50 | + expect(result?.duration).toBeCloseTo(12 / 8, 1) |
| 51 | + expect(result?.fps).toBeCloseTo(8, 1) |
42 | 52 | }) |
43 | 53 |
|
44 | | - it('returns undefined for non-view urls without fetching', async () => { |
45 | | - const result = await fetchVideoMetadata('blob:abc') |
| 54 | + it('returns undefined for non-video bytes', async () => { |
| 55 | + const bytes = new TextEncoder().encode('not actually a video file') |
| 56 | + |
| 57 | + const result = await extractVideoMetadata(bufferSource(bytes)) |
46 | 58 |
|
47 | | - expect(api.fetchApi).not.toHaveBeenCalled() |
48 | 59 | expect(result).toBeUndefined() |
49 | 60 | }) |
50 | 61 |
|
51 | | - it('rejects view urls from untrusted origins without fetching', async () => { |
52 | | - const result = await fetchVideoMetadata( |
53 | | - 'https://attacker.invalid/view?filename=a.mp4' |
| 62 | + it('returns undefined when the signal is already aborted', async () => { |
| 63 | + const controller = new AbortController() |
| 64 | + controller.abort() |
| 65 | + |
| 66 | + const result = await extractVideoMetadata( |
| 67 | + bufferSource(readFixture('tiny.mp4')), |
| 68 | + controller.signal |
54 | 69 | ) |
55 | 70 |
|
56 | | - expect(api.fetchApi).not.toHaveBeenCalled() |
57 | 71 | expect(result).toBeUndefined() |
58 | 72 | }) |
59 | 73 |
|
60 | | - it('returns undefined when the backend lacks the endpoint', async () => { |
61 | | - mockResponse(false) |
| 74 | + it('reports a null size when the source size is unknown', async () => { |
| 75 | + const source = bufferSource(readFixture('tiny.mp4')) |
| 76 | + source.getSizeOrNull = async () => null |
62 | 77 |
|
63 | | - const result = await fetchVideoMetadata('/api/view?filename=a.mp4') |
| 78 | + const result = await extractVideoMetadata(source) |
64 | 79 |
|
65 | | - expect(result).toBeUndefined() |
| 80 | + expect(result).toBeDefined() |
| 81 | + expect(result?.size).toBeNull() |
| 82 | + expect(result?.width).toBe(64) |
66 | 83 | }) |
| 84 | +}) |
67 | 85 |
|
68 | | - it('returns undefined for malformed responses', async () => { |
69 | | - mockResponse(true, { unexpected: true }) |
| 86 | +describe('snapToStandardFrameRate', () => { |
| 87 | + it('snaps near-standard measurements to the exact rate', () => { |
| 88 | + expect(snapToStandardFrameRate(29.972)).toBe(30_000 / 1_001) |
| 89 | + expect(snapToStandardFrameRate(23.98)).toBe(24_000 / 1_001) |
| 90 | + expect(snapToStandardFrameRate(30.005)).toBe(30) |
| 91 | + expect(snapToStandardFrameRate(59.945)).toBe(60_000 / 1_001) |
| 92 | + }) |
70 | 93 |
|
71 | | - const result = await fetchVideoMetadata('/api/view?filename=a.mp4') |
| 94 | + it('leaves non-standard rates unchanged', () => { |
| 95 | + expect(snapToStandardFrameRate(8)).toBe(8) |
| 96 | + expect(snapToStandardFrameRate(33.3)).toBe(33.3) |
| 97 | + }) |
| 98 | +}) |
72 | 99 |
|
73 | | - expect(result).toBeUndefined() |
| 100 | +describe('fetchVideoMetadata url gating', () => { |
| 101 | + afterEach(() => { |
| 102 | + vi.unstubAllGlobals() |
| 103 | + }) |
| 104 | + |
| 105 | + it('extracts metadata from a trusted view url', async () => { |
| 106 | + const bytes = readFixture('tiny.mp4') |
| 107 | + vi.stubGlobal( |
| 108 | + 'fetch', |
| 109 | + vi.fn(async () => new Response(new Uint8Array(bytes).buffer)) |
| 110 | + ) |
| 111 | + |
| 112 | + const result = await fetchVideoMetadata( |
| 113 | + 'http://localhost:8188/api/view?filename=tiny.mp4&type=input' |
| 114 | + ) |
| 115 | + |
| 116 | + expect(result).toBeDefined() |
| 117 | + expect(result?.width).toBe(64) |
| 118 | + expect(result?.height).toBe(48) |
| 119 | + expect(result?.fps).toBeCloseTo(8, 1) |
| 120 | + }) |
| 121 | + |
| 122 | + it('returns undefined for non-view urls', async () => { |
| 123 | + expect(await fetchVideoMetadata('blob:abc')).toBeUndefined() |
| 124 | + expect( |
| 125 | + await fetchVideoMetadata('http://localhost:8188/api/other?filename=a.mp4') |
| 126 | + ).toBeUndefined() |
| 127 | + }) |
| 128 | + |
| 129 | + it('returns undefined for view urls without a filename', async () => { |
| 130 | + expect( |
| 131 | + await fetchVideoMetadata('http://localhost:8188/api/view?type=input') |
| 132 | + ).toBeUndefined() |
| 133 | + }) |
| 134 | + |
| 135 | + it('rejects view urls from untrusted origins', async () => { |
| 136 | + expect( |
| 137 | + await fetchVideoMetadata('https://attacker.invalid/view?filename=a.mp4') |
| 138 | + ).toBeUndefined() |
74 | 139 | }) |
75 | 140 | }) |
0 commit comments