import {
existsSync,
linkSync,
mkdtempSync,
mkdirSync,
readFileSync,
rmSync,
writeFileSync,
} from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { beforeAll, describe, expect, it, onTestFinished, vi } from "vitest";
import { runCommand } from "citty";
import type { ArgsDef } from "citty";
import { ensureDOMParser } from "../utils/dom.js";
import keyframesCommand from "./keyframes.js";
import {
collectCompositions,
collectShotSelectors,
resolveScope,
singleFileStage,
surfaceComposition,
} from "./keyframes.js";
import { ensureShotOutputDir } from "./motionShot.js";
// citty types `args` as Resolvable (object | promise | thunk); this
// command always uses a static object, same narrowing as assertKnownFlags.
function layoutArgDescription(): string {
const rawDef = keyframesCommand.args;
const args = rawDef && typeof rawDef === "object" ? (rawDef as ArgsDef) : undefined;
const layout = args?.["layout"];
if (!layout || typeof layout !== "object" || !("description" in layout)) {
throw new Error("expected keyframesCommand.args.layout.description to be defined");
}
return String(layout.description);
}
beforeAll(() => ensureDOMParser());
const wrap = (script: string) =>
``;
describe("keyframes JSON output", () => {
it.each(["css", "anime"])("emits parseable JSON with --runtime %s", async (runtime) => {
const dir = mkdtempSync(join(tmpdir(), "hf-keyframes-json-"));
onTestFinished(() => rmSync(dir, { recursive: true, force: true }));
const source = join(dir, "index.html");
writeFileSync(source, wrap(""));
const log = vi.spyOn(console, "log").mockImplementation(() => {});
onTestFinished(() => log.mockRestore());
await runCommand(keyframesCommand, { rawArgs: [source, "--runtime", runtime, "--json"] });
const output = log.mock.calls.map((args) => args.join(" ")).join("\n");
const result: unknown = JSON.parse(output);
expect(result).toMatchObject({
project: "index.html",
runtime,
compositions: [{ composition: "index.html", tweens: [], cssKeyframes: [], anime: [] }],
});
});
});
describe("keyframes human output", () => {
it("prints selectors, values and keyframe text from the project with control characters escaped", async () => {
// Built rather than written: a literal control character in the source is a lint error.
const esc = String.fromCharCode(27);
const bel = String.fromCharCode(7);
const dir = mkdtempSync(join(tmpdir(), "hf-keyframes-human-"));
onTestFinished(() => rmSync(dir, { recursive: true, force: true }));
const source = join(dir, "index.html");
writeFileSync(
source,
wrap(
`const tl = gsap.timeline({ paused: true }); tl.to("#dot${esc}[2J", { x: "40px${esc}]52;c;cA==${bel}", duration: 1 }, 0); window.__timelines = { main: tl };`,
).replace(
"