Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

test: pin Brain first-page cursor selection at the wrapper and router boundaries (#10870)

Adam Eivy9189bd4720ee
diff --git a/client/src/services/apiBrain.test.js b/client/src/services/apiBrain.test.js
new file mode 100644
index 0000000000..3a4e4ffeef
--- /dev/null
+++ b/client/src/services/apiBrain.test.js
@@ -0,0 +1,52 @@
+import { describe, it, expect, vi, beforeEach } from 'vitest';
+
+// Only the transport is stubbed: the assertion is the URL each wrapper builds.
+// The Brain router selects cursor paging only when `cursor` is present in the
+// query (server/routes/brainCrud.js), so a first-page request that drops its
+// null cursor silently gets the legacy offset envelope with no `nextCursor`.
+const { request } = vi.hoisted(() => ({ request: vi.fn() }));
+vi.mock('./apiCore.js', () => ({ request }));
+
+import {
+ getBrainAdmin, getBrainIdeas, getBrainMemories, getBrainPeople, getBrainProjects
+} from './apiBrain.js';
+
+const wrappers = { people: getBrainPeople, projects: getBrainProjects, ideas: getBrainIdeas, admin: getBrainAdmin, memories: getBrainMemories };
+const lastCall = () => request.mock.calls[request.mock.calls.length - 1];
+
+beforeEach(() => request.mockReset());
+
+describe.each(Object.entries(wrappers))('%s collection wrapper', (type, fetchPage) => {
+ it('sends the empty initial cursor when the caller opts into cursor paging with null', () => {
+ fetchPage({ cursor: null, limit: 25 });
+ expect(lastCall()[0]).toBe(`/brain/${type}?cursor=&limit=25`);
+ });
+
+ it('forwards a continuation cursor unchanged', () => {
+ fetchPage({ cursor: 'abc_123', limit: 25 });
+ expect(lastCall()[0]).toBe(`/brain/${type}?cursor=abc_123&limit=25`);
+ });
+
+ it('keeps the legacy request (no cursor) for callers that omit the option', () => {
+ fetchPage();
+ expect(lastCall()[0]).toBe(`/brain/${type}`);
+ fetchPage({ limit: 10, offset: 5 });
+ expect(lastCall()[0]).toBe(`/brain/${type}?limit=10&offset=5`);
+ });
+
+ it('keeps search, request options and cancellation alongside the initial cursor', () => {
+ const signal = new AbortController().signal;
+ fetchPage({ cursor: null, search: 'needle', limit: 25, signal, silent: true });
+ const [path, options] = lastCall();
+ expect(path).toContain('cursor=');
+ expect(path).toContain('search=needle');
+ expect(options).toEqual({ silent: true, signal });
+ });
+});
+
+describe('status filters', () => {
+ it.each(['projects', 'ideas', 'admin', 'memories'])('%s keeps status next to the initial cursor', (type) => {
+ wrappers[type]({ cursor: null, status: 'active', limit: 25 });
+ expect(lastCall()[0]).toBe(`/brain/${type}?status=active&cursor=&limit=25`);
+ });
+});
diff --git a/server/routes/brainCollectionPagination.test.js b/server/routes/brainCollectionPagination.test.js
index fc00be0540..2cdad38e60 100644
--- a/server/routes/brainCollectionPagination.test.js
+++ b/server/routes/brainCollectionPagination.test.js
@@ -27,18 +27,7 @@ vi.mock('../services/userActions.js', () => ({
recordUserAction: vi.fn(async () => ({ id: 'evt' }))
}));
-// The real client wrappers run against the mounted router: only the transport
-// (`request`) is replaced, so the URL each wrapper builds is what the router sees.
-const transport = vi.hoisted(() => ({ calls: [], send: null }));
-vi.mock('../../client/src/services/apiCore.js', () => ({
- request: async (path, options) => {
- transport.calls.push({ path, options });
- return transport.send(path);
- }
-}));
-
// Import after hoisting mocks
-import * as clientBrainApi from '../../client/src/services/apiBrain.js';
import * as brainStorage from '../services/brainStorage.js';
import brainRoutes from './brain.js';
@@ -49,13 +38,7 @@ app.use('/api/brain', brainRoutes);
const TOTAL_INBOX = 1200;
const TOTAL_MEMORIES = 1500;
const TOTAL_ENTITIES = 60;
-const ENTITY_TYPES = {
- people: clientBrainApi.getBrainPeople,
- projects: clientBrainApi.getBrainProjects,
- ideas: clientBrainApi.getBrainIdeas,
- admin: clientBrainApi.getBrainAdmin,
- memories: clientBrainApi.getBrainMemories
-};
+const ENTITY_TYPES = ['people', 'projects', 'ideas', 'admin'];
const BASE_TIME = Date.parse('2026-08-01T00:00:00.000Z');
async function seedRecord(type, id, record) {
@@ -112,14 +95,13 @@ describe('Brain collection pagination (synthetic thousands-record fixtures)', ()
};
});
- for (const type of ['people', 'projects', 'ideas', 'admin']) {
+ for (const type of ENTITY_TYPES) {
await seedRecords(type, TOTAL_ENTITIES, (i) => {
const updatedAt = new Date(BASE_TIME + i * 60000).toISOString();
return {
name: `Example ${type} ${i}`,
title: `Example ${type} ${i}`,
status: i % 2 === 0 ? 'active' : 'done',
- tags: i === 7 ? ['wanted-tag'] : [],
createdAt: updatedAt,
updatedAt
};
@@ -127,10 +109,6 @@ describe('Brain collection pagination (synthetic thousands-record fixtures)', ()
}
brainStorage.invalidateAllCaches();
- transport.send = async (path) => {
- const res = await request(app).get(`/api${path}`);
- return res.body;
- };
});
afterAll(() => {
@@ -299,57 +277,38 @@ describe('Brain collection pagination (synthetic thousands-record fixtures)', ()
expect(res.body.nextCursor).toBeUndefined();
});
});
- describe('Initial entity request from the client wrappers', () => {
- const lastPath = () => transport.calls[transport.calls.length - 1].path;
-
- it.each(Object.keys(ENTITY_TYPES))('%s: a first-page request (cursor: null) selects cursor mode and can reach every record', async (type) => {
+ // The client wrappers (client/src/services/apiBrain.test.js) send `cursor=` on a
+ // collection's first request; this pins what the router does with that URL.
+ describe('First-page request from the Brain collection views (empty initial cursor)', () => {
+ it.each([...ENTITY_TYPES, 'memories'])('%s: cursor mode returns a continuation that reaches every record', async (type) => {
const total = type === 'memories' ? 1350 : TOTAL_ENTITIES;
- const fetchPage = ENTITY_TYPES[type];
- const first = await fetchPage({ cursor: null, limit: 25 });
- expect(lastPath()).toContain('cursor=');
- expect(first.items).toHaveLength(25);
- expect(first.total).toBe(total);
- expect(first.nextCursor).toBeTruthy();
- expect(first.offset).toBeUndefined();
-
- let seen = first.items.map(item => item.id);
- let cursor = first.nextCursor;
+ const first = await request(app).get(`/api/brain/${type}?cursor=&limit=25`);
+ expect(first.status).toBe(200);
+ expect(first.body.items).toHaveLength(25);
+ expect(first.body.total).toBe(total);
+ expect(first.body.nextCursor).toBeTruthy();
+ expect(first.body.offset).toBeUndefined();
+
+ let seen = first.body.items.map(item => item.id);
+ let cursor = first.body.nextCursor;
while (cursor) {
- const next = await fetchPage({ cursor, limit: 25 });
- seen = seen.concat(next.items.map(item => item.id));
- cursor = next.nextCursor;
+ const next = await request(app).get(`/api/brain/${type}?cursor=${encodeURIComponent(cursor)}&limit=25`);
+ seen = seen.concat(next.body.items.map(item => item.id));
+ cursor = next.body.nextCursor;
}
- expect(seen).toHaveLength(total);
expect(new Set(seen).size).toBe(total);
});
- it('serves the compact memory projection on the initial page', async () => {
- const first = await clientBrainApi.getBrainMemories({ cursor: null, limit: 25 });
- expect(first.items.every(item => item.contentTruncated === true && item.content.length <= 300)).toBe(true);
- });
-
- it.each(['people', 'projects', 'ideas', 'admin', 'memories'])('%s: a caller that omits the cursor option keeps the legacy contract', async (type) => {
- const legacyArray = await ENTITY_TYPES[type]();
- expect(lastPath()).not.toContain('cursor');
- expect(Array.isArray(legacyArray)).toBe(true);
-
- const envelope = await ENTITY_TYPES[type]({ limit: 10, offset: 5 });
- expect(lastPath()).not.toContain('cursor');
- expect(envelope).toMatchObject({ limit: 10, offset: 5 });
- expect(envelope.nextCursor).toBeUndefined();
+ it.each(ENTITY_TYPES)('%s: a request without cursor keeps the legacy offset envelope', async (type) => {
+ const res = await request(app).get(`/api/brain/${type}?limit=25`);
+ expect(res.body).toMatchObject({ total: TOTAL_ENTITIES, limit: 25, offset: 0 });
+ expect(res.body.nextCursor).toBeUndefined();
});
- it('keeps search, status filters and request options alongside the initial cursor', async () => {
- const signal = new AbortController().signal;
- const filtered = await clientBrainApi.getBrainProjects({ cursor: null, limit: 25, status: 'active', signal, silent: true });
- expect(transport.calls[transport.calls.length - 1].options).toEqual({ silent: true, signal });
- expect(lastPath()).toContain('status=active');
- expect(filtered.total).toBe(TOTAL_ENTITIES / 2);
- expect(filtered.items.every(item => item.status === 'active')).toBe(true);
-
- const searched = await clientBrainApi.getBrainPeople({ cursor: null, limit: 25, search: 'wanted-tag' });
- expect(searched.total).toBe(1);
- expect(searched.nextCursor).toBeNull();
+ it('combines the empty cursor with a status filter', async () => {
+ const res = await request(app).get('/api/brain/projects?cursor=&limit=25&status=active');
+ expect(res.body.total).toBe(TOTAL_ENTITIES / 2);
+ expect(res.body.items.every(item => item.status === 'active')).toBe(true);
});
});
});