🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { createFileStoreImageUploader, type FileStorePort } from './fileStoreImageUploader';
// sha256 of the bytes behind `PNG_BASE64`, i.e. what the port sees as `hash`.
const PNG_BASE64 = Buffer.from('fake-png-bytes').toString('base64');
const PNG_HASH = '3c6ed5fc41c950bf0db531eb22f945467fb8d999f80d82ba27dcc9fd90add54d';
const createPort = (overrides: Partial<FileStorePort> = {}): FileStorePort => ({
abortS3Upload: vi.fn().mockResolvedValue({ success: true }),
checkFileHash: vi.fn().mockResolvedValue({ isExist: false }),
createFile: vi.fn().mockResolvedValue({ id: 'file_1', url: 'https://cdn/x.png' }),
createS3PreSignedUrl: vi.fn().mockResolvedValue('https://s3/presigned'),
...overrides,
});
describe('createFileStoreImageUploader', () => {
beforeEach(() => {
vi.stubGlobal('fetch', vi.fn().mockResolvedValue({ ok: true, status: 200, statusText: 'OK' }));
});
afterEach(() => {
vi.unstubAllGlobals();
});
it('uploads a tool-result image and returns its stored file reference', async () => {
const port = createPort();
const upload = createFileStoreImageUploader(async () => port);
const result = await upload({ data: PNG_BASE64, mediaType: 'image/png' });
expect(result).toEqual({ fileId: 'file_1', url: 'https://cdn/x.png' });
// The bytes are PUT to the pre-signed URL with the image's own media type.
expect(fetch).toHaveBeenCalledWith('https://s3/presigned', {
body: Buffer.from(PNG_BASE64, 'base64'),
headers: { 'Content-Type': 'image/png' },
method: 'PUT',
});
expect(port.createS3PreSignedUrl).toHaveBeenCalledWith({
pathname: expect.stringMatching(/^files\/\d{4}-\d{2}-\d{2}\/[a-f0-9]{64}\.png$/),
size: Buffer.from(PNG_BASE64, 'base64').length,
});
expect(port.checkFileHash).toHaveBeenCalledWith({ hash: PNG_HASH });
const createFileInput = vi.mocked(port.createFile).mock.calls[0][0];
expect(createFileInput).toMatchObject({
fileType: 'image/png',
hash: PNG_HASH,
name: 'cc-read-image.png',
size: Buffer.from(PNG_BASE64, 'base64').length,
// Run tokens may create tool-result attachments outside a Task run.
source: 'tool-result',
});
// `url` is the S3 pathname, not the pre-signed URL.
expect(createFileInput.url).toMatch(/^files\/\d{4}-\d{2}-\d{2}\/[a-f0-9]{64}\.png$/);
expect(createFileInput.metadata).not.toHaveProperty('width');
expect(createFileInput.metadata).not.toHaveProperty('ratio');
});
it('records the intrinsic size on the file metadata, matching the web uploader', async () => {
const port = createPort();
const upload = createFileStoreImageUploader(async () => port);
await upload({ data: PNG_BASE64, height: 800, mediaType: 'image/png', width: 1280 });
expect(vi.mocked(port.createFile).mock.calls[0][0].metadata).toMatchObject({
height: 800,
ratio: 1.6,
width: 1280,
});
});
it('reuses the stored object and skips the S3 PUT when the hash already exists', async () => {
const port = createPort({
checkFileHash: vi.fn().mockResolvedValue({ isExist: true, url: 'files/old/abc.png' }),
});
const upload = createFileStoreImageUploader(async () => port);
await upload({ data: PNG_BASE64, mediaType: 'image/png' });
expect(port.createS3PreSignedUrl).not.toHaveBeenCalled();
expect(fetch).not.toHaveBeenCalled();
expect(vi.mocked(port.createFile).mock.calls[0][0].url).toBe('files/old/abc.png');
});
it('falls back to a png extension for an unknown media type', async () => {
const port = createPort();
const upload = createFileStoreImageUploader(async () => port);
await upload({ data: PNG_BASE64, mediaType: 'image/avif' });
expect(vi.mocked(port.createFile).mock.calls[0][0].name).toBe('cc-read-image.png');
});
it('returns undefined when no file store is available, so the placeholder survives', async () => {
const upload = createFileStoreImageUploader(async () => undefined);
await expect(upload({ data: PNG_BASE64, mediaType: 'image/png' })).resolves.toBeUndefined();
expect(fetch).not.toHaveBeenCalled();
});
it('throws when the S3 PUT fails, letting the pipeline degrade to the placeholder', async () => {
vi.stubGlobal(
'fetch',
vi.fn().mockResolvedValue({ ok: false, status: 403, statusText: 'Forbidden' }),
);
const port = createPort();
const upload = createFileStoreImageUploader(async () => port);
await expect(upload({ data: PNG_BASE64, mediaType: 'image/png' })).rejects.toThrow(
'Upload failed: 403 Forbidden',
);
expect(port.abortS3Upload).toHaveBeenCalledWith({
pathname: expect.stringMatching(/^files\//),
});
expect(port.createFile).not.toHaveBeenCalled();
});
});