diff --git a/apps/web/cypress/e2e/recipes.ts b/apps/web/cypress/e2e/recipes.ts index 9af2311..5acf3ee 100644 --- a/apps/web/cypress/e2e/recipes.ts +++ b/apps/web/cypress/e2e/recipes.ts @@ -100,7 +100,15 @@ When("I choose {string} as the correct technique", (label: string) => { }); Then("the correction request should have been made", () => { - cy.wait("@correction"); + // Asserts the actual span, not just that *a* request fired — a real bug + // (StepDescription.tsx's click handler reading a shared, still-mutating + // `offset` variable by reference instead of a value captured at render + // time) once sent `end` all the way to the end of the description + // instead of "Cuire"'s own tight [0, 5) span, and a request-fired-only + // assertion here didn't catch it — found only via manual testing. + cy.wait("@correction") + .its("request.body") + .should("deep.include", { start: 0, end: 5, previousTechStepId: 1 }); }); Then("the recipe {string} should not be visible in the table", (name: string) => { diff --git a/apps/web/src/features/recipes/steps/StepDescription.tsx b/apps/web/src/features/recipes/steps/StepDescription.tsx index fd1ccdc..b37100a 100644 --- a/apps/web/src/features/recipes/steps/StepDescription.tsx +++ b/apps/web/src/features/recipes/steps/StepDescription.tsx @@ -92,6 +92,14 @@ export function StepDescription({ {segments.map((segment, index) => { const start = offset; offset += segment.text.length; + // Captured now, not read as `offset` later inside a click + // handler below — `offset` keeps mutating for every subsequent + // segment this same `.map()` pass renders, so a closure + // referencing it directly would see its *final* value (the end + // of the whole description) whenever it actually fires, long + // after render — found via a real correction submitted with + // `end` far past this segment's own text. + const end = offset; // A segment's own text/techStep don't uniquely identify it (the // same word can appear twice in one description) — index is the // only thing that does, but this list is fully regenerated from @@ -130,7 +138,7 @@ export function StepDescription({ editable ? () => setActiveCorrection({ - range: { start, end: offset }, + range: { start, end }, selectedText: segment.text, previousTechStepId: techStep.id, })