Files
HealthLog/src/lib/ai/__tests__/feedback-attribution.test.ts
T
Marc-André BombeckandClaude Opus 4.7 8255e3e49a feat(api): /api/insights/feedback persists thumbs with provider attribution
v1.4.16 phase B5e: ship the POST endpoint that backs the per-rec
thumbs-up / thumbs-down UI. Body is the four rec-attributes the
client just rendered (id, text-snapshot, severity, metric-source +
time-range) plus the boolean verdict; server-fills providerType +
promptVersion via `resolveFeedbackAttribution()` so the client cannot
poison the daily aggregator's slice by claiming a thumbs-down was
attributed to a rival provider.

Idempotent via the existing `withIdempotency()` wrapper — a retry
with the same Idempotency-Key replays the cached response with
`X-Idempotent-Replay: true`. The dedup unique key on the model
(userId, recommendationId, recommendationText) returns 409 when the
same rec-text is re-rated — that's by design, the aggregator wants
per-text signal not per-id signal so a regeneration that rewrites
the same id with new text re-rates cleanly.

The attribution helper prefers `chainProviderType` over `providerType`
in the latest insights.generate audit row so when the B5b multi-
provider chain falls through to a secondary, the feedback row
reflects the working provider not the configured one. Falls back to
"unknown" + the current PROMPT_VERSION constant when no audit row
exists yet — the aggregator buckets that separately.

Audit row: action `insights.recommendation.feedback`, details carry
the feedback id, severity, helpful, providerType, promptVersion.

Tests:
  - 7 unit tests for the Zod request schema (valid path, severity
    poisoning, time-range poisoning, empty fields, stringified bool,
    extra-field stripping)
  - 5 unit tests for `pickProviderType()` priority + fallback
  - 4 integration tests: 201 happy path with provider attribution,
    409 dedup, 422 validation, fallback to unknown when no audit row

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-10 01:30:06 +02:00

49 lines
1.6 KiB
TypeScript

import { describe, it, expect } from "vitest";
import {
FEEDBACK_ATTRIBUTION_FALLBACK,
pickProviderType,
} from "../feedback-attribution";
import { PROMPT_VERSION } from "@/lib/ai/prompts/insight-generator";
describe("pickProviderType", () => {
it("prefers chainProviderType when present", () => {
// chainProviderType captures the runner's chosen entry across the
// multi-provider chain (B5b). When a fallback fires, the working
// provider differs from the user's primary — the feedback row must
// reflect the working provider, not the configured one.
const got = pickProviderType({
chainProviderType: "openai",
providerType: "codex",
});
expect(got).toBe("openai");
});
it("falls back to providerType when chainProviderType is missing", () => {
const got = pickProviderType({ providerType: "codex" });
expect(got).toBe("codex");
});
it("falls back to 'unknown' when neither field is present", () => {
const got = pickProviderType({});
expect(got).toBe("unknown");
});
it("ignores non-string values", () => {
const got = pickProviderType({
chainProviderType: 123 as unknown as string,
providerType: { name: "evil" } as unknown as string,
});
expect(got).toBe("unknown");
});
});
describe("FEEDBACK_ATTRIBUTION_FALLBACK", () => {
it("uses 'unknown' for the providerType so the aggregator groups separately", () => {
expect(FEEDBACK_ATTRIBUTION_FALLBACK.providerType).toBe("unknown");
});
it("snapshots the current PROMPT_VERSION so the row is persistable", () => {
expect(FEEDBACK_ATTRIBUTION_FALLBACK.promptVersion).toBe(PROMPT_VERSION);
});
});