From 8c34df8be3d67ee7817479e66d6bbc63aaf8b0c7 Mon Sep 17 00:00:00 2001 From: Preston Cabe Date: Tue, 27 Oct 2026 18:16:30 -0400 Subject: [PATCH 01/11] List unanswered questions for undecided benefits and printouts --- builder-frontend/README.md | 16 ++ .../src/components/screener/FormRenderer.tsx | 14 +- .../nextSteps/NextSteps.test.ts | 28 ++++ .../formJsExtensions/nextSteps/NextSteps.ts | 14 +- .../printSummary/PrintSummary.test.ts | 3 +- .../printSummary/PrintSummary.ts | 1 + .../screeningResults.integration.test.tsx | 32 +++- .../screeningResults/screeningResults.test.ts | 16 ++ .../screeningResults/screeningResults.ts | 25 ++- .../screenerEditor/preview/FormRenderer.tsx | 14 +- .../src/utils/unansweredQuestions.test.ts | 145 ++++++++++++++++++ .../src/utils/unansweredQuestions.ts | 78 ++++++++++ e2e/tests/nextStepsPublic.spec.ts | 66 ++++++-- 13 files changed, 434 insertions(+), 18 deletions(-) create mode 100644 builder-frontend/src/utils/unansweredQuestions.test.ts create mode 100644 builder-frontend/src/utils/unansweredQuestions.ts diff --git a/builder-frontend/README.md b/builder-frontend/README.md index 7ad9a04c..51f3ed52 100644 --- a/builder-frontend/README.md +++ b/builder-frontend/README.md @@ -104,3 +104,19 @@ The component renders sanitized Markdown without template evaluation. Web links accept HTTP(S) URLs and open in a new tab. Pending or failed evaluations hide old instructions until fresh decisions arrive. Forms containing the component use its progress heading; existing forms without it keep the legacy completion message. + +## Unanswered questions in Next steps + +For an undecided benefit, Next steps lists the form's question labels for missing +inputs used by that benefit's undecided checks. Shared inputs are deduplicated; +inputs used only by decided checks are omitted. Zero, No, and explicit None +answers remain answered. The list uses the same normalized data sent to the +eligibility service, including unknown CRM prefill preserved as null. + +Published screeners, Preview, and the print summary share these labels. Pending +or failed evaluations suppress old prompts along with old instructions, and +prompts disappear when the benefit is decided. Labels are kept separately from +answers in the results snapshot; answers and technical input paths are never +printed by this feature. If an input has no static form label, Next steps keeps +its general need-more-information message rather than inventing a question. +The CRM `nextSteps` extension continues to contain decided benefits only. diff --git a/builder-frontend/src/components/screener/FormRenderer.tsx b/builder-frontend/src/components/screener/FormRenderer.tsx index c77f83cc..ac766268 100644 --- a/builder-frontend/src/components/screener/FormRenderer.tsx +++ b/builder-frontend/src/components/screener/FormRenderer.tsx @@ -1,4 +1,11 @@ -import { Accessor, createEffect, on, onCleanup, onMount } from "solid-js"; +import { + Accessor, + createMemo, + createEffect, + on, + onCleanup, + onMount, +} from "solid-js"; import debounce from "lodash.debounce"; import cloneDeep from "lodash/cloneDeep"; @@ -17,6 +24,7 @@ import { type NextStepsProvider, } from "@/integrations/nextSteps"; import type { ScreenerResult } from "@/types"; +import { unansweredQuestions } from "@/utils/unansweredQuestions"; import { hideQuestions } from "@/utils/questionVotes"; import { normalizeArrayFieldData } from "@/utils/arrayFieldData"; import { @@ -90,12 +98,16 @@ function FormRenderer({ container, additionalModules: [CustomFormFieldsModule, screeningResultsModule], }); + const missingQuestions = createMemo(() => + unansweredQuestions(schema, formData(), results()), + ); connectScreeningResults(form, { results, screenerName, pending, failed, onNextStepsShownChange, + unansweredQuestions: missingQuestions, }); currentData = cloneDeep(formData()); diff --git a/builder-frontend/src/components/screenerEditor/formJsExtensions/nextSteps/NextSteps.test.ts b/builder-frontend/src/components/screenerEditor/formJsExtensions/nextSteps/NextSteps.test.ts index fd0d5632..beb56805 100644 --- a/builder-frontend/src/components/screenerEditor/formJsExtensions/nextSteps/NextSteps.test.ts +++ b/builder-frontend/src/components/screenerEditor/formJsExtensions/nextSteps/NextSteps.test.ts @@ -96,6 +96,34 @@ it("shows decided benefits immediately, preserves answers/schema, and lists unde expect((form as any)._getState().data).toEqual({}); }); +it("lists missing questions for undecided benefits and hides stale questions while pending", async () => { + const { container, service } = await mount(); + const current = result(); + service.update(current, false, false, { + food: ["Your date of birth", "Annual income"], + }); + await vi.waitFor(() => + expect(container.textContent).toContain("Find answers to these questions"), + ); + expect(container.textContent).toContain("Your date of birth"); + service.update(current, true, false, { food: ["Your date of birth"] }); + await vi.waitFor(() => + expect(container.textContent).not.toContain("Your date of birth"), + ); + service.update( + { ...current, food: { ...current.food, result: "TRUE" } }, + false, + false, + {}, + ); + await vi.waitFor(() => + expect(container.textContent).toContain("Screening complete"), + ); + expect(container.textContent).not.toContain( + "Find answers to these questions", + ); +}); + it("uses failed check aliases with name fallback and the analyst's note", async () => { const { container, service } = await mount(); const results = result("FALSE"); diff --git a/builder-frontend/src/components/screenerEditor/formJsExtensions/nextSteps/NextSteps.ts b/builder-frontend/src/components/screenerEditor/formJsExtensions/nextSteps/NextSteps.ts index 71860c8f..866512da 100644 --- a/builder-frontend/src/components/screenerEditor/formJsExtensions/nextSteps/NextSteps.ts +++ b/builder-frontend/src/components/screenerEditor/formJsExtensions/nextSteps/NextSteps.ts @@ -7,6 +7,7 @@ import { Text, } from "@bpmn-io/form-js-viewer"; import type { BenefitResult } from "@/types"; +import type { UnansweredQuestions } from "@/utils/unansweredQuestions"; import { useScreeningResults } from "../screeningResults/useScreeningResults"; import { NEXT_STEPS_TYPE, @@ -106,7 +107,8 @@ export function NextSteps({ field }: { field: NextStepsField }) { () => getService("screeningResults", false)?.showNextSteps(field.id), [getService, field.id], ); - const { results, pending, failed, mode } = useScreeningResults(); + const { results, pending, failed, mode, unansweredQuestions } = + useScreeningResults(); const benefits = Object.entries(results ?? {}); const complete = benefits.length > 0 && benefits.every(([, benefit]) => isDecided(benefit)); @@ -124,6 +126,7 @@ export function NextSteps({ field }: { field: NextStepsField }) { pending=${pending} failed=${failed} mode=${mode} + unansweredQuestions=${unansweredQuestions} /> `; } @@ -136,6 +139,7 @@ export function NextStepsBody({ failed, mode, forPrint = false, + unansweredQuestions, }: { field: NextStepsField; benefits: [string, BenefitResult][]; @@ -144,6 +148,7 @@ export function NextStepsBody({ failed: boolean; mode: "live" | "editor"; forPrint?: boolean; + unansweredQuestions?: UnansweredQuestions; }) { if (failed) return html`
@@ -186,6 +191,13 @@ export function NextStepsBody({ return html`

${benefit.name}

Still needs more information.

+ ${!!unansweredQuestions?.[id]?.length && + html` +

Find answers to these questions to continue:

+
    + ${unansweredQuestions[id].map((label) => html`
  • ${label}
  • `)} +
+ `}
`; if (hideDecided) return null; const unmet = steps.unmetRequirements; diff --git a/builder-frontend/src/components/screenerEditor/formJsExtensions/printSummary/PrintSummary.test.ts b/builder-frontend/src/components/screenerEditor/formJsExtensions/printSummary/PrintSummary.test.ts index 0bf0e288..c3c5ec3e 100644 --- a/builder-frontend/src/components/screenerEditor/formJsExtensions/printSummary/PrintSummary.test.ts +++ b/builder-frontend/src/components/screenerEditor/formJsExtensions/printSummary/PrintSummary.test.ts @@ -96,7 +96,7 @@ it("prints current results and copied content with full URLs, without answers, a const { button, service, container } = await mount(); expect(button.disabled).toBe(true); service.setScreenerName("Local benefit screener"); - service.update(results, false); + service.update(results, false, false, { utility: ["Your date of birth"] }); await vi.waitFor(() => expect(button.disabled).toBe(false)); let root!: HTMLElement; let printing = false; @@ -115,6 +115,7 @@ it("prints current results and copied content with full URLs, without answers, a expect(root.textContent).toContain("Income limit"); expect(root.textContent).toContain("Ask about other help"); expect(root.textContent).toContain("Still needs more information"); + expect(root.textContent).toContain("Your date of birth"); expect(root.textContent).toContain( "https://example.org/apply?program=housing", ); diff --git a/builder-frontend/src/components/screenerEditor/formJsExtensions/printSummary/PrintSummary.ts b/builder-frontend/src/components/screenerEditor/formJsExtensions/printSummary/PrintSummary.ts index 2a7bc7d9..17a9c500 100644 --- a/builder-frontend/src/components/screenerEditor/formJsExtensions/printSummary/PrintSummary.ts +++ b/builder-frontend/src/components/screenerEditor/formJsExtensions/printSummary/PrintSummary.ts @@ -111,6 +111,7 @@ export function PrintSummary() { failed=${false} mode="live" forPrint=${true} + unansweredQuestions=${current.unansweredQuestions} /> `, root, diff --git a/builder-frontend/src/components/screenerEditor/formJsExtensions/screeningResults/screeningResults.integration.test.tsx b/builder-frontend/src/components/screenerEditor/formJsExtensions/screeningResults/screeningResults.integration.test.tsx index a1307b2e..795efea1 100644 --- a/builder-frontend/src/components/screenerEditor/formJsExtensions/screeningResults/screeningResults.integration.test.tsx +++ b/builder-frontend/src/components/screenerEditor/formJsExtensions/screeningResults/screeningResults.integration.test.tsx @@ -67,7 +67,7 @@ describe.each(["published", "preview"])( document.body.replaceChildren(); }); - async function mount() { + async function mount(data = { answer: "Keep this answer" }) { const [results, setResults] = createSignal(); const [pending, setPending] = createSignal(false); const [failed, setFailed] = createSignal(false); @@ -75,7 +75,7 @@ describe.each(["published", "preview"])( document.createElement("div"), ); const common = { - formData: () => ({ answer: "Keep this answer" }), + formData: () => data, hiddenQuestionPaths: () => [], submitForm: vi.fn(), results, @@ -110,6 +110,34 @@ describe.each(["published", "preview"])( }; } + it("maps unanswered inputs to question labels and clears them after a decision", async () => { + const s = await mount({ answer: "" }); + s.setResults({ + benefit: { + name: "Benefit", + result: "UNABLE_TO_DETERMINE", + check_results: { + missing: { + result: "UNABLE_TO_DETERMINE", + inputPaths: ["answer"], + } as any, + }, + }, + }); + await vi.waitFor(() => + expect(s.snapshot().unansweredQuestions).toEqual({ + benefit: ["Answer"], + }), + ); + expect(s.snapshot()).not.toHaveProperty("answers"); + s.setResults({ + benefit: { name: "Benefit", result: "TRUE", check_results: {} }, + }); + await vi.waitFor(() => + expect(s.snapshot().unansweredQuestions).toEqual({}), + ); + }); + it("updates custom fields without reimporting the schema or submitting answers", async () => { const s = await mount(); const importSchema = vi.spyOn(s.form, "importSchema"); diff --git a/builder-frontend/src/components/screenerEditor/formJsExtensions/screeningResults/screeningResults.test.ts b/builder-frontend/src/components/screenerEditor/formJsExtensions/screeningResults/screeningResults.test.ts index 588b7e16..3ed90ef4 100644 --- a/builder-frontend/src/components/screenerEditor/formJsExtensions/screeningResults/screeningResults.test.ts +++ b/builder-frontend/src/components/screenerEditor/formJsExtensions/screeningResults/screeningResults.test.ts @@ -25,6 +25,22 @@ const decided: ScreenerResult = { }; describe("screening results service", () => { + it("publishes immutable question labels without storing the evaluated answers", () => { + const { service } = setup(); + const questions = { benefit: ["Annual income"] }; + service.update(decided, false, false, questions); + questions.benefit.push("Later edit"); + expect(service.getSnapshot().unansweredQuestions).toEqual({ + benefit: ["Annual income"], + }); + expect(() => + (service.getSnapshot().unansweredQuestions!.benefit as string[]).push( + "Edit", + ), + ).toThrow(TypeError); + service.update(decided, false); + expect(service.getSnapshot().unansweredQuestions).toBeUndefined(); + }); it("keeps the screener name local to the form without notifying results subscribers", () => { const { service } = setup(); const other = setup().service; diff --git a/builder-frontend/src/components/screenerEditor/formJsExtensions/screeningResults/screeningResults.ts b/builder-frontend/src/components/screenerEditor/formJsExtensions/screeningResults/screeningResults.ts index bc35b2f6..429a8661 100644 --- a/builder-frontend/src/components/screenerEditor/formJsExtensions/screeningResults/screeningResults.ts +++ b/builder-frontend/src/components/screenerEditor/formJsExtensions/screeningResults/screeningResults.ts @@ -1,5 +1,6 @@ import { Accessor, createEffect } from "solid-js"; import cloneDeep from "lodash/cloneDeep"; +import type { UnansweredQuestions } from "@/utils/unansweredQuestions"; import type { BenefitDetail, BenefitResult, @@ -17,6 +18,8 @@ export interface ScreeningResultsSnapshot { /** True when the latest evaluation failed, until one succeeds. Pending stays true meanwhile. */ readonly failed: boolean; readonly mode: "live" | "editor"; + /** Question labels still needed for undecided benefits; contains no answers. */ + readonly unansweredQuestions?: UnansweredQuestions; } interface EventBus { @@ -41,6 +44,8 @@ export class ScreeningResultsService { // Fields get a frozen copy so they can't change the parent's results. private liveSource: ScreenerResult | undefined; private liveCopy: ScreenerResult | undefined; + private questionsSource: UnansweredQuestions | undefined; + private questionsCopy: UnansweredQuestions | undefined; // IDs of the rendered Next steps fields, once per rendering. private nextStepsShown: string[] = []; private screenerName: string | undefined; @@ -93,16 +98,25 @@ export class ScreeningResultsService { results: ScreenerResult | undefined, pending: boolean, failed = false, + unansweredQuestions?: UnansweredQuestions, ): void { if (results !== this.liveSource) { this.liveSource = results; this.liveCopy = results && deepFreeze(cloneDeep(results)); } + if (unansweredQuestions !== this.questionsSource) { + this.questionsSource = unansweredQuestions; + this.questionsCopy = + unansweredQuestions && deepFreeze(cloneDeep(unansweredQuestions)); + } this.publish({ results: this.liveCopy, pending, failed, mode: "live", + ...(this.questionsCopy === undefined + ? {} + : { unansweredQuestions: this.questionsCopy }), }); } @@ -120,7 +134,8 @@ export class ScreeningResultsService { next.results === this.snapshot.results && next.pending === this.snapshot.pending && next.failed === this.snapshot.failed && - next.mode === this.snapshot.mode + next.mode === this.snapshot.mode && + next.unansweredQuestions === this.snapshot.unansweredQuestions ) return; this.snapshot = Object.freeze(next); @@ -210,12 +225,18 @@ export function connectScreeningResults( pending: Accessor; failed: Accessor; screenerName?: Accessor; + unansweredQuestions?: Accessor; onNextStepsShownChange?: (shown: boolean) => void; }, ): void { const service = form.get("screeningResults"); createEffect(() => - service.update(live.results(), live.pending(), live.failed()), + service.update( + live.results(), + live.pending(), + live.failed(), + live.unansweredQuestions?.(), + ), ); createEffect(() => service.setScreenerName(live.screenerName?.())); const { onNextStepsShownChange } = live; diff --git a/builder-frontend/src/components/screenerEditor/preview/FormRenderer.tsx b/builder-frontend/src/components/screenerEditor/preview/FormRenderer.tsx index d829d1b1..e222b594 100644 --- a/builder-frontend/src/components/screenerEditor/preview/FormRenderer.tsx +++ b/builder-frontend/src/components/screenerEditor/preview/FormRenderer.tsx @@ -1,4 +1,11 @@ -import { Accessor, createEffect, on, onCleanup, onMount } from "solid-js"; +import { + Accessor, + createMemo, + createEffect, + on, + onCleanup, + onMount, +} from "solid-js"; import debounce from "lodash.debounce"; import cloneDeep from "lodash/cloneDeep"; import isEqual from "lodash/isEqual"; @@ -12,6 +19,7 @@ import { screeningResultsModule, } from "../formJsExtensions/screeningResults/screeningResults"; import type { ScreenerResult } from "@/types"; +import { unansweredQuestions } from "@/utils/unansweredQuestions"; import { hideQuestions } from "@/utils/questionVotes"; import { normalizeArrayFieldData } from "@/utils/arrayFieldData"; @@ -75,12 +83,16 @@ function FormRenderer({ container, additionalModules: [CustomFormFieldsModule, screeningResultsModule], }); + const missingQuestions = createMemo(() => + unansweredQuestions(schema(), formData(), results()), + ); connectScreeningResults(form, { results, screenerName, pending, failed, onNextStepsShownChange, + unansweredQuestions: missingQuestions, }); currentData = cloneDeep(formData()); diff --git a/builder-frontend/src/utils/unansweredQuestions.test.ts b/builder-frontend/src/utils/unansweredQuestions.test.ts new file mode 100644 index 00000000..099d8156 --- /dev/null +++ b/builder-frontend/src/utils/unansweredQuestions.test.ts @@ -0,0 +1,145 @@ +import { expect, it } from "vitest"; +import { unansweredQuestions } from "./unansweredQuestions"; +import { normalizeArrayFieldData } from "./arrayFieldData"; +import { + findFilledUnknownPaths, + restoreUnknownAnswers, +} from "./unknownAnswers"; +import type { ScreenerResult } from "@/types"; + +const schema = { + components: [ + { key: "custom.income", label: "Annual household income" }, + { key: "custom.resident", label: "Do you live here?" }, + { + key: "custom.programs", + type: "checklist_none", + label: "Current benefit programs", + }, + { + path: "client", + components: [ + { key: "birthdate", subtype: "date", dateLabel: "Your date of birth" }, + ], + }, + ], +}; +const check = (inputPaths: string[], result = "UNABLE_TO_DETERMINE") => + ({ inputPaths, result }) as any; +const results: ScreenerResult = { + housing: { + name: "Housing", + result: "UNABLE_TO_DETERMINE", + check_results: { + a: check([ + "custom.income", + "custom.resident", + "custom.programs", + "client.birthdate", + ]), + b: check(["custom.income"]), + decided: check(["not-needed"], "TRUE"), + }, + }, + decided: { + name: "Decided", + result: "FALSE", + check_results: { a: check(["custom.income"]) }, + }, +}; + +it("lists mapped unanswered questions only for unknown checks of undecided benefits", () => { + expect( + unansweredQuestions( + schema, + { + custom: { income: null, resident: false, programs: [] }, + client: { birthdate: "" }, + }, + results, + ), + ).toEqual({ + housing: ["Annual household income", "Your date of birth"], + }); + expect( + unansweredQuestions( + schema, + { + custom: { income: 0, resident: false, programs: [] }, + client: { birthdate: "1950-01-01" }, + }, + results, + ), + ).toEqual({}); +}); + +it("distinguishes unknown CRM prefill and untouched lists from explicit No and None", () => { + const prefill = { custom: { resident: null } }; + const initialized = { custom: { resident: false, programs: [] } }; + const normalized = normalizeArrayFieldData( + schema, + restoreUnknownAnswers( + initialized, + findFilledUnknownPaths(prefill, initialized), + ), + ); + expect(unansweredQuestions(schema, normalized, results).housing).toContain( + "Do you live here?", + ); + expect(unansweredQuestions(schema, normalized, results).housing).toContain( + "Current benefit programs", + ); + const explicit = normalizeArrayFieldData(schema, { + custom: { resident: false, programs: ["__bdt_none_of_these__"] }, + }); + expect(unansweredQuestions(schema, explicit, results).housing).not.toContain( + "Do you live here?", + ); + expect(unansweredQuestions(schema, explicit, results).housing).not.toContain( + "Current benefit programs", + ); +}); + +it("handles repeated sections and shared votes without exposing paths or values", () => { + const form = { + components: [ + { path: "people", components: [{ key: "age", label: "Age" }] }, + { key: "secret", label: "=secret" }, + { key: "templated", label: "Age of {{ client.name }}" }, + ], + }; + const result: ScreenerResult = { + benefit: { + name: "Benefit", + result: "UNABLE_TO_DETERMINE", + check_results: { + a: check(["people.age", "secret", "templated", "unbound.path"]), + b: check(["people.age"]), + }, + }, + }; + expect( + unansweredQuestions( + form, + { people: [{ age: 42 }, { age: null }], secret: null }, + result, + ), + ).toEqual({ benefit: ["Age"] }); + expect( + unansweredQuestions(form, { people: [{ age: 42 }], secret: null }, result), + ).toEqual({}); + expect(unansweredQuestions(form, { people: [] }, result)).toEqual({ + benefit: ["Age"], + }); +}); + +it("does not prompt for missing inputs in already decided checks", () => { + expect( + unansweredQuestions( + { components: [{ key: "not-needed", label: "Already decided" }] }, + {}, + results, + ), + ).toEqual({}); + expect(unansweredQuestions(schema, {}, undefined)).toEqual({}); +}); diff --git a/builder-frontend/src/utils/unansweredQuestions.ts b/builder-frontend/src/utils/unansweredQuestions.ts new file mode 100644 index 00000000..35a7467a --- /dev/null +++ b/builder-frontend/src/utils/unansweredQuestions.ts @@ -0,0 +1,78 @@ +import type { ScreenerResult } from "@/types"; + +export type UnansweredQuestions = Readonly>; +type Field = { + key?: string; + path?: string; + label?: string; + dateLabel?: string; + timeLabel?: string; + subtype?: string; + components?: Field[]; +}; + +// A repeated section may hold several answers at the same schema path. +function valuesAt(value: unknown, segments: string[]): unknown[] { + if (!segments.length) return [value]; + if (Array.isArray(value)) + return value.length + ? value.flatMap((item) => valuesAt(item, segments)) + : [undefined]; + if (!value || typeof value !== "object") return [undefined]; + const [key, ...rest] = segments; + return valuesAt( + Object.hasOwn(value, key) + ? (value as Record)[key] + : undefined, + rest, + ); +} + +/** Labels only: never expose answers, technical paths, or evaluated label templates. */ +export function unansweredQuestions( + schema: { components?: Field[] }, + evaluatedAnswers: unknown, + results: ScreenerResult | undefined, +): UnansweredQuestions { + const labels = new Map(); + const visit = (fields: Field[], parent = "") => { + for (const field of fields) { + const prefix = [parent, field.path].filter(Boolean).join("."); + const label = + field.label || + (field.subtype === "time" ? field.timeLabel : field.dateLabel); + if ( + field.key && + typeof label === "string" && + label.trim() && + !label.trim().startsWith("=") && + !/{{[\s\S]*?}}/.test(label) + ) { + const path = [prefix, field.key].filter(Boolean).join("."); + labels.set(path, [...(labels.get(path) ?? []), label.trim()]); + } + visit(field.components ?? [], prefix); + } + }; + visit(schema.components ?? []); + return Object.fromEntries( + Object.entries(results ?? {}).flatMap(([id, benefit]) => { + if (benefit.result !== "UNABLE_TO_DETERMINE") return []; + const paths = new Set( + Object.values(benefit.check_results ?? {}) + .filter((check) => check.result === "UNABLE_TO_DETERMINE") + .flatMap((check) => check.inputPaths ?? []), + ); + const questions = new Set(); + for (const path of paths) { + const missing = valuesAt(evaluatedAnswers, path.split(".")).some( + (value) => + value == null || (typeof value === "string" && !value.trim()), + ); + if (missing) + for (const label of labels.get(path) ?? []) questions.add(label); + } + return questions.size ? [[id, [...questions]]] : []; + }), + ); +} diff --git a/e2e/tests/nextStepsPublic.spec.ts b/e2e/tests/nextStepsPublic.spec.ts index 03a50e33..737e6fb4 100644 --- a/e2e/tests/nextStepsPublic.spec.ts +++ b/e2e/tests/nextStepsPublic.spec.ts @@ -30,10 +30,10 @@ for (const showWhen of ["whenDecided", "whenComplete"]) { label: "Household income", }, { - id: "done", - type: "checkbox", - key: "done", - label: "Answer remaining question", + id: "household-size", + type: "number", + key: "householdSize", + label: "Number of people in your household", }, { id: "print", type: "print_summary" }, { @@ -76,8 +76,19 @@ for (const showWhen of ["whenDecided", "whenComplete"]) { }, food: { name: "Food assistance", - result: answers.done ? "TRUE" : "UNABLE_TO_DETERMINE", - check_results: {}, + result: + answers.householdSize == null ? "UNABLE_TO_DETERMINE" : "TRUE", + check_results: { + size: { + name: "Household size", + result: + answers.householdSize == null + ? "UNABLE_TO_DETERMINE" + : "TRUE", + inputPaths: ["householdSize"], + parameters: {}, + }, + }, }, }, }); @@ -105,13 +116,42 @@ for (const showWhen of ["whenDecided", "whenComplete"]) { page.getByRole("link", { name: "Apply for this benefit" }), ).toHaveCount(0); } - await page.getByLabel("Answer remaining question", { exact: true }).check(); + const nextSteps = page.getByRole("region", { + name: "Your next steps", + exact: true, + }); + await expect( + nextSteps.getByText("Number of people in your household", { + exact: true, + }), + ).toBeVisible(); + await page.evaluate(() => { + window.print = () => { + (window as any).partialPrint = + document.querySelector(".bdt-print-root")!.textContent; + window.dispatchEvent(new Event("afterprint")); + }; + }); + await page.getByRole("button", { name: "Print / Save as PDF" }).click(); + const partialPrint = await page.evaluate( + () => (window as any).partialPrint, + ); + expect(partialPrint).toContain("Number of people in your household"); + expect(partialPrint).not.toContain("Household income"); + await page + .getByLabel("Number of people in your household", { exact: true }) + .fill("2"); await expect( page.getByRole("heading", { name: "Screening complete — your next steps", exact: true, }), ).toBeVisible(); + await expect( + nextSteps.getByText("Find answers to these questions to continue:", { + exact: true, + }), + ).toHaveCount(0); const next = page.getByRole("region", { name: "Your next steps", exact: true, @@ -132,7 +172,9 @@ for (const showWhen of ["whenDecided", "whenComplete"]) { (window as any).printed = { text: root.textContent, summaryDisplay: getComputedStyle(root).display, - otherDisplays: Array.from(document.body.children).filter(el => el !== root).map(el => getComputedStyle(el).display), + otherDisplays: Array.from(document.body.children) + .filter((el) => el !== root) + .map((el) => getComputedStyle(el).display), }; }; }); @@ -144,14 +186,18 @@ for (const showWhen of ["whenDecided", "whenComplete"]) { expect(printed.text).toContain("https://example.org/help"); expect(printed.text).not.toContain("Household income"); expect(printed.summaryDisplay).toBe("block"); - expect(printed.otherDisplays.every((display: string) => display === "none")).toBe(true); + expect( + printed.otherDisplays.every((display: string) => display === "none"), + ).toBe(true); // Like mobile browsers, this print() returns before printing ends, so the tree stays // until the print media query stops matching. await expect(page.locator(".bdt-print-root")).toHaveCount(1); await page.emulateMedia({ media: "screen" }); await expect(page.locator(".bdt-print-root")).toHaveCount(0); await income.fill("2000"); - await expect(page.getByRole("button", { name: "Print / Save as PDF" })).toBeDisabled(); + await expect( + page.getByRole("button", { name: "Print / Save as PDF" }), + ).toBeDisabled(); await expect( page.getByText("Updating your next steps…", { exact: true }), ).toBeVisible(); From cc3dc09d3370408807365ec289f41fcb26b149f0 Mon Sep 17 00:00:00 2001 From: Preston Cabe Date: Thu, 29 Oct 2026 15:46:06 -0400 Subject: [PATCH 02/11] Hide unneeded questions by full path, not bare key A nested field sharing its key with an unneeded top-level path was removed from the form while still listed as unanswered, and nested fields were never hidden because checks name them by full path. Co-Authored-By: Claude Opus 5.5 --- .../src/utils/questionVotes.test.ts | 23 +++++++++++++++++++ builder-frontend/src/utils/questionVotes.ts | 21 ++++++++++++++--- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/builder-frontend/src/utils/questionVotes.test.ts b/builder-frontend/src/utils/questionVotes.test.ts index 8b72ad60..b8959943 100644 --- a/builder-frontend/src/utils/questionVotes.test.ts +++ b/builder-frontend/src/utils/questionVotes.test.ts @@ -100,4 +100,27 @@ describe("question votes", () => { expect(filtered.components[1].components).toEqual([]); expect(schema.components[1].components).toHaveLength(1); }); + + it("matches fields by their full path, including group paths", () => { + const schema = { + components: [ + { key: "age", type: "number" }, + { + type: "dynamiclist", + path: "people", + components: [{ key: "age", type: "number" }], + }, + ], + }; + + expect(hideQuestions(schema, ["age"]).components).toEqual([ + schema.components[1], + ]); + expect( + hideQuestions(schema, ["people.age"]).components[1].components, + ).toEqual([]); + expect(hideQuestions(schema, ["people.age"]).components[0]).toEqual( + schema.components[0], + ); + }); }); diff --git a/builder-frontend/src/utils/questionVotes.ts b/builder-frontend/src/utils/questionVotes.ts index 1a23e96a..fc73ee37 100644 --- a/builder-frontend/src/utils/questionVotes.ts +++ b/builder-frontend/src/utils/questionVotes.ts @@ -61,13 +61,28 @@ export function hideQuestions( ): any { const hidden = new Set(hiddenPaths); - const filterComponents = (components: any[] | undefined): any[] => + // Match the full path, as checks name inputs: a group's path prefixes its fields' keys. + const filterComponents = ( + components: any[] | undefined, + parentPath = "", + ): any[] => (components ?? []) - .filter((component) => !component.key || !hidden.has(component.key)) + .filter( + (component) => + !component.key || + !hidden.has( + [parentPath, component.path, component.key] + .filter(Boolean) + .join("."), + ), + ) .map((component) => { const copy = { ...component }; if (Array.isArray(component.components)) { - copy.components = filterComponents(component.components); + copy.components = filterComponents( + component.components, + [parentPath, component.path].filter(Boolean).join("."), + ); } return copy; }); From 9766ebf188fd5b1c9e65ecf099f4901ef43eebf0 Mon Sep 17 00:00:00 2001 From: Preston Cabe Date: Thu, 29 Oct 2026 15:46:54 -0400 Subject: [PATCH 03/11] Count whitespace answers as answered in next-steps prompts The eligibility service receives whitespace unchanged, so prompting for it contradicted what was evaluated. Co-Authored-By: Claude Opus 5.5 --- .../src/utils/unansweredQuestions.test.ts | 13 +++++++++++++ builder-frontend/src/utils/unansweredQuestions.ts | 4 ++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/builder-frontend/src/utils/unansweredQuestions.test.ts b/builder-frontend/src/utils/unansweredQuestions.test.ts index 099d8156..5e8d0a5a 100644 --- a/builder-frontend/src/utils/unansweredQuestions.test.ts +++ b/builder-frontend/src/utils/unansweredQuestions.test.ts @@ -133,6 +133,19 @@ it("handles repeated sections and shared votes without exposing paths or values" }); }); +it("treats whitespace as answered, as the eligibility service receives it", () => { + expect( + unansweredQuestions( + schema, + { + custom: { income: " ", resident: false, programs: ["x"] }, + client: { birthdate: "1950-01-01" }, + }, + results, + ), + ).toEqual({}); +}); + it("does not prompt for missing inputs in already decided checks", () => { expect( unansweredQuestions( diff --git a/builder-frontend/src/utils/unansweredQuestions.ts b/builder-frontend/src/utils/unansweredQuestions.ts index 35a7467a..b6536c6b 100644 --- a/builder-frontend/src/utils/unansweredQuestions.ts +++ b/builder-frontend/src/utils/unansweredQuestions.ts @@ -65,9 +65,9 @@ export function unansweredQuestions( ); const questions = new Set(); for (const path of paths) { + // Match what the eligibility service receives: whitespace is sent as typed. const missing = valuesAt(evaluatedAnswers, path.split(".")).some( - (value) => - value == null || (typeof value === "string" && !value.trim()), + (value) => value == null || value === "", ); if (missing) for (const label of labels.get(path) ?? []) questions.add(label); From b565e4f48cafaea10855b3d429eb9b9b45e2a45e Mon Sep 17 00:00:00 2001 From: Preston Cabe Date: Thu, 29 Oct 2026 15:47:07 -0400 Subject: [PATCH 04/11] Reuse the next-steps region locator in the public e2e test Co-Authored-By: Claude Opus 5.5 --- e2e/tests/nextStepsPublic.spec.ts | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/e2e/tests/nextStepsPublic.spec.ts b/e2e/tests/nextStepsPublic.spec.ts index 737e6fb4..1276954d 100644 --- a/e2e/tests/nextStepsPublic.spec.ts +++ b/e2e/tests/nextStepsPublic.spec.ts @@ -152,13 +152,9 @@ for (const showWhen of ["whenDecided", "whenComplete"]) { exact: true, }), ).toHaveCount(0); - const next = page.getByRole("region", { - name: "Your next steps", - exact: true, - }); - await expect(next.locator("strong")).toHaveText("photo ID"); + await expect(nextSteps.locator("strong")).toHaveText("photo ID"); await expect( - next.getByRole("link", { name: "Help", exact: true }), + nextSteps.getByRole("link", { name: "Help", exact: true }), ).toHaveAttribute("target", "_blank"); await expect( page.getByText("Screening complete. All benefits", { exact: false }), @@ -205,12 +201,14 @@ for (const showWhen of ["whenDecided", "whenComplete"]) { page.getByRole("link", { name: "Apply for this benefit" }), ).toHaveCount(0); await expect( - next.getByText("Requirements not met", { exact: true }), + nextSteps.getByText("Requirements not met", { exact: true }), ).toBeVisible(); - await expect(next.getByRole("listitem")).toHaveText( + await expect(nextSteps.getByRole("listitem")).toHaveText( "Income must be below $1,000", ); - await expect(next.getByText("Ask about other assistance.")).toBeVisible(); + await expect( + nextSteps.getByText("Ask about other assistance."), + ).toBeVisible(); fail = true; await income.fill("600"); await expect( From 813a4abcaf01c46827d20e6fd938951b0ecca208 Mon Sep 17 00:00:00 2001 From: Preston Cabe Date: Thu, 29 Oct 2026 15:48:10 -0400 Subject: [PATCH 05/11] Share one schema field walk across form data utilities Array normalization, unanswered-question labels and question hiding each joined group paths and keys separately. They now share forEachKeyedField and joinPath, and the answer lookup refuses unsafe object keys like normalizeArrayFieldData does. Co-Authored-By: Claude Opus 5.5 --- builder-frontend/src/utils/arrayFieldData.ts | 25 +++-------- builder-frontend/src/utils/formFields.ts | 26 +++++++++++ builder-frontend/src/utils/questionVotes.ts | 9 ++-- .../src/utils/unansweredQuestions.ts | 44 ++++++++----------- 4 files changed, 52 insertions(+), 52 deletions(-) create mode 100644 builder-frontend/src/utils/formFields.ts diff --git a/builder-frontend/src/utils/arrayFieldData.ts b/builder-frontend/src/utils/arrayFieldData.ts index 19a9ec5d..46598889 100644 --- a/builder-frontend/src/utils/arrayFieldData.ts +++ b/builder-frontend/src/utils/arrayFieldData.ts @@ -2,15 +2,9 @@ import { CHECKLIST_WITH_NONE_TYPE, NONE_OF_THESE_VALUE, } from '@/components/screenerEditor/formJsExtensions/customFormFields/ChecklistWithNone'; +import { forEachKeyedField, type FormComponent } from '@/utils/formFields'; import { isUnsafeObjectKey } from '@/utils/unsafeObjectKeys'; -interface FormComponent { - type: string; - key?: string; - path?: string; - components?: FormComponent[]; -} - // Field types whose answers are arrays of selected options. const ARRAY_FIELD_TYPES = ['checklist', 'taglist', CHECKLIST_WITH_NONE_TYPE]; @@ -47,19 +41,10 @@ export function normalizeArrayFieldData( : Array.isArray(value) && value.length > 0 ? value : null; } - function visit(components: FormComponent[] | undefined, parentPath = '') { - for (const component of components ?? []) { - const prefix = [parentPath, component.path].filter(Boolean).join('.'); - - if (ARRAY_FIELD_TYPES.includes(component.type) && component.key) { - const path = [prefix, component.key].filter(Boolean).join('.'); - normalizeAtPath(result, path.split('.')); - } - - visit(component.components, prefix); + forEachKeyedField(schema?.components, (component, path) => { + if (ARRAY_FIELD_TYPES.includes(component.type ?? '')) { + normalizeAtPath(result, path.split('.')); } - } - - visit(schema?.components); + }); return result; } diff --git a/builder-frontend/src/utils/formFields.ts b/builder-frontend/src/utils/formFields.ts new file mode 100644 index 00000000..93fa4f98 --- /dev/null +++ b/builder-frontend/src/utils/formFields.ts @@ -0,0 +1,26 @@ +export interface FormComponent { + type?: string; + key?: string; + path?: string; + components?: FormComponent[]; +} + +/** Joins path parts, skipping empty ones, as form-js does for group paths and keys. */ +export const joinPath = (...parts: (string | undefined)[]) => + parts.filter(Boolean).join("."); + +/** + * Calls `visit` for each field with a key, passing the full path checks use to name + * its input: the paths of its groups and repeated sections, then its key. + */ +export function forEachKeyedField( + components: T[] | undefined, + visit: (field: T, path: string) => void, + parentPath = "", +): void { + for (const component of components ?? []) { + const prefix = joinPath(parentPath, component.path); + if (component.key) visit(component, joinPath(prefix, component.key)); + forEachKeyedField(component.components as T[] | undefined, visit, prefix); + } +} diff --git a/builder-frontend/src/utils/questionVotes.ts b/builder-frontend/src/utils/questionVotes.ts index fc73ee37..596e76fd 100644 --- a/builder-frontend/src/utils/questionVotes.ts +++ b/builder-frontend/src/utils/questionVotes.ts @@ -1,4 +1,5 @@ import type { BenefitResult, ScreenerResult } from "@/types"; +import { joinPath } from "@/utils/formFields"; export function haveSameQuestionPaths( previous: readonly string[] | undefined, @@ -70,18 +71,14 @@ export function hideQuestions( .filter( (component) => !component.key || - !hidden.has( - [parentPath, component.path, component.key] - .filter(Boolean) - .join("."), - ), + !hidden.has(joinPath(parentPath, component.path, component.key)), ) .map((component) => { const copy = { ...component }; if (Array.isArray(component.components)) { copy.components = filterComponents( component.components, - [parentPath, component.path].filter(Boolean).join("."), + joinPath(parentPath, component.path), ); } return copy; diff --git a/builder-frontend/src/utils/unansweredQuestions.ts b/builder-frontend/src/utils/unansweredQuestions.ts index b6536c6b..3daebb2d 100644 --- a/builder-frontend/src/utils/unansweredQuestions.ts +++ b/builder-frontend/src/utils/unansweredQuestions.ts @@ -1,15 +1,14 @@ import type { ScreenerResult } from "@/types"; +import { forEachKeyedField, type FormComponent } from "@/utils/formFields"; +import { isUnsafeObjectKey } from "@/utils/unsafeObjectKeys"; export type UnansweredQuestions = Readonly>; -type Field = { - key?: string; - path?: string; +interface Field extends FormComponent { label?: string; dateLabel?: string; timeLabel?: string; subtype?: string; - components?: Field[]; -}; +} // A repeated section may hold several answers at the same schema path. function valuesAt(value: unknown, segments: string[]): unknown[] { @@ -18,8 +17,9 @@ function valuesAt(value: unknown, segments: string[]): unknown[] { return value.length ? value.flatMap((item) => valuesAt(item, segments)) : [undefined]; - if (!value || typeof value !== "object") return [undefined]; const [key, ...rest] = segments; + if (!value || typeof value !== "object" || isUnsafeObjectKey(key)) + return [undefined]; return valuesAt( Object.hasOwn(value, key) ? (value as Record)[key] @@ -35,26 +35,18 @@ export function unansweredQuestions( results: ScreenerResult | undefined, ): UnansweredQuestions { const labels = new Map(); - const visit = (fields: Field[], parent = "") => { - for (const field of fields) { - const prefix = [parent, field.path].filter(Boolean).join("."); - const label = - field.label || - (field.subtype === "time" ? field.timeLabel : field.dateLabel); - if ( - field.key && - typeof label === "string" && - label.trim() && - !label.trim().startsWith("=") && - !/{{[\s\S]*?}}/.test(label) - ) { - const path = [prefix, field.key].filter(Boolean).join("."); - labels.set(path, [...(labels.get(path) ?? []), label.trim()]); - } - visit(field.components ?? [], prefix); - } - }; - visit(schema.components ?? []); + forEachKeyedField(schema.components, (field, path) => { + const label = + field.label || + (field.subtype === "time" ? field.timeLabel : field.dateLabel); + if ( + typeof label === "string" && + label.trim() && + !label.trim().startsWith("=") && + !/{{[\s\S]*?}}/.test(label) + ) + labels.set(path, [...(labels.get(path) ?? []), label.trim()]); + }); return Object.fromEntries( Object.entries(results ?? {}).flatMap(([id, benefit]) => { if (benefit.result !== "UNABLE_TO_DETERMINE") return []; From bace9973a772f0a278c413be153c39c739c74c0f Mon Sep 17 00:00:00 2001 From: Preston Cabe Date: Thu, 29 Oct 2026 15:49:10 -0400 Subject: [PATCH 06/11] Tell apart unanswered questions that share a label Questions were deduplicated by label text, so client and spouse fields both labelled "Date of birth", or several people in a repeated section, collapsed into one entry that didn't say whose answer was missing. Co-Authored-By: Claude Opus 5.5 --- builder-frontend/README.md | 4 +- builder-frontend/src/utils/formFields.ts | 14 ++- .../src/utils/unansweredQuestions.test.ts | 53 ++++++++++ .../src/utils/unansweredQuestions.ts | 97 ++++++++++++++----- 4 files changed, 139 insertions(+), 29 deletions(-) diff --git a/builder-frontend/README.md b/builder-frontend/README.md index 51f3ed52..85407732 100644 --- a/builder-frontend/README.md +++ b/builder-frontend/README.md @@ -109,7 +109,9 @@ its progress heading; existing forms without it keep the legacy completion messa For an undecided benefit, Next steps lists the form's question labels for missing inputs used by that benefit's undecided checks. Shared inputs are deduplicated; -inputs used only by decided checks are omitted. Zero, No, and explicit None +inputs used only by decided checks are omitted. Fields sharing a label add their +group's label, and items of a repeated section with more than one item are +numbered, such as "Age (Household member #2)". Zero, No, and explicit None answers remain answered. The list uses the same normalized data sent to the eligibility service, including unknown CRM prefill preserved as null. diff --git a/builder-frontend/src/utils/formFields.ts b/builder-frontend/src/utils/formFields.ts index 93fa4f98..254ebeed 100644 --- a/builder-frontend/src/utils/formFields.ts +++ b/builder-frontend/src/utils/formFields.ts @@ -11,16 +11,22 @@ export const joinPath = (...parts: (string | undefined)[]) => /** * Calls `visit` for each field with a key, passing the full path checks use to name - * its input: the paths of its groups and repeated sections, then its key. + * its input (the paths of its groups and repeated sections, then its key) and the + * components containing it, outermost first. */ export function forEachKeyedField( components: T[] | undefined, - visit: (field: T, path: string) => void, + visit: (field: T, path: string, ancestors: readonly T[]) => void, parentPath = "", + ancestors: readonly T[] = [], ): void { for (const component of components ?? []) { const prefix = joinPath(parentPath, component.path); - if (component.key) visit(component, joinPath(prefix, component.key)); - forEachKeyedField(component.components as T[] | undefined, visit, prefix); + if (component.key) + visit(component, joinPath(prefix, component.key), ancestors); + forEachKeyedField(component.components as T[] | undefined, visit, prefix, [ + ...ancestors, + component, + ]); } } diff --git a/builder-frontend/src/utils/unansweredQuestions.test.ts b/builder-frontend/src/utils/unansweredQuestions.test.ts index 5e8d0a5a..b51bb4ff 100644 --- a/builder-frontend/src/utils/unansweredQuestions.test.ts +++ b/builder-frontend/src/utils/unansweredQuestions.test.ts @@ -133,6 +133,59 @@ it("handles repeated sections and shared votes without exposing paths or values" }); }); +it("tells apart fields sharing a label and items of a repeated section", () => { + const form = { + components: [ + { + path: "client", + label: "You", + components: [{ key: "birthdate", label: "Date of birth" }], + }, + { + path: "spouse", + label: "Your spouse", + components: [{ key: "birthdate", label: "Date of birth" }], + }, + { + path: "people", + label: "Household member", + isRepeating: true, + components: [{ key: "age", label: "Age" }], + }, + ], + }; + const result: ScreenerResult = { + benefit: { + name: "Benefit", + result: "UNABLE_TO_DETERMINE", + check_results: { + a: check(["client.birthdate", "spouse.birthdate", "people.age"]), + }, + }, + }; + expect( + unansweredQuestions( + form, + { client: {}, spouse: {}, people: [{ age: 4 }, { age: null }, {}] }, + result, + ), + ).toEqual({ + benefit: [ + "Date of birth (You)", + "Date of birth (Your spouse)", + "Age (Household member #2)", + "Age (Household member #3)", + ], + }); + expect( + unansweredQuestions( + form, + { client: { birthdate: "1950-01-01" }, people: [{ age: null }] }, + result, + ), + ).toEqual({ benefit: ["Date of birth (Your spouse)", "Age"] }); +}); + it("treats whitespace as answered, as the eligibility service receives it", () => { expect( unansweredQuestions( diff --git a/builder-frontend/src/utils/unansweredQuestions.ts b/builder-frontend/src/utils/unansweredQuestions.ts index 3daebb2d..9158a816 100644 --- a/builder-frontend/src/utils/unansweredQuestions.ts +++ b/builder-frontend/src/utils/unansweredQuestions.ts @@ -8,23 +8,47 @@ interface Field extends FormComponent { dateLabel?: string; timeLabel?: string; subtype?: string; + isRepeating?: boolean; +} +interface Question { + label: string; + // Labels of the field's groups, to tell apart fields sharing a label. + groups: string[]; + // Labels of its repeated sections, outermost first, to number their items. + repeats: string[]; +} +// Which item of each repeated section, outermost first, holds an answer. +type Item = { index: number; count: number }; + +// Labels only: an expression or template label would evaluate answers into the prompt. +function staticLabel(label: unknown): string | undefined { + if (typeof label !== "string") return; + const text = label.trim(); + if (text && !text.startsWith("=") && !/{{[\s\S]*?}}/.test(text)) return text; } // A repeated section may hold several answers at the same schema path. -function valuesAt(value: unknown, segments: string[]): unknown[] { - if (!segments.length) return [value]; +function valuesAt( + value: unknown, + segments: string[], + items: Item[] = [], +): { value: unknown; items: Item[] }[] { + if (!segments.length) return [{ value, items }]; if (Array.isArray(value)) return value.length - ? value.flatMap((item) => valuesAt(item, segments)) - : [undefined]; + ? value.flatMap((item, index) => + valuesAt(item, segments, [...items, { index, count: value.length }]), + ) + : [{ value: undefined, items }]; const [key, ...rest] = segments; if (!value || typeof value !== "object" || isUnsafeObjectKey(key)) - return [undefined]; + return [{ value: undefined, items }]; return valuesAt( Object.hasOwn(value, key) ? (value as Record)[key] : undefined, rest, + items, ); } @@ -34,19 +58,43 @@ export function unansweredQuestions( evaluatedAnswers: unknown, results: ScreenerResult | undefined, ): UnansweredQuestions { - const labels = new Map(); - forEachKeyedField(schema.components, (field, path) => { - const label = + const questionsAt = new Map(); + const labelPaths = new Map>(); + forEachKeyedField(schema.components, (field, path, ancestors) => { + const label = staticLabel( field.label || - (field.subtype === "time" ? field.timeLabel : field.dateLabel); - if ( - typeof label === "string" && - label.trim() && - !label.trim().startsWith("=") && - !/{{[\s\S]*?}}/.test(label) - ) - labels.set(path, [...(labels.get(path) ?? []), label.trim()]); + (field.subtype === "time" ? field.timeLabel : field.dateLabel), + ); + if (!label) return; + labelPaths.set(label, (labelPaths.get(label) ?? new Set()).add(path)); + questionsAt.set(path, [ + ...(questionsAt.get(path) ?? []), + { + label, + groups: ancestors + .filter((group) => !group.isRepeating) + .flatMap((group) => staticLabel(group.label) ?? []), + repeats: ancestors + .filter((group) => group.isRepeating) + .map((group) => staticLabel(group.label) ?? "Entry"), + }, + ]); }); + // Name a field's groups only when another field shares its label, and number + // a repeated section's items only when it has more than one. + const describe = (question: Question, items: Item[]) => { + const context = [ + ...items.flatMap(({ index, count }, depth) => + count > 1 && question.repeats[depth] + ? [`${question.repeats[depth]} #${index + 1}`] + : [], + ), + ...(labelPaths.get(question.label)!.size > 1 ? question.groups : []), + ]; + return context.length + ? `${question.label} (${context.join(", ")})` + : question.label; + }; return Object.fromEntries( Object.entries(results ?? {}).flatMap(([id, benefit]) => { if (benefit.result !== "UNABLE_TO_DETERMINE") return []; @@ -56,14 +104,15 @@ export function unansweredQuestions( .flatMap((check) => check.inputPaths ?? []), ); const questions = new Set(); - for (const path of paths) { - // Match what the eligibility service receives: whitespace is sent as typed. - const missing = valuesAt(evaluatedAnswers, path.split(".")).some( - (value) => value == null || value === "", - ); - if (missing) - for (const label of labels.get(path) ?? []) questions.add(label); - } + for (const path of paths) + for (const { value, items } of valuesAt( + evaluatedAnswers, + path.split("."), + )) + // Match what the eligibility service receives: whitespace is sent as typed. + if (value == null || value === "") + for (const question of questionsAt.get(path) ?? []) + questions.add(describe(question, items)); return questions.size ? [[id, [...questions]]] : []; }), ); From d1f29b6a5a9f8426917e1d491a7d4a7613352d63 Mon Sep 17 00:00:00 2001 From: Preston Cabe Date: Thu, 29 Oct 2026 15:50:07 -0400 Subject: [PATCH 07/11] Derive unanswered questions in connectScreeningResults Both form renderers copied the same memo; the shared connection now takes the schema and evaluated answers and derives the questions itself, so later fixes land in one place. Co-Authored-By: Claude Opus 5.5 --- .../src/components/screener/FormRenderer.tsx | 16 +++----------- .../screeningResults/screeningResults.ts | 21 +++++++++++-------- .../screenerEditor/preview/FormRenderer.tsx | 16 +++----------- 3 files changed, 18 insertions(+), 35 deletions(-) diff --git a/builder-frontend/src/components/screener/FormRenderer.tsx b/builder-frontend/src/components/screener/FormRenderer.tsx index ac766268..9b82e890 100644 --- a/builder-frontend/src/components/screener/FormRenderer.tsx +++ b/builder-frontend/src/components/screener/FormRenderer.tsx @@ -1,11 +1,4 @@ -import { - Accessor, - createMemo, - createEffect, - on, - onCleanup, - onMount, -} from "solid-js"; +import { Accessor, createEffect, on, onCleanup, onMount } from "solid-js"; import debounce from "lodash.debounce"; import cloneDeep from "lodash/cloneDeep"; @@ -24,7 +17,6 @@ import { type NextStepsProvider, } from "@/integrations/nextSteps"; import type { ScreenerResult } from "@/types"; -import { unansweredQuestions } from "@/utils/unansweredQuestions"; import { hideQuestions } from "@/utils/questionVotes"; import { normalizeArrayFieldData } from "@/utils/arrayFieldData"; import { @@ -98,16 +90,14 @@ function FormRenderer({ container, additionalModules: [CustomFormFieldsModule, screeningResultsModule], }); - const missingQuestions = createMemo(() => - unansweredQuestions(schema, formData(), results()), - ); connectScreeningResults(form, { + schema: () => schema, + evaluatedAnswers: formData, results, screenerName, pending, failed, onNextStepsShownChange, - unansweredQuestions: missingQuestions, }); currentData = cloneDeep(formData()); diff --git a/builder-frontend/src/components/screenerEditor/formJsExtensions/screeningResults/screeningResults.ts b/builder-frontend/src/components/screenerEditor/formJsExtensions/screeningResults/screeningResults.ts index 429a8661..8e8e1472 100644 --- a/builder-frontend/src/components/screenerEditor/formJsExtensions/screeningResults/screeningResults.ts +++ b/builder-frontend/src/components/screenerEditor/formJsExtensions/screeningResults/screeningResults.ts @@ -1,6 +1,9 @@ -import { Accessor, createEffect } from "solid-js"; +import { Accessor, createEffect, createMemo } from "solid-js"; import cloneDeep from "lodash/cloneDeep"; -import type { UnansweredQuestions } from "@/utils/unansweredQuestions"; +import { + unansweredQuestions, + type UnansweredQuestions, +} from "@/utils/unansweredQuestions"; import type { BenefitDetail, BenefitResult, @@ -221,22 +224,22 @@ function deepFreeze(value: T): T { export function connectScreeningResults( form: { get(name: string): T }, live: { + schema: Accessor[0]>; + /** The normalized answers last sent for evaluation. */ + evaluatedAnswers: Accessor; results: Accessor; pending: Accessor; failed: Accessor; screenerName?: Accessor; - unansweredQuestions?: Accessor; onNextStepsShownChange?: (shown: boolean) => void; }, ): void { const service = form.get("screeningResults"); + const questions = createMemo(() => + unansweredQuestions(live.schema(), live.evaluatedAnswers(), live.results()), + ); createEffect(() => - service.update( - live.results(), - live.pending(), - live.failed(), - live.unansweredQuestions?.(), - ), + service.update(live.results(), live.pending(), live.failed(), questions()), ); createEffect(() => service.setScreenerName(live.screenerName?.())); const { onNextStepsShownChange } = live; diff --git a/builder-frontend/src/components/screenerEditor/preview/FormRenderer.tsx b/builder-frontend/src/components/screenerEditor/preview/FormRenderer.tsx index e222b594..6405373e 100644 --- a/builder-frontend/src/components/screenerEditor/preview/FormRenderer.tsx +++ b/builder-frontend/src/components/screenerEditor/preview/FormRenderer.tsx @@ -1,11 +1,4 @@ -import { - Accessor, - createMemo, - createEffect, - on, - onCleanup, - onMount, -} from "solid-js"; +import { Accessor, createEffect, on, onCleanup, onMount } from "solid-js"; import debounce from "lodash.debounce"; import cloneDeep from "lodash/cloneDeep"; import isEqual from "lodash/isEqual"; @@ -19,7 +12,6 @@ import { screeningResultsModule, } from "../formJsExtensions/screeningResults/screeningResults"; import type { ScreenerResult } from "@/types"; -import { unansweredQuestions } from "@/utils/unansweredQuestions"; import { hideQuestions } from "@/utils/questionVotes"; import { normalizeArrayFieldData } from "@/utils/arrayFieldData"; @@ -83,16 +75,14 @@ function FormRenderer({ container, additionalModules: [CustomFormFieldsModule, screeningResultsModule], }); - const missingQuestions = createMemo(() => - unansweredQuestions(schema(), formData(), results()), - ); connectScreeningResults(form, { + schema, + evaluatedAnswers: formData, results, screenerName, pending, failed, onNextStepsShownChange, - unansweredQuestions: missingQuestions, }); currentData = cloneDeep(formData()); From 37919394ceddbc3c6f88101ea2650955bd1e7b86 Mon Sep 17 00:00:00 2001 From: Preston Cabe Date: Thu, 29 Oct 2026 15:50:34 -0400 Subject: [PATCH 08/11] Keep unanswered questions when their content is unchanged The memo compared by identity and the helper returns a new object each time, so every answer edit or evaluation copied, froze and republished identical questions. Co-Authored-By: Claude Opus 5.5 --- .../screeningResults.integration.test.tsx | 15 +++++++++++++++ .../screeningResults/screeningResults.ts | 14 ++++++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/builder-frontend/src/components/screenerEditor/formJsExtensions/screeningResults/screeningResults.integration.test.tsx b/builder-frontend/src/components/screenerEditor/formJsExtensions/screeningResults/screeningResults.integration.test.tsx index 795efea1..8fb56d1a 100644 --- a/builder-frontend/src/components/screenerEditor/formJsExtensions/screeningResults/screeningResults.integration.test.tsx +++ b/builder-frontend/src/components/screenerEditor/formJsExtensions/screeningResults/screeningResults.integration.test.tsx @@ -130,6 +130,21 @@ describe.each(["published", "preview"])( }), ); expect(s.snapshot()).not.toHaveProperty("answers"); + const service = s.form.get("screeningResults") as ScreeningResultsService; + const questions = service.getSnapshot().unansweredQuestions; + s.setResults({ + benefit: { + name: "Benefit", + result: "UNABLE_TO_DETERMINE", + check_results: { + missing: { + result: "UNABLE_TO_DETERMINE", + inputPaths: ["answer"], + } as any, + }, + }, + }); + expect(service.getSnapshot().unansweredQuestions).toBe(questions); s.setResults({ benefit: { name: "Benefit", result: "TRUE", check_results: {} }, }); diff --git a/builder-frontend/src/components/screenerEditor/formJsExtensions/screeningResults/screeningResults.ts b/builder-frontend/src/components/screenerEditor/formJsExtensions/screeningResults/screeningResults.ts index 8e8e1472..bc30dd44 100644 --- a/builder-frontend/src/components/screenerEditor/formJsExtensions/screeningResults/screeningResults.ts +++ b/builder-frontend/src/components/screenerEditor/formJsExtensions/screeningResults/screeningResults.ts @@ -1,5 +1,6 @@ import { Accessor, createEffect, createMemo } from "solid-js"; import cloneDeep from "lodash/cloneDeep"; +import isEqual from "lodash/isEqual"; import { unansweredQuestions, type UnansweredQuestions, @@ -235,8 +236,17 @@ export function connectScreeningResults( }, ): void { const service = form.get("screeningResults"); - const questions = createMemo(() => - unansweredQuestions(live.schema(), live.evaluatedAnswers(), live.results()), + // Keep the same questions while their content is unchanged, so the service + // doesn't copy and republish them for every answer edit. + const questions = createMemo( + () => + unansweredQuestions( + live.schema(), + live.evaluatedAnswers(), + live.results(), + ), + undefined, + { equals: isEqual }, ); createEffect(() => service.update(live.results(), live.pending(), live.failed(), questions()), From f46f159d2c0451d683c4029a956872e3c8d42692 Mon Sep 17 00:00:00 2001 From: Preston Cabe Date: Thu, 29 Oct 2026 15:51:50 -0400 Subject: [PATCH 09/11] Index question labels once per schema The schema was walked again on every answer edit or evaluation to rebuild the path-to-label map. indexQuestions builds it once per schema, and unansweredQuestions only looks answers up against it. Co-Authored-By: Claude Opus 5.5 --- .../screeningResults/screeningResults.ts | 6 +- .../src/utils/unansweredQuestions.test.ts | 10 +++- .../src/utils/unansweredQuestions.ts | 57 ++++++++++++------- 3 files changed, 48 insertions(+), 25 deletions(-) diff --git a/builder-frontend/src/components/screenerEditor/formJsExtensions/screeningResults/screeningResults.ts b/builder-frontend/src/components/screenerEditor/formJsExtensions/screeningResults/screeningResults.ts index bc30dd44..d01f56c5 100644 --- a/builder-frontend/src/components/screenerEditor/formJsExtensions/screeningResults/screeningResults.ts +++ b/builder-frontend/src/components/screenerEditor/formJsExtensions/screeningResults/screeningResults.ts @@ -2,6 +2,7 @@ import { Accessor, createEffect, createMemo } from "solid-js"; import cloneDeep from "lodash/cloneDeep"; import isEqual from "lodash/isEqual"; import { + indexQuestions, unansweredQuestions, type UnansweredQuestions, } from "@/utils/unansweredQuestions"; @@ -225,7 +226,7 @@ function deepFreeze(value: T): T { export function connectScreeningResults( form: { get(name: string): T }, live: { - schema: Accessor[0]>; + schema: Accessor[0]>; /** The normalized answers last sent for evaluation. */ evaluatedAnswers: Accessor; results: Accessor; @@ -236,12 +237,13 @@ export function connectScreeningResults( }, ): void { const service = form.get("screeningResults"); + const questionIndex = createMemo(() => indexQuestions(live.schema())); // Keep the same questions while their content is unchanged, so the service // doesn't copy and republish them for every answer edit. const questions = createMemo( () => unansweredQuestions( - live.schema(), + questionIndex(), live.evaluatedAnswers(), live.results(), ), diff --git a/builder-frontend/src/utils/unansweredQuestions.test.ts b/builder-frontend/src/utils/unansweredQuestions.test.ts index b51bb4ff..0103dcc3 100644 --- a/builder-frontend/src/utils/unansweredQuestions.test.ts +++ b/builder-frontend/src/utils/unansweredQuestions.test.ts @@ -1,5 +1,8 @@ import { expect, it } from "vitest"; -import { unansweredQuestions } from "./unansweredQuestions"; +import { + indexQuestions, + unansweredQuestions as fromIndex, +} from "./unansweredQuestions"; import { normalizeArrayFieldData } from "./arrayFieldData"; import { findFilledUnknownPaths, @@ -7,6 +10,11 @@ import { } from "./unknownAnswers"; import type { ScreenerResult } from "@/types"; +const unansweredQuestions = ( + schema: Parameters[0], + ...rest: [unknown, ScreenerResult | undefined] +) => fromIndex(indexQuestions(schema), ...rest); + const schema = { components: [ { key: "custom.income", label: "Annual household income" }, diff --git a/builder-frontend/src/utils/unansweredQuestions.ts b/builder-frontend/src/utils/unansweredQuestions.ts index 9158a816..8fdec592 100644 --- a/builder-frontend/src/utils/unansweredQuestions.ts +++ b/builder-frontend/src/utils/unansweredQuestions.ts @@ -12,7 +12,7 @@ interface Field extends FormComponent { } interface Question { label: string; - // Labels of the field's groups, to tell apart fields sharing a label. + // Labels of the field's groups, kept only to tell apart fields sharing a label. groups: string[]; // Labels of its repeated sections, outermost first, to number their items. repeats: string[]; @@ -52,12 +52,12 @@ function valuesAt( ); } -/** Labels only: never expose answers, technical paths, or evaluated label templates. */ -export function unansweredQuestions( - schema: { components?: Field[] }, - evaluatedAnswers: unknown, - results: ScreenerResult | undefined, -): UnansweredQuestions { +/** A form's question labels by input path; build once per schema. */ +export type QuestionIndex = ReadonlyMap; + +export function indexQuestions(schema: { + components?: Field[]; +}): QuestionIndex { const questionsAt = new Map(); const labelPaths = new Map>(); forEachKeyedField(schema.components, (field, path, ancestors) => { @@ -80,21 +80,34 @@ export function unansweredQuestions( }, ]); }); - // Name a field's groups only when another field shares its label, and number - // a repeated section's items only when it has more than one. - const describe = (question: Question, items: Item[]) => { - const context = [ - ...items.flatMap(({ index, count }, depth) => - count > 1 && question.repeats[depth] - ? [`${question.repeats[depth]} #${index + 1}`] - : [], - ), - ...(labelPaths.get(question.label)!.size > 1 ? question.groups : []), - ]; - return context.length - ? `${question.label} (${context.join(", ")})` - : question.label; - }; + // Name a field's groups only when another field shares its label. + for (const questions of questionsAt.values()) + for (const question of questions) + if (labelPaths.get(question.label)!.size === 1) question.groups = []; + return questionsAt; +} + +// Number a repeated section's items only when it has more than one. +function describe(question: Question, items: Item[]): string { + const context = [ + ...items.flatMap(({ index, count }, depth) => + count > 1 && question.repeats[depth] + ? [`${question.repeats[depth]} #${index + 1}`] + : [], + ), + ...question.groups, + ]; + return context.length + ? `${question.label} (${context.join(", ")})` + : question.label; +} + +/** Labels only: never expose answers, technical paths, or evaluated label templates. */ +export function unansweredQuestions( + questionsAt: QuestionIndex, + evaluatedAnswers: unknown, + results: ScreenerResult | undefined, +): UnansweredQuestions { return Object.fromEntries( Object.entries(results ?? {}).flatMap(([id, benefit]) => { if (benefit.result !== "UNABLE_TO_DETERMINE") return []; From e6237a2810c63c24ea08cc467bc44d9b39bc6fe3 Mon Sep 17 00:00:00 2001 From: Preston Cabe Date: Thu, 29 Oct 2026 15:52:33 -0400 Subject: [PATCH 10/11] Publish unanswered questions without a second copy The memo already builds a fresh object for each form, so the service now freezes it in place instead of deep-cloning it, and assigns it directly instead of through a conditional spread. Co-Authored-By: Claude Opus 5.5 --- .../screeningResults/screeningResults.test.ts | 8 ++------ .../screeningResults/screeningResults.ts | 13 +++---------- 2 files changed, 5 insertions(+), 16 deletions(-) diff --git a/builder-frontend/src/components/screenerEditor/formJsExtensions/screeningResults/screeningResults.test.ts b/builder-frontend/src/components/screenerEditor/formJsExtensions/screeningResults/screeningResults.test.ts index 3ed90ef4..56c87954 100644 --- a/builder-frontend/src/components/screenerEditor/formJsExtensions/screeningResults/screeningResults.test.ts +++ b/builder-frontend/src/components/screenerEditor/formJsExtensions/screeningResults/screeningResults.test.ts @@ -29,15 +29,11 @@ describe("screening results service", () => { const { service } = setup(); const questions = { benefit: ["Annual income"] }; service.update(decided, false, false, questions); - questions.benefit.push("Later edit"); + expect(service.getSnapshot().unansweredQuestions).toBe(questions); + expect(() => questions.benefit.push("Later edit")).toThrow(TypeError); expect(service.getSnapshot().unansweredQuestions).toEqual({ benefit: ["Annual income"], }); - expect(() => - (service.getSnapshot().unansweredQuestions!.benefit as string[]).push( - "Edit", - ), - ).toThrow(TypeError); service.update(decided, false); expect(service.getSnapshot().unansweredQuestions).toBeUndefined(); }); diff --git a/builder-frontend/src/components/screenerEditor/formJsExtensions/screeningResults/screeningResults.ts b/builder-frontend/src/components/screenerEditor/formJsExtensions/screeningResults/screeningResults.ts index d01f56c5..bfe3cfe4 100644 --- a/builder-frontend/src/components/screenerEditor/formJsExtensions/screeningResults/screeningResults.ts +++ b/builder-frontend/src/components/screenerEditor/formJsExtensions/screeningResults/screeningResults.ts @@ -49,8 +49,6 @@ export class ScreeningResultsService { // Fields get a frozen copy so they can't change the parent's results. private liveSource: ScreenerResult | undefined; private liveCopy: ScreenerResult | undefined; - private questionsSource: UnansweredQuestions | undefined; - private questionsCopy: UnansweredQuestions | undefined; // IDs of the rendered Next steps fields, once per rendering. private nextStepsShown: string[] = []; private screenerName: string | undefined; @@ -99,6 +97,7 @@ export class ScreeningResultsService { return () => this.eventBus.off(SCREENING_RESULTS_CHANGED, listener); }; + /** Freezes `unansweredQuestions` in place: pass questions built for this form alone. */ update( results: ScreenerResult | undefined, pending: boolean, @@ -109,19 +108,13 @@ export class ScreeningResultsService { this.liveSource = results; this.liveCopy = results && deepFreeze(cloneDeep(results)); } - if (unansweredQuestions !== this.questionsSource) { - this.questionsSource = unansweredQuestions; - this.questionsCopy = - unansweredQuestions && deepFreeze(cloneDeep(unansweredQuestions)); - } this.publish({ results: this.liveCopy, pending, failed, mode: "live", - ...(this.questionsCopy === undefined - ? {} - : { unansweredQuestions: this.questionsCopy }), + unansweredQuestions: + unansweredQuestions && deepFreeze(unansweredQuestions), }); } From 497a87e80aa0b2eb3622ef85140864281928ad1b Mon Sep 17 00:00:00 2001 From: Preston Cabe Date: Thu, 29 Oct 2026 15:54:16 -0400 Subject: [PATCH 11/11] Leave out unanswered questions hidden by a form condition Labels came from every keyed field, so Next steps and the print summary could ask for an answer to a question form-js was hiding, such as spouse income for someone who isn't married. The value paths form-js clears for hidden fields are now skipped, judged again after each import. Co-Authored-By: Claude Opus 5.5 --- builder-frontend/README.md | 3 +- .../screeningResults.integration.test.tsx | 51 +++++++++++++++++-- .../screeningResults/screeningResults.ts | 33 ++++++++++-- .../src/utils/unansweredQuestions.test.ts | 31 +++++++++++ .../src/utils/unansweredQuestions.ts | 42 ++++++++++----- 5 files changed, 140 insertions(+), 20 deletions(-) diff --git a/builder-frontend/README.md b/builder-frontend/README.md index 85407732..64c0f3bc 100644 --- a/builder-frontend/README.md +++ b/builder-frontend/README.md @@ -111,7 +111,8 @@ For an undecided benefit, Next steps lists the form's question labels for missin inputs used by that benefit's undecided checks. Shared inputs are deduplicated; inputs used only by decided checks are omitted. Fields sharing a label add their group's label, and items of a repeated section with more than one item are -numbered, such as "Age (Household member #2)". Zero, No, and explicit None +numbered, such as "Age (Household member #2)". Questions a form condition +currently hides are left out, since no one can answer them. Zero, No, and explicit None answers remain answered. The list uses the same normalized data sent to the eligibility service, including unknown CRM prefill preserved as null. diff --git a/builder-frontend/src/components/screenerEditor/formJsExtensions/screeningResults/screeningResults.integration.test.tsx b/builder-frontend/src/components/screenerEditor/formJsExtensions/screeningResults/screeningResults.integration.test.tsx index 8fb56d1a..49854de9 100644 --- a/builder-frontend/src/components/screenerEditor/formJsExtensions/screeningResults/screeningResults.integration.test.tsx +++ b/builder-frontend/src/components/screenerEditor/formJsExtensions/screeningResults/screeningResults.integration.test.tsx @@ -67,7 +67,10 @@ describe.each(["published", "preview"])( document.body.replaceChildren(); }); - async function mount(data = { answer: "Keep this answer" }) { + async function mount( + data: object = { answer: "Keep this answer" }, + formSchema: object = schema, + ) { const [results, setResults] = createSignal(); const [pending, setPending] = createSignal(false); const [failed, setFailed] = createSignal(false); @@ -86,9 +89,9 @@ describe.each(["published", "preview"])( render( () => mode === "published" ? ( - + ) : ( - schema} {...common} /> + formSchema} {...common} /> ), container, ), @@ -153,6 +156,48 @@ describe.each(["published", "preview"])( ); }); + it("leaves out unanswered questions a condition hides", async () => { + const s = await mount( + { answer: "", married: false }, + { + ...schema, + components: [ + ...schema.components, + { + id: "married", + type: "checkbox", + key: "married", + label: "Married", + }, + { + id: "spouseIncome", + type: "number", + key: "spouse.income", + label: "Spouse income", + conditional: { hide: "=married != true" }, + }, + ], + }, + ); + s.setResults({ + benefit: { + name: "Benefit", + result: "UNABLE_TO_DETERMINE", + check_results: { + missing: { + result: "UNABLE_TO_DETERMINE", + inputPaths: ["answer", "spouse.income"], + } as any, + }, + }, + }); + await vi.waitFor(() => + expect(s.snapshot().unansweredQuestions).toEqual({ + benefit: ["Answer"], + }), + ); + }); + it("updates custom fields without reimporting the schema or submitting answers", async () => { const s = await mount(); const importSchema = vi.spyOn(s.form, "importSchema"); diff --git a/builder-frontend/src/components/screenerEditor/formJsExtensions/screeningResults/screeningResults.ts b/builder-frontend/src/components/screenerEditor/formJsExtensions/screeningResults/screeningResults.ts index bfe3cfe4..eb04180e 100644 --- a/builder-frontend/src/components/screenerEditor/formJsExtensions/screeningResults/screeningResults.ts +++ b/builder-frontend/src/components/screenerEditor/formJsExtensions/screeningResults/screeningResults.ts @@ -1,4 +1,4 @@ -import { Accessor, createEffect, createMemo } from "solid-js"; +import { Accessor, createEffect, createMemo, createSignal } from "solid-js"; import cloneDeep from "lodash/cloneDeep"; import isEqual from "lodash/isEqual"; import { @@ -215,6 +215,23 @@ function deepFreeze(value: T): T { return value; } +/** Value paths, such as "people.1.age", of the fields and repeated sections a condition hides. */ +function hiddenAnswerPaths(form: { get(name: string): T }): Set { + const hidden = new Set(); + if (!form.get("formFieldRegistry").getForm()) return hidden; + const pathRegistry = form.get("pathRegistry"); + const { data } = form.get("form")._getState(); + // form-js asks for the path of each field it clears because a condition hides it. + form.get("conditionChecker").applyConditions(data, data, { + getFilterPath: (field: unknown, indexes: Record) => { + const path = pathRegistry.getValuePath(field, { indexes }); + hidden.add(path.join(".")); + return path; + }, + }); + return hidden; +} + /** Keeps a live form's service in step with its parent's results; call from the owning component. */ export function connectScreeningResults( form: { get(name: string): T }, @@ -231,15 +248,23 @@ export function connectScreeningResults( ): void { const service = form.get("screeningResults"); const questionIndex = createMemo(() => indexQuestions(live.schema())); + // Conditions are judged against the imported schema, so judge again after each import. + const [imports, setImports] = createSignal(0); + form + .get("eventBus") + .on("import.done", () => setImports((count) => count + 1)); // Keep the same questions while their content is unchanged, so the service // doesn't copy and republish them for every answer edit. const questions = createMemo( - () => - unansweredQuestions( + () => { + imports(); + return unansweredQuestions( questionIndex(), live.evaluatedAnswers(), live.results(), - ), + hiddenAnswerPaths(form), + ); + }, undefined, { equals: isEqual }, ); diff --git a/builder-frontend/src/utils/unansweredQuestions.test.ts b/builder-frontend/src/utils/unansweredQuestions.test.ts index 0103dcc3..4a2659c9 100644 --- a/builder-frontend/src/utils/unansweredQuestions.test.ts +++ b/builder-frontend/src/utils/unansweredQuestions.test.ts @@ -194,6 +194,37 @@ it("tells apart fields sharing a label and items of a repeated section", () => { ).toEqual({ benefit: ["Date of birth (Your spouse)", "Age"] }); }); +it("skips fields and repeated sections hidden by a condition", () => { + const form = { + components: [ + { + path: "spouse", + components: [{ key: "income", label: "Spouse income" }], + }, + { + path: "people", + label: "Household member", + isRepeating: true, + components: [{ key: "age", label: "Age" }], + }, + ], + }; + const result: ScreenerResult = { + benefit: { + name: "Benefit", + result: "UNABLE_TO_DETERMINE", + check_results: { a: check(["spouse.income", "people.age"]) }, + }, + }; + const answers = { people: [{ age: null }, { age: null }] }; + const ask = (hidden: string[]) => + fromIndex(indexQuestions(form), answers, result, new Set(hidden)).benefit; + expect(ask(["spouse.income", "people.0.age"])).toEqual([ + "Age (Household member #2)", + ]); + expect(ask(["spouse.income", "people"])).toBeUndefined(); +}); + it("treats whitespace as answered, as the eligibility service receives it", () => { expect( unansweredQuestions( diff --git a/builder-frontend/src/utils/unansweredQuestions.ts b/builder-frontend/src/utils/unansweredQuestions.ts index 8fdec592..7884f99c 100644 --- a/builder-frontend/src/utils/unansweredQuestions.ts +++ b/builder-frontend/src/utils/unansweredQuestions.ts @@ -27,28 +27,37 @@ function staticLabel(label: unknown): string | undefined { if (text && !text.startsWith("=") && !/{{[\s\S]*?}}/.test(text)) return text; } +type Answer = { value: unknown; items: Item[]; path: (string | number)[] }; + // A repeated section may hold several answers at the same schema path. function valuesAt( value: unknown, segments: string[], items: Item[] = [], -): { value: unknown; items: Item[] }[] { - if (!segments.length) return [{ value, items }]; + path: (string | number)[] = [], +): Answer[] { + if (!segments.length) return [{ value, items, path }]; if (Array.isArray(value)) return value.length ? value.flatMap((item, index) => - valuesAt(item, segments, [...items, { index, count: value.length }]), + valuesAt( + item, + segments, + [...items, { index, count: value.length }], + [...path, index], + ), ) - : [{ value: undefined, items }]; + : [{ value: undefined, items, path: [...path, ...segments] }]; const [key, ...rest] = segments; if (!value || typeof value !== "object" || isUnsafeObjectKey(key)) - return [{ value: undefined, items }]; + return [{ value: undefined, items, path: [...path, ...segments] }]; return valuesAt( Object.hasOwn(value, key) ? (value as Record)[key] : undefined, rest, items, + [...path, key], ); } @@ -102,12 +111,21 @@ function describe(question: Question, items: Item[]): string { : question.label; } -/** Labels only: never expose answers, technical paths, or evaluated label templates. */ +/** + * Labels only: never expose answers, technical paths, or evaluated label templates. + * `hiddenAnswerPaths` are value paths, such as "people.1.age", of fields or repeated + * sections hidden by a condition: no one can answer them, so they're never listed. + */ export function unansweredQuestions( questionsAt: QuestionIndex, evaluatedAnswers: unknown, results: ScreenerResult | undefined, + hiddenAnswerPaths: ReadonlySet = new Set(), ): UnansweredQuestions { + const isHidden = (path: (string | number)[]) => + path.some((_, end) => + hiddenAnswerPaths.has(path.slice(0, end + 1).join(".")), + ); return Object.fromEntries( Object.entries(results ?? {}).flatMap(([id, benefit]) => { if (benefit.result !== "UNABLE_TO_DETERMINE") return []; @@ -118,14 +136,14 @@ export function unansweredQuestions( ); const questions = new Set(); for (const path of paths) - for (const { value, items } of valuesAt( - evaluatedAnswers, - path.split("."), - )) + for (const answer of valuesAt(evaluatedAnswers, path.split("."))) // Match what the eligibility service receives: whitespace is sent as typed. - if (value == null || value === "") + if ( + (answer.value == null || answer.value === "") && + !isHidden(answer.path) + ) for (const question of questionsAt.get(path) ?? []) - questions.add(describe(question, items)); + questions.add(describe(question, answer.items)); return questions.size ? [[id, [...questions]]] : []; }), );