mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-17 23:52:24 +00:00
Tell the four document-summary outcomes apart
Opening a document said "the summary is being prepared" and kept saying it. The view had one fallback for a null summary column and used it for every outcome: a document nobody ever enqueued, a missing vision provider, a spent budget, a withdrawn consent, an unreadable file, a provider error, and a summary the outbound screen withheld all rendered as work in progress. The common case is not the screen. The background job is enqueued at exactly one place — a fresh upload with the auto-read opt-in already on — and no backfill reaches anything else. Every document uploaded before the toggle went on has a null column no job will ever fill, and the view promised all of them a summary that was never coming. Record the outcome as `summary_state` (migration 0254, additive, defaults to NONE which is truthful for every existing row) and branch on it. Only PENDING, set when a job genuinely exists, may claim generation is running. NONE, WITHHELD and UNAVAILABLE each say what happened and offer to generate one. The withhold keeps its rule and loses its dead end. Blocked prose is still never shown and never stored — only the fact of the refusal is kept, and it is no longer terminal, so asking again re-runs the gauntlet. That was the original intent; leaving the column null defeated it, because null read as "generating" and the guard meant nothing would ever overwrite it. The on-demand summary now persists what it produces. It is the only repair path for the documents the job never ran for, and re-deriving the same paragraph on every open costs a provider call for nothing. Generation is still only ever a click — nothing runs because a view rendered. Transcription stays session-only. Also drop the "read by AI" line from the detail footer.
This commit is contained in:
1 parent
e6e2210314
commit
d773f460e5
19 files changed
+713
-82
No files matched your search
@@ -0,0 +1,141 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { renderToStaticMarkup } from "react-dom/server";
|
||||
|
||||
import { I18nProvider } from "@/lib/i18n/context";
|
||||
import type { DocumentSummaryStateValue } from "@/lib/validations/inbound-documents";
|
||||
import { DocumentSummaryBlock } from "../document-summary-block";
|
||||
|
||||
/**
|
||||
* The detail view used to read a null summary as "being generated" and say so
|
||||
* forever — for a document nobody ever enqueued as readily as for one actually
|
||||
* in flight. These pin the states apart: only PENDING may claim generation is
|
||||
* running, a stored summary comes from storage, and every dead end offers a way
|
||||
* out.
|
||||
*/
|
||||
|
||||
function render(node: React.ReactNode) {
|
||||
return renderToStaticMarkup(
|
||||
<I18nProvider initialLocale="en">{node}</I18nProvider>,
|
||||
);
|
||||
}
|
||||
|
||||
const noop = () => {};
|
||||
|
||||
function base(
|
||||
overrides: Partial<React.ComponentProps<typeof DocumentSummaryBlock>> = {},
|
||||
) {
|
||||
const props: React.ComponentProps<typeof DocumentSummaryBlock> = {
|
||||
summary: null,
|
||||
summaryState: "NONE",
|
||||
generatedAtLabel: null,
|
||||
aiEnabled: true,
|
||||
isGenerating: false,
|
||||
actionsDisabled: false,
|
||||
onGenerate: noop,
|
||||
...overrides,
|
||||
};
|
||||
return render(<DocumentSummaryBlock {...props} />);
|
||||
}
|
||||
|
||||
/** Every state that has no summary to show. */
|
||||
const ABSENT_STATES: DocumentSummaryStateValue[] = [
|
||||
"NONE",
|
||||
"WITHHELD",
|
||||
"UNAVAILABLE",
|
||||
];
|
||||
|
||||
describe("DocumentSummaryBlock — a stored summary", () => {
|
||||
it("renders the stored summary from storage, with its date", () => {
|
||||
const html = base({
|
||||
summary: "A discharge letter from a city hospital.",
|
||||
summaryState: "READY",
|
||||
generatedAtLabel: "14 Feb 2026",
|
||||
});
|
||||
|
||||
expect(html).toContain("A discharge letter from a city hospital.");
|
||||
expect(html).toContain("14 Feb 2026");
|
||||
expect(html).toContain('data-slot="document-detail-summary"');
|
||||
// Nothing claims it is being made — it already exists.
|
||||
expect(html).not.toContain("being prepared");
|
||||
expect(html).not.toContain("document-detail-summary-generate");
|
||||
});
|
||||
|
||||
it("shows a stored summary even if the state disagrees", () => {
|
||||
// The bytes are the truth. A stale state must never hide a real summary.
|
||||
const html = base({ summary: "Stored prose.", summaryState: "PENDING" });
|
||||
|
||||
expect(html).toContain("Stored prose.");
|
||||
expect(html).not.toContain("being prepared");
|
||||
});
|
||||
});
|
||||
|
||||
describe("DocumentSummaryBlock — honest absent states", () => {
|
||||
it("claims generation is running ONLY for PENDING", () => {
|
||||
const pending = base({ summaryState: "PENDING" });
|
||||
|
||||
expect(pending).toContain("being prepared");
|
||||
expect(pending).toContain('data-slot="document-detail-summary-pending"');
|
||||
// A job is running; offering a second one would double-spend.
|
||||
expect(pending).not.toContain("document-detail-summary-generate");
|
||||
});
|
||||
|
||||
it.each(ABSENT_STATES)(
|
||||
"never says 'being generated' for %s",
|
||||
(summaryState) => {
|
||||
const html = base({ summaryState });
|
||||
|
||||
expect(html).not.toContain("being prepared");
|
||||
expect(html).toContain('data-slot="document-detail-summary-absent"');
|
||||
},
|
||||
);
|
||||
|
||||
it("says a summary was never generated for NONE", () => {
|
||||
expect(base({ summaryState: "NONE" })).toContain(
|
||||
"No summary has been generated for this document yet.",
|
||||
);
|
||||
});
|
||||
|
||||
it("says the summary was withheld, and never shows blocked prose", () => {
|
||||
const html = base({ summaryState: "WITHHELD" });
|
||||
|
||||
expect(html).toContain("did not pass the safety check");
|
||||
// The withheld state carries no summary text — that is the whole contract.
|
||||
expect(html).not.toContain("document-detail-summary"");
|
||||
});
|
||||
|
||||
it("says none could be produced for UNAVAILABLE", () => {
|
||||
expect(base({ summaryState: "UNAVAILABLE" })).toContain(
|
||||
"No summary could be generated for this document.",
|
||||
);
|
||||
});
|
||||
|
||||
it.each(ABSENT_STATES)("offers a way out of %s", (summaryState) => {
|
||||
const html = base({ summaryState });
|
||||
|
||||
expect(html).toContain('data-slot="document-detail-summary-generate"');
|
||||
expect(html).toContain("Generate summary");
|
||||
});
|
||||
});
|
||||
|
||||
describe("DocumentSummaryBlock — the action", () => {
|
||||
it("states the outcome but offers no action without a provider", () => {
|
||||
const html = base({ summaryState: "UNAVAILABLE", aiEnabled: false });
|
||||
|
||||
// The state is still honest; only the button that would 422 is gone.
|
||||
expect(html).toContain("No summary could be generated for this document.");
|
||||
expect(html).not.toContain("document-detail-summary-generate");
|
||||
});
|
||||
|
||||
it("disables the action while generating, and says so", () => {
|
||||
const html = base({ summaryState: "NONE", isGenerating: true });
|
||||
|
||||
expect(html).toContain("disabled");
|
||||
expect(html).toContain("Generating");
|
||||
});
|
||||
|
||||
it("disables the action while the capability probe resolves", () => {
|
||||
expect(base({ summaryState: "NONE", actionsDisabled: true })).toContain(
|
||||
"disabled",
|
||||
);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user