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.

Fix animation-download purchase counting + add contract tests

nirholas7ed805446541
diff --git a/api/x402/animation-download.js b/api/x402/animation-download.js
index e19a349e9..1756dc334 100644
--- a/api/x402/animation-download.js
+++ b/api/x402/animation-download.js
@@ -16,7 +16,7 @@ import { buildBazaarSchema } from '../_lib/x402-spec.js';
import { withService } from '../_lib/x402/bazaar-helpers.js';
import { sql } from '../_lib/db.js';
import { presignGet } from '../_lib/r2.js';
-import { error } from '../_lib/http.js';
+import { error, json } from '../_lib/http.js';
import { env } from '../_lib/env.js';
const ROUTE = '/api/x402/animation-download';
@@ -131,6 +131,9 @@ async function presignPayload(row) {
};
}
+// Exported for contract tests (price/statement/payout logic) without a live DB.
+export const __test__ = { priceAtomics, buildSiwxStatement, buildPayToOverride, UUID_RE };
+
export default async function handler(req, res) {
const id = req.query?.id ? String(req.query.id).trim() : '';
if (!UUID_RE.test(id)) {
@@ -150,7 +153,8 @@ export default async function handler(req, res) {
// Free listing — no paywall, hand back the presigned URL directly.
if (!row.price_amount || Number(row.price_amount) <= 0) {
try {
- return res.status(200).json(await presignPayload(row));
+ res.setHeader('Cache-Control', 'no-store');
+ return json(res, 200, await presignPayload(row));
} catch (err) {
return error(res, 502, 'presign_failed', err.message);
}
@@ -179,8 +183,11 @@ export default async function handler(req, res) {
expirationSeconds: 300,
},
async handler(ctx) {
- // Count only fresh paid settlements — not free SIWX re-downloads.
- if (ctx?.payer && !ctx?.bypass) {
+ // Count only fresh paid settlements. `requirement` is set solely on
+ // the verified-payment path — the free SIWX re-download path (which
+ // also carries `payer`) and auth-hints bypass leave it null, so this
+ // never inflates the count on repeat downloads.
+ if (ctx?.requirement && ctx?.payer) {
queueMicrotask(async () => {
try {
await sql`update animation_clips set purchase_count = purchase_count + 1 where id = ${row.id}`;
diff --git a/tests/api/animation-download.test.js b/tests/api/animation-download.test.js
new file mode 100644
index 000000000..e838babc1
--- /dev/null
+++ b/tests/api/animation-download.test.js
@@ -0,0 +1,43 @@
+import { describe, it, expect } from 'vitest';
+import { __test__ } from '../../api/x402/animation-download.js';
+
+const { priceAtomics, buildSiwxStatement, buildPayToOverride, UUID_RE } = __test__;
+
+describe('animation-download priceAtomics', () => {
+ it('converts a human USDC amount to 6-decimal atomic units', () => {
+ expect(priceAtomics('2.5')).toBe('2500000');
+ expect(priceAtomics(1)).toBe('1000000');
+ expect(priceAtomics('0.01')).toBe('10000');
+ });
+});
+
+describe('animation-download buildSiwxStatement', () => {
+ it('is a single line of EIP-4361-safe characters', () => {
+ const s = buildSiwxStatement('Spin Kick "Combo" {v2}');
+ expect(s).not.toMatch(/[`{}"]/);
+ expect(s.split('\n')).toHaveLength(1);
+ expect(s).toContain('Spin Kick');
+ });
+ it('falls back when the name is empty', () => {
+ expect(buildSiwxStatement('')).toMatch(/re-download this animation/);
+ });
+});
+
+describe('animation-download buildPayToOverride', () => {
+ it('includes only the chains a creator has set', () => {
+ expect(buildPayToOverride({ creator_payto_base: '0xabc', creator_payto_solana: null, creator_payto_bsc: null }))
+ .toEqual({ base: '0xabc' });
+ });
+ it('returns undefined when no payout is configured (falls back to platform default)', () => {
+ expect(buildPayToOverride({ creator_payto_base: null, creator_payto_solana: null, creator_payto_bsc: null }))
+ .toBeUndefined();
+ });
+});
+
+describe('animation-download UUID guard', () => {
+ it('accepts a uuid and rejects junk', () => {
+ expect(UUID_RE.test('11111111-2222-3333-4444-555555555555')).toBe(true);
+ expect(UUID_RE.test('../../etc/passwd')).toBe(false);
+ expect(UUID_RE.test('spin-kick')).toBe(false);
+ });
+});
diff --git a/tests/api/marketplace-animations.test.js b/tests/api/marketplace-animations.test.js
new file mode 100644
index 000000000..f62369cae
--- /dev/null
+++ b/tests/api/marketplace-animations.test.js
@@ -0,0 +1,78 @@
+import { describe, it, expect } from 'vitest';
+import { __test__ } from '../../api/marketplace/animations.js';
+
+const { shape, encodeCursor, decodeCursor, SORTS } = __test__;
+
+const UUID = '11111111-2222-3333-4444-555555555555';
+
+const baseRow = {
+ id: UUID,
+ slug: 'spin-kick',
+ name: 'Spin Kick',
+ description: 'A crisp roundhouse loop.',
+ kind: 'loop',
+ duration_ms: 2400,
+ frame_count: 6,
+ fps: 30,
+ loop: true,
+ tags: ['combat', 'loop'],
+ thumbnail_key: null,
+ price_amount: '2.5',
+ price_currency: 'USDC',
+ artifact_bytes: '248400',
+ play_count: '12',
+ purchase_count: '3',
+ created_at: '2026-06-15T00:00:00.000Z',
+ creator_name: 'Mira',
+ creator_username: 'mira',
+ creator_avatar: null,
+};
+
+describe('marketplace/animations shape', () => {
+ it('projects a paid listing with price + download route', () => {
+ const out = shape(baseRow);
+ expect(out.id).toBe(UUID);
+ expect(out.price).toEqual({ amount: '2.5', currency: 'USDC' });
+ expect(out.free).toBe(false);
+ expect(out.duration).toBe(2.4);
+ expect(out.size_bytes).toBe(248400);
+ expect(out.play_count).toBe(12);
+ expect(out.purchase_count).toBe(3);
+ expect(out.download_url).toBe(`/api/x402/animation-download?id=${UUID}`);
+ expect(out.creator).toEqual({ name: 'Mira', username: 'mira', avatar_url: null });
+ });
+
+ it('marks a free listing and never leaks the baked clip', () => {
+ const out = shape({ ...baseRow, price_amount: null, price_currency: null });
+ expect(out.free).toBe(true);
+ expect(out.price).toBeNull();
+ // The feed surface must only ever carry metadata — never the motion data.
+ expect(out).not.toHaveProperty('clip');
+ expect(out).not.toHaveProperty('editor_doc');
+ });
+
+ it('defaults currency to USDC and falls back to a creator name', () => {
+ const out = shape({ ...baseRow, price_currency: null, creator_name: null });
+ expect(out.price.currency).toBe('USDC');
+ expect(out.creator.name).toBe('mira');
+ const anon = shape({ ...baseRow, creator_name: null, creator_username: null });
+ expect(anon.creator.name).toBe('Anonymous');
+ });
+});
+
+describe('marketplace/animations cursor', () => {
+ it('round-trips a created_at cursor', () => {
+ const cur = encodeCursor({ createdAt: baseRow.created_at });
+ const back = decodeCursor(cur);
+ expect(back.createdAt.toISOString()).toBe(baseRow.created_at);
+ });
+ it('returns null for a malformed cursor', () => {
+ expect(decodeCursor('@@not-base64@@')).toBeNull();
+ });
+});
+
+describe('marketplace/animations sort whitelist', () => {
+ it('exposes the four sortable orders', () => {
+ expect(Object.keys(SORTS).sort()).toEqual(['popular', 'price_high', 'price_low', 'recent']);
+ });
+});