import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { afterEach, describe, expect, it, vi } from "vitest";
import { defaultCloudFps, readCompositionFps } from "./compositionFps.js";
const wrap = (body: string) => `
${body}`;
describe("readCompositionFps", () => {
it("reads data-fps from the explicit data-root composition element", () => {
const html = wrap('x
');
expect(readCompositionFps(html)).toBe("24");
});
it.each(["DATA-FPS", "Data-FpS"])("reads an HTML %s attribute", (attribute) => {
const html = wrap(`x
`);
expect(readCompositionFps(html)).toBe("24");
});
it("prefers an explicit root with mixed-case HTML attribute names", () => {
const html = wrap(
'' +
'x
',
);
expect(readCompositionFps(html)).toBe("24");
});
it("reads the outermost composition with mixed-case HTML attribute names", () => {
const html = wrap(
'',
);
expect(readCompositionFps(html)).toBe("48");
});
it.each([
'DATA-FPS="24" data-fps="60"',
'Data-FpS="24" DATA-FPS="60"',
'data-fps="24" DATA-FPS="60"',
])("keeps the first duplicate HTML attribute in %s", (attributes) => {
const html = wrap(`x
`);
expect(readCompositionFps(html)).toBe("24");
});
it("keeps the first duplicate data-root value when selecting the root", () => {
const html = wrap(
'' +
'x
',
);
expect(readCompositionFps(html)).toBe("24");
});
it("reads data-fps from the outermost composition when no data-root is marked", () => {
const html = wrap(
'',
);
expect(readCompositionFps(html)).toBe("48");
});
it("preserves a fractional rate verbatim for parseFps to validate", () => {
const html = wrap(
'x
',
);
expect(readCompositionFps(html)).toBe("30000/1001");
});
it("returns null when the root has no data-fps", () => {
expect(readCompositionFps(wrap('x
'))).toBeNull();
});
it("returns null when there is no composition root", () => {
expect(readCompositionFps(wrap("plain
"))).toBeNull();
});
it("returns null for a blank data-fps", () => {
expect(
readCompositionFps(wrap('x
')),
).toBeNull();
});
});
describe("defaultCloudFps", () => {
const projectDirs: string[] = [];
afterEach(() => {
vi.restoreAllMocks();
for (const dir of projectDirs.splice(0)) {
rmSync(dir, { recursive: true, force: true });
}
});
function makeProject(indexBody: string): string {
const dir = mkdtempSync(join(tmpdir(), "hyperframes-composition-fps-"));
projectDirs.push(dir);
writeFileSync(join(dir, "index.html"), wrap(indexBody));
return dir;
}
function root(fps: string): string {
return makeProject(
`x
`,
);
}
it.each([24, 25, 30, 60])("uses a supported data-fps=%i without warning", (fps) => {
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
expect(defaultCloudFps(root(String(fps)))).toBe(fps);
expect(warn).not.toHaveBeenCalled();
});
it.each(["48", "50", "30000/1001"])("falls back to 30 and warns for data-fps=%s", (fps) => {
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
expect(defaultCloudFps(root(fps))).toBe(30);
expect(warn).toHaveBeenCalledTimes(1);
expect(String(warn.mock.calls[0]?.[0])).toContain(`data-fps="${fps}"`);
});
it("reads mixed-case HTML attributes before validating the cloud fps", () => {
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
const dir = makeProject(
'x
',
);
expect(defaultCloudFps(dir)).toBe(24);
expect(warn).not.toHaveBeenCalled();
});
it("uses 30 without warning when index.html has no data-fps", () => {
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
expect(
defaultCloudFps(makeProject('x
')),
).toBe(30);
expect(warn).not.toHaveBeenCalled();
});
it("uses 30 without warning when index.html cannot be read", () => {
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
const dir = mkdtempSync(join(tmpdir(), "hyperframes-composition-fps-missing-"));
projectDirs.push(dir);
expect(defaultCloudFps(dir)).toBe(30);
expect(warn).not.toHaveBeenCalled();
});
});