Skip to content

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.

3.7 KB
Raw Blame
import { describe, it, expect, beforeAll, afterAll } from 'vitest';
import { readFileSync } from 'node:fs';
import { startTestServer } from './helpers/test-server.js';
import { fileURLToPath } from 'node:url';
import { globSync } from 'node:fs';

// Guards a production outage class: `method(req, res, allowed)` (api/_lib/http.js)
// returns TRUE when the verb is allowed and answers 405 itself otherwise. So the
// only correct guard is `if (!method(...)) return;`.
//
// api/diorama.js shipped `if (method(req, res, ['POST'])) return;` — the negation
// dropped. That returns from the handler on exactly the allowed verb WITHOUT ever
// writing a response, so every POST /api/diorama hung until the Cloud Run request
// timeout (observed live 2026-07-10: GET → 400 in 90ms, POST → no bytes after 70s).
// It silently killed the entire Diorama feature: compose, save, export, build, and
// the Scene MCP tools that call the public endpoint.
//
// One inverted call site out of 993 was enough. These tests pin both the specific
// route and the whole class.

const repoRoot = fileURLToPath(new URL('..', import.meta.url));

describe('method() guard is negated at every call site', () => {
	it('no api handler calls `if (method(...))` without negating it', () => {
		const files = globSync('api/**/*.js', { cwd: repoRoot });
		expect(files.length).toBeGreaterThan(100);

		const offenders = [];
		for (const rel of files) {
			const src = readFileSync(new URL(`../${rel}`, import.meta.url), 'utf8');
			src.split('\n').forEach((line, i) => {
				// Match `if (method(req, res, ...)` but not `if (!method(`.
				if (/\bif\s*\(\s*method\s*\(/.test(line)) {
					offenders.push(`${rel}:${i + 1}: ${line.trim()}`);
				}
			});
		}
		expect(offenders, `un-negated method() guard hangs every allowed request:\n${offenders.join('\n')}`).toEqual([]);
	});
});

describe('POST /api/diorama answers instead of hanging', () => {
	let BASE;
	let server;

	// 90s, not 30s: startTestServer waits up to 60s for readiness because a boot
	// that takes ~1.5s idle takes tens of seconds under full-suite load. A hook
	// budget below that budget just fails first, with a less useful message.
	beforeAll(async () => {
		server = await startTestServer();
		BASE = server.base;
	}, 90000);

	afterAll(() => server?.close());

	// The unknown-action branch sits after the method guard and before any rate
	// limiter, LLM call, or DB read — so this asserts the guard alone, with no
	// Redis or provider dependency.
	it('POST with an unknown action returns 400, not a hang', async () => {
		const res = await fetch(`${BASE}/api/diorama`, {
			method: 'POST',
			headers: { 'content-type': 'application/json' },
			body: JSON.stringify({ action: 'bogus' }),
			signal: AbortSignal.timeout(10000),
		});
		expect(res.status).toBe(400);
		expect((await res.json()).error).toBe('unknown_action');
	}, 15000);

	// readJson() rejects a non-object JSON payload with `bad_request` before the
	// handler's own `invalid_body` branch is reached. Either way the contract that
	// matters here is the same: a POST gets a fast, structured 400 — never a hang.
	it('POST with a non-object body returns 400', async () => {
		const res = await fetch(`${BASE}/api/diorama`, {
			method: 'POST',
			headers: { 'content-type': 'application/json' },
			body: '"just a string"',
			signal: AbortSignal.timeout(10000),
		});
		expect(res.status).toBe(400);
		expect((await res.json()).error).toBe('bad_request');
	}, 15000);

	it('an unsupported verb still gets a 405 with an allow header', async () => {
		const res = await fetch(`${BASE}/api/diorama`, {
			method: 'DELETE',
			signal: AbortSignal.timeout(10000),
		});
		expect(res.status).toBe(405);
		expect(res.headers.get('allow')).toContain('POST');
	}, 15000);
});