Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

replicate-image.test.ts 4.5 KB

You have to be logged in to leave a comment. Sign In
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
  1. import { ReplicateImageProvider } from '../../src/providers/replicate';
  2. import { fetchWithCache } from '../../src/cache';
  3. jest.mock('../../src/cache');
  4. const mockedFetchWithCache = jest.mocked(fetchWithCache);
  5. describe('ReplicateImageProvider Demonstration', () => {
  6. const mockApiKey = 'test-api-key';
  7. beforeEach(() => {
  8. jest.resetAllMocks();
  9. });
  10. it('demonstrates FLUX 1.1 Pro Ultra image generation', async () => {
  11. // Mock successful API response
  12. mockedFetchWithCache.mockResolvedValue({
  13. data: {
  14. id: 'test-prediction-id',
  15. status: 'succeeded',
  16. output: ['https://replicate.delivery/pbxt/flux-ultra-example/beautiful-landscape.webp'],
  17. },
  18. cached: false,
  19. status: 200,
  20. statusText: 'OK',
  21. });
  22. const provider = new ReplicateImageProvider('black-forest-labs/flux-1.1-pro-ultra', {
  23. config: {
  24. apiKey: mockApiKey,
  25. width: 1024,
  26. height: 1024,
  27. output_format: 'webp',
  28. },
  29. });
  30. const prompt = 'A majestic mountain landscape at golden hour';
  31. const result = await provider.callApi(prompt);
  32. // The provider formats the output as markdown
  33. expect(result.output).toBe(
  34. '![A majestic mountain landscape at golden hour](https://replicate.delivery/pbxt/flux-ultra-example/beautiful-landscape.webp)',
  35. );
  36. expect(result.error).toBeUndefined();
  37. // Verify the API was called correctly
  38. expect(mockedFetchWithCache).toHaveBeenCalledWith(
  39. 'https://api.replicate.com/v1/models/black-forest-labs/flux-1.1-pro-ultra/predictions',
  40. expect.objectContaining({
  41. method: 'POST',
  42. headers: expect.objectContaining({
  43. Authorization: `Bearer ${mockApiKey}`,
  44. 'Content-Type': 'application/json',
  45. Prefer: 'wait=60',
  46. }),
  47. body: expect.stringContaining('"prompt":"A majestic mountain landscape at golden hour"'),
  48. }),
  49. expect.any(Number),
  50. 'json',
  51. );
  52. });
  53. it('demonstrates multiple image outputs handling', async () => {
  54. // Some models return multiple images
  55. mockedFetchWithCache.mockResolvedValue({
  56. data: {
  57. id: 'test-prediction-id',
  58. status: 'succeeded',
  59. output: [
  60. 'https://replicate.delivery/pbxt/example/image1.png',
  61. 'https://replicate.delivery/pbxt/example/image2.png',
  62. 'https://replicate.delivery/pbxt/example/image3.png',
  63. ],
  64. },
  65. cached: false,
  66. status: 200,
  67. statusText: 'OK',
  68. });
  69. const provider = new ReplicateImageProvider('test-model', {
  70. config: { apiKey: mockApiKey },
  71. });
  72. const result = await provider.callApi('Generate variations');
  73. // Only the first image is returned for simplicity
  74. expect(result.output).toBe(
  75. '![Generate variations](https://replicate.delivery/pbxt/example/image1.png)',
  76. );
  77. });
  78. it('demonstrates raw mode for FLUX 1.1 Pro Ultra', async () => {
  79. mockedFetchWithCache.mockResolvedValue({
  80. data: {
  81. id: 'test-prediction-id',
  82. status: 'succeeded',
  83. output: ['https://replicate.delivery/pbxt/flux-raw/photorealistic.png'],
  84. },
  85. cached: false,
  86. status: 200,
  87. statusText: 'OK',
  88. });
  89. const provider = new ReplicateImageProvider('black-forest-labs/flux-1.1-pro-ultra', {
  90. config: {
  91. apiKey: mockApiKey,
  92. raw: true, // Enable raw mode for photorealistic results
  93. },
  94. });
  95. const result = await provider.callApi('Professional headshot, natural lighting');
  96. expect(result.output).toBe(
  97. '![Professional headshot, natural lighting](https://replicate.delivery/pbxt/flux-raw/photorealistic.png)',
  98. );
  99. // Verify raw parameter was passed (note: raw should be in input)
  100. const callArgs = mockedFetchWithCache.mock.calls[0];
  101. expect(callArgs).toBeDefined();
  102. if (callArgs && callArgs[1] && callArgs[1].body) {
  103. const bodyJson = JSON.parse(callArgs[1].body as string);
  104. expect(bodyJson.input.prompt).toBe('Professional headshot, natural lighting');
  105. }
  106. });
  107. it('demonstrates error handling', async () => {
  108. mockedFetchWithCache.mockResolvedValue({
  109. data: {
  110. id: 'test-prediction-id',
  111. status: 'failed',
  112. error: 'NSFW content detected',
  113. },
  114. cached: false,
  115. status: 200,
  116. statusText: 'OK',
  117. });
  118. const provider = new ReplicateImageProvider('test-model', {
  119. config: { apiKey: mockApiKey },
  120. });
  121. const result = await provider.callApi('inappropriate content');
  122. expect(result.error).toBe('NSFW content detected');
  123. expect(result.output).toBeUndefined();
  124. });
  125. });
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...