|
| 1 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import { Hono } from "hono"; |
| 3 | +import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; |
| 4 | +import { tmpdir } from "node:os"; |
| 5 | +import { join } from "node:path"; |
| 6 | +import { registerMediaRoutes } from "./media"; |
| 7 | +import { registerRenderRoutes } from "./render"; |
| 8 | +import type { MediaProcessingJobState, RenderJobState, StudioApiAdapter } from "../types"; |
| 9 | + |
| 10 | +const MINUTE = 60_000; |
| 11 | +const dirs: string[] = []; |
| 12 | + |
| 13 | +afterEach(() => { |
| 14 | + vi.useRealTimers(); |
| 15 | + for (const dir of dirs.splice(0)) rmSync(dir, { recursive: true, force: true }); |
| 16 | +}); |
| 17 | + |
| 18 | +function adapterFor(project: string, extra: Partial<StudioApiAdapter>): StudioApiAdapter { |
| 19 | + return { |
| 20 | + listProjects: () => [], |
| 21 | + resolveProject: async (id: string) => ({ id, dir: project }), |
| 22 | + bundle: async () => null, |
| 23 | + lint: async () => ({ findings: [] }), |
| 24 | + runtimeUrl: "/api/runtime.js", |
| 25 | + rendersDir: () => join(project, "renders"), |
| 26 | + startRender: (opts) => ({ |
| 27 | + id: opts.jobId, |
| 28 | + status: "rendering", |
| 29 | + progress: 0, |
| 30 | + outputPath: opts.outputPath, |
| 31 | + }), |
| 32 | + ...extra, |
| 33 | + }; |
| 34 | +} |
| 35 | + |
| 36 | +/** The progress route's status for a job: 200 while the server still has it, 404 once it is forgotten. */ |
| 37 | +async function progressStatus(app: Hono, path: string): Promise<number> { |
| 38 | + const response = await app.request(`http://localhost${path}`); |
| 39 | + await response.body?.cancel(); |
| 40 | + return response.status; |
| 41 | +} |
| 42 | + |
| 43 | +const project = () => { |
| 44 | + const dir = mkdtempSync(join(tmpdir(), "hf-finished-jobs-")); |
| 45 | + dirs.push(dir); |
| 46 | + return dir; |
| 47 | +}; |
| 48 | + |
| 49 | +describe("a finished job stays readable after it finishes, however long it ran", () => { |
| 50 | + it("keeps a render that ran 6 minutes for the sweep after it finished, then forgets it", async () => { |
| 51 | + vi.useFakeTimers(); |
| 52 | + const app = new Hono(); |
| 53 | + let job: RenderJobState | undefined; |
| 54 | + registerRenderRoutes( |
| 55 | + app, |
| 56 | + adapterFor(project(), { |
| 57 | + startRender: (opts) => |
| 58 | + (job = { id: opts.jobId, status: "rendering", progress: 0, outputPath: opts.outputPath }), |
| 59 | + }), |
| 60 | + ); |
| 61 | + const started = await app.request("http://localhost/projects/demo/render", { |
| 62 | + method: "POST", |
| 63 | + headers: { "content-type": "application/json" }, |
| 64 | + body: JSON.stringify({ fps: 30, quality: "standard", format: "mp4" }), |
| 65 | + }); |
| 66 | + const { jobId } = (await started.json()) as { jobId: string }; |
| 67 | + const progress = `/render/${jobId}/progress`; |
| 68 | + |
| 69 | + await vi.advanceTimersByTimeAsync(6 * MINUTE); |
| 70 | + Object.assign(job!, { status: "complete", progress: 100 }); |
| 71 | + await vi.advanceTimersByTimeAsync(MINUTE + 1_000); |
| 72 | + expect(await progressStatus(app, progress)).toBe(200); |
| 73 | + |
| 74 | + await vi.advanceTimersByTimeAsync(6 * MINUTE); |
| 75 | + expect(await progressStatus(app, progress)).toBe(404); |
| 76 | + }); |
| 77 | + |
| 78 | + it("keeps a background removal that ran 6 minutes for the TTL after it finished", async () => { |
| 79 | + vi.useFakeTimers({ toFake: ["Date"] }); |
| 80 | + const dir = project(); |
| 81 | + mkdirSync(join(dir, "assets"), { recursive: true }); |
| 82 | + writeFileSync(join(dir, "assets", "clip.mp4"), "video"); |
| 83 | + let job: MediaProcessingJobState | undefined; |
| 84 | + const app = new Hono(); |
| 85 | + registerMediaRoutes( |
| 86 | + app, |
| 87 | + adapterFor(dir, { |
| 88 | + startBackgroundRemoval: (opts) => |
| 89 | + (job = { |
| 90 | + id: opts.jobId, |
| 91 | + status: "processing", |
| 92 | + progress: 0, |
| 93 | + inputAssetPath: opts.inputAssetPath, |
| 94 | + outputAssetPath: opts.outputAssetPath, |
| 95 | + outputPath: opts.outputPath, |
| 96 | + }), |
| 97 | + }), |
| 98 | + ); |
| 99 | + const started = await app.request("http://localhost/projects/demo/media/remove-background", { |
| 100 | + method: "POST", |
| 101 | + headers: { "content-type": "application/json" }, |
| 102 | + body: JSON.stringify({ inputPath: "assets/clip.mp4" }), |
| 103 | + }); |
| 104 | + const { jobId } = (await started.json()) as { jobId: string }; |
| 105 | + const progress = `/media-jobs/${jobId}/progress`; |
| 106 | + |
| 107 | + vi.setSystemTime(Date.now() + 6 * MINUTE); |
| 108 | + Object.assign(job!, { status: "complete", progress: 100 }); |
| 109 | + expect(await progressStatus(app, progress)).toBe(200); |
| 110 | + vi.setSystemTime(Date.now() + MINUTE); |
| 111 | + expect(await progressStatus(app, progress)).toBe(200); |
| 112 | + |
| 113 | + vi.setSystemTime(Date.now() + 5 * MINUTE); |
| 114 | + expect(await progressStatus(app, progress)).toBe(404); |
| 115 | + }); |
| 116 | +}); |
0 commit comments