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.

9.1 KB
Raw Blame
// The launcher and market-maker config endpoints take agent and config ids
// straight from the query string or body into uuid columns. Before these
// guards a caller who typed a bad id got a 500 plus an ops page, when the
// truthful answer is a 400 they can act on. These tests pin the boundary:
// a malformed id never reaches a query, and the happy paths still work.

import { describe, it, expect, vi, beforeEach } from 'vitest';

const sqlMock = vi.fn();
vi.mock('../api/_lib/db.js', () => ({
	sql: sqlMock,
	isDbUnavailableError: () => false,
	isDbCapacityError: () => false,
}));

const getSessionUserMock = vi.fn();
vi.mock('../api/_lib/auth.js', () => ({
	getSessionUser: (...a) => getSessionUserMock(...a),
	authenticateBearer: vi.fn(async () => null),
	extractBearer: vi.fn(() => null),
}));

// Mirror the real `limits` surface these handlers touch: GETs use the generous
// authed-read bucket and writes the strict credential bucket (376929cb1). A
// mock that only knows authIp makes every GET throw on `undefined()` inside
// wrap(), which reports as a 500 and hides the 400/200 this file exists to pin.
vi.mock('../api/_lib/rate-limit.js', () => ({
	limits: {
		authIp: vi.fn(async () => ({ success: true, reset: 1_000 })),
		authedReadIp: vi.fn(async () => ({ success: true, reset: 1_000 })),
	},
	clientIp: () => '203.0.113.8',
}));

vi.mock('../api/_lib/env.js', () => ({
	env: { APP_ORIGIN: 'http://localhost:3000', ISSUER: 'http://t', MCP_RESOURCE: 'http://t' },
}));

const { default: launcher } = await import('../api/agent/launcher.js');
const { default: marketMaker } = await import('../api/agent/market-maker.js');

const AGENT = 'aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa';
const CONFIG = 'cccccccc-cccc-4ccc-8ccc-cccccccccccc';
// $THREE, the platform's own coin, as a real-shaped mint for the upsert path.
const THREE_MINT = 'FeMbDoX7R1Psc4GEcvJdsbNbZA3bfztcyDCatJVJpump';

function mkReq({ method = 'GET', url = '/', headers = {}, body = null } = {}) {
	const hdrs = { ...headers };
	if (body != null && !hdrs['content-type']) hdrs['content-type'] = 'application/json';
	return {
		method, url, headers: hdrs,
		on(event, cb) {
			if (event === 'data' && body != null) {
				const buf = Buffer.from(typeof body === 'string' ? body : JSON.stringify(body));
				queueMicrotask(() => { cb(buf); this._endCb?.(); });
			} else if (event === 'end') this._endCb = cb;
		},
		destroy() {},
	};
}
function mkRes() {
	return {
		statusCode: 200, headers: {}, body: undefined, writableEnded: false,
		setHeader(k, v) { this.headers[k.toLowerCase()] = v; },
		end(b) { this.body = b; this.writableEnded = true; },
	};
}
const parse = (res) => (res.body ? JSON.parse(res.body) : undefined);

let sqlQueue = [];
beforeEach(() => {
	sqlQueue = [];
	sqlMock.mockReset().mockImplementation(() => Promise.resolve(sqlQueue.length ? sqlQueue.shift() : []));
	getSessionUserMock.mockReset().mockResolvedValue({ id: 'user-1' });
});

describe('/api/agent/launcher id validation', () => {
	it('answers 400 for a non-uuid agentId on GET and never queries', async () => {
		const res = mkRes();
		await launcher(mkReq({ url: '/api/agent/launcher?agentId=not-a-uuid' }), res);
		expect(res.statusCode).toBe(400);
		expect(parse(res).error).toBe('invalid_agent_id');
		expect(sqlMock).not.toHaveBeenCalled();
	});

	it('answers 400 for a non-uuid config id on DELETE', async () => {
		const res = mkRes();
		await launcher(mkReq({ method: 'DELETE', url: '/api/agent/launcher?id=nope' }), res);
		expect(res.statusCode).toBe(400);
		expect(parse(res).error).toBe('invalid_id');
		expect(sqlMock).not.toHaveBeenCalled();
	});

	it('answers 400 for a non-uuid configId on the trigger action', async () => {
		const res = mkRes();
		await launcher(mkReq({
			method: 'POST',
			url: '/api/agent/launcher',
			body: { action: 'trigger', agentId: AGENT, configId: 'nope' },
		}), res);
		expect(res.statusCode).toBe(400);
		expect(parse(res).error).toBe('invalid_config_id');
		expect(sqlMock).not.toHaveBeenCalled();
	});

	it('still returns configs and launched coins for a well-formed owned agent', async () => {
		sqlQueue = [
			[{ id: AGENT }],                                   // ownership
			[{ id: CONFIG, agent_id: AGENT, enabled: true }],  // configs
			[{ id: 'coin-1', agent_id: AGENT }],               // coins
		];
		const res = mkRes();
		await launcher(mkReq({ url: `/api/agent/launcher?agentId=${AGENT}` }), res);
		expect(res.statusCode).toBe(200);
		const out = parse(res);
		expect(out.configs).toHaveLength(1);
		expect(out.coins).toHaveLength(1);
	});
});

