Skip to content

Code-first video & image, rendered natively on WebGPU.

3.0 KB
Raw Blame
import { isSignal, signal } from "@framefields/core";
import { describe, expect, it } from "vitest";
import {
	Blur,
	ColorBalance,
	type Effect,
	type ObjectTrackSignals,
	Vignette,
} from "../index.js";
import { attachSyntheticPersonTrack } from "../test-utils/vision.js";

/**
 * Golden snapshots of what each effect class hands the renderer.
 *
 * The snapshots were recorded against the hand-written classes before they
 * were replaced by generated ones (spec/sync-node.md, Phase 1). They pin the
 * op, the prop set, the per-frame uniform values and whether each payload
 * entry is a signal or a scalar, so a migration cannot change behavior
 * unnoticed. Update a snapshot only for an intentional, documented change.
 */
const round = (value: unknown): unknown => {
	if (typeof value === "number") return Number(value.toFixed(6));
	if (Array.isArray(value)) return value.map(round);
	if (value && typeof value === "object") {
		return Object.fromEntries(
			Object.entries(value as Record<string, unknown>)
				.sort(([a], [b]) => a.localeCompare(b))
				.map(([k, v]) => [k, round(v)]),
		);
	}
	return value;
};

function describeEffect(effect: Effect<object>) {
	const payload = effect.toOperation();
	const keys = Object.keys(effect.config).sort();
	const uniforms = effect.resolveUniforms({ frame: 0, fps: 24 } as never);
	return {
		op: payload.op,
		props: keys,
		uniforms: round(uniforms),
		payload: Object.fromEntries(
			keys.map((key) => {
				const val = (payload as Record<string, unknown>)[key];
				return [key, isSignal(val) ? "signal" : typeof val];
			}),
		),
	};
}

function syntheticTrack(): ObjectTrackSignals {
	const { vision } = attachSyntheticPersonTrack();
	return vision.objects.byCategory("person");
}

describe("effect golden snapshots", () => {
	it("Vignette: defaults", () => {
		expect(describeEffect(new Vignette())).toMatchSnapshot();
	});

	it("Vignette: custom values and a signal", () => {
		expect(
			describeEffect(
				new Vignette({ strength: 80, radius: signal(0.7), centerX: 0.25 }),
			),
		).toMatchSnapshot();
	});

	it("Blur: defaults", () => {
		expect(describeEffect(new Blur())).toMatchSnapshot();
	});

	it("Blur: custom values", () => {
		expect(
			describeEffect(
				new Blur({
					strength: 12,
					blurType: "Motion",
					angle: 45,
					partialBlur: true,
					shape: "rect",
					radius: 0.4,
					radiusY: 0.2,
				}),
			),
		).toMatchSnapshot();
	});

	it("Blur: driven by a track", () => {
		expect(
			describeEffect(new Blur({ track: syntheticTrack() })),
		).toMatchSnapshot();
	});

	it("Blur: track with overrides", () => {
		expect(
			describeEffect(
				new Blur({ track: syntheticTrack(), strength: 25, shape: "ellipse" }),
			),
		).toMatchSnapshot();
	});

	it("ColorBalance: defaults", () => {
		expect(describeEffect(new ColorBalance())).toMatchSnapshot();
	});

	it("ColorBalance: partial nested values", () => {
		expect(
			describeEffect(
				new ColorBalance({
					shadows: { cyanRed: 10 },
					highlights: { yellowBlue: -20, magentaGreen: 5 },
					preserveLuminosity: false,
				}),
			),
		).toMatchSnapshot();
	});
});