import { describe, it, before, after } from "node:test";
import assert from "node:assert/strict";
import { createServer, type Server } from "node:http";
import { mkdtempSync, readFileSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { captureSite, looksLikeVideoFile, type CaptureResult } from "../src/capture/capture.js";
// A minimal-but-valid mp4 shape: [size]"ftyp" brand box followed by payload bytes.
const FULL_MP4 = Buffer.concat([
Buffer.from([0x00, 0x00, 0x00, 0x18]),
Buffer.from("ftypisom"),
Buffer.from([0x00, 0x00, 0x02, 0x00]),
Buffer.from("isomiso2"),
Buffer.alloc(64_000, 0x07),
]);
// The pathological range fragment observed in the wild: a tail slice (moov atom region)
// of the file — starts mid-container, no ftyp/EBML magic.
const TAIL_FRAGMENT = FULL_MP4.subarray(FULL_MP4.length - 1000);
describe("looksLikeVideoFile (container magic)", () => {
it("accepts mp4-family (ftyp), webm/mkv (EBML), and ogg (OggS) heads", () => {
assert.equal(looksLikeVideoFile(FULL_MP4), true);
assert.equal(looksLikeVideoFile(Buffer.concat([Buffer.from([0x1a, 0x45, 0xdf, 0xa3]), Buffer.alloc(100)])), true);
assert.equal(looksLikeVideoFile(Buffer.concat([Buffer.from("OggS"), Buffer.alloc(100)])), true);
});
it("rejects range fragments, HTML error bodies, and tiny buffers", () => {
assert.equal(looksLikeVideoFile(TAIL_FRAGMENT), false, "moov-tail fragment is not a video file");
assert.equal(looksLikeVideoFile(Buffer.from("
404")), false);
assert.equal(looksLikeVideoFile(Buffer.from([0x00, 0x00])), false);
});
});
// A