diff --git a/src/app.js b/src/app.js index 774f012..045cdc3 100644 --- a/src/app.js +++ b/src/app.js @@ -215,6 +215,7 @@ app.use('/api/internal', internalRoutes); // A1 S3 — partner attribution report. Internal-key gated (router-level // requireInternalAuth); no Next proxy on purpose — never browser-facing. app.use('/api/partners', require('./routes/partners')); +app.use('/api/founders', require('./routes/founders')); // Session 10 — Sentry's Express error handler catches uncaught // errors from every route mounted above. Must come AFTER routes but diff --git a/src/routes/founders.js b/src/routes/founders.js new file mode 100644 index 0000000..1bdbfed --- /dev/null +++ b/src/routes/founders.js @@ -0,0 +1,70 @@ +/** + * GET /api/founders/count — the REAL founder-seat counter. + * + * TRUTH LAW (Truth-Everywhere Part 2, item 2): the ClaimMeter used to render a + * hardcoded "47 / 100 CLAIMED". This counts ONLY real paying founders — the + * Stripe-synced subscription records in Supabase (`user_profiles` where + * founder_pricing = true AND subscription_status = 'active'). Counting the + * webhook-synced mirror, not the Stripe API, is the cache — we never hammer + * Stripe. Cached 5 min in Redis on top of that. + * + * If the source is unavailable (Supabase unconfigured, query error, column not + * migrated), we return { available: false } and the UI HIDES the counter — we + * never fall back to a number. A low real count is fine; the truth is the feature. + */ + +const express = require('express'); +const { getSupabaseServiceClient } = require('../utils/supabase'); +const { cacheGet, cacheSet } = require('../utils/redis'); + +const router = express.Router(); + +const TOTAL = Number(process.env.FOUNDER_SEATS_TOTAL || 100); +const CACHE_KEY = 'founders:count'; +const CACHE_TTL = 300; // 5 min — the founder count moves slowly + +// Injectable for tests (never hits network in the unit suite). +let _getClient = getSupabaseServiceClient; +let _cacheGet = cacheGet; +let _cacheSet = cacheSet; +function __setDeps({ getClient, cacheGet: cg, cacheSet: cs } = {}) { + _getClient = getClient || getSupabaseServiceClient; + _cacheGet = cg || cacheGet; + _cacheSet = cs || cacheSet; +} + +router.get('/count', async (req, res) => { + // Cache first — don't recount on every landing hit. + try { + const cached = await _cacheGet(CACHE_KEY); + if (cached && typeof cached.claimed === 'number') { + return res.json({ available: true, claimed: cached.claimed, total: TOTAL }); + } + } catch { /* cache miss/degraded — fall through to a live count */ } + + let supabase; + try { + supabase = _getClient(); + } catch { + return res.json({ available: false }); // unconfigured → hide, never a number + } + if (!supabase) return res.json({ available: false }); + + try { + const { count, error } = await supabase + .from('user_profiles') + .select('id', { count: 'exact', head: true }) + .eq('founder_pricing', true) + .eq('subscription_status', 'active'); + if (error) return res.json({ available: false }); // column missing / query error → hide + const claimed = Math.max(0, Number(count) || 0); + try { await _cacheSet(CACHE_KEY, { claimed }, CACHE_TTL); } catch { /* best-effort */ } + return res.json({ available: true, claimed, total: TOTAL }); + } catch { + return res.json({ available: false }); // any failure → hide, never fabricate + } +}); + +router.__setDeps = __setDeps; +router.__internals = { TOTAL, CACHE_KEY, CACHE_TTL }; +module.exports = router; diff --git a/tests/integration/foundersRoute.test.js b/tests/integration/foundersRoute.test.js new file mode 100644 index 0000000..af0b1f3 --- /dev/null +++ b/tests/integration/foundersRoute.test.js @@ -0,0 +1,76 @@ +'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); + }); +}); diff --git a/web/src/app/api/founders/count/route.ts b/web/src/app/api/founders/count/route.ts new file mode 100644 index 0000000..40ad788 --- /dev/null +++ b/web/src/app/api/founders/count/route.ts @@ -0,0 +1,25 @@ +import { NextResponse } from 'next/server'; + +export const dynamic = 'force-dynamic'; + +const BACKEND_URL = process.env.BACKEND_URL || 'http://localhost:3000'; + +/** + * Founder-seat count proxy (Truth-Everywhere Part 2, item 2) — forwards + * GET /api/founders/count to Express (real active paying founders). On any + * upstream failure we return { available: false } so the ClaimMeter HIDES — + * never a fabricated number. + */ +export async function GET() { + try { + const upstream = await fetch(`${BACKEND_URL}/api/founders/count`, { + method: 'GET', + headers: { Accept: 'application/json' }, + cache: 'no-store', + }); + const data = await upstream.json().catch(() => ({ available: false })); + return NextResponse.json(data, { status: upstream.ok ? 200 : 200 }); + } catch { + return NextResponse.json({ available: false }, { status: 200 }); + } +} diff --git a/web/src/components/vyndr/ClaimMeter.tsx b/web/src/components/vyndr/ClaimMeter.tsx index 3710e71..7deae8a 100644 --- a/web/src/components/vyndr/ClaimMeter.tsx +++ b/web/src/components/vyndr/ClaimMeter.tsx @@ -1,17 +1,43 @@ +'use client'; + +import { useEffect, useState } from 'react'; import SectionHead from '@/components/vyndr/SectionHead'; -interface ClaimMeterProps { +interface FounderCount { + available: boolean; claimed?: number; total?: number; } /** - * Founder-seat scarcity meter (§12 ClaimMeter) — "47 / 100 seats claimed", - * amber bar. Cosmetic conversion driver; the live tick-up wires into the - * living layer in Session 38. Static here. + * Founder-seat scarcity meter (§12 ClaimMeter). + * + * TRUTH LAW (Truth-Everywhere Part 2, item 2): this used to render a hardcoded + * "47 / 100 CLAIMED". It now fetches the REAL count of active paying founders + * from /api/founders/count (Stripe-synced, cached). If the source is + * unavailable — or the fetch fails — the counter and progress bar HIDE + * entirely. We never fall back to a number. A low real count is fine; the + * truth is the feature. */ -export default function ClaimMeter({ claimed = 47, total = 100 }: ClaimMeterProps) { +export default function ClaimMeter() { + const [data, setData] = useState(null); + + useEffect(() => { + let active = true; + fetch('/api/founders/count', { cache: 'no-store' }) + .then((r) => r.json()) + .then((d) => { if (active) setData(d); }) + .catch(() => { if (active) setData({ available: false }); }); + return () => { active = false; }; + }, []); + + // Hide until we have a REAL number. Unavailable source → render nothing. + if (!data || !data.available || typeof data.claimed !== 'number') return null; + + const total = data.total || 100; + const claimed = Math.max(0, Math.min(total, data.claimed)); const pct = Math.min(100, Math.round((claimed / total) * 100)); + return (