describe('/api/agent/market-maker id validation', () => {
	it('answers 400 for a non-uuid agentId on GET and never queries', async () => {
		const res = mkRes();
		await marketMaker(mkReq({ url: '/api/agent/market-maker?agentId=not-a-uuid' }), res);
		expect(res.statusCode).toBe(400);
		expect(parse(res).error).toBe('validation_error');
		expect(sqlMock).not.toHaveBeenCalled();
	});

	it('answers 400 for a non-uuid config id on DELETE', async () => {
		const res = mkRes();
		await marketMaker(mkReq({ method: 'DELETE', url: '/api/agent/market-maker?id=nope' }), res);
		expect(res.statusCode).toBe(400);
		expect(sqlMock).not.toHaveBeenCalled();
	});

	it('answers 400 for a non-uuid agentId on POST before touching the database', async () => {
		const res = mkRes();
		await marketMaker(mkReq({
			method: 'POST',
			url: '/api/agent/market-maker',
			body: { agentId: 'nope', mint: THREE_MINT },
		}), res);
		expect(res.statusCode).toBe(400);
		expect(sqlMock).not.toHaveBeenCalled();
	});

	it('rejects a mint that is not a base-58 Solana address', async () => {
		const res = mkRes();
		await marketMaker(mkReq({
			method: 'POST',
			url: '/api/agent/market-maker',
			body: { agentId: AGENT, mint: 'not-a-mint' },
		}), res);
		expect(res.statusCode).toBe(400);
		expect(sqlMock).not.toHaveBeenCalled();
	});

	it('upserts a config for a well-formed owned agent and pins the network to a real cluster', async () => {
		sqlQueue = [
			[{ id: AGENT }],                                  // ownership
			[{ id: CONFIG, agent_id: AGENT, network: 'mainnet' }], // upsert
		];
		const res = mkRes();
		await marketMaker(mkReq({
			method: 'POST',
			url: '/api/agent/market-maker',
			body: { agentId: AGENT, mint: THREE_MINT, network: 'not-a-cluster' },
		}), res);
		expect(res.statusCode).toBe(200);
		expect(parse(res).config.id).toBe(CONFIG);
		// An unknown cluster name falls back to mainnet rather than writing a
		// config no worker would ever pick up.
		expect(sqlMock.mock.calls[1].slice(1)).toContain('mainnet');
		expect(sqlMock.mock.calls[1].slice(1)).not.toContain('not-a-cluster');
	});
});

// The Capabilities command center reads every capability for the whole account
// at once. Before `all=1` it asked once per agent, which on a real roster spent
// more of the per-IP budget than the bucket held: the overflow came back 429,
// the page discarded it, and an account with launchers and live markets was
// shown a confident row of zeros. These pin the account-wide contract.
describe('account-wide reads (all=1)', () => {
	it('answers the launcher for every owned agent in two queries, with no agentId', async () => {
		sqlQueue = [
			[{ id: CONFIG, agent_id: AGENT, agent_name: 'Roster Agent', agent_image: null }],
			[{ mint: THREE_MINT, agent_id: AGENT, agent_name: 'Roster Agent', agent_image: null }],
		];
		const res = mkRes();
		await launcher(mkReq({ url: '/api/agent/launcher?all=1' }), res);
		expect(res.statusCode).toBe(200);
		const body = parse(res);
		expect(body.scope).toBe('all');
		expect(body.configs).toHaveLength(1);
		expect(body.coins).toHaveLength(1);
		// Rows carry the owning agent inline so the caller needs no second lookup.
		expect(body.configs[0].agent_name).toBe('Roster Agent');
		// Two queries total: no per-agent ownership round trip.
		expect(sqlMock).toHaveBeenCalledTimes(2);
	});

	it('answers the market maker for every owned agent in two queries, with no agentId', async () => {
		sqlQueue = [
			[{ id: CONFIG, agent_id: AGENT, agent_name: 'Roster Agent', agent_image: null }],
			[{ id: 'trade-1', agent_id: AGENT, side: 'buy', agent_name: 'Roster Agent', agent_image: null }],
		];
		const res = mkRes();
		await marketMaker(mkReq({ url: '/api/agent/market-maker?all=1' }), res);
		expect(res.statusCode).toBe(200);
		const body = parse(res);
		expect(body.scope).toBe('all');
		expect(body.configs).toHaveLength(1);
		expect(body.recent_trades).toHaveLength(1);
		expect(body.recent_trades[0].agent_name).toBe('Roster Agent');
		expect(sqlMock).toHaveBeenCalledTimes(2);
	});

	it('still demands an agentId when all is absent or not truthy', async () => {
		for (const url of ['/api/agent/launcher', '/api/agent/launcher?all=0', '/api/agent/launcher?all=maybe']) {
			sqlMock.mockClear();
			const res = mkRes();
			await launcher(mkReq({ url }), res);
			expect(res.statusCode).toBe(400);
			expect(parse(res).error).toBe('missing_agent_id');
			expect(sqlMock).not.toHaveBeenCalled();
		}
		for (const url of ['/api/agent/market-maker', '/api/agent/market-maker?all=0']) {
			sqlMock.mockClear();
			const res = mkRes();
			await marketMaker(mkReq({ url }), res);
			expect(res.statusCode).toBe(400);
			expect(sqlMock).not.toHaveBeenCalled();
		}
	});
});