diff options
Diffstat (limited to 'castle-search/test')
| -rw-r--r-- | castle-search/test/video.test.ts | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/castle-search/test/video.test.ts b/castle-search/test/video.test.ts new file mode 100644 index 0000000..ece0353 --- /dev/null +++ b/castle-search/test/video.test.ts @@ -0,0 +1,79 @@ +import assert from "node:assert/strict"; +import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { test } from "node:test"; + +import { handleVideo } from "../src/http.ts"; +import { clipName, planOf, usedToday, type VideoRequest, videoRef } from "../src/video.ts"; + +const req = (over: Partial<VideoRequest> = {}): VideoRequest => ({ prompt: "a fox", kind: "fast", portrait: false, ...over }); + +test("each kind asks its own model, with its own fixed length and sound", () => { + assert.equal(planOf(req()).model, "FastVideo/FastWan2.2-TI2V-5B-FullAttn-Diffusers"); + assert.deepEqual(planOf(req()).body, { prompt: "a fox", seconds: 5, resolution: "720p", orientation: "landscape" }); + assert.equal(planOf(req({ kind: "quality", portrait: true })).body["orientation"], "portrait"); + const long = planOf(req({ kind: "long", seconds: 12, imageUrl: "https://x/y.png" })); + assert.equal(long.model, "Lightricks/LTX-2.3-Fast"); + assert.deepEqual(long.body, { prompt: "a fox", duration: 12, resolution: "1280x720", generate_audio: true, image_url: "https://x/y.png" }); + assert.equal(planOf(req({ kind: "long" })).seconds, 6); + const sound = planOf(req({ kind: "sound", portrait: true })); + assert.equal(sound.model, "google/veo-3.1-fast"); + assert.equal(sound.body["aspect_ratio"], "9:16"); + assert.equal(sound.sound, true); + assert.equal(planOf(req()).sound, false); +}); + +test("a clip is named by the time and the prompt's first words", () => { + const at = new Date(2026, 10, 19, 21, 5, 9); + assert.equal(clipName("A red fox, trotting through fresh snow in a pine forest", at), "2026-11-19-210509-a-red-fox-trotting-through-fresh.mp4"); + assert.equal(clipName("¡¿", at), "2026-11-19-210509.mp4"); + assert.equal(clipName("Café crème", at), "2026-11-19-210509-cafe-creme.mp4"); +}); + +test("the daily count is today's only, and a missing or broken file is none", () => { + const dir = mkdtempSync(join(tmpdir(), "video-")); + try { + const state = join(dir, "state.json"); + const now = new Date("2026-11-19T12:00:00Z"); + assert.equal(usedToday(state, now), 0); + assert.equal(usedToday(undefined, now), 0); + writeFileSync(state, JSON.stringify({ day: "2026-11-19", clips: 4 })); + assert.equal(usedToday(state, now), 4); + assert.equal(usedToday(state, new Date("2026-11-20T00:00:01Z")), 0); + writeFileSync(state, "{nope"); + assert.equal(usedToday(state, now), 0); + } finally { + rmSync(dir, { recursive: true, force: true }); + } +}); + +test("the video comes from video_url or the first of videos", () => { + assert.equal(videoRef({ video_url: "data:video/mp4;base64,AA" }), "data:video/mp4;base64,AA"); + assert.equal(videoRef({ status: "ok", videos: ["/model/inference/x.mp4"] }), "/model/inference/x.mp4"); + assert.equal(videoRef({ status: "error", videos: [] }), null); +}); + +test("/video checks its body before spending anything", async () => { + const calls: VideoRequest[] = []; + const run = async (r: VideoRequest) => { + calls.push(r); + return { file: "video/x.mp4" } as never; + }; + const bad = async (body: unknown) => (await handleVideo(JSON.stringify(body), run)).status; + assert.equal(await bad({}), 400); + assert.equal(await bad({ prompt: " " }), 400); + assert.equal(await bad({ prompt: "x".repeat(2001) }), 400); + assert.equal(await bad({ prompt: "a fox", kind: "cinema" }), 400); + assert.equal(await bad({ prompt: "a fox", orientation: "square" }), 400); + assert.equal(await bad({ prompt: "a fox", seconds: 10 }), 400); + assert.equal(await bad({ prompt: "a fox", kind: "long", seconds: 7 }), 400); + assert.equal(await bad({ prompt: "a fox", image_url: "https://x/y.png" }), 400); + assert.equal(await bad({ prompt: "a fox", kind: "long", image_url: "file:///etc/passwd" }), 400); + assert.equal(calls.length, 0); + + assert.equal(await bad({ prompt: " a fox " }), 200); + assert.deepEqual(calls.at(-1), { prompt: "a fox", kind: "fast", portrait: false, seconds: undefined, imageUrl: undefined }); + assert.equal(await bad({ prompt: "a fox", kind: "long", seconds: 10, orientation: "portrait", image_url: "data:image/png;base64,AA" }), 200); + assert.deepEqual(calls.at(-1), { prompt: "a fox", kind: "long", portrait: true, seconds: 10, imageUrl: "data:image/png;base64,AA" }); +}); |
