diff --git a/packages/bridge/src/hook-status.ts b/packages/bridge/src/hook-status.ts new file mode 100644 index 00000000..414b3199 --- /dev/null +++ b/packages/bridge/src/hook-status.ts @@ -0,0 +1,31 @@ +import type { HookProcessStatus } from './types'; + +/** + * The flags that fully determine a HookSession's *settled* status. + * `wasLoggedIn` carries the one bit of history the derivation needs: + * on ANY injected-but-not-connected path (connect failure, pipe close, + * refresh-while-down), the session reads `disconnected` only if it had + * reached login, otherwise it's still `connecting`. Capture it from + * `loggedIn` *before* tearing the client down, since teardown clears it. + */ +export interface HookFlags { + injected: boolean; + connected: boolean; + loggedIn: boolean; + wasLoggedIn: boolean; +} + +/** + * Single source of truth for the five *settled* HookProcessStatus values + * — `available` / `connecting` / `loaded` / `online` / `disconnected`. + * + * Pure: status is a function of the flags alone. The two *out-of-band* + * states `loading` (injection in flight) and `error` (carries a message) + * are set explicitly by HookSession and are deliberately NOT derivable + * here. + */ +export function statusFor(flags: HookFlags): HookProcessStatus { + if (!flags.injected) return 'available'; + if (flags.connected) return flags.loggedIn ? 'online' : 'loaded'; + return flags.wasLoggedIn ? 'disconnected' : 'connecting'; +} diff --git a/packages/bridge/tests/hook-status.test.ts b/packages/bridge/tests/hook-status.test.ts new file mode 100644 index 00000000..2d1e21f8 --- /dev/null +++ b/packages/bridge/tests/hook-status.test.ts @@ -0,0 +1,56 @@ +import { describe, it, expect } from 'vitest'; +import { statusFor, type HookFlags } from '../src/hook-status'; +import type { HookProcessStatus } from '../src/types'; + +const F = (o: Partial): HookFlags => ({ + injected: false, connected: false, loggedIn: false, wasLoggedIn: false, ...o, +}); + +describe('statusFor — settled HookProcessStatus derivation', () => { + it('not injected → available (regardless of other flags)', () => { + expect(statusFor(F({ injected: false }))).toBe('available'); + // !injected dominates: stale connected/loggedIn bits must not leak through. + expect(statusFor(F({ injected: false, connected: true, loggedIn: true, wasLoggedIn: true }))).toBe('available'); + }); + + it('injected, connected, loggedIn → online', () => { + expect(statusFor(F({ injected: true, connected: true, loggedIn: true }))).toBe('online'); + }); + + it('injected, connected, not loggedIn → loaded', () => { + expect(statusFor(F({ injected: true, connected: true, loggedIn: false }))).toBe('loaded'); + }); + + it('injected, not connected, had logged in → disconnected', () => { + expect(statusFor(F({ injected: true, connected: false, wasLoggedIn: true }))).toBe('disconnected'); + }); + + it('injected, not connected, never logged in → connecting', () => { + expect(statusFor(F({ injected: true, connected: false, wasLoggedIn: false }))).toBe('connecting'); + }); + + it('disconnected vs connecting differ ONLY by wasLoggedIn (same injected+!connected)', () => { + const base = { injected: true, connected: false, loggedIn: false }; + expect(statusFor(F({ ...base, wasLoggedIn: true }))).toBe('disconnected'); + expect(statusFor(F({ ...base, wasLoggedIn: false }))).toBe('connecting'); + }); + + it('covers exactly the five settled states across the flag space', () => { + const seen = new Set(); + for (const injected of [false, true]) { + for (const connected of [false, true]) { + for (const loggedIn of [false, true]) { + for (const wasLoggedIn of [false, true]) { + seen.add(statusFor({ injected, connected, loggedIn, wasLoggedIn })); + } + } + } + } + expect([...seen].sort()).toEqual( + ['available', 'connecting', 'disconnected', 'loaded', 'online'].sort(), + ); + // The two out-of-band states are never produced by the pure derivation. + expect(seen.has('loading')).toBe(false); + expect(seen.has('error')).toBe(false); + }); +});