|
| 1 | +import { describe, expect, it, vi } from "vitest"; |
| 2 | +import { Hono } from "hono"; |
| 3 | +import { mkdtempSync, rmSync } from "node:fs"; |
| 4 | +import { tmpdir } from "node:os"; |
| 5 | +import { join } from "node:path"; |
| 6 | +import { registerRenderRoutes } from "./render"; |
| 7 | +import type { StudioApiAdapter } from "../types"; |
| 8 | + |
| 9 | +function createAdapter( |
| 10 | + startRenderSpy: ReturnType<typeof vi.fn>, |
| 11 | + rendersDir = mkdtempSync(join(tmpdir(), "hf-render-test-")), |
| 12 | +): { adapter: StudioApiAdapter; rendersDir: string } { |
| 13 | + const adapter: StudioApiAdapter = { |
| 14 | + listProjects: () => [], |
| 15 | + resolveProject: async (id: string) => ({ id, dir: "/tmp/proj" }), |
| 16 | + bundle: async () => null, |
| 17 | + lint: async () => ({ findings: [] }), |
| 18 | + runtimeUrl: "/api/runtime.js", |
| 19 | + rendersDir: () => rendersDir, |
| 20 | + startRender: (opts) => { |
| 21 | + startRenderSpy(opts); |
| 22 | + return { |
| 23 | + id: opts.jobId, |
| 24 | + status: "rendering", |
| 25 | + progress: 0, |
| 26 | + outputPath: opts.outputPath, |
| 27 | + }; |
| 28 | + }, |
| 29 | + }; |
| 30 | + return { adapter, rendersDir }; |
| 31 | +} |
| 32 | + |
| 33 | +function buildApp(spy: ReturnType<typeof vi.fn>): { app: Hono; cleanup: () => void } { |
| 34 | + const { adapter, rendersDir } = createAdapter(spy); |
| 35 | + const app = new Hono(); |
| 36 | + registerRenderRoutes(app, adapter); |
| 37 | + return { app, cleanup: () => rmSync(rendersDir, { recursive: true, force: true }) }; |
| 38 | +} |
| 39 | + |
| 40 | +describe("POST /projects/:id/render — outputResolution forwarding", () => { |
| 41 | + it("forwards a valid resolution preset to the adapter", async () => { |
| 42 | + const spy = vi.fn(); |
| 43 | + const { app, cleanup } = buildApp(spy); |
| 44 | + try { |
| 45 | + const res = await app.request("http://localhost/projects/demo/render", { |
| 46 | + method: "POST", |
| 47 | + headers: { "content-type": "application/json" }, |
| 48 | + body: JSON.stringify({ |
| 49 | + fps: 30, |
| 50 | + quality: "high", |
| 51 | + format: "mp4", |
| 52 | + resolution: "landscape-4k", |
| 53 | + }), |
| 54 | + }); |
| 55 | + expect(res.status).toBe(200); |
| 56 | + expect(spy).toHaveBeenCalledOnce(); |
| 57 | + const opts = spy.mock.calls[0][0]; |
| 58 | + expect(opts.outputResolution).toBe("landscape-4k"); |
| 59 | + } finally { |
| 60 | + cleanup(); |
| 61 | + } |
| 62 | + }); |
| 63 | + |
| 64 | + it("omits outputResolution when the request does not specify one", async () => { |
| 65 | + const spy = vi.fn(); |
| 66 | + const { app, cleanup } = buildApp(spy); |
| 67 | + try { |
| 68 | + const res = await app.request("http://localhost/projects/demo/render", { |
| 69 | + method: "POST", |
| 70 | + headers: { "content-type": "application/json" }, |
| 71 | + body: JSON.stringify({ fps: 30, quality: "standard", format: "mp4" }), |
| 72 | + }); |
| 73 | + expect(res.status).toBe(200); |
| 74 | + const opts = spy.mock.calls[0][0]; |
| 75 | + expect(opts.outputResolution).toBeUndefined(); |
| 76 | + } finally { |
| 77 | + cleanup(); |
| 78 | + } |
| 79 | + }); |
| 80 | + |
| 81 | + it("drops an invalid resolution string (defense-in-depth, not a 400)", async () => { |
| 82 | + // The route is intentionally lenient on unknown enum values — the producer |
| 83 | + // is the source of truth for validation and emits a clear error message. |
| 84 | + // We just want to make sure garbage doesn't propagate as if it were valid. |
| 85 | + const spy = vi.fn(); |
| 86 | + const { app, cleanup } = buildApp(spy); |
| 87 | + try { |
| 88 | + const res = await app.request("http://localhost/projects/demo/render", { |
| 89 | + method: "POST", |
| 90 | + headers: { "content-type": "application/json" }, |
| 91 | + body: JSON.stringify({ fps: 30, quality: "standard", format: "mp4", resolution: "8k" }), |
| 92 | + }); |
| 93 | + expect(res.status).toBe(200); |
| 94 | + const opts = spy.mock.calls[0][0]; |
| 95 | + expect(opts.outputResolution).toBeUndefined(); |
| 96 | + } finally { |
| 97 | + cleanup(); |
| 98 | + } |
| 99 | + }); |
| 100 | + |
| 101 | + it("accepts each of the four canonical preset values", async () => { |
| 102 | + for (const preset of ["landscape", "portrait", "landscape-4k", "portrait-4k"] as const) { |
| 103 | + const spy = vi.fn(); |
| 104 | + const { app, cleanup } = buildApp(spy); |
| 105 | + try { |
| 106 | + await app.request("http://localhost/projects/demo/render", { |
| 107 | + method: "POST", |
| 108 | + headers: { "content-type": "application/json" }, |
| 109 | + body: JSON.stringify({ fps: 30, quality: "standard", format: "mp4", resolution: preset }), |
| 110 | + }); |
| 111 | + expect(spy.mock.calls[0][0].outputResolution).toBe(preset); |
| 112 | + } finally { |
| 113 | + cleanup(); |
| 114 | + } |
| 115 | + } |
| 116 | + }); |
| 117 | +}); |
0 commit comments