repositories / pi-ext
pi-ext
bugabingas pi extensions
owned by admin
extensions/minimax/__tests__/preview.test.ts
Rawimport { chmod, mkdtemp, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { stripVTControlCharacters } from "node:util";
import { initTheme, type Theme } from "@earendil-works/pi-coding-agent";
import {
Container,
Image,
resetCapabilitiesCache,
setCapabilities,
Text,
} from "@earendil-works/pi-tui";
import { afterEach, describe, expect, it } from "vitest";
import { posterStrip } from "../src/preview.ts";
import { renderResult } from "../src/render.ts";
import { result } from "../src/tools.ts";
const jpeg = Buffer.from([255, 216, 255, 224, 0, 16, 74, 70, 73, 70]);
const preview = {
mimeType: "image/jpeg" as const,
data: jpeg.toString("base64"),
};
const theme = (): Theme => {
initTheme("default", false);
return {
fg: (_c: string, t: string) => t,
bold: (t: string) => t,
} as unknown as Theme;
};
const temporary: string[] = [];
afterEach(async () => {
resetCapabilitiesCache();
for (const dir of temporary.splice(0))
await rm(dir, { recursive: true, force: true });
});
function retrieved(path = "/tmp/out dir/clip ü.mp4") {
return result(`MiniMax H3 job · task · completed → ${path}`, {
operation: "video",
job: "job",
taskId: "task",
state: "completed",
requestedDuration: 4,
resolution: "768P",
artifact: { path, format: "mp4", bytes: 1234 },
preview,
});
}
describe("render-only video preview", () => {
it("keeps poster bytes out of model text and structuredContent", () => {
const value = retrieved();
const text = value.content
.map((b) => (b.type === "text" ? b.text : ""))
.join("");
expect(value.content.every((b) => b.type === "text")).toBe(true);
expect(text).not.toContain(preview.data);
expect(value.structuredContent).not.toHaveProperty("preview");
expect(JSON.stringify(value.structuredContent)).not.toContain(preview.data);
expect(value.details).toHaveProperty("preview");
});
it("shows the inline image only when showImages is on, and reuses it", () => {
const value = retrieved();
const render = renderResult("video");
const off = render(value, { expanded: false, isPartial: false }, theme(), {
showImages: false,
});
expect(off).toBeInstanceOf(Text);
const on = render(value, { expanded: false, isPartial: false }, theme(), {
showImages: true,
});
expect(on).toBeInstanceOf(Container);
const image = (on as Container).children.find((c) => c instanceof Image);
expect(image).toBeDefined();
const again = render(value, { expanded: true, isPartial: false }, theme(), {
showImages: true,
}) as Container;
expect(again.children.find((c) => c instanceof Image)).toBe(image);
const expanded = again.children[0]!.render(120)
.map(stripVTControlCharacters)
.join("\n");
expect(expanded).not.toContain(preview.data);
});
it.each([true, false])(
"links the artifact path only with hyperlink capability %s",
(hyperlinks) => {
setCapabilities({ images: null, trueColor: true, hyperlinks });
const out = (
renderResult("video")(
retrieved(),
{ expanded: false, isPartial: false },
theme(),
{
showImages: false,
},
) as Text
)
.render(200)
.join("\n");
const link = "\u001b]8;;file:///tmp/out%20dir/clip%20%C3%BC.mp4";
if (hyperlinks) expect(out).toContain(link);
else expect(out).not.toContain("\u001b]8;");
expect(stripVTControlCharacters(out)).toContain(
"/tmp/out dir/clip ü.mp4",
);
},
);
it("labels repeat observations instead of repeating the estimate", () => {
const value = retrieved();
value.details["accounting"] = { kind: "catalog-estimate", usd: 0.32 };
value.details["accountingDelivery"] = "already emitted; not recounted";
const out = stripVTControlCharacters(
(
renderResult("video")(
value,
{ expanded: false, isPartial: false },
theme(),
{},
) as Text
)
.render(200)
.join("\n"),
);
expect(out).toContain("counted earlier");
expect(out).not.toContain("$0.32");
});
});
describe.skipIf(process.platform === "win32")(
"posterStrip process boundary",
() => {
async function fake(body: string): Promise<string> {
const dir = await mkdtemp(join(tmpdir(), "minimax-ffmpeg-"));
temporary.push(dir);
const path = join(dir, "ffmpeg");
await writeFile(path, `#!${process.execPath}\n${body}\n`);
await chmod(path, 0o755);
return path;
}
it("returns a JPEG strip from ffmpeg stdout", async () => {
const ffmpeg = await fake(
`process.stdout.write(Buffer.from([${[...jpeg].join(",")}]));`,
);
expect(await posterStrip("/tmp/x.mp4", 4, undefined, ffmpeg)).toEqual(
preview,
);
});
it.each([
["missing ffmpeg", undefined],
["nonzero exit", "process.exit(1);"],
["non-JPEG output", "process.stdout.write('nope');"],
["oversized output", "process.stdout.write(Buffer.alloc(80*1024, 255));"],
])("degrades to no preview on %s", async (_name, body) => {
const ffmpeg = body === undefined ? undefined : await fake(body);
expect(
await posterStrip("/tmp/x.mp4", 4, undefined, ffmpeg ?? ""),
).toBeUndefined();
});
it("settles promptly when cancelled while ffmpeg hangs", async () => {
const ffmpeg = await fake("setInterval(() => {}, 1000);");
const controller = new AbortController();
const pending = posterStrip("/tmp/x.mp4", 4, controller.signal, ffmpeg);
setTimeout(() => controller.abort(), 50);
const started = Date.now();
expect(await pending).toBeUndefined();
expect(Date.now() - started).toBeLessThan(3000);
});
},
);