diff --git a/frontend/src/lib/coach.test.js b/frontend/src/lib/coach.test.js index e2f048a..65c13cd 100644 --- a/frontend/src/lib/coach.test.js +++ b/frontend/src/lib/coach.test.js @@ -443,3 +443,26 @@ describe('validation', () => { expect(validateProposal({ changes: [change()] })).toBe(true) }) }) + +describe('the gate has something real to read', () => { + it('the store actually exposes the `config` field every Coach entry point reads', async () => { + // coachAvailable(config, …) is false when `config` is undefined, which is indistinguishable + // from "this instance has no Coach". The views were ported once without the store field they + // depend on, so every Coach surface silently vanished on every instance: no entry point, and + // /#/coach redirecting straight back to /home. A missing key must fail here, not in a browser. + // Asserted against the source rather than the live store: useStore touches `document` at + // import time, so it cannot be instantiated in this environment. Crude, but it fails on + // exactly the two edits that broke it — dropping the field, or dropping the fetch. + const { readFileSync } = await import('node:fs') + const src = readFileSync(new URL('../store/useStore.js', import.meta.url), 'utf8') + expect(src).toMatch(/\bconfig:\s*null\b/) + expect(src).toMatch(/set\(\s*\{\s*config:\s*await api\(['"]\/api\/config['"]\)/) + }) + + it('a configured instance with a signed-in user opens the gate', () => { + // The exact shape GET /api/config returns when the Coach is on. + const config = { invite_only: false, coach: { enabled: true, provider: 'claude', authMode: 'instance' } } + expect(coachAvailable(config, { id: 'u' })).toBe(true) + expect(coachAvailable(undefined, { id: 'u' })).toBe(false) // the bug, pinned + }) +}) diff --git a/frontend/src/store/useStore.js b/frontend/src/store/useStore.js index 2b41633..a6367d4 100644 --- a/frontend/src/store/useStore.js +++ b/frontend/src/store/useStore.js @@ -83,6 +83,11 @@ export const useStore = create((set, get) => { S: (() => { const s = loadState(); registerCustom(s.customEx); return s })(), user: (() => { try { return JSON.parse(localStorage.getItem('gym_user')) || null } catch { return null } })(), ready: false, + /* Instance capabilities from GET /api/config. `config.coach` is present only when the owner + has both enabled the Coach and connected a provider — every Coach entry point in the app + hangs off it via coachAvailable(), so an unconfigured instance renders exactly what it + always did, and a configured one is the only place any of it appears. */ + config: null, // Mutate a draft of S via producer fn, then persist + schedule sync. update(mut, push = true) { @@ -172,6 +177,9 @@ export const useStore = create((set, get) => { set({ ready: true }) return } + // Instance capabilities are public and are needed whether or not anyone is signed in — + // fetched before /api/me so the first render already knows what this instance offers. + try { set({ config: await api('/api/config') }) } catch { /* offline — assume nothing extra */ } try { const me = await api('/api/me') get().setUser(me.user)