import { test, expect } from "@playwright/test"; import JSZip from "jszip"; import { NextRequest, NextResponse } from "next/server"; import sharp from "sharp"; import { once } from "node:events"; import { Readable } from "node:stream"; import { mkdtemp, rm } from "node:fs/promises"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { randomUUID } from "node:crypto"; import { serverLoader } from "../helpers/canvas-download-server-loader"; test("the worker prepares 13 images and uploads the ZIP through Qiniu multipart", async () => { const directory = await mkdtemp(join(tmpdir(), "zip-worker-test-")); const token = "local-test-secret".repeat(3); const id = randomUUID(); const key = "canvas-downloads/" + id + ".zip"; const rgb = { r: 25, g: 100, b: 170 }; const png = await sharp({ create: { width: 800, height: 800, channels: 3, background: rgb } }).png().toBuffer(); const jpeg = await sharp({ create: { width: 800, height: 800, channels: 3, background: rgb } }).jpeg().toBuffer(); const parts: Buffer[] = []; let complete = 0, downloads = 0; const loader = serverLoader({ "../../lib/video-canvas/download-media": { openPublicMedia: async (url: string, options: { allowUrl: (url: URL) => boolean }) => { const parsed = new URL(url); expect(options.allowUrl(parsed)).toBe(true); expect(options.allowUrl(new URL("https://ai.yydd.ai/api/image-proxy"))).toBe(false); downloads++; const original = parsed.pathname.endsWith("original.jpeg") ? jpeg : png; return Object.assign(Readable.from([original]), { statusCode: 200, headers: { "content-type": original === png ? "image/png" : "image/jpeg" }, }); }, }, fetch: async (uri: URL | string, init: RequestInit) => { const url = new URL(uri); if (init.method === "HEAD") return new Response(null, { status: 404 }); expect(url.hostname).toBe("upload.qiniup.com"); expect(new Headers(init.headers).get("Authorization")).toBe("UpToken scoped-test-token"); if (url.pathname.endsWith("/uploads")) return Response.json({ uploadId: "one-upload" }); if (init.method === "PUT") { parts.push(Buffer.from(init.body as Uint8Array)); return Response.json({ etag: "one-part" }); } expect(init.method).toBe("POST"); expect(JSON.parse(init.body as string).mimeType).toBe("application/zip"); complete++; return Response.json({ key, fsize: Buffer.concat(parts).length }); }, }, { CANVAS_DOWNLOAD_ZIP_DIR: directory, CANVAS_ZIP_WORKER_TOKEN: token }); const { router } = loader("services/canvas-zip/server.ts"); router.listen(0, "127.0.0.1"); await once(router, "listening"); const endpoint = "http://127.0.0.1:" + router.address().port + "/v1/jobs/" + id; const files = Array.from({ length: 13 }, (_, i) => ({ name: "详情图-" + i, sources: ["https://cdn.example/" + i + (i === 12 ? "/original.jpeg" : "/original.png")], })); const job = { attempt: 0, folderName: "详情图", expiresAt: Date.now() + 60_000, appOrigin: "https://ai.yydd.ai", files, storage: { bucket: "qa-bucket", token: "scoped-test-token", publicUrl: "https://cdn.example/" + key }, }; const post = async () => fetch(endpoint, { method: "PUT", headers: { Authorization: "Bearer " + token, "content-type": "application/json" }, body: JSON.stringify(job), }); try { expect((await fetch(endpoint, { method: "PUT" })).status).toBe(401); expect((await post()).status).toBe(200); await expect.poll(async () => { const response = await post(); const state = await response.json(); return state.status; }, { timeout: 10_000 }).toBe("completed"); await expect((await post()).json()).resolves.toMatchObject({ successCount: 13, failedCount: 0 }); expect(downloads).toBe(13); expect(complete).toBe(1); const archive = await JSZip.loadAsync(Buffer.concat(parts), { checkCRC32: true }); const images = Object.values(archive.files).filter(file => !file.dir); expect(images).toHaveLength(13); for (const [i, entry] of images.entries()) { const data = await entry.async("nodebuffer"); expect(entry.name).toMatch(/\.png$/); if (i < 12) expect(data).toEqual(png); else { const metadata = await sharp(data).metadata(); expect(metadata).toMatchObject({ width: 800, height: 800, space: "srgb", format: "png" }); const color = (await sharp(data).extract({ left: 20, top: 20, width: 1, height: 1 }).raw().toBuffer()); expect(Math.abs(color[0] - rgb.r)).toBeLessThan(4); expect(Math.abs(color[1] - rgb.g)).toBeLessThan(4); expect(Math.abs(color[2] - rgb.b)).toBeLessThan(4); } } } finally { await new Promise(resolve => router.close(() => resolve())); await rm(directory, { recursive: true, force: true }); } }); test("the style-detail route gives the worker addresses and returns a CDN redirect even when local is requested", async () => { const directory = await mkdtemp(join(tmpdir(), "zip-remote-route-")); const id = randomUUID(); const callbacks: Array<() => Promise> = []; let owner = "owner"; const zipUrl = "https://cdn.example/canvas-downloads/" + id + ".zip"; const raw = { id: "storage", isEnabled: true }; const worker = "http://127.0.0.1:18343"; const config = { provider: "qiniu-kodo", region: "cn-east-1", bucket: "qa", publicUrl: "https://cdn.example", accessKeyId: "fake-public-id", secretAccessKey: "fake-private-key" }; let workerCalls = 0; const route = serverLoader({ "@/lib/observability/active-work": { trackRuntimeWork: async (fn: () => Promise) => fn() }, "@/app/(auth)/auth": { auth: async () => ({ user: { id: owner } }) }, "next/server": { NextResponse, after: (fn: () => Promise) => callbacks.push(fn) }, "@/lib/canvas/photoshop-safe-download": { fetchCanvasDownloadAsset: () => { throw new Error("The production server must not fetch the originals"); }, }, "@/lib/storage/image-archive-db": { getArchivedImageUrlMappings: async () => ({ "https://ai.yydd.ai/archive": "https://cdn.example/archive.png", }) }, "@/lib/db/queries": { getDefaultStorageConfig: async () => raw, getStorageConfigById: async () => raw }, "@/lib/storage/service": { toDbS3UploadConfig: () => config, buildS3PublicUrl: () => zipUrl }, "@/lib/storage/canvas-zip-delivery": { publishCanvasZip: () => { throw new Error("The production server must not upload the ZIP"); }, }, fetch: async (uri: URL | string, init: RequestInit) => { const url = new URL(uri); if (url.origin === worker) { workerCalls++; const job = JSON.parse(init.body as string); expect(job.files[0].sources).toEqual(["https://cdn.example/archive.png"]); expect(job.files[1].sources).toEqual(["https://provider.example/result.png"]); expect(job).not.toHaveProperty("cookie"); expect(job.storage).not.toHaveProperty("secretAccessKey"); const policy = JSON.parse(Buffer.from(job.storage.token.split(":")[2], "base64url").toString()); expect(policy.scope).toBe("qa:canvas-downloads/" + id + ".zip"); return Response.json({ status: "completed", stage: "打包完成", processedCount: 2, successCount: 2, failedCount: 0, compressPercent: 100, bytes: 12345 }); } expect(url.href).toContain(zipUrl); expect(init.method).toBe("HEAD"); return new Response(null, { headers: { "content-length": "12345" } }); }, }, { CANVAS_DOWNLOAD_ZIP_DIR: directory, CANVAS_ZIP_WORKER_URL: worker, CANVAS_ZIP_WORKER_TOKEN: "local-test-secret".repeat(3) })("app/api/canvas/download-zip-task/route.ts"); const endpoint = "https://ai.yydd.ai/api/canvas/download-zip-task"; try { const started = await route.POST(new NextRequest(endpoint, { method: "POST", body: JSON.stringify({ taskId: id, scope: "style-detail", folderName: "详情图", files: [ { url: "https://ai.yydd.ai/archive" }, { url: "https://provider.example/result.png" }, ] }), headers: { cookie: "auth=private-test-cookie" }, })); expect(started.status).toBe(202); while (callbacks.length) await callbacks.shift()!(); expect(workerCalls).toBe(1); const download = await route.GET(new NextRequest(endpoint + "?taskId=" + id + "&download=1&local=1")); expect(download.status).toBe(307); expect(download.headers.get("location")).toContain(zipUrl); owner = "another-user"; const denied = await route.GET(new NextRequest(endpoint + "?taskId=" + id + "&download=1")); expect(denied.status).toBe(404); } finally { await rm(directory, { recursive: true, force: true }); } });