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.

4.1 KB
Raw Blame
// Regression: strategy-run live buys must reserve against the daily SOL cap
// atomically. The old read-only checkBuyAllowed gate let two concurrent runs
// both read the same 24h total and both pass. No funds move here: PGlite only.

import { beforeAll, describe, it, expect, vi } from 'vitest';
import { createPgliteSql } from './_helpers/pglite-sql.js';

const holder = vi.hoisted(() => ({ db: null }));

vi.mock('../api/_lib/db.js', async (importActual) => {
	const actual = await importActual();
	const sql = (...args) => holder.db.sql(...args);
	sql.transaction = async (queries) => {
		const out = [];
		for (const q of queries) out.push(await q);
		return out;
	};
	return { ...actual, sql };
});

vi.mock('../api/_lib/avatar-wallet.js', () => ({ solUsdPrice: vi.fn(async () => 200) }));

process.env.DATABASE_URL ||= 'postgres://pglite.test/db';

const AGENT = '00000000-0000-0000-0000-000000000031';
const META = { spend_policy: { max_sol_per_tx: 4, daily_sol_cap: 5 } };

async function rows() {
	return holder.db.query(
		`select type, payload from agent_actions where agent_id = $1 order by created_at`,
		[AGENT],
	);
}

beforeAll(async () => {
	holder.db = createPgliteSql();
	await holder.db.exec(`create table agent_actions (
		id uuid primary key default gen_random_uuid(), agent_id uuid, type text, payload jsonb,
		source_skill text, signature text, signer_address text, created_at timestamptz default now());`);
});

describe('strategy spend guard reserves atomically', () => {
	it('lets only one of two concurrent 3 SOL buys through a 5 SOL daily cap', async () => {
		const { createStrategySpendGuard } = await import('../api/_lib/agent-spend-policy.js');
		const a = createStrategySpendGuard({ agentId: AGENT, meta: META });
		const b = createStrategySpendGuard({ agentId: AGENT, meta: META });
		const [ra, rb] = await Promise.all([
			a.guard({ mint: 'THREEsyntheticMintA', amountSol: 3 }),
			b.guard({ mint: 'THREEsyntheticMintB', amountSol: 3 }),
		]);
		const blocked = [ra, rb].filter(Boolean);
		expect(blocked).toHaveLength(1);
		expect(blocked[0].code).toBe('daily_cap_exceeded');
		expect((await rows()).length).toBe(1);
	});

	it('finalizes the reservation on success and releases it on failure', async () => {
		const { createStrategySpendGuard } = await import('../api/_lib/agent-spend-policy.js');
		await holder.db.query(`delete from agent_actions`);
		const g = createStrategySpendGuard({ agentId: AGENT, meta: META });
		expect(await g.guard({ mint: 'THREEsyntheticMintC', amountSol: 2 })).toBeNull();
		expect(await g.guard({ mint: 'THREEsyntheticMintD', amountSol: 2 })).toBeNull();
		await g.settle({ mint: 'THREEsyntheticMintC', ok: true, sig: 'sigC' });
		await g.settle({ mint: 'THREEsyntheticMintD', ok: false });
		const r = await rows();
		expect(r).toHaveLength(1);
		expect(r[0].type).toBe('pumpfun.buy');
		expect(r[0].payload.status).toBe('confirmed');
		expect(r[0].payload.sig).toBe('sigC');
	});
});

describe('strategy spend guard honours the full agent spend policy', () => {
	it('refuses a live buy for a frozen wallet and reserves nothing', async () => {
		const { createStrategySpendGuard } = await import('../api/_lib/agent-spend-policy.js');
		await holder.db.query(`delete from agent_actions`);
		const frozen = { ...META, spend_limits: { frozen: true } };
		const g = createStrategySpendGuard({ agentId: AGENT, meta: frozen });
		const blocked = await g.guard({ mint: 'THREEsyntheticMintE', amountSol: 0.1 });
		expect(blocked).toMatchObject({ code: 'wallet_frozen' });
		expect(await rows()).toHaveLength(0);
	});

	it('refuses a live buy over the per-transaction USD limit', async () => {
		const { createStrategySpendGuard } = await import('../api/_lib/agent-spend-policy.js');
		await holder.db.query(`delete from agent_actions`);
		const capped = { ...META, spend_limits: { per_tx_usd: 10 } };
		const g = createStrategySpendGuard({ agentId: AGENT, meta: capped });
		const blocked = await g.guard({ mint: 'THREEsyntheticMintF', amountSol: 1 }); // $200 at the mocked price
		expect(blocked).toMatchObject({ code: 'per_tx_exceeded' });
		expect(await rows()).toHaveLength(0);
	});
});