mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-17 07:42:23 +00:00
feat(settings): explain operator switches and keep the Coach memory reachable
Settings, AI says which AI features the server's operator switched off and shows the one setup hint while no provider is configured. The consent withdrawal says what it deletes and what stays, and a document-reading consent on file gets its own withdrawal. Regenerate follows the briefing capability and the connection test names an operator refusal. The Coach memory (facts and stored conversations) is never gated on the Coach: Settings, Coach always shows it, and Settings, AI shows it while the Coach page is out of reach, whenever rows exist. Onboarding offers AI only where it can be set up here: the done-screen panel and the getting-started row go away while the operator has AI off or inside somebody else's record.
This commit is contained in:
1 parent
4449b34ac7
commit
3d85445e50
24 files changed
+913
-232
No files matched your search
@@ -44,6 +44,23 @@ vi.mock("@/hooks/use-auth", () => ({
|
||||
}),
|
||||
}));
|
||||
|
||||
// The AI capability map on `/api/auth/me`: everything available, a provider
|
||||
// configured, nothing switched off by the operator. The notices and the
|
||||
// setup hint above the provider card have their own suites.
|
||||
vi.mock("@/hooks/use-ai-capability", () => ({
|
||||
useAiCapability: () => ({
|
||||
available: true,
|
||||
reason: null,
|
||||
onDeviceAllowed: true,
|
||||
}),
|
||||
useAiCapabilityMap: () => null,
|
||||
useAiProviderState: () => ({
|
||||
configured: true,
|
||||
managedBy: "user",
|
||||
canConfigure: true,
|
||||
}),
|
||||
}));
|
||||
|
||||
// Stable mocks for every TanStack Query the section reads. The chain
|
||||
// fixture mirrors the default order (codex first) so the rows render in
|
||||
// a known order; insights settings + ai-provider GET return enough to
|
||||
|
||||
@@ -0,0 +1,144 @@
|
||||
/**
|
||||
* What the Coach stored stays readable and deletable while the Coach is off.
|
||||
*
|
||||
* Coach conversations and the facts the Coach remembered are the person's
|
||||
* own records, not caches. Before v1.39 Settings → Coach hid the memory card
|
||||
* together with the Coach's tuning as soon as the Coach was hidden, and with
|
||||
* the Coach module off the whole page was out of reach, so a person could
|
||||
* neither see nor erase what had been stored. Now:
|
||||
*
|
||||
* - Settings → Coach always shows the memory (facts and conversations),
|
||||
* whatever `disableCoach` says;
|
||||
* - Settings → AI shows the same cards through `<StoredCoachMemory>` while
|
||||
* Settings → Coach is not reachable, and only when rows exist.
|
||||
*/
|
||||
import { renderToStaticMarkup } from "react-dom/server";
|
||||
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
const authState = vi.hoisted(() => ({ disableCoach: true }));
|
||||
vi.mock("@/hooks/use-auth", () => ({
|
||||
useAuth: () => ({
|
||||
user: {
|
||||
id: "u1",
|
||||
role: "USER",
|
||||
timezone: "Europe/Berlin",
|
||||
disableCoach: authState.disableCoach,
|
||||
},
|
||||
isAuthenticated: true,
|
||||
isLoading: false,
|
||||
refetch: vi.fn(),
|
||||
}),
|
||||
}));
|
||||
// Past hydration: the section's own gates read the account only once
|
||||
// mounted, and a static render is otherwise the pre-hydration pass, where
|
||||
// every card shows and the test could not tell a gate from its absence.
|
||||
vi.mock("@/hooks/use-mounted", () => ({ useMounted: () => true }));
|
||||
vi.mock("next/navigation", () => ({
|
||||
useRouter: () => ({ push: vi.fn(), replace: vi.fn() }),
|
||||
usePathname: () => "/settings/coach",
|
||||
}));
|
||||
|
||||
import { I18nProvider } from "@/lib/i18n/context";
|
||||
import { queryKeys } from "@/lib/query-keys";
|
||||
|
||||
import { CoachSection } from "../coach-section";
|
||||
import { StoredCoachMemory } from "../coach-memory-section";
|
||||
|
||||
const FACT = {
|
||||
id: "fact-1",
|
||||
category: "goal",
|
||||
text: "Wants to walk to work twice a week",
|
||||
confidence: 0.9,
|
||||
createdAt: "2026-09-01T08:00:00.000Z",
|
||||
};
|
||||
const CONVERSATION = {
|
||||
id: "conv-1",
|
||||
title: "Morning blood pressure",
|
||||
createdAt: "2026-09-01T08:00:00.000Z",
|
||||
updatedAt: "2026-09-01T08:05:00.000Z",
|
||||
messageCount: 4,
|
||||
fenced: false,
|
||||
attachments: [],
|
||||
};
|
||||
|
||||
function client(stored: { facts: unknown[]; conversations: unknown[] }) {
|
||||
const c = new QueryClient({
|
||||
defaultOptions: { queries: { retry: 0, staleTime: Infinity } },
|
||||
});
|
||||
c.setQueryData(queryKeys.coachFacts(), stored.facts);
|
||||
c.setQueryData(queryKeys.coachConversationHistory(""), {
|
||||
pages: [{ conversations: stored.conversations, nextCursor: null }],
|
||||
pageParams: [null],
|
||||
});
|
||||
return c;
|
||||
}
|
||||
|
||||
function render(
|
||||
node: React.ReactNode,
|
||||
stored: { facts: unknown[]; conversations: unknown[] },
|
||||
) {
|
||||
return renderToStaticMarkup(
|
||||
<QueryClientProvider client={client(stored)}>
|
||||
<I18nProvider initialLocale="en">{node}</I18nProvider>
|
||||
</QueryClientProvider>,
|
||||
);
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
// Everything is seeded; a background refetch must not reach the network.
|
||||
vi.stubGlobal(
|
||||
"fetch",
|
||||
vi.fn(async () => new Response("{}", { status: 500 })),
|
||||
);
|
||||
});
|
||||
afterEach(() => vi.unstubAllGlobals());
|
||||
|
||||
describe("Settings → Coach keeps the Coach memory while the Coach is hidden", () => {
|
||||
it("shows the facts and the conversations with Hide Coach on", () => {
|
||||
authState.disableCoach = true;
|
||||
const html = render(<CoachSection />, {
|
||||
facts: [FACT],
|
||||
conversations: [CONVERSATION],
|
||||
});
|
||||
expect(html).toContain('data-testid="settings-coach-memory-card"');
|
||||
expect(html).toContain("Wants to walk to work twice a week");
|
||||
expect(html).toContain('data-testid="settings-coach-conversations-card"');
|
||||
expect(html).toContain("Morning blood pressure");
|
||||
// Deletable: the per-row controls are there.
|
||||
expect(html).toContain('data-slot="settings-coach-memory-forget"');
|
||||
expect(html).toContain('data-slot="settings-coach-conversation-delete"');
|
||||
// The Coach's tuning stays hidden with the Coach.
|
||||
expect(html).not.toContain('id="coach-nudge"');
|
||||
});
|
||||
});
|
||||
|
||||
describe("<StoredCoachMemory> (Settings → AI while the Coach page is out of reach)", () => {
|
||||
it("shows what is stored, readable and deletable", () => {
|
||||
const html = render(<StoredCoachMemory isAuthenticated />, {
|
||||
facts: [FACT],
|
||||
conversations: [CONVERSATION],
|
||||
});
|
||||
expect(html).toContain('data-testid="settings-coach-memory-card"');
|
||||
expect(html).toContain('data-testid="settings-coach-conversations-card"');
|
||||
expect(html).toContain('data-slot="settings-coach-conversation-open"');
|
||||
expect(html).toContain('data-slot="settings-coach-conversation-delete"');
|
||||
});
|
||||
|
||||
it("shows only the card that has rows", () => {
|
||||
const html = render(<StoredCoachMemory isAuthenticated />, {
|
||||
facts: [],
|
||||
conversations: [CONVERSATION],
|
||||
});
|
||||
expect(html).not.toContain('data-testid="settings-coach-memory-card"');
|
||||
expect(html).toContain('data-testid="settings-coach-conversations-card"');
|
||||
});
|
||||
|
||||
it("renders nothing for somebody the Coach never stored anything for", () => {
|
||||
const html = render(<StoredCoachMemory isAuthenticated />, {
|
||||
facts: [],
|
||||
conversations: [],
|
||||
});
|
||||
expect(html).toBe("");
|
||||
});
|
||||
});
|
||||
@@ -16,6 +16,7 @@ vi.mock("next/navigation", () => ({
|
||||
|
||||
vi.mock("@tanstack/react-query", () => ({
|
||||
useQuery: () => ({ data: null, isLoading: false, refetch: vi.fn() }),
|
||||
useInfiniteQuery: () => ({ data: undefined, isLoading: false }),
|
||||
useMutation: () => ({ mutate: vi.fn(), isPending: false }),
|
||||
useQueryClient: () => ({ invalidateQueries: vi.fn() }),
|
||||
}));
|
||||
|
||||
@@ -13,8 +13,13 @@ vi.mock("@/hooks/use-auth", () => ({
|
||||
user: { modules: modulesRef.value },
|
||||
}),
|
||||
}));
|
||||
vi.mock("@/hooks/use-feature-flags", () => ({
|
||||
useFeatureFlags: () => ({ briefing: true }),
|
||||
const briefingRef: { available: boolean } = { available: true };
|
||||
vi.mock("@/hooks/use-ai-capability", () => ({
|
||||
useAiCapability: () => ({
|
||||
available: briefingRef.available,
|
||||
reason: briefingRef.available ? null : "user_disabled",
|
||||
onDeviceAllowed: false,
|
||||
}),
|
||||
}));
|
||||
vi.mock("@/hooks/use-insights-layout", () => ({
|
||||
useInsightsLayoutQuery: () => ({
|
||||
@@ -63,4 +68,12 @@ describe("<InsightsOverviewArrangeSection> module gate", () => {
|
||||
it("marks nothing for AI analysis off", () => {
|
||||
expect(gatedFor({ insights: false })).toEqual([]);
|
||||
});
|
||||
|
||||
it("marks only the daily briefing when the briefing is unavailable", () => {
|
||||
// The period review renders its composed narrative whatever the AI
|
||||
// state, so it stays a live row.
|
||||
briefingRef.available = false;
|
||||
expect(gatedFor({})).toEqual(["daily-briefing"]);
|
||||
briefingRef.available = true;
|
||||
});
|
||||
});
|
||||
@@ -63,21 +63,18 @@ vi.mock("@/hooks/use-auth", async () => {
|
||||
return { ...actual, useAuth: () => authSpy() };
|
||||
});
|
||||
|
||||
// The hub now renders a live coach switch, so it reads the operator assistant
|
||||
// flag matrix (`flags.coach`) to decide whether the operator killed Coach
|
||||
// server-wide. Mock the hook directly so the SSR pass doesn't need a
|
||||
// `<QueryClientProvider>`; default all-on.
|
||||
import type { AssistantFlagSet } from "@/hooks/use-feature-flags";
|
||||
const ALL_ON_FLAGS: AssistantFlagSet = {
|
||||
enabled: true,
|
||||
coach: true,
|
||||
briefing: true,
|
||||
insightStatus: true,
|
||||
documentAi: true,
|
||||
// The hub renders a live coach switch and reads the `coach` AI capability to
|
||||
// decide whether the operator switched the Coach off server-wide. Mock the
|
||||
// hook directly so the SSR pass doesn't need a `<QueryClientProvider>`.
|
||||
import type { AiCapabilityState } from "@/lib/ai/capabilities/types";
|
||||
const COACH_AVAILABLE: AiCapabilityState = {
|
||||
available: true,
|
||||
reason: null,
|
||||
onDeviceAllowed: true,
|
||||
};
|
||||
const flagsSpy = vi.fn<() => AssistantFlagSet>(() => ALL_ON_FLAGS);
|
||||
vi.mock("@/hooks/use-feature-flags", () => ({
|
||||
useFeatureFlags: () => flagsSpy(),
|
||||
const coachSpy = vi.fn<() => AiCapabilityState>(() => COACH_AVAILABLE);
|
||||
vi.mock("@/hooks/use-ai-capability", () => ({
|
||||
useAiCapability: () => coachSpy(),
|
||||
}));
|
||||
|
||||
function buildUser(
|
||||
@@ -133,7 +130,7 @@ beforeEach(() => {
|
||||
user: buildUser({}),
|
||||
isAuthenticated: true,
|
||||
}));
|
||||
flagsSpy.mockImplementation(() => ALL_ON_FLAGS);
|
||||
coachSpy.mockImplementation(() => COACH_AVAILABLE);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
@@ -295,7 +292,11 @@ describe("<ModulesSection>", () => {
|
||||
|
||||
it("disables the switch + shows a hint when the operator killed Coach", () => {
|
||||
// Operator assistant master flag off → coach is unavailable server-wide.
|
||||
flagsSpy.mockImplementation(() => ({ ...ALL_ON_FLAGS, coach: false }));
|
||||
coachSpy.mockImplementation(() => ({
|
||||
available: false,
|
||||
reason: "operator_disabled",
|
||||
onDeviceAllowed: false,
|
||||
}));
|
||||
const html = render();
|
||||
const coachTag = html.match(
|
||||
/<button[^>]*id="module-toggle-coach"[^>]*>/,
|
||||
|
||||
@@ -45,6 +45,22 @@ vi.mock("@tanstack/react-query", () => ({
|
||||
useMutation: () => ({ mutate: vi.fn(), isPending: false }),
|
||||
}));
|
||||
|
||||
// The AI capability map on `/api/auth/me`: everything available, a provider
|
||||
// configured, nothing switched off by the operator.
|
||||
vi.mock("@/hooks/use-ai-capability", () => ({
|
||||
useAiCapability: () => ({
|
||||
available: true,
|
||||
reason: null,
|
||||
onDeviceAllowed: true,
|
||||
}),
|
||||
useAiCapabilityMap: () => null,
|
||||
useAiProviderState: () => ({
|
||||
configured: true,
|
||||
managedBy: "user",
|
||||
canConfigure: true,
|
||||
}),
|
||||
}));
|
||||
|
||||
// Stub the auth hook — sections check `isAuthenticated` to enable queries.
|
||||
// v1.16.4 — AccountSection gates its content on `useMounted()` so the
|
||||
// SSR pass and the hydration render agree (React #418 family). The
|
||||
|
||||
Reference in new issue
Block a user