mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-18 16:00:07 +00:00
* fix(studio): dev Studio renders apply the received-project rule for outside files The Vite Studio adapter started every render with files from outside the project allowed. It now asks the CLI's outsideAssetsAllowed, the same check the CLI's embedded Studio, hyperframes render and benchmark use. A project whose received-files record lists code files, or cannot be read, renders without files from outside the project, and the reason prints with a [studio] prefix. The adapter loads the rule from the CLI source through the dev server, as it already does for background removal. It runs only from a source checkout, so studio takes no package dependency on the CLI. * fix(cli): a received project's .env provider key never replaces your own credential A received project's .env could set OPENROUTER_API_KEY, GEMINI_API_KEY, GOOGLE_API_KEY or Vertex settings that outranked a vision or Gemini credential the person set in the shell, so capture, snapshot describe and media-use speech and music could send content to another account. After the CLI and capture load a project .env, each provider key it set is unset when that .env came with a received project (a received-files record lists it, or a record cannot be read) and the person has their own credential for a competing provider. One stderr line names the key, the .env file and the credential used instead. A person's own project .env keeps its keys and its current precedence, so an OPENROUTER_API_KEY in a project .env still wins over a Gemini key in the shell, as the docs describe. Voice needs no change: the person's HeyGen credential already ranks above ElevenLabs. * fix(cli,media-use): media-use scripts apply the received .env rule, ElevenLabs included The media-use audio scripts read the project .env again, so a Gemini key the CLI had left out came back. They now apply the same rule after loading: a key from a received project's .env gives way to the person's own credential for a competing provider. GEMINI_API_KEY and GOOGLE_API_KEY give way to the person's Gemini key or service account. ELEVENLABS_API_KEY gives way to the person's HeyGen sign-in, saved key or shell key. A dropped key gets one stderr line that names the file and the credential used instead. A received ElevenLabs key that is still used, because the person has no competing credential, gets one stderr line naming the .env before speech starts. hyperframes media-use tells the engine which keys it took from a project .env (HYPERFRAMES_PROJECT_ENV_ORIGINS), so a key the CLI loaded is not mistaken for the shell's. When the project's folders are too many to scan for received-files records, the CLI's note now says the project's origin could not be checked, instead of calling it a received project. * refactor(studio): check the shape of the CLI's outside-files rule before calling it The Vite adapter loads outsideAssetsAllowed from the CLI source at run time. It now checks that the export is a function and treats only a true result as allowed, instead of casting the module's export. * docs(auth): a received project's ElevenLabs key gives way to your HeyGen credential * test(studio): load the received-project rule before render waits start
139 lines
5.6 KiB
TypeScript
139 lines
5.6 KiB
TypeScript
import {
|
|
waitForRenderCancellation,
|
|
cancelAndVerifyArtifacts,
|
|
} from "../../test-utils/renderCancellation";
|
|
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
import { tmpdir } from "node:os";
|
|
import { join } from "node:path";
|
|
import { fileURLToPath } from "node:url";
|
|
import { createServer, type ViteDevServer } from "vite";
|
|
import { createProjectSignatureCache, createViteAdapter } from "./vite.adapter";
|
|
|
|
const producer = vi.hoisted(() => ({
|
|
createRenderJob: vi.fn((config: Record<string, unknown>) => ({ config })),
|
|
executeRenderJob: vi.fn(),
|
|
}));
|
|
vi.mock("@hyperframes/producer", () => producer);
|
|
vi.mock("./vite.producer", async (importOriginal) => ({
|
|
...(await importOriginal<typeof import("./vite.producer")>()),
|
|
ensureProducerDist: () => ({ built: false }),
|
|
}));
|
|
|
|
const dirs: string[] = [];
|
|
let server: ViteDevServer | undefined;
|
|
afterEach(async () => {
|
|
await server?.close();
|
|
server = undefined;
|
|
vi.clearAllMocks();
|
|
for (const dir of dirs.splice(0)) rmSync(dir, { recursive: true, force: true });
|
|
});
|
|
|
|
const RECEIVED_RENDER = fileURLToPath(
|
|
new URL("../cli/src/utils/receivedRender.ts", import.meta.url),
|
|
);
|
|
|
|
type RenderOptions = Parameters<ReturnType<typeof createViteAdapter>["startRender"]>[0];
|
|
|
|
async function fixture(extra: Partial<RenderOptions> = {}, setup?: (dir: string) => void) {
|
|
const dir = mkdtempSync(join(tmpdir(), "hf-vite-gif-"));
|
|
dirs.push(dir);
|
|
setup?.(dir);
|
|
server = await createServer({
|
|
configFile: false,
|
|
server: { middlewareMode: true, watch: null, ws: false },
|
|
});
|
|
// Loads the received-project rule before the render starts, so a cold load on CI isn't timed by the waits below.
|
|
await server.ssrLoadModule(RECEIVED_RENDER);
|
|
const adapter = createViteAdapter(
|
|
dir,
|
|
server,
|
|
createProjectSignatureCache({ compute: () => "test" }),
|
|
);
|
|
const outputPath = join(dir, `out.${extra.format ?? "gif"}`);
|
|
const state = adapter.startRender({
|
|
project: { id: "demo", dir },
|
|
outputPath,
|
|
format: "gif",
|
|
gifLoop: 3,
|
|
fps: { num: 30, den: 1 },
|
|
quality: "draft",
|
|
jobId: "gif-job",
|
|
...extra,
|
|
});
|
|
return { dir, state, outputPath, metaPath: join(dir, "out.meta.json") };
|
|
}
|
|
|
|
describe("Vite GIF rendering", { timeout: 30_000 }, () => {
|
|
it("forwards loop count and writes a sidecar without overwriting the GIF", async () => {
|
|
producer.executeRenderJob.mockImplementation(async (_job, _dir, outputPath: string) => {
|
|
writeFileSync(outputPath, "GIF89a");
|
|
});
|
|
const { state, outputPath, metaPath } = await fixture();
|
|
await vi.waitFor(() => expect(state.status, state.error).toBe("complete"));
|
|
expect(producer.createRenderJob).toHaveBeenCalledWith(
|
|
expect.objectContaining({ format: "gif", gifLoop: 3 }),
|
|
);
|
|
expect(readFileSync(outputPath, "utf8")).toBe("GIF89a");
|
|
expect(JSON.parse(readFileSync(metaPath, "utf8"))).toMatchObject({ status: "complete" });
|
|
});
|
|
|
|
it.each([false, true])(
|
|
"cleans up cancelled GIF output and metadata (late completion: %s)",
|
|
async (lateCompletion) => {
|
|
producer.executeRenderJob.mockImplementation(
|
|
async (_job, _dir, outputPath: string, _progress, signal: AbortSignal) => {
|
|
writeFileSync(outputPath, "partial-gif");
|
|
writeFileSync(outputPath.replace(/\.gif$/, ".meta.json"), "{}");
|
|
await waitForRenderCancellation(signal, lateCompletion);
|
|
},
|
|
);
|
|
const { state, outputPath, metaPath } = await fixture();
|
|
await cancelAndVerifyArtifacts(state, outputPath, metaPath);
|
|
},
|
|
);
|
|
});
|
|
|
|
// vi.waitFor gives up after 1 s by default; a loaded Windows runner took longer to finish a mocked render.
|
|
const RENDER_DONE = { timeout: 20_000 };
|
|
|
|
describe("Studio dev server render jobs", { timeout: 30_000 }, () => {
|
|
it("hands the route's pageMotionBlur to the job, so a preview that could not blur renders unblurred", async () => {
|
|
producer.executeRenderJob.mockResolvedValue(undefined);
|
|
const { state } = await fixture({ format: "mp4", pageMotionBlur: "off" });
|
|
await vi.waitFor(() => expect(state.status, state.error).toBe("complete"), RENDER_DONE);
|
|
expect(producer.createRenderJob).toHaveBeenCalledWith(
|
|
expect.objectContaining({ pageMotionBlur: "off" }),
|
|
);
|
|
});
|
|
|
|
it("leaves the job's blur to the composition when the route sends none", async () => {
|
|
producer.executeRenderJob.mockResolvedValue(undefined);
|
|
const { state } = await fixture({ format: "mp4" });
|
|
await vi.waitFor(() => expect(state.status, state.error).toBe("complete"), RENDER_DONE);
|
|
expect(producer.createRenderJob.mock.calls[0]?.[0]).not.toHaveProperty("pageMotionBlur");
|
|
});
|
|
|
|
it.each([
|
|
["allows", "a project of your own", [] as string[], true],
|
|
["refuses", "a project whose received files include HTML", ["index.html"], false],
|
|
])("%s files outside the project for %s", async (_verb, _name, received, allowed) => {
|
|
producer.executeRenderJob.mockResolvedValue(undefined);
|
|
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
const { state } = await fixture({ format: "mp4" }, (dir) => {
|
|
writeFileSync(join(dir, "index.html"), '<img src="../outside.png">');
|
|
if (received.length === 0) return;
|
|
mkdirSync(join(dir, ".hyperframes"));
|
|
writeFileSync(
|
|
join(dir, ".hyperframes", "received-files.json"),
|
|
JSON.stringify({ files: received }),
|
|
);
|
|
});
|
|
await vi.waitFor(() => expect(state.status, state.error).toBe("complete"), RENDER_DONE);
|
|
expect(producer.createRenderJob).toHaveBeenCalledWith(
|
|
expect.objectContaining({ allowOutsideProjectAssets: allowed }),
|
|
);
|
|
warn.mockRestore();
|
|
});
|
|
});
|