ai-chatbot/tests/routes/video-subtitle-erase-tools.test.ts

81 lines
2.2 KiB
TypeScript

import { expect, test } from "@playwright/test";
import {
defaultVideoCanvasSubtitleEraseConfig,
detectSubtitleEraseRegions,
moveSubtitleEraseRegion,
normalizeVideoCanvasSubtitleEraseConfig,
resizeSubtitleEraseRegion,
type SubtitleEraseRegion,
} from "../../lib/video-canvas/subtitle-erase";
const region: SubtitleEraseRegion = {
height: 0.1,
id: "region-1",
width: 0.4,
x: 0.3,
y: 0.7,
};
test.describe("video subtitle erase tools", () => {
test("normalizes administrator processing limits", () => {
expect(normalizeVideoCanvasSubtitleEraseConfig({
blurRadius: 100,
edgeThreshold: 0,
frameRate: 200,
smartSearchEndRatio: 0.2,
smartSearchStartRatio: 0.8,
})).toMatchObject({
blurRadius: 40,
edgeThreshold: 1,
frameRate: 60,
smartSearchEndRatio: 0.84,
smartSearchStartRatio: 0.8,
});
});
test("moves and resizes a selected erase region inside the source", () => {
expect(moveSubtitleEraseRegion(region, 0.8, -0.8)).toEqual({
...region,
x: 0.6,
y: 0,
});
expect(resizeSubtitleEraseRegion(region, "se", 0.95, 0.95)).toEqual({
...region,
height: 0.25,
width: 0.65,
});
});
test("detects a high-contrast subtitle band in the configured lower scan area", () => {
const width = 120;
const height = 100;
const data = new Uint8ClampedArray(width * height * 4);
for (let y = 0; y < height; y += 1) {
for (let x = 0; x < width; x += 1) {
const index = (y * width + x) * 4;
const value = y >= 65 && y <= 74 && x >= 12 && x <= 108
? (x % 4 < 2 ? 255 : 0)
: 48;
data[index] = value;
data[index + 1] = value;
data[index + 2] = value;
data[index + 3] = 255;
}
}
const detected = detectSubtitleEraseRegions(
{ data, height, width } as ImageData,
{
...defaultVideoCanvasSubtitleEraseConfig,
edgeThreshold: 30,
minBandHeightRatio: 0.01,
},
);
expect(detected).toHaveLength(1);
expect(detected[0]).toMatchObject({ id: "smart-subtitle-1", width: 0.94, x: 0.03 });
expect(detected[0].y).toBeGreaterThan(0.6);
expect(detected[0].y + detected[0].height).toBeLessThan(0.8);
});
});