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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
- import { ReplicateImageProvider } from '../../src/providers/replicate';
- import { fetchWithCache } from '../../src/cache';
- jest.mock('../../src/cache');
- const mockedFetchWithCache = jest.mocked(fetchWithCache);
- describe('ReplicateImageProvider Demonstration', () => {
- const mockApiKey = 'test-api-key';
- beforeEach(() => {
- jest.resetAllMocks();
- });
- it('demonstrates FLUX 1.1 Pro Ultra image generation', async () => {
- // Mock successful API response
- mockedFetchWithCache.mockResolvedValue({
- data: {
- id: 'test-prediction-id',
- status: 'succeeded',
- output: ['https://replicate.delivery/pbxt/flux-ultra-example/beautiful-landscape.webp'],
- },
- cached: false,
- status: 200,
- statusText: 'OK',
- });
- const provider = new ReplicateImageProvider('black-forest-labs/flux-1.1-pro-ultra', {
- config: {
- apiKey: mockApiKey,
- width: 1024,
- height: 1024,
- output_format: 'webp',
- },
- });
- const prompt = 'A majestic mountain landscape at golden hour';
- const result = await provider.callApi(prompt);
- // The provider formats the output as markdown
- expect(result.output).toBe(
- '',
- );
- expect(result.error).toBeUndefined();
- // Verify the API was called correctly
- expect(mockedFetchWithCache).toHaveBeenCalledWith(
- 'https://api.replicate.com/v1/models/black-forest-labs/flux-1.1-pro-ultra/predictions',
- expect.objectContaining({
- method: 'POST',
- headers: expect.objectContaining({
- Authorization: `Bearer ${mockApiKey}`,
- 'Content-Type': 'application/json',
- Prefer: 'wait=60',
- }),
- body: expect.stringContaining('"prompt":"A majestic mountain landscape at golden hour"'),
- }),
- expect.any(Number),
- 'json',
- );
- });
- it('demonstrates multiple image outputs handling', async () => {
- // Some models return multiple images
- mockedFetchWithCache.mockResolvedValue({
- data: {
- id: 'test-prediction-id',
- status: 'succeeded',
- output: [
- 'https://replicate.delivery/pbxt/example/image1.png',
- 'https://replicate.delivery/pbxt/example/image2.png',
- 'https://replicate.delivery/pbxt/example/image3.png',
- ],
- },
- cached: false,
- status: 200,
- statusText: 'OK',
- });
- const provider = new ReplicateImageProvider('test-model', {
- config: { apiKey: mockApiKey },
- });
- const result = await provider.callApi('Generate variations');
- // Only the first image is returned for simplicity
- expect(result.output).toBe(
- '',
- );
- });
- it('demonstrates raw mode for FLUX 1.1 Pro Ultra', async () => {
- mockedFetchWithCache.mockResolvedValue({
- data: {
- id: 'test-prediction-id',
- status: 'succeeded',
- output: ['https://replicate.delivery/pbxt/flux-raw/photorealistic.png'],
- },
- cached: false,
- status: 200,
- statusText: 'OK',
- });
- const provider = new ReplicateImageProvider('black-forest-labs/flux-1.1-pro-ultra', {
- config: {
- apiKey: mockApiKey,
- raw: true, // Enable raw mode for photorealistic results
- },
- });
- const result = await provider.callApi('Professional headshot, natural lighting');
- expect(result.output).toBe(
- '',
- );
- // Verify raw parameter was passed (note: raw should be in input)
- const callArgs = mockedFetchWithCache.mock.calls[0];
- expect(callArgs).toBeDefined();
- if (callArgs && callArgs[1] && callArgs[1].body) {
- const bodyJson = JSON.parse(callArgs[1].body as string);
- expect(bodyJson.input.prompt).toBe('Professional headshot, natural lighting');
- }
- });
- it('demonstrates error handling', async () => {
- mockedFetchWithCache.mockResolvedValue({
- data: {
- id: 'test-prediction-id',
- status: 'failed',
- error: 'NSFW content detected',
- },
- cached: false,
- status: 200,
- statusText: 'OK',
- });
- const provider = new ReplicateImageProvider('test-model', {
- config: { apiKey: mockApiKey },
- });
- const result = await provider.callApi('inappropriate content');
- expect(result.error).toBe('NSFW content detected');
- expect(result.output).toBeUndefined();
- });
- });
|