import { afterEach, describe, expect, it } from "vitest"; import { mkdtemp, readFile, rm } from "node:fs/promises"; import { join } from "node:path"; import { tmpdir } from "node:os"; import { downloadHls } from "../src/media/media-worker.js"; const roots:string[]=[]; afterEach(async()=>{for(const root of roots.splice(0)) await rm(root,{recursive:true,force:true});}); describe("private source download",()=>{ it("localizes a signed VOD playlist without leaking credentials to disk",async()=>{ const root=await mkdtemp(join(tmpdir(),"hls-test-"));roots.push(root); const requests:URL[]=[]; const fetcher:typeof fetch=(input,init)=>{ const url=new URL(typeof input==="string"?input:input instanceof URL?input.href:input.url);requests.push(url); expect(init?.redirect).toBe("error");expect(new Headers(init?.headers).has("authorization")).toBe(false); return Promise.resolve(new Response(url.pathname.endsWith(".m3u8")?"#EXTM3U\n#EXTINF:2,\n0.ts\n#EXT-X-ENDLIST\n":new Uint8Array([0x47,0,0,0]))); }; const result=await downloadHls(new URL("https://video.pencil-stub.com/episode/master.m3u8?auth_key=private"),root,new AbortController().signal,{remaining:1024},fetcher); expect(result.duration).toBe(2); const playlist=await readFile(result.path,"utf8"); expect(playlist).toContain("segment-0.ts");expect(playlist).not.toMatch(/private|https:|auth_key/); expect(requests.every(url=>url.origin==="https://video.pencil-stub.com"&&url.search==="?auth_key=private")).toBe(true); }); it("enforces byte quota before persisting oversized segments",async()=>{ const root=await mkdtemp(join(tmpdir(),"hls-test-"));roots.push(root); await expect(downloadHls(new URL("https://video.pencil-stub.com/e/a.m3u8"),root,new AbortController().signal,{remaining:1},()=>Promise.resolve(new Response("#EXTM3U\n#EXTINF:2,\na.ts\n#EXT-X-ENDLIST")))).rejects.toMatchObject({code:"MEDIA_BYTE_LIMIT"}); }); });