'use strict'; // Item 2 (Truth-Everywhere Part 2) — the founder-seat counter is REAL or hidden, // never a fabricated "47 / 100". Redis mocked; the Supabase client injected. const request = require('supertest'); let mockStore = {}; jest.mock('../../src/utils/redis', () => ({ getRedisClient: () => ({}), cacheGet: async (k) => (k in mockStore ? mockStore[k] : null), cacheSet: async (k, v) => { mockStore[k] = v; return true; }, cacheDel: async () => true, isDegraded: () => false, })); const app = require('../../src/app'); const foundersRouter = require('../../src/routes/founders'); // A fake Supabase query builder that resolves to a fixed count/error. function fakeClient({ count = 0, error = null } = {}) { const b = { from() { return b; }, select() { return b; }, eq() { return b; }, then(resolve) { return Promise.resolve({ count, error }).then(resolve); }, }; return b; } beforeEach(() => { mockStore = {}; foundersRouter.__setDeps({}); }); describe('GET /api/founders/count', () => { test('real active-founder count → { available, claimed, total }', async () => { foundersRouter.__setDeps({ getClient: () => fakeClient({ count: 3 }) }); const res = await request(app).get('/api/founders/count'); expect(res.status).toBe(200); expect(res.body.available).toBe(true); expect(res.body.claimed).toBe(3); expect(res.body.total).toBe(100); }); test('a low real count is shown honestly (no fabricated floor)', async () => { foundersRouter.__setDeps({ getClient: () => fakeClient({ count: 0 }) }); const res = await request(app).get('/api/founders/count'); expect(res.body).toEqual({ available: true, claimed: 0, total: 100 }); }); test('query error (column missing) → available:false, NEVER a number', async () => { foundersRouter.__setDeps({ getClient: () => fakeClient({ error: { message: 'no column' } }) }); const res = await request(app).get('/api/founders/count'); expect(res.body).toEqual({ available: false }); expect(res.body).not.toHaveProperty('claimed'); }); test('unconfigured source (no client) → available:false, hidden', async () => { foundersRouter.__setDeps({ getClient: () => null }); const res = await request(app).get('/api/founders/count'); expect(res.body).toEqual({ available: false }); }); test('client throws → available:false, never fabricates', async () => { foundersRouter.__setDeps({ getClient: () => { throw new Error('down'); } }); const res = await request(app).get('/api/founders/count'); expect(res.body).toEqual({ available: false }); }); test('served from cache when warm (no client call)', async () => { mockStore['founders:count'] = { claimed: 7 }; let clientCalled = false; foundersRouter.__setDeps({ getClient: () => { clientCalled = true; return fakeClient({ count: 999 }); } }); const res = await request(app).get('/api/founders/count'); expect(res.body).toEqual({ available: true, claimed: 7, total: 100 }); expect(clientCalled).toBe(false); }); });