Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.
JavaScript 3d-generationai-agentsanimationavatardigital-humangenerative-aiglbgltfimage-to-3dllmmcpmcp-servermodel-context-protocolsolanatext-to-3dthreejsweb-componentswebglwebxrx402
test(avatar): cover the optimize source-size cap directly
nirholas181989003a9c
diff --git a/api/avatar/optimize.js b/api/avatar/optimize.jsindex 9aec0801b..bc451279a 100644--- a/api/avatar/optimize.js+++ b/api/avatar/optimize.js@@ -62,7 +62,7 @@ const SOURCE_FETCH_TIMEOUT_MS = 30_000;** @throws {Error & { code: 'too_large' }} once the cap is exceeded*/-async function readCapped(upstream, cap, abort) {+export async function readCapped(upstream, cap, abort) {if (!upstream.body) {// No stream available (a mocked or already-buffered response): fall back to// buffering, then apply the same cap so the limit still holds.diff --git a/tests/avatar-optimize-source-cap.test.js b/tests/avatar-optimize-source-cap.test.jsnew file mode 100644index 000000000..dfb82e98e--- /dev/null+++ b/tests/avatar-optimize-source-cap.test.js@@ -0,0 +1,97 @@+// /api/avatar/optimize refuses sources over its 50 MB cap.+//+// The cap used to be enforced as `Buffer.from(await upstream.arrayBuffer())`+// followed by a length check. That only measures AFTER the whole body is in+// memory, and it only short-circuits early when the server declares a+// content-length. A chunked response carries no such header, so an oversized+// source was downloaded in full before anyone measured it: the request looked+// like a stall instead of returning the documented 413.+//+// readCapped counts bytes as they arrive and aborts at the cap. These tests+// exercise it directly: the byte accounting and the abort are the whole fix.++import { describe, expect, it, vi } from 'vitest';++const { readCapped } = await import('../api/avatar/optimize.js');++const CAP = 1024;++// A body that yields `chunkSize` bytes per pull, up to `total`, and records how+// much it was actually asked for. A real oversized source is effectively endless;+// what matters is that the reader stops pulling once the cap is passed.+function streamingBody(chunkSize, total) {+ const stats = { yielded: 0 };+ return {+ stats,+ async *[Symbol.asyncIterator]() {+ let sent = 0;+ while (sent < total) {+ const n = Math.min(chunkSize, total - sent);+ sent += n;+ stats.yielded += n;+ yield Buffer.alloc(n, 1);+ }+ },+ };+}++function upstreamWith(body) {+ return { body, arrayBuffer: async () => { throw new Error('arrayBuffer must not be used when a stream exists'); } };+}++describe('readCapped', () => {+ it('returns the full body when it fits under the cap', async () => {+ const body = streamingBody(100, 500);+ const out = await readCapped(upstreamWith(body), CAP, null);++ expect(out.byteLength).toBe(500);+ expect(body.stats.yielded).toBe(500);+ });++ it('returns a body that lands exactly on the cap', async () => {+ const body = streamingBody(256, CAP);+ const out = await readCapped(upstreamWith(body), CAP, null);++ expect(out.byteLength).toBe(CAP);+ });++ it('throws too_large for a chunked body with NO content-length', async () => {+ const body = streamingBody(256, CAP * 100);++ await expect(readCapped(upstreamWith(body), CAP, null)).rejects.toMatchObject({ code: 'too_large' });+ });++ it('stops pulling instead of draining the whole oversized body', async () => {+ // The regression this pins: the old path read every byte before deciding.+ const body = streamingBody(256, CAP * 100);++ await readCapped(upstreamWith(body), CAP, null).catch(() => {});++ // It must give up shortly past the cap, not anywhere near the full body.+ expect(body.stats.yielded).toBeLessThanOrEqual(CAP + 256);+ expect(body.stats.yielded).toBeLessThan(CAP * 100);+ });++ it('aborts the upstream transfer when the cap is passed', async () => {+ const abort = { abort: vi.fn() };+ const body = streamingBody(256, CAP * 10);++ await readCapped(upstreamWith(body), CAP, abort).catch(() => {});++ expect(abort.abort).toHaveBeenCalledTimes(1);+ });++ it('still applies the cap when no stream is available', async () => {+ // Some responses arrive already buffered; the limit must hold there too.+ const buffered = { body: null, arrayBuffer: async () => new ArrayBuffer(CAP * 4) };++ await expect(readCapped(buffered, CAP, null)).rejects.toMatchObject({ code: 'too_large' });+ });++ it('returns a buffered under-cap body unchanged', async () => {+ const buffered = { body: null, arrayBuffer: async () => new ArrayBuffer(64) };++ const out = await readCapped(buffered, CAP, null);+ expect(out.byteLength).toBe(64);+ });+});