diff --git a/packages/onebot/src/actions/stream-file.ts b/packages/onebot/src/actions/stream-file.ts new file mode 100644 index 00000000..5716e7b0 --- /dev/null +++ b/packages/onebot/src/actions/stream-file.ts @@ -0,0 +1,368 @@ +// Stream API file actions (#163) — NapCat-compatible chunked file transfer. +// +// upload_file_stream: a client (possibly on another machine) pushes a file to +// the bot in base64 chunks across repeated calls keyed by `stream_id`; the bot +// reassembles them into a local temp file and returns its path, which the +// client then feeds to a normal send action. This sidesteps the HTTP 2 MB body +// cap and the "client and bot share a filesystem" assumption of plain file:// +// sends. Reassembly streams chunk-by-chunk to disk (no whole-file +// Buffer.concat) so an arbitrarily large upload won't OOM the bot. +// +// clean_stream_temp_file: wipe the stream temp dirs (upload + download), scoped +// so it never touches unrelated temp files. +// +// Hardening over the NapCat original (which has all of these holes): stream_id +// is allowlist-validated + every derived path is fenced inside the upload dir +// (no path traversal); total_chunks/chunk/stream/byte counts are all bounded +// (no single-request OOM or disk/fd exhaustion); chunk indices are claimed +// synchronously (no double-count race); state is namespaced per account. + +import os from 'os'; +import path from 'path'; +import fs from 'fs'; +import { createHash } from 'crypto'; +import { createLogger } from '@snowluma/common/logger'; +import { defineAction, defineStreamAction, f } from '../action-kit'; +import type { ApiActionContext, ApiHandler } from '../api-handler'; +import { okResponse, type ApiResponse } from '../types'; +import { StreamStatus } from '../streaming'; + +const log = createLogger('OneBot.Stream'); + +const STREAM_ROOT = path.join(os.tmpdir(), 'snowluma-stream'); +export const STREAM_UPLOAD_DIR = path.join(STREAM_ROOT, 'upload'); +export const STREAM_DOWNLOAD_DIR = path.join(STREAM_ROOT, 'download'); + +const UPLOAD_TIMEOUT_MS = 10 * 60 * 1000; // idle stream reaped after 10 min +const DEFAULT_RETENTION_MS = 5 * 60 * 1000; // reassembled file auto-deleted after 5 min + +// Resource caps (DoS guards — the whole point of the disk-streaming design is +// blown if a single request can still OOM, and an unbounded server can be +// flooded with streams/chunks/bytes). +const MAX_CONCURRENT_STREAMS = 64; +const MAX_CHUNKS = 200_000; // also bounds the completion scan +const MAX_CHUNK_BYTES = 32 * 1024 * 1024; // one decoded chunk +const MAX_UPLOAD_BYTES = 4 * 1024 * 1024 * 1024; // 4 GiB cumulative per stream + +// stream_id / account key allowlist: filename-safe, no separators, no dots +// (so no `.`/`..` traversal token can ever reach a path segment). +const SAFE_ID_RE = /^[A-Za-z0-9_-]{1,128}$/; + +class StreamError extends Error {} + +function validateStreamId(id: string): void { + if (!SAFE_ID_RE.test(id)) throw new StreamError('invalid stream_id (allowed: 1-128 chars of A-Z a-z 0-9 _ -)'); +} + +/** Account namespace for the in-memory map + on-disk dirs. Empty (tests / no + * login) → flat, un-namespaced. Sanitised so it can't widen the path either. */ +function accountKey(ctx?: ApiActionContext): string { + let raw = ''; + try { raw = String(ctx?.getLoginInfo?.().userId ?? ''); } catch { raw = ''; } + return raw.replace(/[^A-Za-z0-9_-]/g, '').slice(0, 64); +} + +/** Belt-and-braces: assert a derived path stays inside the upload dir even if + * the allowlist above is ever weakened. */ +function assertWithin(root: string, target: string): void { + const rel = path.relative(root, target); + if (rel === '' || rel.startsWith('..') || path.isAbsolute(rel)) { + throw new StreamError('resolved stream path escapes the upload directory'); + } +} + +// ─────────────────────────── upload state ─────────────────────────── + +interface UploadState { + /** Client-facing stream_id (echoed in frames). */ + id: string; + /** Internal map key — `:` or just `` when un-namespaced. */ + key: string; + filename: string; + totalChunks: number; + /** Indices CLAIMED (sync, before the write await) — drives dedup + byte + * accounting so two concurrent calls for one index can't double-count. */ + claimed: Set; + /** Indices actually FLUSHED to disk (added after the write resolves) — drives + * completion + the reported count, so completion can't fire while a chunk + * file is still being written (which would read a truncated part). Neither + * set is pre-allocated to total_chunks (that would defeat the no-OOM goal). */ + written: Set; + bytesWritten: number; + fileSize?: number; + expectedSha256?: string; + tempDir: string; + finalPath: string; + fileRetention: number; + timeoutId: NodeJS.Timeout; +} + +/** Process-wide. Key is account-namespaced so two accounts (same process) can't + * collide on a shared/non-UUID stream_id. */ +const uploads = new Map(); + +function reapTimer(key: string): NodeJS.Timeout { + const t = setTimeout(() => { cleanupUpload(key, true); }, UPLOAD_TIMEOUT_MS); + if (typeof t.unref === 'function') t.unref(); // don't pin the event loop on a stalled upload + return t; +} + +function cleanupUpload(key: string, deleteFinal: boolean): void { + const state = uploads.get(key); + if (!state) return; + clearTimeout(state.timeoutId); + try { fs.rmSync(state.tempDir, { recursive: true, force: true }); } catch { /* best effort */ } + if (deleteFinal) { + try { fs.rmSync(state.finalPath, { force: true }); } catch { /* best effort */ } + } + uploads.delete(key); +} + +function safeFilename(name: string | undefined, id: string): string { + const base = (name ?? '') + .replace(/[/\\]/g, '_') // no directory separators + .replace(/\.\.+/g, '_') // no `..` traversal tokens + .replace(/^\.+/, '') // no leading dots + .trim(); + return base || `upload_${id}`; +} + +function createUpload(p: UploadParams, account: string): UploadState { + if (!p.total_chunks || p.total_chunks <= 0) throw new StreamError('total_chunks required for new stream'); + if (p.total_chunks > MAX_CHUNKS) throw new StreamError(`total_chunks exceeds limit (${MAX_CHUNKS})`); + if (p.file_size !== undefined && p.file_size > MAX_UPLOAD_BYTES) throw new StreamError(`file_size exceeds limit (${MAX_UPLOAD_BYTES} bytes)`); + if (uploads.size >= MAX_CONCURRENT_STREAMS) throw new StreamError(`too many concurrent upload streams (${MAX_CONCURRENT_STREAMS})`); + + const filename = safeFilename(p.filename, p.stream_id); + const dirRoot = account ? path.join(STREAM_UPLOAD_DIR, account) : STREAM_UPLOAD_DIR; + const tempDir = path.join(dirRoot, p.stream_id); + const finalPath = path.join(dirRoot, `${p.stream_id}__${filename}`); + assertWithin(STREAM_UPLOAD_DIR, tempDir); + assertWithin(STREAM_UPLOAD_DIR, finalPath); + fs.mkdirSync(tempDir, { recursive: true }); + + const key = account ? `${account}:${p.stream_id}` : p.stream_id; + const state: UploadState = { + id: p.stream_id, + key, + filename, + totalChunks: p.total_chunks, + claimed: new Set(), + written: new Set(), + bytesWritten: 0, + fileSize: p.file_size, + expectedSha256: p.expected_sha256, + tempDir, + finalPath, + fileRetention: p.file_retention, + timeoutId: reapTimer(key), + }; + uploads.set(key, state); + return state; +} + +function statusFrame(state: UploadState, status: string): ApiResponse { + return okResponse({ + type: StreamStatus.Stream, + stream_id: state.id, + status, + received_chunks: state.written.size, + total_chunks: state.totalChunks, + }); +} + +function writeChunkToStream(stream: fs.WriteStream, buf: Buffer): Promise { + return new Promise((resolve, reject) => { + stream.write(buf, (err) => (err ? reject(err) : resolve())); + }); +} + +async function completeUpload(state: UploadState): Promise { + if (state.written.size !== state.totalChunks) { + const missing: number[] = []; + for (let i = 0; i < state.totalChunks && missing.length < 16; i++) { + if (!state.written.has(i)) missing.push(i); + } + const more = state.totalChunks - state.written.size > missing.length ? '…' : ''; + throw new StreamError(`incomplete stream: missing ${state.totalChunks - state.written.size} chunk(s) [${missing.join(',')}${more}]`); + } + + // Stream each chunk file into the final file in order, hashing as we go — one + // chunk in memory at a time, never the whole payload. + const hash = createHash('sha256'); + const out = fs.createWriteStream(state.finalPath); + try { + for (let i = 0; i < state.totalChunks; i++) { + const buf = await fs.promises.readFile(path.join(state.tempDir, `${i}.chunk`)); + hash.update(buf); + await writeChunkToStream(out, buf); + } + await new Promise((resolve, reject) => out.end((err?: Error | null) => (err ? reject(err) : resolve()))); + } catch (err) { + out.destroy(); + try { fs.rmSync(state.finalPath, { force: true }); } catch { /* best effort */ } + throw err; + } + + const sha256 = hash.digest('hex'); + if (state.expectedSha256 && sha256 !== state.expectedSha256) { + try { fs.rmSync(state.finalPath, { force: true }); } catch { /* best effort */ } + throw new StreamError(`sha256 mismatch (expected ${state.expectedSha256}, got ${sha256})`); + } + + const finalPath = state.finalPath; + const fileSize = fs.statSync(finalPath).size; + const received = state.written.size; + const total = state.totalChunks; + const retention = state.fileRetention; + + // Drop chunk parts + in-memory state but KEEP the reassembled file. + cleanupUpload(state.key, false); + if (retention > 0) { + const t = setTimeout(() => { + fs.rm(finalPath, { force: true }, (err) => { + if (err) log.warn('failed to delete retained stream file %s: %s', finalPath, err.message); + }); + }, retention); + if (typeof t.unref === 'function') t.unref(); + } + + return okResponse({ + type: StreamStatus.Response, + stream_id: state.id, + status: 'file_complete', + received_chunks: received, + total_chunks: total, + file_path: finalPath, + file_size: fileSize, + sha256, + }); +} + +async function handleUpload(p: UploadParams, account = ''): Promise { + validateStreamId(p.stream_id); + const key = account ? `${account}:${p.stream_id}` : p.stream_id; + + if (p.reset) { + cleanupUpload(key, true); + // NapCat surfaces reset as an error frame; mirror it for client parity. + throw new StreamError('Stream reset completed'); + } + + if (p.verify_only) { + const state = uploads.get(key); + if (!state) throw new StreamError('stream not found'); + return statusFrame(state, 'file_created'); + } + + const state = uploads.get(key) ?? createUpload(p, account); + // Metadata may arrive on the creating call or any later one (e.g. the client + // only knows the whole-file sha256 when it sends `is_complete`). + if (p.expected_sha256 !== undefined) state.expectedSha256 = p.expected_sha256; + if (p.file_size !== undefined) state.fileSize = p.file_size; + + if (p.chunk_data !== undefined && p.chunk_index !== undefined) { + const index = p.chunk_index; + if (index < 0 || index >= state.totalChunks) throw new StreamError(`invalid chunk index: ${index}`); + if (state.claimed.has(index)) return statusFrame(state, 'chunk_received'); // duplicate / in-flight — idempotent + + const buf = Buffer.from(p.chunk_data, 'base64'); + if (buf.length > MAX_CHUNK_BYTES) throw new StreamError(`chunk exceeds size limit (${MAX_CHUNK_BYTES} bytes)`); + if (state.bytesWritten + buf.length > MAX_UPLOAD_BYTES) throw new StreamError(`stream exceeds total size limit (${MAX_UPLOAD_BYTES} bytes)`); + + // Claim the index synchronously (before the write await) so two concurrent + // calls for the same index can't both pass the dedup check or double-count + // bytes. `written` is only set AFTER the bytes hit disk, so completion can + // never fire while this chunk file is still being written. + state.claimed.add(index); + state.bytesWritten += buf.length; + try { + await fs.promises.writeFile(path.join(state.tempDir, `${index}.chunk`), buf); + } catch (err) { + state.claimed.delete(index); + state.bytesWritten -= buf.length; + throw err; + } + state.written.add(index); + clearTimeout(state.timeoutId); + state.timeoutId = reapTimer(state.key); + return statusFrame(state, 'chunk_received'); + } + + if (p.is_complete || state.written.size === state.totalChunks) { + return completeUpload(state); + } + + return statusFrame(state, 'file_created'); +} + +// ─────────────────────────── action definitions ─────────────────────────── + +const uploadParams = { + stream_id: f.string({ allowEmpty: false }).describe('流 ID(客户端生成的 UUID,限 [A-Za-z0-9_-])'), + chunk_data: f.string().optional().describe('分块数据(Base64)'), + chunk_index: f.int({ min: 0 }).optional().describe('分块索引(从 0 开始)'), + total_chunks: f.int({ min: 1 }).optional().describe('总分块数(新流必填)'), + file_size: f.int({ min: 0 }).optional().describe('文件总大小(字节)'), + expected_sha256: f.string().optional().describe('期望的整文件 SHA256(校验)'), + is_complete: f.bool().optional().describe('是否为最后一个分块/触发合并'), + filename: f.string().optional().describe('文件名'), + reset: f.bool().optional().describe('重置并丢弃该流'), + verify_only: f.bool().optional().describe('仅查询当前流状态'), + file_retention: f.int({ min: 0 }).default(DEFAULT_RETENTION_MS).describe('合并文件保留毫秒(0=不回收)'), +}; + +type UploadParams = { + stream_id: string; + chunk_data: string | undefined; + chunk_index: number | undefined; + total_chunks: number | undefined; + file_size: number | undefined; + expected_sha256: string | undefined; + is_complete: boolean | undefined; + filename: string | undefined; + reset: boolean | undefined; + verify_only: boolean | undefined; + file_retention: number; +}; + +const uploadFileStream = defineStreamAction({ + name: 'upload_file_stream', + summary: '以流式分块方式上传文件到机器人本地(返回可用于发送的本地路径)', + returns: '流式帧:分块确认 type=stream、完成 type=response(含 file_path/file_size/sha256)', + params: uploadParams, + run: (p, ctx) => handleUpload(p, accountKey(ctx)), +}); + +function cleanDir(dir: string): number { + let removed = 0; + let entries: string[]; + try { entries = fs.readdirSync(dir); } catch { return 0; } // dir absent → nothing to clean + for (const name of entries) { + try { fs.rmSync(path.join(dir, name), { recursive: true, force: true }); removed++; } catch (err) { + log.warn('failed to remove stream temp %s: %s', path.join(dir, name), err instanceof Error ? err.message : String(err)); + } + } + return removed; +} + +const cleanStreamTempFile = defineAction({ + name: 'clean_stream_temp_file', + summary: '清理流式传输临时文件(仅清理 stream 上传/下载目录)', + returns: '{ message, removed }', + params: {}, + run: () => { + const removed = cleanDir(STREAM_UPLOAD_DIR) + cleanDir(STREAM_DOWNLOAD_DIR); + // Active uploads' chunk dirs may have been wiped — drop their state too. + for (const k of [...uploads.keys()]) cleanupUpload(k, false); + return okResponse({ message: 'success', removed }); + }, +}); + +export function register(h: ApiHandler, ctx: ApiActionContext): void { + uploadFileStream.register(h, ctx); + cleanStreamTempFile.register(h, ctx); +} + +// Exposed for tests. +export const __test = { uploads, handleUpload, cleanDir, validateStreamId, STREAM_UPLOAD_DIR, MAX_CONCURRENT_STREAMS, MAX_CHUNKS }; diff --git a/packages/onebot/src/api-handler.ts b/packages/onebot/src/api-handler.ts index 52219035..1cc40c85 100644 --- a/packages/onebot/src/api-handler.ts +++ b/packages/onebot/src/api-handler.ts @@ -11,6 +11,7 @@ import { register as registerInfo } from './actions/info'; import { register as registerMessage } from './actions/message'; import { register as registerQzone } from './actions/qzone'; import { register as registerRequest } from './actions/request'; +import { register as registerStreamFile } from './actions/stream-file'; import type { ForwardPreviewMeta } from './modules/message-actions'; import type { JsonObject, JsonValue, MessageMeta } from './types'; import { RETCODE, failedResponse } from './types'; @@ -120,6 +121,7 @@ export class ApiHandler { registerExtended(this, context); registerGroupAlbum(this, context); registerQzone(this, context); + registerStreamFile(this, context); } registerAction(action: string, handler: ActionHandler): void { diff --git a/packages/onebot/tests/stream-file.test.ts b/packages/onebot/tests/stream-file.test.ts new file mode 100644 index 00000000..a20d688e --- /dev/null +++ b/packages/onebot/tests/stream-file.test.ts @@ -0,0 +1,184 @@ +// Phase 2 — upload_file_stream + clean_stream_temp_file (#163). +import { describe, it, expect, afterEach } from 'vitest'; +import fs from 'fs'; +import os from 'os'; +import path from 'path'; +import { createHash } from 'crypto'; +import { ApiHandler, type ApiActionContext } from '../src/api-handler'; +import { __test, STREAM_UPLOAD_DIR } from '../src/actions/stream-file'; + +const { handleUpload, cleanDir, uploads } = __test; +type UP = Parameters[0]; + +const b64 = (s: string | Buffer): string => Buffer.from(s).toString('base64'); + +// Fill every optional key so the direct handleUpload calls satisfy the type. +function mk(p: Partial & { stream_id: string }): UP { + return { + chunk_data: undefined, chunk_index: undefined, total_chunks: undefined, + file_size: undefined, expected_sha256: undefined, is_complete: undefined, + filename: undefined, reset: undefined, verify_only: undefined, + file_retention: 0, ...p, + }; +} + +afterEach(() => { + cleanDir(STREAM_UPLOAD_DIR); + uploads.clear(); +}); + +describe('upload_file_stream', () => { + it('reassembles ordered chunks into a file with correct content + sha256', async () => { + const id = 'up-ordered'; + const parts = ['hello ', 'streamed ', 'world!']; + const full = parts.join(''); + const sha = createHash('sha256').update(full).digest('hex'); + + let r = await handleUpload(mk({ stream_id: id, total_chunks: 3, chunk_index: 0, chunk_data: b64(parts[0]) })); + expect(r.data).toMatchObject({ type: 'stream', status: 'chunk_received', received_chunks: 1, total_chunks: 3 }); + await handleUpload(mk({ stream_id: id, total_chunks: 3, chunk_index: 1, chunk_data: b64(parts[1]) })); + r = await handleUpload(mk({ stream_id: id, total_chunks: 3, chunk_index: 2, chunk_data: b64(parts[2]) })); + expect(r.data).toMatchObject({ received_chunks: 3 }); + + r = await handleUpload(mk({ stream_id: id, total_chunks: 3, is_complete: true, expected_sha256: sha })); + const data = r.data as Record; + expect(data).toMatchObject({ type: 'response', status: 'file_complete', total_chunks: 3, file_size: full.length, sha256: sha }); + expect(fs.readFileSync(String(data.file_path), 'utf8')).toBe(full); + // state dropped after completion, chunk dir gone + expect(uploads.has(id)).toBe(false); + }); + + it('accepts out-of-order chunks and dedups repeats idempotently', async () => { + const id = 'up-ooo'; + await handleUpload(mk({ stream_id: id, total_chunks: 3, chunk_index: 2, chunk_data: b64('C') })); + await handleUpload(mk({ stream_id: id, total_chunks: 3, chunk_index: 0, chunk_data: b64('A') })); + // duplicate of an already-received chunk → no double count + const dup = await handleUpload(mk({ stream_id: id, total_chunks: 3, chunk_index: 2, chunk_data: b64('C') })); + expect(dup.data).toMatchObject({ received_chunks: 2 }); + await handleUpload(mk({ stream_id: id, total_chunks: 3, chunk_index: 1, chunk_data: b64('B') })); + const r = await handleUpload(mk({ stream_id: id, total_chunks: 3, is_complete: true })); + expect(fs.readFileSync(String((r.data as Record).file_path), 'utf8')).toBe('ABC'); + }); + + it('rejects completion with a missing chunk', async () => { + const id = 'up-missing'; + await handleUpload(mk({ stream_id: id, total_chunks: 2, chunk_index: 0, chunk_data: b64('only') })); + await expect(handleUpload(mk({ stream_id: id, total_chunks: 2, is_complete: true }))).rejects.toThrow(/missing/i); + }); + + it('rejects a sha256 mismatch and deletes the bad file', async () => { + const id = 'up-sha'; + await handleUpload(mk({ stream_id: id, total_chunks: 1, chunk_index: 0, chunk_data: b64('data') })); + await expect(handleUpload(mk({ stream_id: id, total_chunks: 1, is_complete: true, expected_sha256: 'deadbeef' }))).rejects.toThrow(/sha256 mismatch/i); + // A failed completion keeps the stream (client can reset+retry; the reaper + // reclaims it if abandoned), but the corrupt merged file is deleted. + expect(uploads.has(id)).toBe(true); + expect(fs.existsSync(uploads.get(id)!.finalPath)).toBe(false); + }); + + it('rejects an out-of-range chunk index and a new stream without total_chunks', async () => { + await expect(handleUpload(mk({ stream_id: 'up-oob', total_chunks: 2, chunk_index: 5, chunk_data: b64('x') }))).rejects.toThrow(/invalid chunk index/i); + await expect(handleUpload(mk({ stream_id: 'up-no-total', chunk_index: 0, chunk_data: b64('x') }))).rejects.toThrow(/total_chunks required/i); + }); + + it('reset discards the stream (surfaced as an error, NapCat parity)', async () => { + const id = 'up-reset'; + await handleUpload(mk({ stream_id: id, total_chunks: 2, chunk_index: 0, chunk_data: b64('x') })); + expect(uploads.has(id)).toBe(true); + await expect(handleUpload(mk({ stream_id: id, reset: true }))).rejects.toThrow(/reset/i); + expect(uploads.has(id)).toBe(false); + }); + + it('verify_only reports status, errors on unknown stream', async () => { + const id = 'up-verify'; + await handleUpload(mk({ stream_id: id, total_chunks: 4, chunk_index: 0, chunk_data: b64('x') })); + const r = await handleUpload(mk({ stream_id: id, verify_only: true })); + expect(r.data).toMatchObject({ type: 'stream', received_chunks: 1, total_chunks: 4 }); + await expect(handleUpload(mk({ stream_id: 'nope', verify_only: true }))).rejects.toThrow(/not found/i); + }); + + it('strips path separators from filename (no temp-dir escape)', async () => { + const id = 'up-escape'; + await handleUpload(mk({ stream_id: id, total_chunks: 1, chunk_index: 0, chunk_data: b64('x'), filename: '../../evil.sh' })); + const r = await handleUpload(mk({ stream_id: id, total_chunks: 1, is_complete: true })); + const fp = String((r.data as Record).file_path); + expect(path.dirname(fp)).toBe(STREAM_UPLOAD_DIR); + expect(fp).not.toContain('..'); + }); +}); + +describe('upload_file_stream hardening', () => { + it('rejects path-traversal / illegal stream_id (no escape) [C1]', async () => { + const evil = ['../../../../tmp/victim', '..', 'a/b', 'a\\b', 'with space', 'x'.repeat(129), '', 'dot.dot']; + for (const id of evil) { + await expect(handleUpload(mk({ stream_id: id, total_chunks: 1, chunk_index: 0, chunk_data: b64('x') }))).rejects.toThrow(/invalid stream_id/i); + } + // a benign traversal attempt left nothing outside the upload dir + expect(fs.existsSync('/tmp/victim')).toBe(false); + // validateStreamId accepts UUID-ish ids, rejects separators/dots + expect(() => __test.validateStreamId('uuid-1234-5678_ABCdef')).not.toThrow(); + expect(() => __test.validateStreamId('../x')).toThrow(); + expect(() => __test.validateStreamId('a/b')).toThrow(); + expect(() => __test.validateStreamId('a.b')).toThrow(); + }); + + it('rejects total_chunks above the cap [M1]', async () => { + await expect(handleUpload(mk({ stream_id: 'up-huge', total_chunks: __test.MAX_CHUNKS + 1, chunk_index: 0, chunk_data: b64('x') }))).rejects.toThrow(/total_chunks exceeds limit/i); + }); + + it('caps concurrent streams [M2]', async () => { + for (let i = 0; i < __test.MAX_CONCURRENT_STREAMS; i++) { + await handleUpload(mk({ stream_id: `cap-${i}`, total_chunks: 2, chunk_index: 0, chunk_data: b64('x') })); + } + await expect(handleUpload(mk({ stream_id: 'cap-overflow', total_chunks: 2, chunk_index: 0, chunk_data: b64('x') }))).rejects.toThrow(/too many concurrent/i); + }); + + it('counts a concurrently-duplicated chunk index only once [M3]', async () => { + const id = 'up-race'; + const [a, b] = await Promise.all([ + handleUpload(mk({ stream_id: id, total_chunks: 3, chunk_index: 0, chunk_data: b64('A') })), + handleUpload(mk({ stream_id: id, total_chunks: 3, chunk_index: 0, chunk_data: b64('A') })), + ]); + expect(a.status).toBe('ok'); + expect(b.status).toBe('ok'); + expect(uploads.get(id)!.written.size).toBe(1); + expect(uploads.get(id)!.claimed.size).toBe(1); + }); +}); + +describe('upload_file_stream via the streaming dispatch', () => { + it('emits a stream-marked chunk frame end-to-end', async () => { + const h = new ApiHandler({} as ApiActionContext); + expect(h.isStreamAction('upload_file_stream')).toBe(true); + + const frames: string[] = []; + await h.processStreamRequest( + JSON.stringify({ action: 'upload_file_stream', params: { stream_id: 'ws-up', total_chunks: 1, chunk_index: 0, chunk_data: b64('hi') }, echo: 'E' }), + (j) => frames.push(j), + ); + expect(frames).toHaveLength(1); + expect(JSON.parse(frames[0])).toMatchObject({ + status: 'ok', stream: 'stream-action', echo: 'E', + data: { type: 'stream', status: 'chunk_received', received_chunks: 1, total_chunks: 1 }, + }); + }); +}); + +describe('clean_stream_temp_file', () => { + it('removes stream temp entries and is scoped to the stream dirs', async () => { + fs.mkdirSync(STREAM_UPLOAD_DIR, { recursive: true }); + const f1 = path.join(STREAM_UPLOAD_DIR, 'leftover.bin'); + fs.writeFileSync(f1, 'junk'); + // an unrelated temp file outside the stream dir must survive + const outside = path.join(os.tmpdir(), `snowluma-stream-unrelated-${Date.now()}.txt`); + fs.writeFileSync(outside, 'keep'); + + const h = new ApiHandler({} as ApiActionContext); + const r = await h.handle('clean_stream_temp_file', {}); + expect(r.status).toBe('ok'); + expect((r.data as Record).message).toBe('success'); + expect(fs.existsSync(f1)).toBe(false); + expect(fs.existsSync(outside)).toBe(true); + fs.rmSync(outside, { force: true }); + }); +});