|
1 | 1 | import { afterEach, describe, expect, it, vi } from "vitest"; |
2 | 2 | import { Hono } from "hono"; |
3 | | -import { mkdtempSync, mkdirSync, writeFileSync, symlinkSync, rmSync } from "node:fs"; |
| 3 | +import { mkdtempSync, mkdirSync, writeFileSync, symlinkSync, rmSync, existsSync } from "node:fs"; |
4 | 4 | import { tmpdir } from "node:os"; |
5 | 5 | import { join } from "node:path"; |
6 | 6 | import { VALID_CANVAS_RESOLUTIONS } from "@hyperframes/parsers"; |
@@ -1044,3 +1044,126 @@ describe("audioLoweredDb — the limiter's attenuation reaches the host", () => |
1044 | 1044 | } |
1045 | 1045 | }); |
1046 | 1046 | }); |
| 1047 | + |
| 1048 | +describe("Studio GIF renders", () => { |
| 1049 | + it.each([undefined, null, 0, 1, 65535])("accepts GIF with gifLoop %j", async (gifLoop) => { |
| 1050 | + const spy = vi.fn(); |
| 1051 | + const { app, cleanup } = buildApp(spy); |
| 1052 | + try { |
| 1053 | + const res = await app.request("/projects/demo/render", { |
| 1054 | + method: "POST", |
| 1055 | + headers: { "content-type": "application/json" }, |
| 1056 | + body: JSON.stringify({ format: "gif", gifLoop, gpu: true }), |
| 1057 | + }); |
| 1058 | + expect(res.status).toBe(200); |
| 1059 | + expect(spy).toHaveBeenCalledWith( |
| 1060 | + expect.objectContaining({ |
| 1061 | + format: "gif", |
| 1062 | + outputPath: expect.stringMatching(/\.gif$/), |
| 1063 | + useGpu: false, |
| 1064 | + ...(typeof gifLoop === "number" ? { gifLoop } : {}), |
| 1065 | + }), |
| 1066 | + ); |
| 1067 | + if (gifLoop == null) expect(spy.mock.calls[0][0]).not.toHaveProperty("gifLoop"); |
| 1068 | + } finally { |
| 1069 | + cleanup(); |
| 1070 | + } |
| 1071 | + }); |
| 1072 | + |
| 1073 | + it.each([-1, 65536, 1.5, "2", true, {}, []])( |
| 1074 | + "rejects invalid gifLoop %j before starting", |
| 1075 | + async (gifLoop) => { |
| 1076 | + const spy = vi.fn(); |
| 1077 | + const { app, cleanup } = buildApp(spy); |
| 1078 | + try { |
| 1079 | + const res = await app.request("/projects/demo/render", { |
| 1080 | + method: "POST", |
| 1081 | + headers: { "content-type": "application/json" }, |
| 1082 | + body: JSON.stringify({ format: "gif", gifLoop }), |
| 1083 | + }); |
| 1084 | + expect(res.status).toBe(400); |
| 1085 | + expect(await res.json()).toEqual({ error: "gifLoop must be an integer from 0 to 65535" }); |
| 1086 | + expect(spy).not.toHaveBeenCalled(); |
| 1087 | + } finally { |
| 1088 | + cleanup(); |
| 1089 | + } |
| 1090 | + }, |
| 1091 | + ); |
| 1092 | + |
| 1093 | + it("leaves video exports unchanged when a valid GIF loop option is supplied", async () => { |
| 1094 | + const spy = vi.fn(); |
| 1095 | + const { app, cleanup } = buildApp(spy); |
| 1096 | + try { |
| 1097 | + const res = await app.request("/projects/demo/render", { |
| 1098 | + method: "POST", |
| 1099 | + headers: { "content-type": "application/json" }, |
| 1100 | + body: JSON.stringify({ format: "mp4", gifLoop: 3 }), |
| 1101 | + }); |
| 1102 | + expect(res.status).toBe(200); |
| 1103 | + expect(spy.mock.calls[0][0]).not.toHaveProperty("gifLoop"); |
| 1104 | + } finally { |
| 1105 | + cleanup(); |
| 1106 | + } |
| 1107 | + }); |
| 1108 | + |
| 1109 | + it("lists a saved GIF after restart, serves image bytes, and deletes its sidecar", async () => { |
| 1110 | + const { app, rendersDir, cleanup } = buildApp(vi.fn()); |
| 1111 | + try { |
| 1112 | + const output = join(rendersDir, "saved.gif"); |
| 1113 | + const meta = join(rendersDir, "saved.meta.json"); |
| 1114 | + writeFileSync(output, "GIF89a"); |
| 1115 | + writeFileSync(meta, JSON.stringify({ status: "complete", durationMs: 42 })); |
| 1116 | + const list = await app.request("/projects/demo/renders"); |
| 1117 | + expect(await list.json()).toMatchObject({ |
| 1118 | + renders: [{ id: "saved", filename: "saved.gif", durationMs: 42 }], |
| 1119 | + }); |
| 1120 | + for (const url of [ |
| 1121 | + "/projects/demo/renders/file/saved.gif", |
| 1122 | + "/render/saved/view", |
| 1123 | + "/render/saved/download", |
| 1124 | + ]) { |
| 1125 | + const res = await app.request(url); |
| 1126 | + expect(res.status).toBe(200); |
| 1127 | + expect(res.headers.get("content-type")).toBe("image/gif"); |
| 1128 | + expect(await res.text()).toBe("GIF89a"); |
| 1129 | + expect(res.headers.get("content-disposition")).toContain( |
| 1130 | + url.endsWith("download") ? "attachment" : "inline", |
| 1131 | + ); |
| 1132 | + } |
| 1133 | + expect((await app.request("/render/saved", { method: "DELETE" })).status).toBe(200); |
| 1134 | + expect(existsSync(output)).toBe(false); |
| 1135 | + expect(existsSync(meta)).toBe(false); |
| 1136 | + expect(await (await app.request("/projects/demo/renders")).json()).toEqual({ renders: [] }); |
| 1137 | + } finally { |
| 1138 | + cleanup(); |
| 1139 | + } |
| 1140 | + }); |
| 1141 | +}); |
| 1142 | + |
| 1143 | +describe("Studio GIF frame-rate cap", () => { |
| 1144 | + it.each([ |
| 1145 | + { format: "gif", fps: 60, expected: { num: 30, den: 1 } }, |
| 1146 | + { format: "gif", fps: "60000/1001", expected: { num: 30, den: 1 } }, |
| 1147 | + { format: "gif", fps: "30001/1000", expected: { num: 30, den: 1 } }, |
| 1148 | + { format: "gif", fps: "30000/1001", expected: { num: 30000, den: 1001 } }, |
| 1149 | + { format: "gif", fps: 30, expected: { num: 30, den: 1 } }, |
| 1150 | + { format: "gif", fps: 24, expected: { num: 24, den: 1 } }, |
| 1151 | + { format: "mp4", fps: 60, expected: { num: 60, den: 1 } }, |
| 1152 | + { format: "webm", fps: 60, expected: { num: 60, den: 1 } }, |
| 1153 | + { format: "mov", fps: 60, expected: { num: 60, den: 1 } }, |
| 1154 | + ])("forwards $format at $fps fps as $expected", async ({ format, fps, expected }) => { |
| 1155 | + const spy = vi.fn(); |
| 1156 | + const { app, cleanup } = buildApp(spy); |
| 1157 | + try { |
| 1158 | + const res = await app.request("/projects/demo/render", { |
| 1159 | + method: "POST", |
| 1160 | + headers: { "content-type": "application/json" }, |
| 1161 | + body: JSON.stringify({ format, fps }), |
| 1162 | + }); |
| 1163 | + expect(res.status).toBe(200); |
| 1164 | + expect(spy).toHaveBeenCalledWith(expect.objectContaining({ format, fps: expected })); |
| 1165 | + } finally { |
| 1166 | + cleanup(); |
| 1167 | + } |
| 1168 | + }); |
| 1169 | +}); |
0 commit comments