Release Subtitle Tools 0.1.0
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
import { expect, test, type Page } from "@playwright/test";
|
||||
|
||||
const ORIGIN = "http://127.0.0.1:4197";
|
||||
async function localOnly(page: Page) {
|
||||
const external: string[] = [];
|
||||
await page.route("**/*", async (route) => {
|
||||
const url = new URL(route.request().url());
|
||||
if (
|
||||
(url.protocol === "http:" || url.protocol === "https:") &&
|
||||
url.origin !== ORIGIN
|
||||
) {
|
||||
external.push(url.href);
|
||||
await route.abort();
|
||||
} else await route.continue();
|
||||
});
|
||||
return external;
|
||||
}
|
||||
|
||||
test("runs from a nested path without external requests", async ({ page }) => {
|
||||
const errors: string[] = [];
|
||||
page.on("pageerror", (error) => errors.push(error.message));
|
||||
page.on("console", (message) => {
|
||||
if (message.type() === "error") errors.push(message.text());
|
||||
});
|
||||
const external = await localOnly(page);
|
||||
await page.goto("/deep/nested/subtitle/");
|
||||
await expect(
|
||||
page.getByRole("heading", { name: "Subtitle Tools" }),
|
||||
).toBeVisible();
|
||||
await expect(page.getByText("No network requests")).toBeVisible();
|
||||
expect(external).toEqual([]);
|
||||
expect(errors).toEqual([]);
|
||||
});
|
||||
|
||||
test("parses, validates, edits and converts the example", async ({ page }) => {
|
||||
const external = await localOnly(page);
|
||||
await page.goto("/deep/nested/subtitle/");
|
||||
await expect(page.getByText("SRT", { exact: true })).toBeVisible();
|
||||
await page.getByRole("tab", { name: "Cues & checks" }).click();
|
||||
await expect(page.getByText("overlap", { exact: true })).toBeVisible();
|
||||
await page.getByLabel("Text").fill("Edited locally");
|
||||
await page.getByRole("tab", { name: "Source & export" }).click();
|
||||
await page.getByLabel("Output format").selectOption("vtt");
|
||||
await expect(page.getByLabel("Export preview")).toHaveValue(/WEBVTT/u);
|
||||
await expect(page.getByLabel("Export preview")).toHaveValue(
|
||||
/Edited locally/u,
|
||||
);
|
||||
expect(external).toEqual([]);
|
||||
});
|
||||
|
||||
test("applies a bounded timing shift", async ({ page }) => {
|
||||
await page.goto("/deep/nested/subtitle/");
|
||||
await page.getByRole("tab", { name: "Timing" }).click();
|
||||
await page.getByLabel("Shift (ms)").fill("500");
|
||||
await page.getByRole("button", { name: "Apply timing transform" }).click();
|
||||
await page.getByRole("tab", { name: "Cues & checks" }).click();
|
||||
await expect(page.getByText(/00:00:01\.500/u).first()).toBeVisible();
|
||||
});
|
||||
|
||||
test("serves release identity and hardened local-only headers", async ({
|
||||
request,
|
||||
}) => {
|
||||
const index = await request.get("/deep/nested/subtitle/");
|
||||
expect(index.ok()).toBe(true);
|
||||
expect(index.headers()["content-security-policy"]).toContain(
|
||||
"connect-src 'self'",
|
||||
);
|
||||
expect(index.headers()["content-security-policy"]).not.toMatch(
|
||||
/connect-src[^;]*https?:/u,
|
||||
);
|
||||
expect(await index.text()).not.toMatch(/\b(?:src|href)=["']\//u);
|
||||
const manifest = await request.get("/deep/nested/subtitle/toolbox-app.json");
|
||||
await expect(manifest.json()).resolves.toMatchObject({
|
||||
id: "de.add-ideas.subtitle-tools",
|
||||
version: "0.1.0",
|
||||
entry: "./",
|
||||
privacy: { processing: "local", telemetry: false },
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,18 @@
|
||||
import { render, screen } from "@testing-library/react";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { App } from "../../src/App";
|
||||
|
||||
describe("Subtitle Tools", () => {
|
||||
it("renders the local workbench and standard shell", async () => {
|
||||
vi.stubGlobal(
|
||||
"fetch",
|
||||
vi.fn(async () => new Response("Not found", { status: 404 })),
|
||||
);
|
||||
render(<App />);
|
||||
expect(
|
||||
await screen.findByRole("heading", { name: "Subtitle Tools" }),
|
||||
).toBeVisible();
|
||||
expect(await screen.findByText("No network requests")).toBeVisible();
|
||||
expect(screen.getByRole("tab", { name: "Timing" })).toBeVisible();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,97 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import {
|
||||
compareDocuments,
|
||||
convertFrameRate,
|
||||
parseSubtitle,
|
||||
serializeSubtitle,
|
||||
transformTiming,
|
||||
validateDocument,
|
||||
} from "../../src/subtitle/model";
|
||||
|
||||
const SRT = `1\r\n00:00:01,000 --> 00:00:02,500\r\nHello world\r\n\r\n2\r\n00:00:02,400 --> 00:00:04,000\r\nSecond cue\r\n`;
|
||||
|
||||
describe("subtitle model", () => {
|
||||
it("parses SRT and reports overlap", () => {
|
||||
const document = parseSubtitle(SRT);
|
||||
expect(document.format).toBe("srt");
|
||||
expect(document.cues).toHaveLength(2);
|
||||
expect(document.cues[0]).toMatchObject({
|
||||
startMs: 1000,
|
||||
endMs: 2500,
|
||||
text: "Hello world",
|
||||
});
|
||||
expect(
|
||||
validateDocument(document).some((issue) => issue.code === "overlap"),
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it("round-trips WebVTT identifiers, settings and notes", () => {
|
||||
const source = `WEBVTT - sample\n\nNOTE inert note\nline two\n\nc1\n00:00:01.000 --> 00:00:02.000 align:start\n<v Ada>Hello</v>\n`;
|
||||
const document = parseSubtitle(source);
|
||||
expect(document).toMatchObject({ format: "vtt" });
|
||||
expect(document.cues[0]).toMatchObject({
|
||||
id: "c1",
|
||||
settings: "align:start",
|
||||
});
|
||||
const serialized = serializeSubtitle(document, "vtt");
|
||||
expect(serialized).toContain("NOTE inert note");
|
||||
expect(serialized).toContain("align:start");
|
||||
expect(parseSubtitle(serialized).cues[0]?.text).toBe("<v Ada>Hello</v>");
|
||||
});
|
||||
|
||||
it("parses ASS dialogue fields containing commas and preserves styles", () => {
|
||||
const source = `[Script Info]\nScriptType: v4.00+\n\n[V4+ Styles]\nFormat: Name, Fontname\nStyle: Loud,Arial\n\n[Events]\nFormat: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text\nDialogue: 0,0:00:01.00,0:00:03.25,Loud,Ada,0,0,0,,Hello, world\\Nagain\n`;
|
||||
const document = parseSubtitle(source);
|
||||
expect(document.format).toBe("ass");
|
||||
expect(document.cues[0]).toMatchObject({
|
||||
startMs: 1000,
|
||||
endMs: 3250,
|
||||
text: "Hello, world\nagain",
|
||||
});
|
||||
const serialized = serializeSubtitle(document, "ass");
|
||||
expect(serialized).toContain("Dialogue: 0,0:00:01.00,0:00:03.25,Loud");
|
||||
expect(serialized).toContain("Hello, world\\Nagain");
|
||||
});
|
||||
|
||||
it("writes an Events format even when only a style format was supplied", () => {
|
||||
const source = `[Script Info]\nScriptType: v4.00+\n\n[V4+ Styles]\nFormat: Name, Fontname\nStyle: Default,Arial\n\n[Events]\nDialogue: 0,0:00:01.00,0:00:02.00,Default,,0,0,0,,Hello\n`;
|
||||
const serialized = serializeSubtitle(parseSubtitle(source), "ass");
|
||||
expect(serialized).toMatch(
|
||||
/\[Events\]\nFormat: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text/u,
|
||||
);
|
||||
expect(parseSubtitle(serialized).cues).toHaveLength(1);
|
||||
});
|
||||
|
||||
it("converts formats and timing without silently creating negatives", () => {
|
||||
const document = parseSubtitle(SRT);
|
||||
expect(() => transformTiming(document, { shiftMs: -2000 })).toThrow(
|
||||
/negative/u,
|
||||
);
|
||||
const clamped = transformTiming(document, {
|
||||
shiftMs: -2000,
|
||||
clampToZero: true,
|
||||
});
|
||||
expect(clamped.cues[0]?.startMs).toBe(0);
|
||||
const converted = convertFrameRate(document, 25, 50);
|
||||
expect(converted.cues[0]).toMatchObject({ startMs: 500, endMs: 1250 });
|
||||
expect(serializeSubtitle(converted, "vtt")).toMatch(/^WEBVTT/u);
|
||||
});
|
||||
|
||||
it("compares revisions deterministically", () => {
|
||||
const before = parseSubtitle(SRT);
|
||||
const after = parseSubtitle(SRT.replace("Hello world", "Hello again"));
|
||||
expect(compareDocuments(before, after)).toMatchObject([
|
||||
{ index: 0, kind: "text" },
|
||||
]);
|
||||
});
|
||||
|
||||
it("rejects pathological input bounds", () => {
|
||||
expect(() => parseSubtitle("x".repeat(4_000_001))).toThrow(/4,000,000/u);
|
||||
expect(() =>
|
||||
parseSubtitle(`1\n00:61:00,000 --> 00:61:01,000\nBad`),
|
||||
).not.toThrow();
|
||||
expect(
|
||||
parseSubtitle(`1\n00:61:00,000 --> 00:61:01,000\nBad`).cues,
|
||||
).toHaveLength(0);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user