Coach: the store field the whole UI reads, which was never ported

The entry point in the previous commit was necessary but not sufficient: every
Coach surface gates on coachAvailable(config, user), config comes from
useStore(s => s.config), and that field does not exist. 4/6 brought the views
across and left behind the one line of store they all depend on.

So `config` was undefined at all five call sites, the gate was false on every
instance, and the feature was invisible everywhere — no row on Plan, and
/#/coach redirecting straight back to /home. Enabled, credentialed, a real
round trip through the provider passing, and no way in. Indistinguishable from
"this instance has no Coach", which is exactly why it survived review: the
failure mode is silence.

Adds `config: null` and the boot-time fetch, ahead of /api/me because instance
capabilities are public and needed whether or not anyone is signed in.

Two tests pin it. One asserts the gate opens for the exact shape GET
/api/config returns, and stays shut for `undefined` — the bug itself. The other
asserts the store source still declares the field and still fetches it; that one
reads the file rather than the live store, because useStore touches `document`
at import time and cannot be instantiated in this environment. Crude, but it
fails on precisely the two edits that broke this.

frontend 406/406, build clean.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
AlexandClaude Opus 5 committed 2026-08-07 15:27:02 +01:00
1 parent 4ee9a2abbf
commit 5d8332d11d
2 files changed
+31

No files matched your search

+23
View File
@@ -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
})
})
+8
View File
@@ -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)