fix(medications): make import replay results honest

This commit is contained in:
Marc-André Bombeck committed 2026-07-29 16:11:45 +02:00
1 parent 5b695f43ea
commit 271f0ed28a
25 files changed
+607 -27

No files matched your search

@@ -73,6 +73,55 @@ describe("<IntakeImportResultView> — an import that wrote nothing", () => {
expect(html).toContain("another entry in the same file");
expect(countListItems(html)).toBe(2);
});
it("calls an all-existing replay already recorded instead of a failed import", () => {
const html = render({
kind: "outcome",
imported: 0,
skipped: 3386,
skipReasons: [{ reason: "already_recorded", count: 3386 }],
});
expect(html).toContain("3386 intakes are already recorded");
expect(html).toContain("Nothing new was added");
expect(html).not.toContain("Nothing was imported");
});
it("uses the already-recorded headline when preflight refusals also exist", () => {
const html = render({
kind: "outcome",
imported: 0,
skipped: 3395,
skipReasons: [
{ reason: "already_recorded", count: 3386 },
{ reason: "status_no_dose_information", count: 8 },
{ reason: "duplicate_in_file", count: 1 },
],
});
expect(html).toContain("3386 intakes are already recorded");
expect(html).toContain("8 entries skipped");
expect(html).toContain("1 entry skipped");
expect(html).not.toContain("Nothing was imported");
});
it("shows bounded line-and-reason detail without health data", () => {
const html = render({
kind: "outcome",
imported: 0,
skipped: 3,
skipReasons: [{ reason: "already_recorded", count: 3 }],
skipDetails: [
{ line: 2, reason: "already_recorded" },
{ line: 9, reason: "already_recorded" },
],
skippedDetailsOmitted: 1,
});
expect(html).toContain('data-testid="intake-import-skip-details"');
expect(html).toContain("Line 2");
expect(html).toContain("Line 9");
expect(html).toContain("1 more skipped rows");
expect(html).not.toContain("medicationId");
expect(html).not.toContain("takenAt");
});
});
describe("<IntakeImportResultView> — the other outcomes", () => {
@@ -43,6 +43,8 @@ export type IntakeImportResultState =
imported: number;
skipped: number;
skipReasons: IntakeImportSkipGroup[];
skipDetails?: Array<{ line: number; reason: string }>;
skippedDetailsOmitted?: number;
};
/**
@@ -119,6 +121,7 @@ export function IntakeImportResultView({
result: IntakeImportResultState;
}) {
const { t } = useTranslations();
const labelFor = useSkipReasonLabel();
if (result.kind === "notice") {
return (
@@ -136,10 +139,20 @@ export function IntakeImportResultView({
written: result.imported,
skipped: result.skipped,
});
const alreadyRecorded = result.skipReasons.find(
(group) => group.reason === "already_recorded",
);
const replayWithNoWrites =
result.imported === 0 && (alreadyRecorded?.count ?? 0) > 0;
const message = (() => {
if (outcome === "empty") return t("medications.import.resultEmpty");
if (outcome === "failed") {
if (replayWithNoWrites) {
return t("medications.import.resultAlreadyRecorded", {
recorded: alreadyRecorded?.count ?? 0,
});
}
return t("medications.import.resultNothing", { skipped: result.skipped });
}
if (outcome === "partial") {
@@ -159,6 +172,35 @@ export function IntakeImportResultView({
testId="intake-import-outcome"
/>
<IntakeImportSkipGroups groups={result.skipReasons} />
{(result.skipDetails?.length ?? 0) > 0 && (
<details
data-testid="intake-import-skip-details"
className="text-muted-foreground text-xs"
>
<summary className="cursor-pointer">
{t("medications.import.showSkipDetails", {
count: result.skipDetails?.length ?? 0,
})}
</summary>
<ul className="mt-1 max-h-48 space-y-0.5 overflow-y-auto pl-4">
{result.skipDetails?.map((detail) => (
<li key={`${detail.line}:${detail.reason}`}>
{t("medications.import.skipDetailLine", {
line: detail.line,
reason: labelFor(detail.reason),
})}
</li>
))}
</ul>
{(result.skippedDetailsOmitted ?? 0) > 0 && (
<p className="mt-1">
{t("medications.import.skipDetailsOmitted", {
count: result.skippedDetailsOmitted ?? 0,
})}
</p>
)}
</details>
)}
</div>
);
}
@@ -26,7 +26,10 @@ import { AUTO_EXPORT_COLUMN_RULINGS } from "@/lib/medications/import/auto-export
import { IntakeImportSkipGroups } from "@/components/medications/intake-import-result";
import { DoseHistoryColumnRulings } from "../dose-history-columns";
import { DoseHistoryImportCard } from "../dose-history-import-card";
import {
DoseHistoryImportCard,
shouldShowDoseHistoryVerdict,
} from "../dose-history-import-card";
import {
DoseHistoryVerdictView,
type DoseHistoryFileVerdict,
@@ -84,6 +87,21 @@ describe("<DoseHistoryImportCard>", () => {
expect(html).not.toContain('data-outcome="success"');
expect(html).not.toContain("text-success");
});
it("replaces preflight counts with the final result instead of repeating skips", () => {
expect(shouldShowDoseHistoryVerdict(null)).toBe(true);
expect(
shouldShowDoseHistoryVerdict({
kind: "outcome",
imported: 0,
skipped: 9,
skipReasons: [
{ reason: "status_no_dose_information", count: 8 },
{ reason: "duplicate_in_file", count: 1 },
],
}),
).toBe(false);
});
});
describe("<DoseHistoryVerdictView>", () => {
@@ -115,13 +133,15 @@ describe("<DoseHistoryVerdictView>", () => {
expect(html).not.toContain("intake-import-outcome");
});
it("reports the archived rows as imported rather than as a reason to drop them", () => {
it("reports archived rows as file content without claiming they were written", () => {
const html = render(
<DoseHistoryVerdictView verdict={verdict} previewOnly={false} />,
);
expect(html).toContain('data-testid="dose-history-archived"');
expect(html).toContain("1462");
expect(html).toContain("imported anyway");
expect(html).toContain("neither skips nor reactivates");
expect(html).toContain("final result");
expect(html).not.toContain("imported anyway");
// Archived rows are reported, never refused.
expect(html).not.toContain("dose-history-unmatched");
});
@@ -51,6 +51,13 @@ interface DoseHistoryKickoff {
file: DoseHistoryFileVerdict;
}
/** A final result supersedes the preflight counts so skips are stated once. */
export function shouldShowDoseHistoryVerdict(
outcome: IntakeImportResultState | null,
): boolean {
return outcome === null;
}
/**
* Plain language per machine-readable file-level refusal. Literal `t()` calls so
* the i18n call-site guard sees every one.
@@ -182,6 +189,8 @@ export function DoseHistoryImportCard() {
imported: result.imported,
skipped: result.skipped,
skipReasons: result.skipReasons,
skipDetails: result.skipDetails,
skippedDetailsOmitted: result.skippedDetailsOmitted,
});
void invalidateMedicationReads(queryClient);
} catch (err) {
@@ -243,7 +252,7 @@ export function DoseHistoryImportCard() {
<DoseHistoryColumnRulings />
<div aria-live="polite" className="space-y-2">
{verdict && (
{verdict && shouldShowDoseHistoryVerdict(outcome) && (
<DoseHistoryVerdictView verdict={verdict} previewOnly={previewOnly} />
)}
{outcome && <IntakeImportResultView result={outcome} />}
@@ -103,9 +103,12 @@ export function DoseHistoryVerdictView({
className="text-muted-foreground"
data-testid="dose-history-archived"
>
{t("settings.sections.export.import.doseHistory.archivedRows", {
count: verdict.fromArchivedMedications,
})}
{t(
"settings.sections.export.import.doseHistory.archivedRowsPresent",
{
count: verdict.fromArchivedMedications,
},
)}
</p>
)}
</div>