161 lines
11 KiB
TypeScript
161 lines
11 KiB
TypeScript
import { expect, test } from "@playwright/test";
|
||
import { randomUUID } from "node:crypto";
|
||
import { serverLoader } from "../helpers/canvas-download-server-loader";
|
||
|
||
const auth = { auth: async () => ({ user: { id: "owner" } }) };
|
||
const request = () => ({
|
||
url: "https://app.example/api/canvas/download-image", headers: new Headers(),
|
||
json: async () => ({ url: "https://provider.example/original.jpg", preferDirect: true, preparation: "async" }),
|
||
});
|
||
|
||
test("download failures distinguish permanent sources, transport, conversion and storage with actionable advice", () => {
|
||
const { getDownloadFailure, formatDownloadFailure } = serverLoader({})("lib/canvas/download-error.ts");
|
||
for (const [error, code, status] of [
|
||
["HTTP 404", "SOURCE_NOT_FOUND", 404], ["HTTP 403", "SOURCE_FORBIDDEN", 403],
|
||
["TimeoutError: slow body", "SOURCE_TIMEOUT", 504], ["fetch failed", "SOURCE_UNAVAILABLE", 503],
|
||
["source returned an error document", "INVALID_IMAGE", 422], ["unsupported image format", "IMAGE_CONVERSION_FAILED", 422],
|
||
["STORAGE_WRITE_FAILED", "DOWNLOAD_STORAGE_FAILED", 503], ["DOWNLOAD_BUSY", "DOWNLOAD_BUSY", 429],
|
||
]) {
|
||
const failure = getDownloadFailure(new Error(error as string));
|
||
expect(failure).toMatchObject({ code, status });
|
||
expect(failure.recommendation).toBeTruthy();
|
||
expect(formatDownloadFailure(failure, "feedback-id")).toContain("下载编号:feedback-id");
|
||
if (status === 404) expect(failure.recommendation).not.toContain("稍后重试");
|
||
}
|
||
});
|
||
|
||
test("failed preparation polls retain the same cause and feedback ID without rerunning the load", async () => {
|
||
const { createDownloadPreparationQueue } = serverLoader({})("lib/canvas/download-preparation.ts");
|
||
const runs: Array<() => Promise<void>> = [];
|
||
let loads = 0;
|
||
const queue = createDownloadPreparationQueue({
|
||
load: async () => { loads++; throw new Error("HTTP 404 https://source.example/private?token=private"); },
|
||
read: async () => null, write: async () => {}, schedule: (run: () => Promise<void>) => runs.push(run),
|
||
});
|
||
const input = { userId: "owner", urls: ["https://source.example/original"], requestOrigin: "https://app.example", cookie: "session=private" };
|
||
const accepted = await queue.request(input);
|
||
await runs[0]();
|
||
for (let i = 0; i < 3; i++) {
|
||
const failed = await queue.request(input);
|
||
expect(failed).toMatchObject({ status: "failed", requestId: accepted.requestId, failure: { code: "SOURCE_NOT_FOUND", status: 404 } });
|
||
expect(failed.error).not.toContain("token=private");
|
||
}
|
||
expect(loads).toBe(1);
|
||
});
|
||
|
||
test("download queue saturation returns a retryable 429 instead of a source 502", async () => {
|
||
const { createDownloadPreparationQueue } = serverLoader({})("lib/canvas/download-preparation.ts");
|
||
const queue = createDownloadPreparationQueue({ load: async () => { throw new Error("not started"); }, read: async () => null, write: async () => {}, schedule() {} });
|
||
for (let i = 0; i < 4; i++) await queue.request({ userId: "owner", urls: [`https://source.example/${i}`], requestOrigin: "https://app.example", cookie: "" });
|
||
expect(await queue.request({ userId: "owner", urls: ["https://source.example/fifth"], requestOrigin: "https://app.example", cookie: "" }))
|
||
.toMatchObject({ status: "failed", failure: { code: "DOWNLOAD_BUSY", status: 429, retryable: true } });
|
||
});
|
||
|
||
test("cold archived downloads wait for CDN preparation and never buffer the file through the app", async () => {
|
||
const queued: string[] = [];
|
||
let ready = false;
|
||
const route = serverLoader({
|
||
"@/app/(auth)/auth": auth,
|
||
"next/server": { NextResponse: Response, after() {} },
|
||
"@/lib/storage/canvas-cdn-download": { resolveCanvasCdnDownload: async () => ({
|
||
task: { id: "archive-task", targetUrl: "https://cdn.example/original.jpg", metadata: { downloadAsset: { status: "pending" } } },
|
||
canPrepareDirect: true,
|
||
direct: ready ? { delivery: "cdn", downloadUrl: "https://cdn.example/prepared.png", filename: "original.png", bytes: 4000000 } : null,
|
||
}) },
|
||
"@/lib/storage/image-download-preparation": { scheduleImageDownloadPreparation: (id: string) => queued.push(id) },
|
||
"@/lib/canvas/photoshop-safe-download": { sanitizeCanvasDownloadFileName: () => "original", fetchCanvasDownloadAsset: () => { throw new Error("Must use CDN delivery"); } },
|
||
})("app/api/canvas/download-image/route.ts");
|
||
const first = await route.POST(request());
|
||
expect(first.status).toBe(202);
|
||
expect(await first.json()).toMatchObject({ delivery: "preparing", requestId: "archive-task" });
|
||
ready = true;
|
||
const second = await route.POST(request());
|
||
expect(second.status).toBe(200);
|
||
expect(await second.json()).toMatchObject({ delivery: "cdn", bytes: 4000000 });
|
||
expect(queued).toEqual(["archive-task"]);
|
||
});
|
||
|
||
test("an expired archived source returns its permanent cause and download task ID", async () => {
|
||
const errors = serverLoader({})("lib/canvas/download-error.ts");
|
||
const route = serverLoader({
|
||
"@/app/(auth)/auth": auth, "next/server": { NextResponse: Response, after() {} },
|
||
"@/lib/storage/canvas-cdn-download": { resolveCanvasCdnDownload: async () => ({ task: {
|
||
id: "failed-archive", targetUrl: "https://cdn.example/missing.png",
|
||
metadata: { downloadAsset: { status: "abandoned", failure: errors.getDownloadFailure("HTTP 404") } },
|
||
}, canPrepareDirect: true, direct: null }) },
|
||
"@/lib/storage/image-download-preparation": { scheduleImageDownloadPreparation() { throw new Error("Do not retry a permanent failure"); } },
|
||
})("app/api/canvas/download-image/route.ts");
|
||
const response = await route.POST(request());
|
||
expect(response.status).toBe(404);
|
||
expect(await response.json()).toMatchObject({ requestId: "failed-archive", failure: { code: "SOURCE_NOT_FOUND" } });
|
||
});
|
||
|
||
test("the download browser keeps the structured cause, advice and feedback ID", async () => {
|
||
const failure = { code: "SOURCE_FORBIDDEN", message: "图片源站拒绝访问原图。", recommendation: "请联系管理员检查访问权限。", status: 403, retryable: false };
|
||
const client = serverLoader({ fetch: async () => Response.json({ failure, requestId: "real-feedback" }, { status: 403 }) })("lib/client/canvas-image-download.ts");
|
||
let caught: unknown;
|
||
try { await client.downloadCanvasImageAsPng({ url: "https://source.example/original" }); } catch (error) { caught = error; }
|
||
expect(client.getCanvasImageDownloadErrorMessage(caught)).toBe("图片源站拒绝访问原图。请联系管理员检查访问权限。\n下载编号:real-feedback");
|
||
});
|
||
|
||
test("the preparation deadline cancels the source body as well as its headers", async () => {
|
||
const cache = serverLoader({ fetch: async (_url: string, options: { signal: AbortSignal }) => ({
|
||
ok: true, headers: new Headers({ "content-type": "image/png" }),
|
||
arrayBuffer: () => new Promise((_resolve, reject) => options.signal.addEventListener("abort", () => reject(options.signal.reason), { once: true })),
|
||
}) })("lib/storage/image-byte-cache.ts");
|
||
const controller = new AbortController();
|
||
const fetching = cache.fetchImageBytes(`https://source.example/${randomUUID()}`, { timeoutMs: 10000, signal: controller.signal });
|
||
setTimeout(() => controller.abort(new Error("Preparation canceled")), 20);
|
||
await expect(fetching).rejects.toThrow("Preparation canceled");
|
||
});
|
||
|
||
test("requested downloads are prioritized with two workers before scanning background assets", async () => {
|
||
let active = 0; let maximum = 0; let scans = 0;
|
||
const releases: Array<() => void> = [];
|
||
const workRecords: Array<{ active: number; durable: number }> = [];
|
||
const claimed: string[] = []; const saved: string[] = [];
|
||
const bytes = Buffer.from("compatible PNG fixture");
|
||
const worker = serverLoader({
|
||
"node:fs": { writeFileSync: (_path: string, value: string) => workRecords.push(JSON.parse(value)), unlinkSync() {} },
|
||
"./image-archive-db": {
|
||
claimImageDownloadTask: async (id: string) => { claimed.push(id); return { id, targetUrl: `https://cdn.example/${id}.png`, metadata: {} }; },
|
||
saveImageDownloadAsset: async (id: string) => saved.push(id),
|
||
getImageDownloadTasks: async () => { scans++; return []; }, hasPendingImageDownloads: async () => false,
|
||
},
|
||
"./image-byte-cache": { fetchImageBytes: async () => { active++; maximum = Math.max(maximum, active); await new Promise<void>(resolve => releases.push(resolve)); active--; return { buffer: bytes, contentType: "image/png" }; }, cacheImageBytes: async () => {} },
|
||
"@/lib/canvas/photoshop-safe-download": { normalizeCanvasDownloadAsset: async () => ({ ok: true, bytes, contentType: "image/png" }) },
|
||
"./qiniu-image-archive": { getReadyQiniuImageDownload: async () => undefined, prepareQiniuWebpDownload: async () => undefined },
|
||
"./service": { uploadFile() { throw new Error("Compatible PNG must retain its object"); } },
|
||
})("lib/storage/image-download-preparation.ts");
|
||
worker.scheduleImageDownloadPreparation("one"); worker.scheduleImageDownloadPreparation("two"); worker.scheduleImageDownloadPreparation("three");
|
||
await expect.poll(() => active).toBe(2);
|
||
expect(workRecords.at(-1)).toMatchObject({ active: 1, durable: 0 });
|
||
expect(scans).toBe(0);
|
||
releases.splice(0).forEach(release => release());
|
||
await expect.poll(() => claimed.length).toBe(3);
|
||
releases.splice(0).forEach(release => release());
|
||
await expect.poll(() => saved.length).toBe(3);
|
||
expect(maximum).toBe(2); expect(scans).toBe(0);
|
||
});
|
||
|
||
test("archive URL replacement cannot block the download worker wakeup", async () => {
|
||
let scheduled = false; let finish!: () => void; let replacing = false;
|
||
const gate = new Promise<void>(resolve => { finish = resolve; });
|
||
const job = serverLoader({
|
||
"@/lib/observability/active-work": { trackRuntimeWork: (execute: () => Promise<unknown>) => execute() },
|
||
"@/lib/storage/image-download-preparation": { scheduleImageDownloadPreparation() { scheduled = true; } },
|
||
"@/lib/storage/image-archive-db": { recoverStaleImageArchiveTasks: async () => {}, getImageArchiveTasksToStore: async () => [],
|
||
getImageArchiveTasksToReplace: async () => [{ id: "archive", sourceUrl: "https://source.example/a", targetUrl: "https://cdn.example/a" }],
|
||
markImageArchiveTaskReplacing: async () => true, markImageArchiveTaskCompleted: async () => {}, hasPendingImageDownloads: async () => false,
|
||
getImageArchiveTaskStats: async () => ({ pendingStore: 0, pendingReplace: 0 }),
|
||
},
|
||
"@/lib/storage/image-url-replacement": { replaceImageUrlInDatabase: async () => { replacing = true; await gate; return { changed: 1 }; } },
|
||
"@/lib/storage/image-archive": {},
|
||
})("lib/jobs/image-archive-job.ts");
|
||
const running = job.runImageArchiveJob();
|
||
await expect.poll(() => replacing).toBe(true);
|
||
expect(scheduled).toBe(true);
|
||
finish();
|
||
expect((await running).replace.succeeded).toBe(1);
|
||
});
|