Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
230 changes: 177 additions & 53 deletions apps/api/src/media-lit-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,56 +7,108 @@ afterEach(() => {
globalThis.fetch = originalFetch;
});

// Response bodies in MediaLit v0.8.0's shape (`MediaResponse` in its
// apps/api/src/media/schemas.ts). Unsealed media is served from the private
// `i/` prefix through a signed URL; for an app with WebP on, sealing converts
// the image and publishes it under `p/` as `main.webp`.
const mediaId = "medialit-asset-1";
const originalSize = 2048;
const webpSize = 612;

const unsealedPng = {
mediaId,
originalFileName: "cover.png",
mimeType: "image/png",
size: originalSize,
access: "public",
file: `https://cdn.example/i/${mediaId}/main.png?Expires=123&Signature=sig`,
thumbnail: `https://cdn.example/i/${mediaId}/thumb.webp?Expires=123&Signature=sig`,
group: "school-internal-id",
processing: { status: "pending" },
derivatives: {},
};

const sealedWebp = {
...unsealedPng,
mimeType: "image/webp",
size: webpSize,
file: `https://cdn.example/p/${mediaId}/main.webp`,
thumbnail: `https://cdn.example/p/${mediaId}/thumb.webp`,
processing: { status: "ready" },
derivatives: {
thumbnail: {
status: "ready",
url: `https://cdn.example/p/${mediaId}/thumb.webp`,
mimeType: "image/webp",
size: 180,
},
},
};

// WebP conversion failed: MediaLit keeps and publishes the original.
const sealedFailedConversion = {
...unsealedPng,
file: `https://cdn.example/p/${mediaId}/main.png`,
thumbnail: `https://cdn.example/p/${mediaId}/thumb.webp`,
processing: { status: "failed", error: "WebP conversion failed" },
derivatives: {
thumbnail: {
status: "ready",
url: `https://cdn.example/p/${mediaId}/thumb.webp`,
mimeType: "image/webp",
size: 180,
},
preview: { status: "failed", error: "unsupported" },
},
};

function client() {
return new HttpMediaLitClient({
endpoint: "https://media.example/",
apiKey: "server-only-key",
});
}

function recordCalls(
handle: (url: string) => Response,
): Array<{ url: string; method: string }> {
const calls: Array<{ url: string; method: string }> = [];
globalThis.fetch = (async (input, init) => {
const url = String(input);
calls.push({ url, method: init?.method ?? "GET" });
return handle(url);
}) as typeof fetch;
return calls;
}

