mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-26 00:57:05 +00:00
* test(aws-lambda): build the audio sidecar test's large video in well under a second * test(aws-lambda): check the audio sidecar holds only an audio stream * test(aws-lambda): end ffprobe options before the sidecar, and skip without ffprobe
375 lines
13 KiB
TypeScript
375 lines
13 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, it } from "bun:test";
|
|
import { spawnSync } from "node:child_process";
|
|
import {
|
|
appendFileSync,
|
|
mkdirSync,
|
|
mkdtempSync,
|
|
rmSync,
|
|
symlinkSync,
|
|
writeFileSync,
|
|
} from "node:fs";
|
|
import { tmpdir } from "node:os";
|
|
import { join } from "node:path";
|
|
import { createHash } from "node:crypto";
|
|
import type { S3Client } from "@aws-sdk/client-s3";
|
|
import { AUDIO_SIDECAR_MIN_BYTES } from "@hyperframes/producer/distributed";
|
|
import { audioSidecarUri, probeFactsUri } from "../contentStore.js";
|
|
import { parseSiteManifest } from "../siteManifest.js";
|
|
import { asS3Client, FakeS3 } from "./__fixtures__/fakeS3.js";
|
|
import { deploySite } from "./deploySite.js";
|
|
|
|
let projectDir: string;
|
|
let scratch: string;
|
|
|
|
beforeEach(() => {
|
|
projectDir = mkdtempSync(join(tmpdir(), "hf-deploy-site-test-"));
|
|
scratch = mkdtempSync(join(tmpdir(), "hf-deploy-site-scratch-"));
|
|
mkdirSync(join(projectDir, "assets"));
|
|
writeFileSync(join(projectDir, "index.html"), "<html><body>hi</body></html>");
|
|
writeFileSync(join(projectDir, "assets", "style.css"), "body { color: red; }");
|
|
});
|
|
|
|
afterEach(() => {
|
|
rmSync(projectDir, { recursive: true, force: true });
|
|
rmSync(scratch, { recursive: true, force: true });
|
|
});
|
|
|
|
function sha256(text: string): string {
|
|
return createHash("sha256").update(text).digest("hex");
|
|
}
|
|
|
|
function storeKey(text: string): string {
|
|
const digest = sha256(text);
|
|
return `cas/sha256/${digest.slice(0, 2)}/${digest}`;
|
|
}
|
|
|
|
function sitePaths(s3: FakeS3, siteId: string): string[] {
|
|
const manifest = s3.bodies.get(`test-bucket/sites/${siteId}/files.json`)?.toString() ?? "";
|
|
return parseSiteManifest(manifest).map((file) => file.path);
|
|
}
|
|
|
|
const HTML = "<html><body>hi</body></html>";
|
|
const CSS = "body { color: red; }";
|
|
|
|
describe("deploySite", () => {
|
|
it("names expectedBucketOwner as the bucket's owner on every request", async () => {
|
|
const s3 = new FakeS3();
|
|
await deploySite({
|
|
projectDir,
|
|
bucketName: "test-bucket",
|
|
expectedBucketOwner: "123456789012",
|
|
s3: asS3Client(s3),
|
|
});
|
|
expect(s3.ops.length).toBeGreaterThan(0);
|
|
expect(s3.ops.filter((op) => op.owner !== "123456789012")).toEqual([]);
|
|
});
|
|
|
|
it("uploads each file once under its content hash, then the site manifest", async () => {
|
|
const s3 = new FakeS3();
|
|
const result = await deploySite({
|
|
projectDir,
|
|
bucketName: "test-bucket",
|
|
s3: asS3Client(s3),
|
|
});
|
|
|
|
expect(result.uploaded).toBe(true);
|
|
expect(result.siteId).toMatch(/^[0-9a-f]{16}$/);
|
|
expect(result.bucketName).toBe("test-bucket");
|
|
expect(result.projectS3Uri).toBe(`s3://test-bucket/sites/${result.siteId}/files.json`);
|
|
expect(result.bytes).toBe(HTML.length + CSS.length);
|
|
const puts = s3.ops.filter((op) => op.kind === "put").map((op) => op.key);
|
|
expect(puts.slice(0, 2).sort()).toEqual([storeKey(HTML), storeKey(CSS)].sort());
|
|
expect(puts[2]).toBe(`sites/${result.siteId}/files.json`);
|
|
expect(s3.bodies.get(`test-bucket/${storeKey(CSS)}`)?.toString()).toBe(CSS);
|
|
expect(parseSiteManifest(s3.bodies.get(`test-bucket/${puts[2]}`)?.toString() ?? "")).toEqual([
|
|
{ path: "assets/style.css", sha256: sha256(CSS), sizeBytes: CSS.length },
|
|
{ path: "index.html", sha256: sha256(HTML), sizeBytes: HTML.length },
|
|
]);
|
|
});
|
|
|
|
it("skips a file another site already stored", async () => {
|
|
const s3 = new FakeS3();
|
|
const cssId = `test-bucket/${storeKey(CSS)}`;
|
|
s3.existing.add(cssId);
|
|
s3.bodies.set(cssId, Buffer.from(CSS));
|
|
s3.metadata.set(cssId, { sha256: sha256(CSS) });
|
|
|
|
await deploySite({ projectDir, bucketName: "test-bucket", s3: asS3Client(s3) });
|
|
|
|
const puts = s3.ops.filter((op) => op.kind === "put").map((op) => op.key);
|
|
expect(puts).toHaveLength(2);
|
|
expect(puts).not.toContain(storeKey(CSS));
|
|
expect(puts).toContain(storeKey(HTML));
|
|
});
|
|
|
|
it("yields a stable siteId across re-runs of the same tree", async () => {
|
|
const s3a = new FakeS3();
|
|
const a = await deploySite({
|
|
projectDir,
|
|
bucketName: "test-bucket",
|
|
s3: asS3Client(s3a),
|
|
});
|
|
const s3b = new FakeS3();
|
|
const b = await deploySite({
|
|
projectDir,
|
|
bucketName: "test-bucket",
|
|
s3: asS3Client(s3b),
|
|
});
|
|
expect(a.siteId).toBe(b.siteId);
|
|
});
|
|
|
|
it("changes siteId when a file's content changes", async () => {
|
|
const s3 = new FakeS3();
|
|
const before = await deploySite({
|
|
projectDir,
|
|
bucketName: "test-bucket",
|
|
s3: asS3Client(s3),
|
|
});
|
|
|
|
writeFileSync(join(projectDir, "index.html"), "<html><body>changed</body></html>");
|
|
const s3b = new FakeS3();
|
|
const after = await deploySite({
|
|
projectDir,
|
|
bucketName: "test-bucket",
|
|
s3: asS3Client(s3b),
|
|
});
|
|
expect(after.siteId).not.toBe(before.siteId);
|
|
});
|
|
|
|
it("uploads nothing when the same tree is deployed again", async () => {
|
|
const s3 = new FakeS3();
|
|
const first = await deploySite({
|
|
projectDir,
|
|
bucketName: "test-bucket",
|
|
s3: asS3Client(s3),
|
|
});
|
|
|
|
const second = await deploySite({
|
|
projectDir,
|
|
bucketName: "test-bucket",
|
|
s3: asS3Client(s3),
|
|
});
|
|
|
|
expect(second.uploaded).toBe(false);
|
|
expect(second.siteId).toBe(first.siteId);
|
|
// Two files plus the manifest, each put once and HEAD-checked on both deploys.
|
|
expect(s3.ops.filter((op) => op.kind === "put")).toHaveLength(3);
|
|
expect(s3.ops.filter((op) => op.kind === "head")).toHaveLength(6);
|
|
});
|
|
|
|
it("honours a caller-supplied siteId", async () => {
|
|
const s3 = new FakeS3();
|
|
const result = await deploySite({
|
|
projectDir,
|
|
bucketName: "test-bucket",
|
|
siteId: "release-v1.2.3",
|
|
s3: asS3Client(s3),
|
|
});
|
|
expect(result.siteId).toBe("release-v1.2.3");
|
|
expect(result.projectS3Uri).toBe("s3://test-bucket/sites/release-v1.2.3/files.json");
|
|
});
|
|
|
|
it("propagates non-404 S3 errors", async () => {
|
|
const errS3 = {
|
|
async send(_cmd: unknown): Promise<unknown> {
|
|
const err = new Error("Access Denied") as Error & {
|
|
$metadata: { httpStatusCode: number };
|
|
};
|
|
err.$metadata = { httpStatusCode: 403 };
|
|
throw err;
|
|
},
|
|
};
|
|
await expect(
|
|
deploySite({
|
|
projectDir,
|
|
bucketName: "test-bucket",
|
|
s3: errS3 as unknown as S3Client,
|
|
}),
|
|
).rejects.toThrow(/Access Denied/);
|
|
});
|
|
|
|
it("ignores SKIP_TOP_LEVEL dirs when hashing", async () => {
|
|
const s3 = new FakeS3();
|
|
const before = await deploySite({
|
|
projectDir,
|
|
bucketName: "test-bucket",
|
|
s3: asS3Client(s3),
|
|
});
|
|
|
|
mkdirSync(join(projectDir, "node_modules"));
|
|
writeFileSync(join(projectDir, "node_modules", "junk.bin"), "x".repeat(100));
|
|
const s3b = new FakeS3();
|
|
const after = await deploySite({
|
|
projectDir,
|
|
bucketName: "test-bucket",
|
|
s3: asS3Client(s3b),
|
|
});
|
|
|
|
// node_modules contents shouldn't move the hash.
|
|
expect(after.siteId).toBe(before.siteId);
|
|
});
|
|
|
|
it("ships a symlinked project, and its files linked in from outside only when included", async () => {
|
|
mkdirSync(join(scratch, "media"));
|
|
writeFileSync(join(scratch, "media", "clip.txt"), "clip");
|
|
writeFileSync(join(scratch, "logo.txt"), "logo");
|
|
symlinkSync(join(scratch, "media"), join(projectDir, "media"));
|
|
symlinkSync(join(scratch, "logo.txt"), join(projectDir, "logo.txt"));
|
|
symlinkSync(projectDir, join(scratch, "fx"));
|
|
const s3 = new FakeS3();
|
|
const notices: string[] = [];
|
|
const leftOut = await deploySite({
|
|
projectDir: join(scratch, "fx"),
|
|
bucketName: "test-bucket",
|
|
onNotice: (line) => notices.push(line),
|
|
s3: asS3Client(s3),
|
|
});
|
|
expect(sitePaths(s3, leftOut.siteId)).toEqual(["assets/style.css", "index.html"]);
|
|
expect(notices.join("\n")).toContain("outside the project: logo.txt, media.");
|
|
|
|
const included = await deploySite({
|
|
projectDir: join(scratch, "fx"),
|
|
bucketName: "test-bucket",
|
|
includeOutsideFiles: true,
|
|
s3: asS3Client(s3),
|
|
});
|
|
expect(sitePaths(s3, included.siteId)).toEqual([
|
|
"assets/style.css",
|
|
"index.html",
|
|
"logo.txt",
|
|
"media/clip.txt",
|
|
]);
|
|
});
|
|
});
|
|
|
|
const FFMPEG = process.env.HYPERFRAMES_FFMPEG_PATH?.trim() || "ffmpeg";
|
|
const HAS_FFMPEG = spawnSync(FFMPEG, ["-version"]).status === 0;
|
|
const FFPROBE = process.env.HYPERFRAMES_FFPROBE_PATH?.trim() || "ffprobe";
|
|
const HAS_FFMPEG_AND_FFPROBE = HAS_FFMPEG && spawnSync(FFPROBE, ["-version"]).status === 0;
|
|
|
|
describe.skipIf(!HAS_FFMPEG_AND_FFPROBE)("deploySite audio sidecars", () => {
|
|
function appendMp4FreeBoxEveryReaderSkips(file: string, bytes: number): void {
|
|
const free = Buffer.alloc(bytes);
|
|
free.writeUInt32BE(bytes, 0);
|
|
free.write("free", 4, "latin1");
|
|
appendFileSync(file, free);
|
|
}
|
|
|
|
function addLargeVideo(): void {
|
|
const take = join(projectDir, "assets", "take.mp4");
|
|
const made = spawnSync(FFMPEG, [
|
|
...["-v", "error", "-y", "-f", "lavfi", "-i", "testsrc2=size=64x64:rate=25:duration=2"],
|
|
...["-f", "lavfi", "-i", "sine=frequency=440:sample_rate=48000:duration=2"],
|
|
...["-c:v", "mpeg4", "-c:a", "aac", take],
|
|
]);
|
|
expect(made.status).toBe(0);
|
|
appendMp4FreeBoxEveryReaderSkips(take, AUDIO_SIDECAR_MIN_BYTES);
|
|
}
|
|
|
|
function streamTypes(media: Buffer): string[] {
|
|
const file = join(scratch, "probed.m4a");
|
|
writeFileSync(file, media);
|
|
const probed = spawnSync(
|
|
FFPROBE,
|
|
["-v", "error", "-show_entries", "stream=codec_type", "-of", "csv=p=0", "--", file],
|
|
{ encoding: "utf8" },
|
|
);
|
|
expect(probed.status).toBe(0);
|
|
return probed.stdout.trim().split("\n");
|
|
}
|
|
|
|
it("stores a large video's audio beside it, and leaves the site's file list as it was", async () => {
|
|
addLargeVideo();
|
|
const s3 = new FakeS3();
|
|
const site = await deploySite({ projectDir, bucketName: "test-bucket", s3: asS3Client(s3) });
|
|
|
|
const take = parseSiteManifest(
|
|
s3.bodies.get(`test-bucket/sites/${site.siteId}/files.json`)?.toString() ?? "",
|
|
).find((file) => file.path === "assets/take.mp4");
|
|
expect(take?.sizeBytes).toBeGreaterThan(AUDIO_SIDECAR_MIN_BYTES);
|
|
const sidecar = audioSidecarUri("test-bucket", take?.sha256 ?? "").replace("s3://", "");
|
|
expect(s3.existing.has(sidecar)).toBe(true);
|
|
expect(streamTypes(s3.bodies.get(sidecar) ?? Buffer.alloc(0))).toEqual(["audio"]);
|
|
expect(sitePaths(s3, site.siteId)).toEqual([
|
|
"assets/style.css",
|
|
"assets/take.mp4",
|
|
"index.html",
|
|
]);
|
|
});
|
|
|
|
it("deploys the same site with no sidecar when there is no ffmpeg, and says so", async () => {
|
|
addLargeVideo();
|
|
const notices: string[] = [];
|
|
const before = process.env.HYPERFRAMES_FFMPEG_PATH;
|
|
process.env.HYPERFRAMES_FFMPEG_PATH = join(scratch, "no-such-ffmpeg");
|
|
const s3 = new FakeS3();
|
|
try {
|
|
await deploySite({
|
|
projectDir,
|
|
bucketName: "test-bucket",
|
|
s3: asS3Client(s3),
|
|
onNotice: (message) => notices.push(message),
|
|
});
|
|
} finally {
|
|
if (before === undefined) delete process.env.HYPERFRAMES_FFMPEG_PATH;
|
|
else process.env.HYPERFRAMES_FFMPEG_PATH = before;
|
|
}
|
|
|
|
expect([...s3.existing].filter((id) => id.endsWith(".audio.m4a"))).toEqual([]);
|
|
expect(notices).toHaveLength(1);
|
|
expect(notices[0]).toContain("ffmpeg");
|
|
});
|
|
});
|
|
|
|
describe.skipIf(!HAS_FFMPEG_AND_FFPROBE)("deploySite probe facts", () => {
|
|
function addVideo(): void {
|
|
const made = spawnSync(FFMPEG, [
|
|
...["-v", "error", "-y", "-f", "lavfi", "-i", "testsrc2=size=64x64:rate=25:duration=2"],
|
|
...["-c:v", "mpeg4", join(projectDir, "assets", "take.mp4")],
|
|
]);
|
|
expect(made.status).toBe(0);
|
|
}
|
|
|
|
it("stores each video's ffprobe JSON beside it, and leaves the site's file list as it was", async () => {
|
|
addVideo();
|
|
const s3 = new FakeS3();
|
|
const site = await deploySite({ projectDir, bucketName: "test-bucket", s3: asS3Client(s3) });
|
|
|
|
const take = parseSiteManifest(
|
|
s3.bodies.get(`test-bucket/sites/${site.siteId}/files.json`)?.toString() ?? "",
|
|
).find((file) => file.path === "assets/take.mp4");
|
|
const facts = probeFactsUri("test-bucket", take?.sha256 ?? "").replace("s3://", "");
|
|
const stored = JSON.parse(s3.bodies.get(facts)?.toString() ?? "{}");
|
|
expect(stored.ffprobe.streams[0].codec_type).toBe("video");
|
|
expect(stored.finalFrame.seconds).toBeGreaterThan(1.5);
|
|
expect(stored.finalFrame.lastPacketSeconds).toBeGreaterThan(0);
|
|
expect(sitePaths(s3, site.siteId)).toEqual([
|
|
"assets/style.css",
|
|
"assets/take.mp4",
|
|
"index.html",
|
|
]);
|
|
});
|
|
|
|
it("deploys the same site with no probe stored when there is no ffprobe, and says so", async () => {
|
|
addVideo();
|
|
const notices: string[] = [];
|
|
const before = process.env.HYPERFRAMES_FFPROBE_PATH;
|
|
process.env.HYPERFRAMES_FFPROBE_PATH = join(scratch, "no-such-ffprobe");
|
|
const s3 = new FakeS3();
|
|
try {
|
|
await deploySite({
|
|
projectDir,
|
|
bucketName: "test-bucket",
|
|
s3: asS3Client(s3),
|
|
onNotice: (message) => notices.push(message),
|
|
});
|
|
} finally {
|
|
if (before === undefined) delete process.env.HYPERFRAMES_FFPROBE_PATH;
|
|
else process.env.HYPERFRAMES_FFPROBE_PATH = before;
|
|
}
|
|
|
|
expect([...s3.existing].filter((id) => id.endsWith(".probe.v2.json"))).toEqual([]);
|
|
expect(notices.some((message) => message.includes("ffprobe"))).toBe(true);
|
|
});
|
|
});
|