@fungi.computer/darkroom 0.11.11 → 0.11.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -0
- package/examples/standalone/README.md +52 -0
- package/examples/standalone/worker.ts +87 -0
- package/examples/standalone/wrangler.jsonc +26 -0
- package/package.json +8 -2
- package/src/app-media-contracts.ts +142 -0
- package/src/audio-providers/elevenlabs.ts +108 -0
- package/src/audio-reference-types.ts +47 -0
- package/src/audio-references.ts +107 -0
- package/src/browser-failure.ts +73 -0
- package/src/browser.ts +7 -0
- package/src/cloudflare/provider-reason.ts +89 -0
- package/src/cloudflare/provider.ts +405 -0
- package/src/cloudflare/r2-destination.ts +130 -0
- package/src/cloudflare/reference-images.ts +193 -0
- package/src/cloudflare.ts +38 -0
- package/src/cost-estimate.ts +129 -0
- package/src/daily-media-meter.ts +163 -0
- package/src/effect.ts +2 -0
- package/src/execution-error.ts +98 -0
- package/src/failure.ts +13 -0
- package/src/gateway-models.ts +304 -0
- package/src/gateway-wire.ts +36 -0
- package/src/index.ts +1454 -0
- package/src/media-binding.ts +260 -0
- package/src/media-content-type.ts +60 -0
- package/src/media-generation.ts +171 -0
- package/src/media-operation-contract.ts +173 -0
- package/src/media-operation-do.ts +257 -0
- package/src/media-operation-owner.ts +1786 -0
- package/src/media-operation-storage.ts +110 -0
- package/src/media-operation.ts +454 -0
- package/src/media-path.ts +8 -0
- package/src/media-quote.ts +34 -0
- package/src/media-run-lifetime.ts +5 -0
- package/src/media-written.ts +38 -0
- package/src/model-catalog.ts +1574 -0
- package/src/neurons.ts +6 -0
- package/src/node-webpmux.d.ts +12 -0
- package/src/provenance-bytes.ts +22 -0
- package/src/provenance-export.ts +126 -0
- package/src/provenance-jpeg.ts +83 -0
- package/src/provenance-png.ts +26 -0
- package/src/provenance-signed.ts +7 -0
- package/src/provenance-xmp.ts +57 -0
- package/src/provenance.ts +87 -0
- package/src/reference-image-size.ts +67 -0
- package/src/reference-read.ts +50 -0
- package/src/server.ts +711 -0
- package/src/video-providers/index.ts +18 -0
- package/src/video-providers/minimax.ts +96 -0
- package/src/video-providers/reference-encoding.ts +32 -0
- package/src/video-providers/types.ts +47 -0
- package/src/video-providers/unified.ts +68 -0
- package/test/audio-models.test.ts +428 -0
- package/test/audio-references.test.ts +428 -0
- package/test/custom-catalogue.test.ts +257 -0
- package/test/daily-media-meter.test.ts +287 -0
- package/test/failure.test.ts +190 -0
- package/test/fixtures/cloudflare-workers.fixture.ts +10 -0
- package/test/fixtures/media-operation.fixture.ts +134 -0
- package/test/fixtures/media-team-prompts.ts +7 -0
- package/test/fixtures/native-sqlite.fixture.ts +40 -0
- package/test/gateway-models.test.ts +712 -0
- package/test/media-operation-do.test.ts +1565 -0
- package/test/media-quote.test.ts +81 -0
- package/test/media-retention.test.ts +597 -0
- package/test/media-service.test.ts +549 -0
- package/test/media-team-prompts.test.ts +92 -0
- package/test/model-facts.test.ts +126 -0
- package/test/prompt-limit.test.ts +53 -0
- package/test/provenance-export.test.ts +144 -0
- package/test/provenance-images.fixture.mjs +17 -0
- package/test/provenance.test.ts +179 -0
- package/test/provider-gateway.test.ts +114 -0
- package/test/provider-reasons.test.ts +133 -0
- package/test/r2-destination.test.ts +166 -0
- package/test/reference-image-size.test.ts +73 -0
- package/test/reference-images-billing.test.ts +40 -0
- package/test/reference-images.test.ts +399 -0
- package/test/standalone.cloudflare.test.mjs +298 -0
- package/test/standalone.worker.mjs +70 -0
- package/test/transform-models.test.ts +400 -0
- package/test/video-capabilities.test.ts +160 -0
- package/test/video-contract.test.ts +533 -0
- package/test/video-failure-lifecycle.test.ts +177 -0
- package/test/video-provider.test.ts +136 -0
- package/test/video-wire.test.ts +251 -0
- package/vitest.cloudflare.config.ts +22 -0
- package/vitest.node.config.ts +14 -0
|
@@ -0,0 +1,549 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from "vitest";
|
|
2
|
+
import {
|
|
3
|
+
buildProviderInput,
|
|
4
|
+
getMediaModel,
|
|
5
|
+
listMediaModels,
|
|
6
|
+
MAX_REFERENCE_BYTES,
|
|
7
|
+
MAX_REFERENCE_BODY_BYTES,
|
|
8
|
+
normalizeMediaProviderOutput,
|
|
9
|
+
mediaProviderWireSchema,
|
|
10
|
+
resolveMediaReferences,
|
|
11
|
+
validateMediaGenerationSettings,
|
|
12
|
+
validateMediaVideoSelection,
|
|
13
|
+
mediaVideoModelIdSchema,
|
|
14
|
+
estimateImageMilliunits,
|
|
15
|
+
estimateImageNeurons,
|
|
16
|
+
type MediaReferenceBytes,
|
|
17
|
+
} from "../src/index.js";
|
|
18
|
+
|
|
19
|
+
const png = Uint8Array.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]);
|
|
20
|
+
const jpeg = Uint8Array.from([0xff, 0xd8, 0xff, 0xd9]);
|
|
21
|
+
const b64 = (bytes: Uint8Array): string => {
|
|
22
|
+
let binary = "";
|
|
23
|
+
for (const byte of bytes) binary += String.fromCharCode(byte);
|
|
24
|
+
return btoa(binary);
|
|
25
|
+
};
|
|
26
|
+
const base = {
|
|
27
|
+
model: "@cf/black-forest-labs/flux-1-schnell",
|
|
28
|
+
prompt: " a quiet room ",
|
|
29
|
+
width: 1024,
|
|
30
|
+
height: 768,
|
|
31
|
+
};
|
|
32
|
+
const reference: MediaReferenceBytes = {
|
|
33
|
+
bytes: png,
|
|
34
|
+
format: "png",
|
|
35
|
+
width: 8,
|
|
36
|
+
height: 8,
|
|
37
|
+
filename: "ref.png",
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
describe("retained Media model boundary", () => {
|
|
41
|
+
it("exposes twenty immutable model definitions", () => {
|
|
42
|
+
expect(listMediaModels()).toHaveLength(20);
|
|
43
|
+
expect(listMediaModels().map(({ id }) => id)).toEqual([
|
|
44
|
+
"@cf/black-forest-labs/flux-1-schnell",
|
|
45
|
+
"@cf/black-forest-labs/flux-2-klein-4b",
|
|
46
|
+
"@cf/black-forest-labs/flux-2-klein-9b",
|
|
47
|
+
"@cf/black-forest-labs/flux-2-dev",
|
|
48
|
+
"@cf/leonardo/lucid-origin",
|
|
49
|
+
"@cf/leonardo/phoenix-1.0",
|
|
50
|
+
"openai/gpt-image-2",
|
|
51
|
+
"bytedance/seedream-5-pro",
|
|
52
|
+
"bria/remove-background",
|
|
53
|
+
"pruna/p-image-upscale",
|
|
54
|
+
"bytedance/seedance-2.5",
|
|
55
|
+
"bytedance/seedance-2.0",
|
|
56
|
+
"bytedance/seedance-2.0-fast",
|
|
57
|
+
"bytedance/seedance-2.0-mini",
|
|
58
|
+
"google/veo-3.1",
|
|
59
|
+
"google/veo-3.1-fast",
|
|
60
|
+
"minimax/h3-max",
|
|
61
|
+
"elevenlabs/eleven-multilingual-v2",
|
|
62
|
+
"elevenlabs/eleven-v3",
|
|
63
|
+
"elevenlabs/music-v2",
|
|
64
|
+
]);
|
|
65
|
+
expect(Object.isFrozen(listMediaModels())).toBe(true);
|
|
66
|
+
expect(Object.isFrozen(listMediaModels()[0])).toBe(true);
|
|
67
|
+
expect(Object.isFrozen(listMediaModels()[0]?.input)).toBe(true);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it("keeps each model family's retained dimensions, steps, and references", () => {
|
|
71
|
+
const byId = (id: string) => getMediaModel(id)?.input;
|
|
72
|
+
expect(byId("@cf/black-forest-labs/flux-1-schnell")).toMatchObject({
|
|
73
|
+
transport: "json",
|
|
74
|
+
dimensions: "none",
|
|
75
|
+
steps: "steps",
|
|
76
|
+
maxSteps: 8,
|
|
77
|
+
defaultSteps: 4,
|
|
78
|
+
maxReferences: 0,
|
|
79
|
+
});
|
|
80
|
+
expect(byId("@cf/black-forest-labs/flux-2-klein-4b")).toMatchObject({
|
|
81
|
+
transport: "multipart",
|
|
82
|
+
dimensions: "own",
|
|
83
|
+
maxDimension: 1920,
|
|
84
|
+
maxDimensionDocumented: false,
|
|
85
|
+
steps: "fixed",
|
|
86
|
+
defaultSteps: 4,
|
|
87
|
+
referenceField: "input_image_",
|
|
88
|
+
maxReferences: 4,
|
|
89
|
+
});
|
|
90
|
+
expect(byId("@cf/black-forest-labs/flux-2-klein-9b")).toMatchObject({
|
|
91
|
+
transport: "multipart",
|
|
92
|
+
dimensions: "own",
|
|
93
|
+
maxDimension: 1920,
|
|
94
|
+
maxDimensionDocumented: false,
|
|
95
|
+
steps: "fixed",
|
|
96
|
+
defaultSteps: 4,
|
|
97
|
+
referenceField: "input_image_",
|
|
98
|
+
maxReferences: 4,
|
|
99
|
+
});
|
|
100
|
+
expect(byId("@cf/black-forest-labs/flux-2-dev")).toMatchObject({
|
|
101
|
+
transport: "multipart",
|
|
102
|
+
dimensions: "own",
|
|
103
|
+
steps: "steps",
|
|
104
|
+
maxSteps: null,
|
|
105
|
+
defaultSteps: 20,
|
|
106
|
+
referenceField: "input_image_",
|
|
107
|
+
maxReferences: 4,
|
|
108
|
+
});
|
|
109
|
+
expect(byId("@cf/leonardo/lucid-origin")).toMatchObject({
|
|
110
|
+
transport: "json",
|
|
111
|
+
dimensions: "own",
|
|
112
|
+
maxDimension: 2500,
|
|
113
|
+
maxDimensionDocumented: true,
|
|
114
|
+
steps: "num_steps",
|
|
115
|
+
maxSteps: 40,
|
|
116
|
+
maxReferences: 0,
|
|
117
|
+
});
|
|
118
|
+
expect(byId("@cf/leonardo/phoenix-1.0")).toMatchObject({
|
|
119
|
+
transport: "json",
|
|
120
|
+
dimensions: "own",
|
|
121
|
+
maxDimension: 2048,
|
|
122
|
+
maxDimensionDocumented: true,
|
|
123
|
+
steps: "num_steps",
|
|
124
|
+
maxSteps: 50,
|
|
125
|
+
defaultSteps: 25,
|
|
126
|
+
maxReferences: 0,
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it("carries each model's guidance into the Workers AI body, caller value overriding", () => {
|
|
131
|
+
const body = (model: string, guidance?: number) => {
|
|
132
|
+
const input = { ...base, model, width: 1024, height: 1024 };
|
|
133
|
+
return buildProviderInput(
|
|
134
|
+
validateMediaGenerationSettings(
|
|
135
|
+
guidance === undefined ? input : { ...input, guidance },
|
|
136
|
+
),
|
|
137
|
+
);
|
|
138
|
+
};
|
|
139
|
+
expect(body("@cf/leonardo/lucid-origin")).toMatchObject({
|
|
140
|
+
body: { guidance: 5 },
|
|
141
|
+
});
|
|
142
|
+
expect(body("@cf/leonardo/phoenix-1.0")).toMatchObject({
|
|
143
|
+
body: { guidance: 3 },
|
|
144
|
+
});
|
|
145
|
+
expect(body("@cf/leonardo/phoenix-1.0", 7.5)).toMatchObject({
|
|
146
|
+
body: { guidance: 7.5 },
|
|
147
|
+
});
|
|
148
|
+
expect(body("@cf/black-forest-labs/flux-1-schnell").transport).toBe("json");
|
|
149
|
+
expect(
|
|
150
|
+
body("@cf/black-forest-labs/flux-1-schnell").body,
|
|
151
|
+
).not.toHaveProperty("guidance");
|
|
152
|
+
expect(() => body("@cf/leonardo/phoenix-1.0", 1)).toThrow(/guidance/iu);
|
|
153
|
+
expect(() => body("@cf/leonardo/lucid-origin", 11)).toThrow(/guidance/iu);
|
|
154
|
+
expect(() => body("@cf/black-forest-labs/flux-1-schnell", 3)).toThrow(
|
|
155
|
+
/does not take guidance/iu,
|
|
156
|
+
);
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
it("normalizes prompts and model defaults while enforcing model limits", () => {
|
|
160
|
+
const phoenix = validateMediaGenerationSettings({
|
|
161
|
+
...base,
|
|
162
|
+
model: "@cf/leonardo/phoenix-1.0",
|
|
163
|
+
prompt: " a phoenix ",
|
|
164
|
+
});
|
|
165
|
+
expect(phoenix).toMatchObject({ prompt: "a phoenix", steps: 25 });
|
|
166
|
+
expect(() =>
|
|
167
|
+
validateMediaGenerationSettings({
|
|
168
|
+
...base,
|
|
169
|
+
model: "@cf/leonardo/phoenix-1.0",
|
|
170
|
+
width: 2_049,
|
|
171
|
+
height: 1024,
|
|
172
|
+
}),
|
|
173
|
+
).toThrow(/dimensions/iu);
|
|
174
|
+
expect(() =>
|
|
175
|
+
validateMediaGenerationSettings({
|
|
176
|
+
...base,
|
|
177
|
+
steps: 9,
|
|
178
|
+
}),
|
|
179
|
+
).toThrow(/at most 8/iu);
|
|
180
|
+
expect(() =>
|
|
181
|
+
validateMediaGenerationSettings({
|
|
182
|
+
...base,
|
|
183
|
+
model: "@cf/black-forest-labs/flux-2-klein-4b",
|
|
184
|
+
steps: 3,
|
|
185
|
+
}),
|
|
186
|
+
).toThrow(/fixed/iu);
|
|
187
|
+
expect(
|
|
188
|
+
validateMediaGenerationSettings({
|
|
189
|
+
...base,
|
|
190
|
+
model: "@cf/black-forest-labs/flux-2-klein-4b",
|
|
191
|
+
}),
|
|
192
|
+
).not.toHaveProperty("steps");
|
|
193
|
+
expect(
|
|
194
|
+
validateMediaGenerationSettings({
|
|
195
|
+
...base,
|
|
196
|
+
model: "@cf/black-forest-labs/flux-2-dev",
|
|
197
|
+
steps: 100,
|
|
198
|
+
}).steps,
|
|
199
|
+
).toBe(100);
|
|
200
|
+
expect(() =>
|
|
201
|
+
validateMediaGenerationSettings({
|
|
202
|
+
...base,
|
|
203
|
+
references: [{ computerId: "c", path: "ref.png" }],
|
|
204
|
+
}),
|
|
205
|
+
).toThrow(/reference/iu);
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
it("builds each retained provider payload without inventing fields", () => {
|
|
209
|
+
// Gateway models (fixed sizes) take gatewayInput; see gateway-models.test.
|
|
210
|
+
for (const model of listMediaModels().filter(
|
|
211
|
+
({ sizes, kind, transform }) =>
|
|
212
|
+
sizes === null && kind === "image" && transform === null,
|
|
213
|
+
)) {
|
|
214
|
+
const settings = validateMediaGenerationSettings({
|
|
215
|
+
...base,
|
|
216
|
+
model: model.id,
|
|
217
|
+
references:
|
|
218
|
+
model.input.maxReferences === 0
|
|
219
|
+
? []
|
|
220
|
+
: [{ computerId: "c", path: "ref.png" }],
|
|
221
|
+
});
|
|
222
|
+
const payload = buildProviderInput(
|
|
223
|
+
settings,
|
|
224
|
+
model.input.maxReferences === 0 ? [] : [reference],
|
|
225
|
+
);
|
|
226
|
+
if (payload.transport === "json") {
|
|
227
|
+
expect(payload.body.prompt).toBe("a quiet room");
|
|
228
|
+
if (model.input.dimensions === "none") {
|
|
229
|
+
expect(payload.body).not.toHaveProperty("width");
|
|
230
|
+
expect(payload.body).not.toHaveProperty("height");
|
|
231
|
+
} else {
|
|
232
|
+
expect(payload.body).toMatchObject({ width: 1024, height: 768 });
|
|
233
|
+
}
|
|
234
|
+
if (
|
|
235
|
+
model.input.steps === "num_steps" &&
|
|
236
|
+
model.input.defaultSteps !== null
|
|
237
|
+
)
|
|
238
|
+
expect(payload.body.num_steps).toBeDefined();
|
|
239
|
+
} else {
|
|
240
|
+
expect(payload.body.get("prompt")).toBe("a quiet room");
|
|
241
|
+
expect(payload.body.get("width")).toBe("1024");
|
|
242
|
+
expect(payload.body.get("height")).toBe("768");
|
|
243
|
+
if (model.input.steps === "fixed")
|
|
244
|
+
expect(payload.body.get("steps")).toBeNull();
|
|
245
|
+
if (model.input.maxReferences > 0)
|
|
246
|
+
expect(payload.body.get("input_image_0")).toBeInstanceOf(Blob);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
});
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
describe("deterministic retained image cost", () => {
|
|
253
|
+
it("quotes each retained formula and converts to milliunits", () => {
|
|
254
|
+
expect(
|
|
255
|
+
estimateImageNeurons("@cf/black-forest-labs/flux-1-schnell", {
|
|
256
|
+
width: 1024,
|
|
257
|
+
height: 1024,
|
|
258
|
+
steps: 4,
|
|
259
|
+
}),
|
|
260
|
+
).toBeCloseTo(57.6, 8);
|
|
261
|
+
expect(
|
|
262
|
+
estimateImageMilliunits("@cf/black-forest-labs/flux-1-schnell", {
|
|
263
|
+
width: 1024,
|
|
264
|
+
height: 1024,
|
|
265
|
+
steps: 4,
|
|
266
|
+
}),
|
|
267
|
+
).toBe(64);
|
|
268
|
+
expect(
|
|
269
|
+
estimateImageNeurons("@cf/black-forest-labs/flux-2-klein-4b", {
|
|
270
|
+
width: 1024,
|
|
271
|
+
height: 1024,
|
|
272
|
+
references: [{ width: 1024, height: 1024 }],
|
|
273
|
+
}),
|
|
274
|
+
).toBeCloseTo(125.68, 8);
|
|
275
|
+
expect(
|
|
276
|
+
estimateImageNeurons("@cf/black-forest-labs/flux-2-dev", {
|
|
277
|
+
width: 1024,
|
|
278
|
+
height: 1024,
|
|
279
|
+
steps: 20,
|
|
280
|
+
}),
|
|
281
|
+
).toBeCloseTo(2981.6, 8);
|
|
282
|
+
expect(
|
|
283
|
+
estimateImageNeurons("@cf/black-forest-labs/flux-2-klein-9b", {
|
|
284
|
+
width: 1024,
|
|
285
|
+
height: 1024,
|
|
286
|
+
references: [{ width: 1024, height: 1024 }],
|
|
287
|
+
}),
|
|
288
|
+
).toBeCloseTo(1545.46, 8);
|
|
289
|
+
expect(
|
|
290
|
+
estimateImageNeurons("@cf/leonardo/lucid-origin", {
|
|
291
|
+
width: 1024,
|
|
292
|
+
height: 1024,
|
|
293
|
+
}),
|
|
294
|
+
).toBeCloseTo(2784, 8);
|
|
295
|
+
expect(
|
|
296
|
+
estimateImageNeurons("@cf/leonardo/phoenix-1.0", {
|
|
297
|
+
width: 1024,
|
|
298
|
+
height: 1024,
|
|
299
|
+
}),
|
|
300
|
+
).toBeCloseTo(2370, 8);
|
|
301
|
+
});
|
|
302
|
+
|
|
303
|
+
it("rejects unknown models and hostile parameter values", () => {
|
|
304
|
+
const valid = { width: 512, height: 512 };
|
|
305
|
+
expect(() => estimateImageNeurons("unknown", valid)).toThrow(/unknown/iu);
|
|
306
|
+
for (const params of [
|
|
307
|
+
{ width: 0, height: 512 },
|
|
308
|
+
{ width: -1, height: 512 },
|
|
309
|
+
{ width: Number.NaN, height: 512 },
|
|
310
|
+
{ width: 512.5, height: 512 },
|
|
311
|
+
{ width: 512, height: Number.POSITIVE_INFINITY },
|
|
312
|
+
{ width: 512, height: 512, steps: 0 },
|
|
313
|
+
{ width: 512, height: 512, steps: Number.NaN },
|
|
314
|
+
{ width: 512, height: 512, references: [{ width: 0, height: 1 }] },
|
|
315
|
+
])
|
|
316
|
+
expect(() =>
|
|
317
|
+
estimateImageNeurons("@cf/black-forest-labs/flux-1-schnell", params),
|
|
318
|
+
).toThrow(/invalid/iu);
|
|
319
|
+
expect(() =>
|
|
320
|
+
estimateImageNeurons("@cf/black-forest-labs/flux-1-schnell", {
|
|
321
|
+
...valid,
|
|
322
|
+
references: [{ width: 512, height: 512 }],
|
|
323
|
+
}),
|
|
324
|
+
).toThrow(/references/iu);
|
|
325
|
+
});
|
|
326
|
+
});
|
|
327
|
+
|
|
328
|
+
describe("retained Media reference boundary", () => {
|
|
329
|
+
const settings = validateMediaGenerationSettings({
|
|
330
|
+
...base,
|
|
331
|
+
model: "@cf/black-forest-labs/flux-2-klein-4b",
|
|
332
|
+
references: [{ computerId: "c", path: "ref.png" }],
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
it("uses the caller-owned reader and accepts bounded references", async () => {
|
|
336
|
+
const calls: string[] = [];
|
|
337
|
+
const resolved = await resolveMediaReferences(settings, {
|
|
338
|
+
read: (input) => {
|
|
339
|
+
calls.push(`${input.computerId}:${input.path}`);
|
|
340
|
+
return Promise.resolve(reference);
|
|
341
|
+
},
|
|
342
|
+
});
|
|
343
|
+
expect(calls).toEqual(["c:ref.png"]);
|
|
344
|
+
expect(resolved).toEqual([reference]);
|
|
345
|
+
});
|
|
346
|
+
|
|
347
|
+
it("rejects oversized dimensions and bytes", async () => {
|
|
348
|
+
await expect(
|
|
349
|
+
resolveMediaReferences(settings, {
|
|
350
|
+
read: () => Promise.resolve({ ...reference, width: 513 }),
|
|
351
|
+
}),
|
|
352
|
+
).rejects.toThrow(/bounded/iu);
|
|
353
|
+
await expect(
|
|
354
|
+
resolveMediaReferences(settings, {
|
|
355
|
+
read: () =>
|
|
356
|
+
Promise.resolve({
|
|
357
|
+
...reference,
|
|
358
|
+
bytes: new Uint8Array(MAX_REFERENCE_BYTES + 1),
|
|
359
|
+
}),
|
|
360
|
+
}),
|
|
361
|
+
).rejects.toThrow(/bounded/iu);
|
|
362
|
+
});
|
|
363
|
+
|
|
364
|
+
it("bounds the total bytes across a 16-reference GPT Image 2 request", async () => {
|
|
365
|
+
const gpt = validateMediaGenerationSettings({
|
|
366
|
+
model: "openai/gpt-image-2",
|
|
367
|
+
prompt: "edit",
|
|
368
|
+
width: 1024,
|
|
369
|
+
height: 1024,
|
|
370
|
+
references: Array.from({ length: 16 }, (_, i) => ({
|
|
371
|
+
computerId: "c",
|
|
372
|
+
path: `/${String(i)}.webp`,
|
|
373
|
+
})),
|
|
374
|
+
});
|
|
375
|
+
const each = (count: number, size: number) =>
|
|
376
|
+
resolveMediaReferences(gpt, {
|
|
377
|
+
read: () =>
|
|
378
|
+
Promise.resolve({
|
|
379
|
+
format: "webp" as const,
|
|
380
|
+
width: 1024,
|
|
381
|
+
height: 1024,
|
|
382
|
+
bytes: new Uint8Array(size),
|
|
383
|
+
}),
|
|
384
|
+
}).then((all) => all.length === count);
|
|
385
|
+
await expect(each(16, MAX_REFERENCE_BODY_BYTES / 16)).resolves.toBe(true);
|
|
386
|
+
await expect(each(16, MAX_REFERENCE_BODY_BYTES / 16 + 1)).rejects.toThrow(
|
|
387
|
+
/total request size/iu,
|
|
388
|
+
);
|
|
389
|
+
});
|
|
390
|
+
|
|
391
|
+
it.each(mediaVideoModelIdSchema.options)(
|
|
392
|
+
"accepts fitted landscape and portrait video references for %s, not short edges below 320",
|
|
393
|
+
async (id) => {
|
|
394
|
+
const video = validateMediaGenerationSettings({
|
|
395
|
+
...validateMediaVideoSelection({ model: id, prompt: base.prompt }),
|
|
396
|
+
references: [{ computerId: "c", path: "first.png" }],
|
|
397
|
+
audioReferences: undefined,
|
|
398
|
+
});
|
|
399
|
+
for (const dimensions of [
|
|
400
|
+
{ width: 640, height: 320 },
|
|
401
|
+
{ width: 320, height: 640 },
|
|
402
|
+
]) {
|
|
403
|
+
const read = vi.fn(() =>
|
|
404
|
+
Promise.resolve({ ...reference, ...dimensions }),
|
|
405
|
+
);
|
|
406
|
+
await expect(resolveMediaReferences(video, { read })).resolves.toEqual([
|
|
407
|
+
{ ...reference, ...dimensions },
|
|
408
|
+
]);
|
|
409
|
+
expect(read).toHaveBeenCalledWith(video.references?.[0], id);
|
|
410
|
+
}
|
|
411
|
+
for (const dimensions of [
|
|
412
|
+
{ width: 512, height: 256 },
|
|
413
|
+
{ width: 256, height: 512 },
|
|
414
|
+
{ width: 640, height: 319 },
|
|
415
|
+
{ width: 0, height: 320 },
|
|
416
|
+
])
|
|
417
|
+
await expect(
|
|
418
|
+
resolveMediaReferences(video, {
|
|
419
|
+
read: () => Promise.resolve({ ...reference, ...dimensions }),
|
|
420
|
+
}),
|
|
421
|
+
).rejects.toThrow(/bounded/iu);
|
|
422
|
+
},
|
|
423
|
+
);
|
|
424
|
+
});
|
|
425
|
+
|
|
426
|
+
describe("retained Media provider output boundary", () => {
|
|
427
|
+
it("normalizes binary responses and all retained envelope forms", async () => {
|
|
428
|
+
await expect(
|
|
429
|
+
normalizeMediaProviderOutput(new Response(png)),
|
|
430
|
+
).resolves.toMatchObject({ format: "png" });
|
|
431
|
+
await expect(
|
|
432
|
+
normalizeMediaProviderOutput(
|
|
433
|
+
mediaProviderWireSchema.parse({
|
|
434
|
+
image: b64(png),
|
|
435
|
+
usage: { input: 1, output: 2 },
|
|
436
|
+
}),
|
|
437
|
+
),
|
|
438
|
+
).resolves.toMatchObject({ format: "png" });
|
|
439
|
+
await expect(
|
|
440
|
+
normalizeMediaProviderOutput({
|
|
441
|
+
image: [{ base64: "bad" }, { data: b64(jpeg) }],
|
|
442
|
+
}),
|
|
443
|
+
).resolves.toMatchObject({ format: "jpeg" });
|
|
444
|
+
await expect(
|
|
445
|
+
normalizeMediaProviderOutput(
|
|
446
|
+
{ images: [{ url: "https://provider.invalid/image.webp" }] },
|
|
447
|
+
{ fetchUrl: () => Promise.resolve(new Response(png)) },
|
|
448
|
+
),
|
|
449
|
+
).resolves.toMatchObject({ format: "png" });
|
|
450
|
+
});
|
|
451
|
+
|
|
452
|
+
it("reports failed provider entries and still accepts the next image without leaking provider data", async () => {
|
|
453
|
+
const report = vi
|
|
454
|
+
.spyOn(console, "error")
|
|
455
|
+
.mockImplementation(() => undefined);
|
|
456
|
+
const failure = new Error("private provider response");
|
|
457
|
+
const fetchUrl = vi
|
|
458
|
+
.fn()
|
|
459
|
+
.mockRejectedValueOnce(failure)
|
|
460
|
+
.mockResolvedValueOnce(new Response(png));
|
|
461
|
+
try {
|
|
462
|
+
await expect(
|
|
463
|
+
normalizeMediaProviderOutput(
|
|
464
|
+
{
|
|
465
|
+
images: [
|
|
466
|
+
{ url: "https://provider.invalid/first" },
|
|
467
|
+
{ url: "https://provider.invalid/second" },
|
|
468
|
+
],
|
|
469
|
+
},
|
|
470
|
+
{ fetchUrl },
|
|
471
|
+
),
|
|
472
|
+
).resolves.toMatchObject({ format: "png" });
|
|
473
|
+
expect(fetchUrl).toHaveBeenCalledTimes(2);
|
|
474
|
+
expect(report).toHaveBeenCalledWith(
|
|
475
|
+
"Media service operation failed",
|
|
476
|
+
{
|
|
477
|
+
operation: "provider-entry-fetch",
|
|
478
|
+
errorKind: "error",
|
|
479
|
+
},
|
|
480
|
+
expect.anything(),
|
|
481
|
+
);
|
|
482
|
+
expect(JSON.stringify(report.mock.calls)).not.toContain(
|
|
483
|
+
"private provider response",
|
|
484
|
+
);
|
|
485
|
+
} finally {
|
|
486
|
+
report.mockRestore();
|
|
487
|
+
}
|
|
488
|
+
});
|
|
489
|
+
|
|
490
|
+
it("rejects malformed output and keeps URL and encoded bytes unambiguous", async () => {
|
|
491
|
+
expect(() =>
|
|
492
|
+
mediaProviderWireSchema.parse({ result: "no image" }),
|
|
493
|
+
).toThrow();
|
|
494
|
+
await expect(
|
|
495
|
+
normalizeMediaProviderOutput({
|
|
496
|
+
image: "https://provider.invalid/image.png",
|
|
497
|
+
}),
|
|
498
|
+
).rejects.toThrow();
|
|
499
|
+
expect(() =>
|
|
500
|
+
mediaProviderWireSchema.parse({
|
|
501
|
+
image: [{ url: "file:///tmp/image.png" }],
|
|
502
|
+
}),
|
|
503
|
+
).toThrow();
|
|
504
|
+
await expect(
|
|
505
|
+
normalizeMediaProviderOutput(
|
|
506
|
+
{ image: [{ url: "https://provider.invalid/image.png" }] },
|
|
507
|
+
{
|
|
508
|
+
fetchUrl: () => Promise.resolve(new Response("not an image")),
|
|
509
|
+
},
|
|
510
|
+
),
|
|
511
|
+
).rejects.toThrow(/valid image/iu);
|
|
512
|
+
});
|
|
513
|
+
|
|
514
|
+
it("rejects output beyond the caller's byte bound before returning bytes", async () => {
|
|
515
|
+
await expect(
|
|
516
|
+
normalizeMediaProviderOutput({ image: b64(png) }, { maxBytes: 2 }),
|
|
517
|
+
).rejects.toThrow(/byte bound/iu);
|
|
518
|
+
await expect(
|
|
519
|
+
normalizeMediaProviderOutput(new Response(png), { maxBytes: 2 }),
|
|
520
|
+
).rejects.toThrow(/byte bound/iu);
|
|
521
|
+
});
|
|
522
|
+
});
|
|
523
|
+
|
|
524
|
+
describe("Workers AI image streams", () => {
|
|
525
|
+
it("normalizes the binding's raw image stream", async () => {
|
|
526
|
+
const stream = new Response(png).body;
|
|
527
|
+
if (stream === null) throw new Error("Missing test response body");
|
|
528
|
+
const output = await normalizeMediaProviderOutput(stream);
|
|
529
|
+
expect(output).toEqual({ bytes: png, format: "png" });
|
|
530
|
+
});
|
|
531
|
+
|
|
532
|
+
it("cancels an oversized stream and preserves the bound error", async () => {
|
|
533
|
+
let cancelled = false;
|
|
534
|
+
const stream = new ReadableStream({
|
|
535
|
+
start(controller) {
|
|
536
|
+
controller.enqueue(png);
|
|
537
|
+
},
|
|
538
|
+
cancel() {
|
|
539
|
+
cancelled = true;
|
|
540
|
+
throw new Error("cleanup failed");
|
|
541
|
+
},
|
|
542
|
+
});
|
|
543
|
+
await expect(
|
|
544
|
+
normalizeMediaProviderOutput(stream, { maxBytes: 4 }),
|
|
545
|
+
).rejects.toThrow("provider response exceeds the byte bound");
|
|
546
|
+
expect(cancelled).toBe(true);
|
|
547
|
+
expect(stream.locked).toBe(false);
|
|
548
|
+
});
|
|
549
|
+
});
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { validateMediaGenerationSettings } from "../src/index.js";
|
|
3
|
+
import { generate, type MediaAiPort } from "../src/cloudflare/provider.js";
|
|
4
|
+
import {
|
|
5
|
+
dinerPrompt,
|
|
6
|
+
dreamGardenPrompt,
|
|
7
|
+
rallyWreckPrompt,
|
|
8
|
+
} from "./fixtures/media-team-prompts.js";
|
|
9
|
+
|
|
10
|
+
// The Prints-side test (apps/prints/test/media-team-prompts.test.ts) drives the
|
|
11
|
+
// same prompts through draft validation; this one covers the provider body.
|
|
12
|
+
const sizes = {
|
|
13
|
+
"480p": { width: 480, height: 854 },
|
|
14
|
+
"720p": { width: 720, height: 1280 },
|
|
15
|
+
} as const;
|
|
16
|
+
const prompts = [
|
|
17
|
+
{ name: "dream garden", prompt: dreamGardenPrompt, references: 2 },
|
|
18
|
+
{ name: "rally wreck", prompt: rallyWreckPrompt, references: 3 },
|
|
19
|
+
{ name: "diner", prompt: dinerPrompt, references: 4 },
|
|
20
|
+
] as const;
|
|
21
|
+
const mp4 = Uint8Array.from([
|
|
22
|
+
0, 0, 0, 0x18, 0x66, 0x74, 0x79, 0x70, 0x69, 0x73, 0x6f, 0x6d, 0, 0, 2, 0,
|
|
23
|
+
0x69, 0x73, 0x6f, 0x6d, 0x6d, 0x70, 0x34, 0x31,
|
|
24
|
+
]);
|
|
25
|
+
const png = (value: number) => ({
|
|
26
|
+
bytes: new Uint8Array([value]),
|
|
27
|
+
format: "png" as const,
|
|
28
|
+
width: 1,
|
|
29
|
+
height: 1,
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
describe.each(["bytedance/seedance-2.0", "bytedance/seedance-2.5"])(
|
|
33
|
+
"media team prompts on %s",
|
|
34
|
+
(model) => {
|
|
35
|
+
it.each(prompts)(
|
|
36
|
+
"$name reaches the provider body unaltered",
|
|
37
|
+
async (entry) => {
|
|
38
|
+
for (const resolution of ["480p", "720p"] as const) {
|
|
39
|
+
for (const durationSeconds of [4, 6, 9]) {
|
|
40
|
+
const settings = validateMediaGenerationSettings({
|
|
41
|
+
model,
|
|
42
|
+
prompt: entry.prompt,
|
|
43
|
+
...sizes[resolution],
|
|
44
|
+
durationSeconds,
|
|
45
|
+
generateAudio: true,
|
|
46
|
+
references: Array.from({ length: entry.references }, (_, i) => ({
|
|
47
|
+
computerId: "pictures",
|
|
48
|
+
path: `/refs/${String(i + 1)}.png`,
|
|
49
|
+
role: "reference" as const,
|
|
50
|
+
})),
|
|
51
|
+
});
|
|
52
|
+
expect(settings).toMatchObject({
|
|
53
|
+
aspectRatio: "9:16",
|
|
54
|
+
resolution,
|
|
55
|
+
});
|
|
56
|
+
const bodies: unknown[] = [];
|
|
57
|
+
const ai: MediaAiPort = {
|
|
58
|
+
run: (_model, input) => {
|
|
59
|
+
bodies.push(input);
|
|
60
|
+
return Promise.resolve({
|
|
61
|
+
state: "Completed",
|
|
62
|
+
result: { video: "https://results.example/clip.mp4" },
|
|
63
|
+
});
|
|
64
|
+
},
|
|
65
|
+
};
|
|
66
|
+
await generate(
|
|
67
|
+
ai,
|
|
68
|
+
settings,
|
|
69
|
+
Array.from({ length: entry.references }, (_, i) => png(i + 1)),
|
|
70
|
+
undefined,
|
|
71
|
+
undefined,
|
|
72
|
+
() => Promise.resolve(new Response(mp4)),
|
|
73
|
+
);
|
|
74
|
+
expect(bodies).toHaveLength(1);
|
|
75
|
+
expect(bodies[0]).toMatchObject({
|
|
76
|
+
prompt: entry.prompt.trim(),
|
|
77
|
+
aspect_ratio: "9:16",
|
|
78
|
+
resolution,
|
|
79
|
+
duration: durationSeconds,
|
|
80
|
+
generate_audio: true,
|
|
81
|
+
reference_images: Array.from(
|
|
82
|
+
{ length: entry.references },
|
|
83
|
+
(_, i) =>
|
|
84
|
+
`data:image/png;base64,${Buffer.from([i + 1]).toString("base64")}`,
|
|
85
|
+
),
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
},
|
|
90
|
+
);
|
|
91
|
+
},
|
|
92
|
+
);
|