describe("MediaLit HTTP client", () => {
it("requests a scoped signature and maps the upload/seal lifecycle", async () => {
const calls: Array<{ url: string; method: string; body?: string }> = [];
globalThis.fetch = (async (input, init) => {
const url = String(input);
calls.push({
url,
method: init?.method ?? "GET",
body: typeof init?.body === "string" ? init.body : undefined,
});
// Stateful like MediaLit: `get` returns the original until sealed, and a
// repeat seal of sealed media answers 200 with the sealed media.
let sealed = false;
const calls = recordCalls((url) => {
if (url.endsWith("/media/signature/create")) {
return Response.json({ signature: "short-lived-upload-token" });
}
if (url.endsWith("/media/seal/medialit-asset-1")) {
return Response.json({
mediaId: "medialit-asset-1",
group: "school-internal-id",
originalFileName: "cover.png",
mimeType: "image/png",
size: 2048,
file: "https://cdn.example/cover.png",
thumbnail: "https://cdn.example/cover-thumb.png",
});
if (url.endsWith(`/media/get/${mediaId}`)) {
return Response.json(sealed ? sealedWebp : unsealedPng);
}
if (url.endsWith("/media/get/medialit-asset-1")) {
return Response.json({
mediaId: "medialit-asset-1",
group: "school-internal-id",
originalFileName: "cover.png",
mimeType: "image/png",
size: 2048,
file: "https://signed.example/cover.png?expires=123",
thumbnail: "https://signed.example/cover-thumb.png?expires=123",
});
if (url.endsWith(`/media/seal/${mediaId}`)) {
sealed = true;
return Response.json(sealedWebp);
}
if (url.endsWith("/media/delete/medialit-asset-1")) {
if (url.endsWith(`/media/delete/${mediaId}`)) {
return new Response(null, { status: 204 });
}
return Response.json({ error: "unexpected request" }, { status: 404 });
}) as typeof fetch;

const client = new HttpMediaLitClient({
endpoint: "https://media.example/",
apiKey: "server-only-key",
});
const authorization = await client.authorizeUpload({

const mediaLit = client();
const authorization = await mediaLit.authorizeUpload({
schoolId: "school-internal-id",
fileName: "cover.png",
mimeType: "image/png",
byteSize: 2048,
byteSize: originalSize,
purpose: "product_artwork",
accessPolicy: "public",
expiresAt: new Date("2026-03-02T00:10:00.000Z"),
Expand All @@ -69,37 +121,109 @@ describe("MediaLit HTTP client", () => {
uploadFields: { access: "public", group: "school-internal-id" },
});

const asset = await client.finalizeUpload({
// Before sealing, MediaLit reports the uploaded original. `processing`
// and `derivatives` are not part of the asset.
const inspected = await mediaLit.inspectUpload({
schoolId: "school-internal-id",
uploadId: "medialit-asset-1",
uploadId: mediaId,
});
expect(asset).toEqual({
mediaLitId: "medialit-asset-1",
expect(inspected).toEqual({
mediaLitId: mediaId,
group: "school-internal-id",
canonicalUrl: "https://cdn.example/cover.png",
thumbnailUrl: "https://cdn.example/cover-thumb.png",
canonicalUrl: unsealedPng.file,
thumbnailUrl: unsealedPng.thumbnail,
fileName: "cover.png",
mimeType: "image/png",
byteSize: 2048,
byteSize: originalSize,
width: null,
height: null,
});
const delivered = await client.getAsset({

// Sealing converts to WebP, so the finalized asset differs from the
// inspected one.
const finalized = await mediaLit.finalizeUpload({
schoolId: "school-internal-id",
mediaLitId: "medialit-asset-1",
uploadId: mediaId,
});
expect(delivered.canonicalUrl).toBe(
"https://signed.example/cover.png?expires=123",
);
await client.deleteAsset({
expect(finalized).toEqual({
mediaLitId: mediaId,
group: "school-internal-id",
canonicalUrl: `https://cdn.example/p/${mediaId}/main.webp`,
thumbnailUrl: `https://cdn.example/p/${mediaId}/thumb.webp`,
fileName: "cover.png",
mimeType: "image/webp",
byteSize: webpSize,
width: null,
height: null,
});

expect(
await mediaLit.getAsset({ schoolId: "school-internal-id", mediaLitId: mediaId }),
).toEqual(finalized);
expect(
await mediaLit.finalizeUpload({
schoolId: "school-internal-id",
uploadId: mediaId,
}),
).toEqual(finalized);

await mediaLit.deleteAsset({
schoolId: "school-internal-id",
mediaLitId: "medialit-asset-1",
mediaLitId: mediaId,
});
expect(calls.map((call) => call.url)).toEqual([
"https://media.example/media/signature/create",
"https://media.example/media/seal/medialit-asset-1",
"https://media.example/media/get/medialit-asset-1",
"https://media.example/media/delete/medialit-asset-1",
`https://media.example/media/get/${mediaId}`,
`https://media.example/media/seal/${mediaId}`,
`https://media.example/media/get/${mediaId}`,
`https://media.example/media/seal/${mediaId}`,
`https://media.example/media/delete/${mediaId}`,
]);
});

it("finalizes media whose conversion failed with the kept original", async () => {
recordCalls((url) =>
url.endsWith(`/media/seal/${mediaId}`)
? Response.json(sealedFailedConversion)
: Response.json({ error: "unexpected request" }, { status: 404 }),
);

const asset = await client().finalizeUpload({
schoolId: "school-internal-id",
uploadId: mediaId,
});
expect(asset).toEqual({
mediaLitId: mediaId,
group: "school-internal-id",
canonicalUrl: `https://cdn.example/p/${mediaId}/main.png`,
thumbnailUrl: `https://cdn.example/p/${mediaId}/thumb.webp`,
fileName: "cover.png",
mimeType: "image/png",
byteSize: originalSize,
width: null,
height: null,
});
});

it("surfaces a seal still running elsewhere as a failed request without retrying", async () => {
// MediaLit waits for the other seal, then gives up with 503 and
// Retry-After. The client does not retry; the caller sees the status.
const calls = recordCalls((url) =>
url.endsWith(`/media/seal/${mediaId}`)
? Response.json(
{
error: "The media is being sealed by another request. Try again shortly.",
},
{ status: 503, headers: { "Retry-After": "1" } },
)
: Response.json({ error: "unexpected request" }, { status: 404 }),
);

await expect(
client().finalizeUpload({ schoolId: "school-internal-id", uploadId: mediaId }),
).rejects.toThrow("medialit_request_failed_503");
expect(calls).toEqual([
{ url: `https://media.example/media/seal/${mediaId}`, method: "POST" },
]);
});

Expand Down
Loading