A calm place to write long-form, and publish it to the open social web.
skypress.blog/
1import { describe, expect, it, vi } from 'vitest';
2import type { Agent } from '@atproto/api';
3import {
4 uploadImageBlob,
5 ImageValidationError,
6 PUBLICATION_ICON_MAX_BYTES,
7} from './uploadImage';
8
9function fileOf( type: string, size: number, name = 'logo.png' ): File {
10 const file = new File( [ 'x' ], name, { type } );
11 Object.defineProperty( file, 'size', { value: size } );
12 return file;
13}
14
15function agentUploading( blob: Record< string, unknown > ): Agent {
16 return { uploadBlob: vi.fn().mockResolvedValue( { data: { blob } } ) } as unknown as Agent;
17}
18
19describe( 'uploadImageBlob', () => {
20 it( 'rejects non-image files without uploading', async () => {
21 const agent = agentUploading( {} );
22 await expect(
23 uploadImageBlob( agent, fileOf( 'application/pdf', 10 ) )
24 ).rejects.toBeInstanceOf( ImageValidationError );
25 expect( ( agent.uploadBlob as ReturnType< typeof vi.fn > ) ).not.toHaveBeenCalled();
26 } );
27
28 it( 'rejects files over the 1MB icon limit without uploading', async () => {
29 const agent = agentUploading( {} );
30 await expect(
31 uploadImageBlob( agent, fileOf( 'image/png', PUBLICATION_ICON_MAX_BYTES + 1 ) )
32 ).rejects.toBeInstanceOf( ImageValidationError );
33 expect( ( agent.uploadBlob as ReturnType< typeof vi.fn > ) ).not.toHaveBeenCalled();
34 } );
35
36 it( 'uploads a valid image and returns its blob ref json', async () => {
37 const agent = agentUploading( {
38 ref: { toString: () => 'bafyicon' },
39 mimeType: 'image/png',
40 size: 500,
41 } );
42 const ref = await uploadImageBlob( agent, fileOf( 'image/png', 500 ) );
43 expect( ref ).toEqual( {
44 $type: 'blob',
45 ref: { $link: 'bafyicon' },
46 mimeType: 'image/png',
47 size: 500,
48 } );
49 } );
50} );