From 31bd8ea77b5cdd05b9c037ba09d52a76e7e6b074 Mon Sep 17 00:00:00 2001 From: DrSmoothl <1787882683@qq.com> Date: Fri, 14 Aug 2026 10:57:17 +0800 Subject: [PATCH] test(protocol): cover highway streaming hash and upload staging --- .../protocol/tests/highway/hash-file.test.ts | 286 +++++++++++ packages/protocol/tests/highway/stage.test.ts | 468 ++++++++++++++++++ 2 files changed, 754 insertions(+) create mode 100644 packages/protocol/tests/highway/hash-file.test.ts create mode 100644 packages/protocol/tests/highway/stage.test.ts diff --git a/packages/protocol/tests/highway/hash-file.test.ts b/packages/protocol/tests/highway/hash-file.test.ts new file mode 100644 index 00000000..501c33f2 --- /dev/null +++ b/packages/protocol/tests/highway/hash-file.test.ts @@ -0,0 +1,286 @@ +// Streaming hasher must be byte-identical to the buffered helpers over the +// same bytes: computeHashes + computeVideoSha1Blocks (+ computeMd5 of the +// head) for hashFileStreaming, and computeHashes + computeSha1StateV for +// hashFlashFileStreaming. Flash Sha1StateV is not video sha1Blocks. + +import { afterAll, beforeAll, describe, expect, it } from 'vitest'; +import { createHash } from 'node:crypto'; +import { mkdtemp, rm, writeFile } from 'node:fs/promises'; +import { tmpdir } from 'node:os'; +import { join } from 'node:path'; +import { hashFileStreaming, hashFlashFileStreaming } from '@snowluma/protocol/highway/hash-file'; +import { computeHashes, computeMd5 } from '@snowluma/protocol/highway/utils'; +import { computeSha1StateV, Sha1Stream } from '@snowluma/protocol/highway/sha1-stream'; +import { computeVideoSha1Blocks } from '../../src/highway/video-upload'; + +const MB = 1024 * 1024; + +// Same generator as video-sha1-blocks.test.ts (and its golden-capture script). +function det(len: number): Uint8Array { + const b = new Uint8Array(len); + for (let i = 0; i < len; i++) b[i] = (i * 31 + 7) & 0xff; + return b; +} + +function hex(u: Uint8Array): string { + return Buffer.from(u).toString('hex'); +} + +function nodeMd5Hex(data: Uint8Array): string { + return createHash('md5').update(Buffer.from(data)).digest('hex'); +} + +function nodeSha1Hex(data: Uint8Array): string { + return createHash('sha1').update(Buffer.from(data)).digest('hex'); +} + +describe('hashFileStreaming', () => { + let dir: string; + beforeAll(async () => { + dir = await mkdtemp(join(tmpdir(), 'sl-hash-file-')); + }); + afterAll(async () => { + await rm(dir, { recursive: true, force: true }); + }); + + async function writeBin(name: string, data: Uint8Array): Promise { + const file = join(dir, name); + await writeFile(file, data); + return file; + } + + it('empty file: well-known empty digests and a single finalized sha1 block', async () => { + const file = await writeBin('empty.bin', new Uint8Array(0)); + const streamed = await hashFileStreaming(file); + + expect(streamed.md5Hex).toBe('d41d8cd98f00b204e9800998ecf8427e'); + expect(streamed.sha1Hex).toBe('da39a3ee5e6b4b0d3255bfef95601890afd80709'); + expect(hex(streamed.md5)).toBe('d41d8cd98f00b204e9800998ecf8427e'); + expect(hex(streamed.sha1)).toBe('da39a3ee5e6b4b0d3255bfef95601890afd80709'); + expect(streamed.headMd5).toBeUndefined(); + expect(streamed.sha1Blocks.map(hex)).toEqual([ + 'da39a3ee5e6b4b0d3255bfef95601890afd80709', + ]); + }); + + it('empty file with headLimit still yields md5 of zero bytes', async () => { + const file = await writeBin('empty-head.bin', new Uint8Array(0)); + const streamed = await hashFileStreaming(file, { headLimit: 10 }); + expect(hex(streamed.headMd5!)).toBe('d41d8cd98f00b204e9800998ecf8427e'); + expect(streamed.md5Hex).toBe('d41d8cd98f00b204e9800998ecf8427e'); + }); + + it('sub-1 MiB file matches RFC hashes and has only the finalized sha1 block', async () => { + const data = Buffer.from('abc', 'utf8'); + const file = await writeBin('abc.bin', data); + const streamed = await hashFileStreaming(file); + + expect(streamed.md5Hex).toBe('900150983cd24fb0d6963f7d28e17f72'); + expect(streamed.sha1Hex).toBe('a9993e364706816aba3e25717850c26c9cd0d89d'); + expect(streamed.sha1Blocks.map(hex)).toEqual([ + 'a9993e364706816aba3e25717850c26c9cd0d89d', + ]); + expect(streamed.headMd5).toBeUndefined(); + }); + + it('det(1000) matches computeHashes + computeVideoSha1Blocks + Node crypto', async () => { + const data = det(1000); + const file = await writeBin('det-1000.bin', data); + const streamed = await hashFileStreaming(file); + const buffered = computeHashes(data); + + expect(streamed.md5Hex).toBe(nodeMd5Hex(data)); + expect(streamed.sha1Hex).toBe(nodeSha1Hex(data)); + expect(hex(streamed.md5)).toBe(hex(buffered.md5)); + expect(hex(streamed.sha1)).toBe(hex(buffered.sha1)); + expect(streamed.md5Hex).toBe(buffered.md5Hex); + expect(streamed.sha1Hex).toBe(buffered.sha1Hex); + expect(streamed.sha1Blocks.map(hex)).toEqual(computeVideoSha1Blocks(data).map(hex)); + expect(streamed.sha1Blocks).toHaveLength(1); + }); + + it('exact 1 MiB emits an un-finalized boundary state then the overall sha1', async () => { + const data = det(MB); + const file = await writeBin('exact-1m.bin', data); + const streamed = await hashFileStreaming(file); + const overall = nodeSha1Hex(data); + const prefixDigest = nodeSha1Hex(data); // whole file *is* the first slice + const manual = new Sha1Stream(); + manual.update(data); + + expect(streamed.md5Hex).toBe(nodeMd5Hex(data)); + expect(streamed.sha1Hex).toBe(overall); + expect(streamed.sha1Blocks).toHaveLength(2); + expect(hex(streamed.sha1Blocks[0]!)).toBe(hex(manual.hash(true))); + expect(hex(streamed.sha1Blocks[0]!)).not.toBe(prefixDigest); + expect(hex(streamed.sha1Blocks[1]!)).toBe(overall); + expect(streamed.sha1Blocks.map(hex)).toEqual(computeVideoSha1Blocks(data).map(hex)); + }); + + it('2 MiB + 12345 reproduces the video-sha1-blocks golden states', async () => { + const data = det(2 * MB + 12345); + const file = await writeBin('golden-2m.bin', data); + const streamed = await hashFileStreaming(file); + + expect(streamed.sha1Blocks.map(hex)).toEqual([ + 'ab9a20fd6c98fc1f8f0c985cd552ff14c6feacfd', + '2ccc5923b75f46ebd8ccb78e8c413c18025178b9', + 'dd9cfd4648ab460260afa820c98bfec1b17655d4', + ]); + expect(streamed.sha1Hex).toBe('dd9cfd4648ab460260afa820c98bfec1b17655d4'); + expect(streamed.md5Hex).toBe(nodeMd5Hex(data)); + expect(streamed.sha1Blocks.map(hex)).toEqual(computeVideoSha1Blocks(data).map(hex)); + }); + + it('omitting opts leaves headMd5 undefined', async () => { + const file = await writeBin('no-opts.bin', det(64)); + const streamed = await hashFileStreaming(file); + expect(streamed.headMd5).toBeUndefined(); + }); + + it('headLimit 0 is present and is the empty-md5', async () => { + const file = await writeBin('head-0.bin', det(200)); + const streamed = await hashFileStreaming(file, { headLimit: 0 }); + expect(hex(streamed.headMd5!)).toBe('d41d8cd98f00b204e9800998ecf8427e'); + expect(streamed.md5Hex).toBe(nodeMd5Hex(det(200))); + }); + + it('headMd5 is computeMd5 of the first N bytes inside the first 1 MiB chunk', async () => { + const data = det(8000); + const file = await writeBin('head-inside.bin', data); + const streamed = await hashFileStreaming(file, { headLimit: 10 }); + expect(hex(streamed.headMd5!)).toBe(hex(computeMd5(data.subarray(0, 10)))); + expect(hex(streamed.headMd5!)).toBe(nodeMd5Hex(data.subarray(0, 10))); + }); + + it('headMd5 across a 1 MiB boundary matches computeMd5 of the clamped prefix', async () => { + const data = det(MB + 500); + const file = await writeBin('head-span.bin', data); + const cases: Array<[number, number]> = [ + [MB - 1, MB - 1], + [MB, MB], + [MB + 1, MB + 1], + [data.length + 1, data.length], + ]; + for (const [limit, take] of cases) { + const streamed = await hashFileStreaming(file, { headLimit: limit }); + expect(hex(streamed.headMd5!)).toBe(hex(computeMd5(data.subarray(0, take)))); + expect(streamed.md5Hex).toBe(nodeMd5Hex(data)); + expect(streamed.sha1Hex).toBe(nodeSha1Hex(data)); + } + }); + + it('rejects a missing path from stat before hashing', async () => { + await expect(hashFileStreaming(join(dir, 'no-such.bin'))).rejects.toMatchObject({ + code: 'ENOENT', + }); + }); +}); + +describe('hashFlashFileStreaming', () => { + let dir: string; + beforeAll(async () => { + dir = await mkdtemp(join(tmpdir(), 'sl-flash-hash-file-')); + }); + afterAll(async () => { + await rm(dir, { recursive: true, force: true }); + }); + + async function writeBin(name: string, data: Uint8Array): Promise { + const file = join(dir, name); + await writeFile(file, data); + return file; + } + + it('empty file: empty digests, sliceCount 0, no Sha1StateV entries', async () => { + const file = await writeBin('empty.bin', new Uint8Array(0)); + const streamed = await hashFlashFileStreaming(file); + + expect(streamed.md5Hex).toBe('d41d8cd98f00b204e9800998ecf8427e'); + expect(streamed.sha1Hex).toBe('da39a3ee5e6b4b0d3255bfef95601890afd80709'); + expect(streamed.sliceCount).toBe(0); + expect(streamed.sha1StateV).toEqual([]); + }); + + it('sub-1 MiB file is a single finalized slice', async () => { + const data = Buffer.from('abc', 'utf8'); + const file = await writeBin('abc.bin', data); + const streamed = await hashFlashFileStreaming(file); + + expect(streamed.sliceCount).toBe(1); + expect(streamed.md5Hex).toBe('900150983cd24fb0d6963f7d28e17f72'); + expect(streamed.sha1Hex).toBe('a9993e364706816aba3e25717850c26c9cd0d89d'); + expect(streamed.sha1StateV.map(hex)).toEqual([ + 'a9993e364706816aba3e25717850c26c9cd0d89d', + ]); + }); + + it('exact 1 MiB omits the extra un-finalized last-slice state video sha1Blocks keeps', async () => { + const data = det(MB); + const file = await writeBin('exact-1m.bin', data); + const flash = await hashFlashFileStreaming(file); + const video = await hashFileStreaming(file); + const overall = nodeSha1Hex(data); + const refState = computeSha1StateV(data, 1, MB); + + expect(flash.sliceCount).toBe(1); + expect(flash.sha1StateV).toHaveLength(1); + expect(hex(flash.sha1StateV[0]!)).toBe(overall); + expect(hex(flash.sha1StateV[0]!)).toBe(hex(refState[0]!)); + expect(video.sha1Blocks).toHaveLength(2); + expect(hex(flash.sha1StateV[0]!)).toBe(hex(video.sha1Blocks[1]!)); + expect(hex(flash.sha1StateV[0]!)).not.toBe(hex(video.sha1Blocks[0]!)); + expect(flash.md5Hex).toBe(nodeMd5Hex(data)); + }); + + it('1 MiB + 1: first state is the un-finalized 1 MiB snapshot', async () => { + const data = det(MB + 1); + const file = await writeBin('1m-plus.bin', data); + const flash = await hashFlashFileStreaming(file); + const manual = new Sha1Stream(); + manual.update(data.subarray(0, MB)); + const refState = computeSha1StateV(data, 2, MB); + + expect(flash.sliceCount).toBe(2); + expect(flash.sha1StateV).toHaveLength(2); + expect(hex(flash.sha1StateV[0]!)).toBe(hex(manual.hash(true))); + expect(hex(flash.sha1StateV[0]!)).not.toBe(nodeSha1Hex(data.subarray(0, MB))); + expect(hex(flash.sha1StateV[1]!)).toBe(nodeSha1Hex(data)); + expect(flash.sha1StateV.map(hex)).toEqual(refState.map(hex)); + }); + + it('exact 2 MiB: flash has 2 states, video sha1Blocks has 3', async () => { + const data = det(2 * MB); + const file = await writeBin('exact-2m.bin', data); + const flash = await hashFlashFileStreaming(file); + const video = await hashFileStreaming(file); + + expect(flash.sliceCount).toBe(2); + expect(flash.sha1StateV).toHaveLength(2); + expect(video.sha1Blocks).toHaveLength(3); + expect(hex(flash.sha1StateV[0]!)).toBe(hex(video.sha1Blocks[0]!)); + expect(hex(flash.sha1StateV[1]!)).toBe(hex(video.sha1Blocks[2]!)); + expect(hex(flash.sha1StateV[1]!)).toBe(nodeSha1Hex(data)); + expect(flash.sha1StateV.map(hex)).toEqual(computeSha1StateV(data, 2, MB).map(hex)); + expect(flash.md5Hex).toBe(computeHashes(data).md5Hex); + }); + + it('2 MiB + 12345 Sha1StateV matches computeSha1StateV and Node overall sha1', async () => { + const data = det(2 * MB + 12345); + const file = await writeBin('golden-2m.bin', data); + const flash = await hashFlashFileStreaming(file); + + expect(flash.sliceCount).toBe(3); + expect(flash.sha1Hex).toBe('dd9cfd4648ab460260afa820c98bfec1b17655d4'); + expect(hex(flash.sha1StateV[2]!)).toBe('dd9cfd4648ab460260afa820c98bfec1b17655d4'); + expect(hex(flash.sha1StateV[0]!)).toBe('ab9a20fd6c98fc1f8f0c985cd552ff14c6feacfd'); + expect(hex(flash.sha1StateV[1]!)).toBe('2ccc5923b75f46ebd8ccb78e8c413c18025178b9'); + expect(flash.sha1StateV.map(hex)).toEqual(computeSha1StateV(data, 3, MB).map(hex)); + }); + + it('rejects a missing path from stat before hashing', async () => { + await expect(hashFlashFileStreaming(join(dir, 'no-such.bin'))).rejects.toMatchObject({ + code: 'ENOENT', + }); + }); +}); diff --git a/packages/protocol/tests/highway/stage.test.ts b/packages/protocol/tests/highway/stage.test.ts new file mode 100644 index 00000000..1cb98da2 --- /dev/null +++ b/packages/protocol/tests/highway/stage.test.ts @@ -0,0 +1,468 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; +import { randomUUID } from 'crypto'; +import fs, { promises as fsp } from 'fs'; +import os from 'os'; +import path from 'path'; +import { pathToFileURL } from 'url'; +import { STAGE_DIR, stageSourceToDisk, sweepStagedUploads } from '@snowluma/protocol/highway/stage'; + +const DOWNLOAD_UA = + 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36'; +const STAGE_TTL_MS = 30 * 60 * 1000; +const SNOW_BYTES = Buffer.from([0x53, 0x4e, 0x4f, 0x57]); + +const live: Array<{ cleanup(): Promise }> = []; +let scratch = ''; + +function hold }>(staged: T): T { + live.push(staged); + return staged; +} + +async function stageNames(): Promise { + try { + return await fsp.readdir(STAGE_DIR); + } catch { + return []; + } +} + +async function newPartNames(before: readonly string[]): Promise { + const prior = new Set(before); + return (await stageNames()).filter((name) => name.endsWith('.part') && !prior.has(name)); +} + +function expectStagedPath(filePath: string, ext: string): void { + expect(filePath.startsWith(`${STAGE_DIR}${path.sep}`)).toBe(true); + expect(path.basename(filePath)).toMatch( + new RegExp(`^stage-[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}${ext.replaceAll('.', '\\.')}$`), + ); +} + +beforeEach(async () => { + scratch = await fsp.mkdtemp(path.join(os.tmpdir(), 'sl-proto-stage-')); +}); + +afterEach(async () => { + vi.unstubAllGlobals(); + vi.restoreAllMocks(); + while (live.length > 0) { + await live.pop()!.cleanup(); + } + await fsp.rm(scratch, { recursive: true, force: true }); +}); + +describe('STAGE_DIR', () => { + it('is os.tmpdir()/media-stage', () => { + expect(STAGE_DIR).toBe(path.join(os.tmpdir(), 'media-stage')); + }); +}); + +describe('stageSourceToDisk — empty / classification', () => { + it('rejects an empty source before touching disk', async () => { + const before = await stageNames(); + await expect(stageSourceToDisk('', 64)).rejects.toThrow('stage source is empty'); + expect(await stageNames()).toEqual(before); + }); + + it('rejects a data URL that is missing the comma separator', async () => { + await expect(stageSourceToDisk('data:image/png;base64', 64)).rejects.toThrow( + 'data URL source is missing its payload separator', + ); + }); + + it('rejects a data URL that is not base64-encoded', async () => { + await expect(stageSourceToDisk('data:text/plain,hello', 64)).rejects.toThrow( + 'data URL source must use base64 encoding', + ); + }); + + it('rejects a missing local path via realpath', async () => { + const missing = path.join(scratch, 'no-such-file.bin'); + await expect(stageSourceToDisk(missing, 64)).rejects.toThrow(/ENOENT/); + }); +}); + +describe('stageSourceToDisk — base64', () => { + it('writes base64:// bytes to a .bin stage file with an empty fileName', async () => { + const staged = hold(await stageSourceToDisk('base64://U05PVw==', 64)); + expect(staged.fileSize).toBe(4); + expect(staged.fileName).toBe(''); + expectStagedPath(staged.filePath, '.bin'); + expect(await fsp.readFile(staged.filePath)).toEqual(SNOW_BYTES); + }); + + it('accepts a case-insensitive BASE64:// prefix and one-byte padding', async () => { + const staged = hold(await stageSourceToDisk('BASE64:///w==', 1)); + expect(staged.fileSize).toBe(1); + expect(staged.fileName).toBe(''); + expect(await fsp.readFile(staged.filePath)).toEqual(Buffer.from([0xff])); + }); + + it('accepts two-byte and three-byte payloads at the decoded-size ceiling', async () => { + const two = hold(await stageSourceToDisk('base64://AAA=', 2)); + const three = hold(await stageSourceToDisk('base64://AAAA', 3)); + expect(two.fileSize).toBe(2); + expect(three.fileSize).toBe(3); + expect(await fsp.readFile(two.filePath)).toEqual(Buffer.from([0x00, 0x00])); + expect(await fsp.readFile(three.filePath)).toEqual(Buffer.from([0x00, 0x00, 0x00])); + }); + + it('stages an RFC 2397 data URL and keeps fileName empty', async () => { + const staged = hold(await stageSourceToDisk('data:image/png;base64,U05PVw==', 4)); + expect(staged.fileSize).toBe(4); + expect(staged.fileName).toBe(''); + expect(await fsp.readFile(staged.filePath)).toEqual(SNOW_BYTES); + }); + + it('accepts DATA:…;BASE64, payloads (case-insensitive metadata)', async () => { + const staged = hold(await stageSourceToDisk('DATA:application/octet-stream;BASE64,AA==', 1)); + expect(staged.fileSize).toBe(1); + expect(await fsp.readFile(staged.filePath)).toEqual(Buffer.from([0x00])); + }); + + it('strips non-base64 characters before the decoded-length check', async () => { + const staged = hold(await stageSourceToDisk('base64://U05P\nVw==', 4)); + expect(staged.fileSize).toBe(4); + expect(await fsp.readFile(staged.filePath)).toEqual(SNOW_BYTES); + }); + + it('stages an empty base64:// payload as a zero-byte file', async () => { + const staged = hold(await stageSourceToDisk('base64://', 0)); + expect(staged.fileSize).toBe(0); + expect(staged.fileName).toBe(''); + expect(await fsp.readFile(staged.filePath)).toEqual(Buffer.alloc(0)); + }); + + it('rejects oversized base64 on decoded length and leaves no new .part', async () => { + const before = await stageNames(); + await expect(stageSourceToDisk('base64://aGVsbG8=', 4)).rejects.toThrow('stage source too large: 5 > 4'); + expect(await newPartNames(before)).toEqual([]); + }); + + it('rejects an oversized data URL on decoded length and leaves no new .part', async () => { + const before = await stageNames(); + await expect(stageSourceToDisk('data:audio/ogg;base64,aGVsbG8=', 4)).rejects.toThrow( + 'stage source too large: 5 > 4', + ); + expect(await newPartNames(before)).toEqual([]); + }); + + it('unlinks the .part when finalize rename fails', async () => { + const before = await stageNames(); + vi.spyOn(fsp, 'rename').mockRejectedValueOnce(new Error('rename failed')); + await expect(stageSourceToDisk('base64://U05PVw==', 64)).rejects.toThrow('rename failed'); + expect(await newPartNames(before)).toEqual([]); + }); +}); + +describe('stageSourceToDisk — http', () => { + it('streams the body to disk and uses the decoded URL basename', async () => { + vi.stubGlobal( + 'fetch', + vi.fn(async () => new Response(Uint8Array.from([0x10, 0x20, 0x30, 0x40]), { status: 200 })), + ); + const staged = hold(await stageSourceToDisk('https://media.example.test/packs/clip%20v2.mp4?sig=9', 64)); + expect(staged.fileName).toBe('clip v2.mp4'); + expect(staged.fileSize).toBe(4); + expectStagedPath(staged.filePath, '.bin'); + expect(await fsp.readFile(staged.filePath)).toEqual(Buffer.from([0x10, 0x20, 0x30, 0x40])); + }); + + it('keeps fileName empty when the URL has no basename', async () => { + vi.stubGlobal('fetch', vi.fn(async () => new Response(Uint8Array.from([0x7f]), { status: 200 }))); + const staged = hold(await stageSourceToDisk('https://media.example.test/', 8)); + expect(staged.fileName).toBe(''); + expect(staged.fileSize).toBe(1); + expect(await fsp.readFile(staged.filePath)).toEqual(Buffer.from([0x7f])); + }); + + it('creates an empty staged file for an empty HTTP body', async () => { + vi.stubGlobal('fetch', vi.fn(async () => new Response(new Uint8Array(0), { status: 200 }))); + const staged = hold(await stageSourceToDisk('HTTP://cdn.example.test/empty.bin', 8)); + expect(staged.fileName).toBe('empty.bin'); + expect(staged.fileSize).toBe(0); + expect(await fsp.readFile(staged.filePath)).toEqual(Buffer.alloc(0)); + }); + + it('rejects a declared Content-Length overflow without retrying and leaves no .part', async () => { + const fetchMock = vi.fn(async () => new Response(new Uint8Array(80), { + status: 200, + headers: { 'content-length': '80' }, + })); + vi.stubGlobal('fetch', fetchMock); + const before = await stageNames(); + await expect(stageSourceToDisk('https://cdn.example.test/huge.bin', 16)).rejects.toThrow( + 'stage too large: 80 > 16', + ); + expect(fetchMock).toHaveBeenCalledTimes(1); + expect(await newPartNames(before)).toEqual([]); + }); + + it('rejects a streamed overflow after the sink opened and discards the .part', async () => { + const stream = new ReadableStream({ + start(controller) { + controller.enqueue(new Uint8Array(10).fill(0x11)); + controller.enqueue(new Uint8Array(10).fill(0x22)); + controller.enqueue(new Uint8Array(10).fill(0x33)); + controller.close(); + }, + }); + vi.stubGlobal('fetch', vi.fn(async () => new Response(stream, { status: 200 }))); + const before = await stageNames(); + await expect(stageSourceToDisk('https://cdn.example.test/stream.bin', 16)).rejects.toThrow( + 'stage too large: > 16', + ); + expect(await newPartNames(before)).toEqual([]); + }); + + it('retries once with Referer after a non-size failure and stages the retry body', async () => { + const url = 'https://media.example.test/packs/retry.bin'; + const fetchMock = vi.fn(async (_input: string, init?: RequestInit) => { + const headers = init?.headers as Record; + if (!headers.Referer) throw new TypeError('fetch failed'); + expect(headers.Referer).toBe(url); + expect(headers['User-Agent']).toBe(DOWNLOAD_UA); + expect(headers.Accept).toBe('*/*'); + expect(init?.redirect).toBe('follow'); + return new Response(Uint8Array.from([0xaa, 0xbb, 0xcc]), { status: 200 }); + }); + vi.stubGlobal('fetch', fetchMock); + const staged = hold(await stageSourceToDisk(url, 32)); + expect(fetchMock).toHaveBeenCalledTimes(2); + expect(staged.fileSize).toBe(3); + expect(await fsp.readFile(staged.filePath)).toEqual(Buffer.from([0xaa, 0xbb, 0xcc])); + }); + + it('surfaces the first HTTP status error when the Referer retry also fails', async () => { + const fetchMock = vi.fn(async () => new Response(null, { status: 503 })); + vi.stubGlobal('fetch', fetchMock); + await expect(stageSourceToDisk('https://cdn.example.test/down.bin', 32)).rejects.toThrow( + 'HTTP download failed: 503', + ); + expect(fetchMock).toHaveBeenCalledTimes(2); + }); + + it('unlinks the http .part when rename after download fails', async () => { + vi.stubGlobal( + 'fetch', + vi.fn(async () => new Response(Uint8Array.from([0x01, 0x02]), { status: 200 })), + ); + vi.spyOn(fsp, 'rename').mockRejectedValueOnce(new Error('http rename failed')); + const before = await stageNames(); + await expect(stageSourceToDisk('https://cdn.example.test/r.bin', 32)).rejects.toThrow( + 'http rename failed', + ); + expect(await newPartNames(before)).toEqual([]); + }); +}); + +describe('stageSourceToDisk — local files', () => { + it('hardlinks a raw path, preserves basename, and pins the inode after source unlink', async () => { + const src = path.join(scratch, 'payload.dat'); + await fsp.writeFile(src, Buffer.from([0xde, 0xad, 0xbe, 0xef])); + const srcStat = await fsp.stat(src); + const staged = hold(await stageSourceToDisk(src, 64)); + expect(staged.fileName).toBe('payload.dat'); + expect(staged.fileSize).toBe(4); + expectStagedPath(staged.filePath, '.dat'); + const stagedStat = await fsp.stat(staged.filePath); + expect(stagedStat.ino).toBe(srcStat.ino); + expect(stagedStat.nlink).toBe(2); + await fsp.unlink(src); + expect(await fsp.readFile(staged.filePath)).toEqual(Buffer.from([0xde, 0xad, 0xbe, 0xef])); + }); + + it('accepts a file:// URL and uses the pre-resolution basename', async () => { + const src = path.join(scratch, 'video.mp4'); + await fsp.writeFile(src, Buffer.from([0x00, 0x00, 0x00, 0x18])); + const staged = hold(await stageSourceToDisk(pathToFileURL(src).href, 64)); + expect(staged.fileName).toBe('video.mp4'); + expect(staged.fileSize).toBe(4); + expectStagedPath(staged.filePath, '.mp4'); + expect(await fsp.readFile(staged.filePath)).toEqual(Buffer.from([0x00, 0x00, 0x00, 0x18])); + }); + + it('resolves a symlink target but keeps the symlink basename and extension', async () => { + const target = path.join(scratch, 'blob-real.bin'); + const linkPath = path.join(scratch, 'alias.mov'); + await fsp.writeFile(target, Buffer.from([0x6d, 0x6f, 0x6f, 0x76])); + await fsp.symlink(target, linkPath); + const staged = hold(await stageSourceToDisk(linkPath, 64)); + expect(staged.fileName).toBe('alias.mov'); + expectStagedPath(staged.filePath, '.mov'); + expect(await fsp.readFile(staged.filePath)).toEqual(Buffer.from([0x6d, 0x6f, 0x6f, 0x76])); + }); + + it('uses .bin when the local path has no extension', async () => { + const src = path.join(scratch, 'noext'); + await fsp.writeFile(src, Buffer.from([0x42])); + const staged = hold(await stageSourceToDisk(src, 8)); + expect(staged.fileName).toBe('noext'); + expectStagedPath(staged.filePath, '.bin'); + }); + + it('preserves a mixed-case extension on the staged path', async () => { + const src = path.join(scratch, 'Photo.JPEG'); + await fsp.writeFile(src, Buffer.from([0xff, 0xd8])); + const staged = hold(await stageSourceToDisk(src, 8)); + expect(staged.fileName).toBe('Photo.JPEG'); + expectStagedPath(staged.filePath, '.JPEG'); + }); + + it('allows a local file whose size equals maxBytes', async () => { + const src = path.join(scratch, 'exact.bin'); + await fsp.writeFile(src, Buffer.from([1, 2, 3, 4, 5, 6, 7, 8])); + const staged = hold(await stageSourceToDisk(src, 8)); + expect(staged.fileSize).toBe(8); + }); + + it('stages an empty regular file', async () => { + const src = path.join(scratch, 'zero.dat'); + await fsp.writeFile(src, Buffer.alloc(0)); + const staged = hold(await stageSourceToDisk(src, 0)); + expect(staged.fileSize).toBe(0); + expect(staged.fileName).toBe('zero.dat'); + expect(await fsp.readFile(staged.filePath)).toEqual(Buffer.alloc(0)); + }); + + it('rejects a directory before creating a stage file', async () => { + await expect(stageSourceToDisk(scratch, 64)).rejects.toThrow( + `stage source is not a regular file: ${scratch}`, + ); + }); + + it('rejects an oversized local file before staging', async () => { + const src = path.join(scratch, 'huge.bin'); + await fsp.writeFile(src, Buffer.alloc(80, 0xab)); + await expect(stageSourceToDisk(src, 16)).rejects.toThrow('stage source too large: 80 > 16'); + }); + + it('copies when link() fails and the copy survives source deletion', async () => { + const src = path.join(scratch, 'xfs.bin'); + const payload = Buffer.from([0xc0, 0xff, 0xee, 0x11]); + await fsp.writeFile(src, payload); + const srcIno = (await fsp.stat(src)).ino; + const linkSpy = vi.spyOn(fsp, 'link').mockRejectedValueOnce( + Object.assign(new Error('EXDEV'), { code: 'EXDEV' }), + ); + const staged = hold(await stageSourceToDisk(src, 64)); + expect(linkSpy).toHaveBeenCalledOnce(); + expect(staged.fileName).toBe('xfs.bin'); + expect(staged.fileSize).toBe(4); + expect((await fsp.stat(staged.filePath)).ino).not.toBe(srcIno); + await fsp.unlink(src); + expect(await fsp.readFile(staged.filePath)).toEqual(payload); + }); + + it('cleans temps when both link() and the copy fallback fail', async () => { + const src = path.join(scratch, 'fail.bin'); + await fsp.writeFile(src, Buffer.from([0x01])); + vi.spyOn(fsp, 'link').mockRejectedValueOnce(Object.assign(new Error('EXDEV'), { code: 'EXDEV' })); + vi.spyOn(fsp, 'copyFile').mockRejectedValueOnce(new Error('ENOSPC: copy failed')); + const before = await stageNames(); + await expect(stageSourceToDisk(src, 64)).rejects.toThrow('ENOSPC: copy failed'); + expect(await newPartNames(before)).toEqual([]); + }); + + it('cleans temps when link() fails and the copy rename fails', async () => { + const src = path.join(scratch, 'rename-fail.bin'); + await fsp.writeFile(src, Buffer.from([0x02, 0x03])); + vi.spyOn(fsp, 'link').mockRejectedValueOnce(Object.assign(new Error('EPERM'), { code: 'EPERM' })); + vi.spyOn(fsp, 'rename').mockRejectedValueOnce(new Error('copy rename failed')); + const before = await stageNames(); + await expect(stageSourceToDisk(src, 64)).rejects.toThrow('copy rename failed'); + expect(await newPartNames(before)).toEqual([]); + }); +}); + +describe('stageSourceToDisk — cleanup', () => { + it('removes only the staged temp, never the caller file, and is idempotent', async () => { + const src = path.join(scratch, 'keep-me.bin'); + await fsp.writeFile(src, Buffer.from([0xde, 0xad, 0xbe, 0xef])); + const staged = hold(await stageSourceToDisk(src, 64)); + expect(fs.existsSync(staged.filePath)).toBe(true); + await staged.cleanup(); + expect(fs.existsSync(staged.filePath)).toBe(false); + expect(fs.existsSync(src)).toBe(true); + expect(await fsp.readFile(src)).toEqual(Buffer.from([0xde, 0xad, 0xbe, 0xef])); + await expect(staged.cleanup()).resolves.toBeUndefined(); + }); + + it('tolerates the staged path already being gone', async () => { + const src = path.join(scratch, 'gone.bin'); + await fsp.writeFile(src, Buffer.from([0x09])); + const staged = hold(await stageSourceToDisk(src, 64)); + await fsp.unlink(staged.filePath); + await expect(staged.cleanup()).resolves.toBeUndefined(); + }); +}); + +describe('sweepStagedUploads', () => { + it('returns 0 when the stage directory does not exist', async () => { + const parked = `${STAGE_DIR}.park-${randomUUID()}`; + let moved = false; + try { + try { + await fsp.rename(STAGE_DIR, parked); + moved = true; + } catch (err) { + if ((err as NodeJS.ErrnoException).code !== 'ENOENT') throw err; + } + await expect(sweepStagedUploads(2_000_000_000_000)).resolves.toBe(0); + } finally { + if (moved) { + await fsp.rm(STAGE_DIR, { recursive: true, force: true }).catch(() => undefined); + await fsp.rename(parked, STAGE_DIR); + } + } + }); + + it('reaps a temp older than 30 minutes, keeps a TTL-edge temp, and skips an active staged file', async () => { + const src = path.join(scratch, 'inflight.bin'); + await fsp.writeFile(src, Buffer.from([0x51, 0x52])); + const staged = hold(await stageSourceToDisk(src, 64)); + + await fsp.mkdir(STAGE_DIR, { recursive: true }); + const now = 2_000_000_000_000; + const aged = path.join(STAGE_DIR, `stage-${randomUUID()}.aged`); + const edge = path.join(STAGE_DIR, `stage-${randomUUID()}.edge`); + const recent = path.join(STAGE_DIR, `stage-${randomUUID()}.recent`); + await fsp.writeFile(aged, Buffer.from('aged-orphan')); + await fsp.writeFile(edge, Buffer.from('ttl-edge')); + await fsp.writeFile(recent, Buffer.from('fresh-orphan')); + // Age by whole seconds: some filesystems store mtime at 1s resolution, so a + // 1ms-past-TTL stamp can round back onto the keep side of `> 30min`. + await fsp.utimes(aged, new Date(now), new Date(now - STAGE_TTL_MS - 1000)); + await fsp.utimes(edge, new Date(now), new Date(now - STAGE_TTL_MS)); + await fsp.utimes(recent, new Date(now), new Date(now)); + await fsp.utimes(staged.filePath, new Date(now - STAGE_TTL_MS - 1000), new Date(now - STAGE_TTL_MS - 1000)); + + try { + const removed = await sweepStagedUploads(now); + expect(fs.existsSync(aged)).toBe(false); + expect(fs.existsSync(edge)).toBe(true); + expect(fs.existsSync(recent)).toBe(true); + expect(fs.existsSync(staged.filePath)).toBe(true); + expect(removed).toBeGreaterThanOrEqual(1); + } finally { + await fsp.rm(edge, { force: true }); + await fsp.rm(recent, { force: true }); + await fsp.rm(aged, { force: true }); + } + }); + + it('uses Date.now() when now is omitted', async () => { + await fsp.mkdir(STAGE_DIR, { recursive: true }); + const orphan = path.join(STAGE_DIR, `stage-${randomUUID()}.epoch`); + await fsp.writeFile(orphan, Buffer.from('epoch-orphan')); + await fsp.utimes(orphan, new Date('1990-01-01T00:00:00.000Z'), new Date('1990-01-01T00:00:00.000Z')); + try { + const removed = await sweepStagedUploads(); + expect(fs.existsSync(orphan)).toBe(false); + expect(removed).toBeGreaterThanOrEqual(1); + } finally { + await fsp.rm(orphan, { force: true }); + } + }); +});