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.

test(bundles): prove include_inactive is owner-only and the characters feed contract

nirholasbeca529cca4b
diff --git a/tests/api/characters.test.js b/tests/api/characters.test.js
index 484f80356..4ba21ed39 100644
--- a/tests/api/characters.test.js
+++ b/tests/api/characters.test.js
@@ -4,17 +4,24 @@
// rate limiter and bucket-URL helper are stubbed at the module boundary. What
// these pin is the pagination contract: a hand-edited `cursor` must come back
// as a 4xx, not as a 500 from `new Date('junk').toISOString()` throwing a
-// RangeError deep inside the handler.
+// RangeError deep inside the handler, and a `next_cursor` must carry every
+// column its sort orders by, or "Top" stops paging after one screen.
import { describe, it, expect, vi, beforeEach } from 'vitest';
const rows = [];
const sqlCalls = [];
vi.mock('../../api/_lib/db.js', () => {
- const sql = vi.fn(async (...args) => {
- sqlCalls.push(args);
- return rows.splice(0, rows.length);
- });
+ // Mirror the real wrapper's shape: every sql`` call returns a composable
+ // thenable, and a fragment spliced into a parent query is never awaited, so
+ // it never runs. Resolving lazily keeps `sqlCalls` to the one query the
+ // handler actually executes, whatever fragments it assembled on the way.
+ const sql = vi.fn((...args) => ({
+ then(resolve, reject) {
+ sqlCalls.push(args);
+ return Promise.resolve(rows.splice(0, rows.length)).then(resolve, reject);
+ },
+ }));
return { sql, isDbUnavailableError: () => false, isDbCapacityError: () => false };
});
@@ -132,6 +139,49 @@ describe('GET /api/characters', () => {
expect(interpolations(0)).toContain('2026-08-16T00:00:00.000Z');
});
+ // sort=chats orders by (chat_count DESC, created_at DESC), so a cursor that
+ // names only created_at cannot resume it: page 2 kept whatever was older than
+ // the last row and re-ranked THAT by chats, which ended the "Top" feed after
+ // a single screen. The cursor now carries both keys.
+ it('issues a composite cursor under sort=chats', async () => {
+ rows.push(AGENT, { ...AGENT, id: 'b', chat_count: 1, created_at: '2026-08-14T00:00:00.000Z' });
+ const res = mkRes();
+ await handler(mkReq({ query: '?limit=1&sort=chats' }), res);
+
+ expect(res.statusCode).toBe(200);
+ expect(res.json.next_cursor).toBe('4:2026-08-15T13:37:01.660Z');
+ });
+
+ it('resumes sort=chats from both keys of a composite cursor', async () => {
+ rows.push(AGENT);
+ const res = mkRes();
+ await handler(mkReq({ query: '?sort=chats&cursor=4%3A2026-08-15T13%3A37%3A01.660Z' }), res);
+
+ expect(res.statusCode).toBe(200);
+ const values = interpolations(0);
+ expect(values).toContain(4);
+ expect(values).toContain('2026-08-15T13:37:01.660Z');
+ });
+
+ it('rejects a cursor whose shape does not match the requested sort', async () => {
+ // A bare timestamp is what sort=new issues; under sort=chats it is either a
+ // hand-edited URL or a cursor carried across a sort switch, and resuming
+ // from it would silently skip rows.
+ const res = mkRes();
+ await handler(mkReq({ query: '?sort=chats&cursor=2026-08-15T13:37:01.660Z' }), res);
+
+ expect(res.statusCode).toBe(400);
+ expect(sqlCalls).toHaveLength(0);
+ });
+
+ it('400s a composite cursor with a junk count', async () => {
+ const res = mkRes();
+ await handler(mkReq({ query: '?sort=chats&cursor=abc%3A2026-08-15T13%3A37%3A01.660Z' }), res);
+
+ expect(res.statusCode).toBe(400);
+ expect(sqlCalls).toHaveLength(0);
+ });
+
it('coerces junk pagination and caps the page size', async () => {
rows.push(AGENT);
const junk = mkRes();
diff --git a/tests/bundles-include-inactive.test.js b/tests/bundles-include-inactive.test.js
new file mode 100644
index 000000000..59767635f
--- /dev/null
+++ b/tests/bundles-include-inactive.test.js
@@ -0,0 +1,133 @@
+// GET /api/agents/:id/bundles - who is allowed to see a paused bundle.
+//
+// `DELETE` on a bundle sets `is_active = false`, and the list was active-only
+// for everyone, which made pausing a one-way door: the owner's own bundle left
+// every surface that could bring it back. `include_inactive=1` reopens it, and
+// that flag is exactly the kind of parameter that quietly turns a public
+// endpoint into a leak. This endpoint is public (no session required), so the
+// flag has to be ignored for anyone who does not own the agent.
+//
+// These tests assert the WHERE fragment the handler actually builds, per caller:
+// only a session that owns the agent may widen it past `is_active = true`.
+
+import { beforeEach, describe, expect, it, vi } from 'vitest';
+
+const queries = [];
+const state = { user: null, ownsAgent: false };
+
+vi.mock('../api/_lib/db.js', () => ({
+ sql: vi.fn((strings, ...values) => {
+ const text = Array.isArray(strings) ? strings.join('?') : String(strings);
+ const lower = text.toLowerCase();
+ queries.push({ text, values });
+ if (/from agent_identities/.test(lower)) {
+ return Promise.resolve(state.ownsAgent ? [{ id: AGENT_ID }] : []);
+ }
+ if (/from skill_bundles/.test(lower)) return Promise.resolve([]);
+ // Anything else is a composable fragment the handler embeds in a query.
+ return { fragment: text };
+ }),
+ isDbUnavailableError: () => false,
+ isDbCapacityError: () => false,
+}));
+
+vi.mock('../api/_lib/auth.js', () => ({
+ getSessionUser: vi.fn(async () => state.user),
+ authenticateBearer: vi.fn(async () => null),
+ extractBearer: vi.fn(() => null),
+}));
+
+vi.mock('../api/_lib/csrf.js', () => ({ requireCsrf: vi.fn(async () => true) }));
+vi.mock('../api/_lib/rate-limit.js', () => ({
+ limits: { authIp: vi.fn(async () => ({ success: true })) },
+ clientIp: () => '127.0.0.1',
+}));
+
+const AGENT_ID = '33333333-3333-4333-8333-333333333333';
+const { default: handler } = await import('../api/agents/[id]/bundles.js');
+
+function makeRes() {
+ return {
+ statusCode: 0,
+ payload: null,
+ headers: {},
+ setHeader(k, v) { this.headers[String(k).toLowerCase()] = v; },
+ getHeader(k) { return this.headers[String(k).toLowerCase()]; },
+ removeHeader(k) { delete this.headers[String(k).toLowerCase()]; },
+ writeHead(code) { this.statusCode = code; return this; },
+ get headersSent() { return false; },
+ get writableEnded() { return false; },
+ end(chunk) {
+ if (chunk) { try { this.payload = JSON.parse(String(chunk)); } catch { this.payload = String(chunk); } }
+ return this;
+ },
+ };
+}
+
+async function list(query) {
+ const res = makeRes();
+ await handler({ method: 'GET', url: `/api/agents/${AGENT_ID}/bundles${query}`, headers: {} }, res);
+ return res;
+}
+
+/** The `${visible}` fragment the list query was built with. */
+function visibilityFragment() {
+ const listQuery = queries.find((q) => /from skill_bundles/i.test(q.text));
+ expect(listQuery, 'the handler never ran the list query').toBeTruthy();
+ const fragment = listQuery.values.find((v) => v && typeof v === 'object' && 'fragment' in v);
+ return fragment?.fragment;
+}
+
+beforeEach(() => {
+ queries.length = 0;
+ state.user = null;
+ state.ownsAgent = false;
+});
+
+describe('GET /api/agents/:id/bundles visibility', () => {
+ it('is active-only for an anonymous caller', async () => {
+ const res = await list('');
+ expect(res.statusCode).toBe(200);
+ expect(visibilityFragment()).toBe('sb.is_active = true');
+ });
+
+ it('ignores include_inactive for an anonymous caller', async () => {
+ const res = await list('?include_inactive=1');
+ expect(res.statusCode).toBe(200);
+ expect(visibilityFragment()).toBe('sb.is_active = true');
+ });
+
+ it('ignores include_inactive for a signed-in caller who does not own the agent', async () => {
+ state.user = { id: 'someone-else' };
+ state.ownsAgent = false;
+ const res = await list('?include_inactive=1');
+ expect(res.statusCode).toBe(200);
+ expect(visibilityFragment()).toBe('sb.is_active = true');
+ });
+
+ it('includes paused bundles for the agent owner', async () => {
+ state.user = { id: 'owner-1' };
+ state.ownsAgent = true;
+ const res = await list('?include_inactive=1');
+ expect(res.statusCode).toBe(200);
+ expect(visibilityFragment()).toBe('TRUE');
+ });
+
+ it('stays active-only for the owner who did not ask for paused bundles', async () => {
+ state.user = { id: 'owner-1' };
+ state.ownsAgent = true;
+ const res = await list('');
+ expect(res.statusCode).toBe(200);
+ expect(visibilityFragment()).toBe('sb.is_active = true');
+ // No ownership lookup is worth doing when the flag is absent.
+ expect(queries.some((q) => /from agent_identities/i.test(q.text))).toBe(false);
+ });
+
+ it('only honours the exact flag value', async () => {
+ state.user = { id: 'owner-1' };
+ state.ownsAgent = true;
+ const res = await list('?include_inactive=true');
+ expect(res.statusCode).toBe(200);
+ expect(visibilityFragment()).toBe('sb.is_active = true');
+ });
+});
diff --git a/tests/choreography.test.js b/tests/choreography.test.js
index e48967564..ca36a2ae8 100644
--- a/tests/choreography.test.js
+++ b/tests/choreography.test.js
@@ -231,6 +231,21 @@ describe('wire format', () => {
const decoded = decodeRoutine(encodeRoutine(routine([{ slot: 'wave' }], { name: 'a|b,c:d' })));
expect(decoded.name).toBe('a|b,c:d');
});
+
+ it('does not let a name ending in ~loop masquerade as the loop marker', () => {
+ const original = routine([{ slot: 'wave' }], { name: 'Encore~loop' });
+ const encoded = encodeRoutine(original);
+ expect(encoded.startsWith('Encore%7Eloop|')).toBe(true);
+ const decoded = decodeRoutine(encoded);
+ expect(decoded.name).toBe('Encore~loop');
+ expect(decoded.loop).toBe(false);
+ });
+
+ it('still reads the ~loop marker on a routine that loops', () => {
+ const decoded = decodeRoutine(encodeRoutine(routine([{ slot: 'wave' }], { name: 'Encore', loop: true })));
+ expect(decoded.name).toBe('Encore');
+ expect(decoded.loop).toBe(true);
+ });
});
describe('RoutinePlayer', () => {