-
Notifications
You must be signed in to change notification settings - Fork 319
Expand file tree
/
Copy pathgenerate_spec.js
More file actions
168 lines (149 loc) · 5.72 KB
/
Copy pathgenerate_spec.js
File metadata and controls
168 lines (149 loc) · 5.72 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
const expect = require('expect.js');
const sinon = require('sinon');
const https = require('https');
const { EventEmitter } = require('events');
const cloudinary = require('../../../../cloudinary');
const uploader = require('../../../../lib/uploader');
const createTestConfig = require('../../../testUtils/createTestConfig');
const CLOUD_NAME = 'test-cloud';
const SECURE_URL = 'https://res.cloudinary.com/test-cloud/image/upload/generated.png';
describe('uploader generate', function () {
let requestStub;
let uploadStub;
let capturedOptions;
let capturedBody;
beforeEach(function () {
cloudinary.config(createTestConfig({
cloud_name: CLOUD_NAME,
api_key: 'test-key',
api_secret: 'test-secret'
}));
capturedOptions = null;
capturedBody = null;
});
afterEach(function () {
if (requestStub && requestStub.restore) {
requestStub.restore();
}
if (uploadStub && uploadStub.restore) {
uploadStub.restore();
}
requestStub = null;
uploadStub = null;
});
// Stub https.request to emit a JSON response with the given status code and body.
function stubGenerateRequest(statusCode, responseBody) {
const mockResponse = new EventEmitter();
mockResponse.statusCode = statusCode;
mockResponse.headers = {};
requestStub = sinon.stub(https, 'request').callsFake(function (options, callback) {
capturedOptions = options;
setTimeout(() => callback(mockResponse), 0);
const mockRequest = new EventEmitter();
mockRequest.write = sinon.stub().callsFake((data) => {
capturedBody = data;
});
mockRequest.end = function () {
setTimeout(() => {
mockResponse.emit('data', JSON.stringify(responseBody));
mockResponse.emit('end');
}, 10);
};
mockRequest.setTimeout = sinon.stub();
return mockRequest;
});
}
function generateSuccessBody(secure_url = SECURE_URL) {
return {
data: {
assets: [
{
secure_url,
format: 'png',
width: 1024,
height: 768,
bytes: 2048576,
model: { family: 'flux', tier: 'premium', model_id: 'flux-2-pro' },
created_at: '2026-04-21T14:30:00Z'
}
]
},
request_id: 'test-request-id'
};
}
it('should call the generate endpoint with the generation params as a JSON body', function () {
stubGenerateRequest(200, generateSuccessBody());
// Prevent the upload step from issuing a real request.
uploadStub = sinon.stub(uploader, 'upload').resolves({ secure_url: SECURE_URL });
return cloudinary.v2.uploader.generate({ prompt: 'A man with a hat', model_family: 'flux' }).then(() => {
sinon.assert.calledWith(requestStub, sinon.match({
pathname: sinon.match(new RegExp(`/v2/processing/${CLOUD_NAME}/generate/image`)),
method: sinon.match('POST')
}));
expect(capturedOptions.headers['Content-Type']).to.eql('application/json');
const body = JSON.parse(capturedBody);
expect(body.prompt).to.eql('A man with a hat');
expect(body.model_family).to.eql('flux');
});
});
it('should upload the generated image and resolve with the upload result', function () {
stubGenerateRequest(200, generateSuccessBody());
const uploadResult = { public_id: 'generated', secure_url: SECURE_URL };
uploadStub = sinon.stub(uploader, 'upload').resolves(uploadResult);
const options = { upload_preset: 'my_preset', tags: ['generated'] };
return cloudinary.v2.uploader.generate({ prompt: 'A man with a hat' }, options).then((result) => {
sinon.assert.calledWith(uploadStub, SECURE_URL);
// Options are forwarded to the upload step.
const forwardedOptions = uploadStub.firstCall.args[2];
expect(forwardedOptions.upload_preset).to.eql('my_preset');
expect(result).to.eql(uploadResult);
});
});
it('should forward the callback to the upload step on success', function (done) {
stubGenerateRequest(200, generateSuccessBody());
const uploadResult = { public_id: 'generated', secure_url: SECURE_URL };
// Mimic the real upload by invoking the callback it receives.
uploadStub = sinon.stub(uploader, 'upload').callsFake((file, callback) => {
if (typeof callback === 'function') {
callback(uploadResult);
}
return Promise.resolve(uploadResult);
});
cloudinary.v2.uploader.generate({ prompt: 'A man with a hat' }, function (error, result) {
try {
expect(error).to.be(undefined);
expect(result).to.eql(uploadResult);
done();
} catch (e) {
done(e);
}
});
});
it('should not call upload and should reject when generation fails', function () {
stubGenerateRequest(400, { error: { message: 'missing parameters' } });
uploadStub = sinon.stub(uploader, 'upload').resolves({});
return cloudinary.v2.uploader.generate({ prompt: '' }).then(() => {
throw new Error('Expected generate to reject');
}, (error) => {
sinon.assert.notCalled(uploadStub);
expect(error).to.be.ok();
});
});
it('should invoke the callback with the error when generation fails', function (done) {
stubGenerateRequest(400, { error: { message: 'missing parameters' } });
uploadStub = sinon.stub(uploader, 'upload').resolves({});
cloudinary.v2.uploader.generate({ prompt: '' }, function (error, result) {
try {
expect(error).to.be.ok();
expect(error.message).to.eql('missing parameters');
expect(result).to.be(undefined);
sinon.assert.notCalled(uploadStub);
done();
} catch (e) {
done(e);
}
}).catch(() => {
// Swallow the rejected promise; assertions are made in the callback.
});
});
});