From 5003151457ce737056950192225cb0ac522571a6 Mon Sep 17 00:00:00 2001 From: Ben Christel Date: Tue, 19 Nov 2024 08:47:30 -0800 Subject: [PATCH] Add and pass one regression test for radio widget parser (#1871) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This PR is a "pilot" demonstrating how regression testing for the PerseusItem parser will work. The regression test uses production data found by running the [exhaustive test tool][1]. [1]: https://github.com/Khan/perseus/pull/1864 Issue: LEMS-2582 ## Test plan: `yarn test` Author: benchristel Reviewers: benchristel, mark-fitzgerald, jeremywiebe, anakaren-rojas, catandthemachines, nishasy Required Reviewers: Approved By: jeremywiebe Checks: ✅ Publish npm snapshot (ubuntu-latest, 20.x), ✅ Lint, Typecheck, Format, and Test (ubuntu-latest, 20.x), ✅ Check builds for changes in size (ubuntu-latest, 20.x), ✅ Cypress (ubuntu-latest, 20.x), ✅ Publish Storybook to Chromatic (ubuntu-latest, 20.x), ✅ Check for .changeset entries for all changed files (ubuntu-latest, 20.x), ✅ gerald Pull Request URL: https://github.com/Khan/perseus/pull/1871 --- .changeset/mean-pens-grow.md | 5 ++ .../perseus-parsers/radio-widget.ts | 2 +- .../data/radio-missing-noneOfTheAbove.json | 48 +++++++++++++++++++ .../parse-perseus-json-regression.test.ts | 20 ++++++++ 4 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 .changeset/mean-pens-grow.md create mode 100644 packages/perseus/src/util/parse-perseus-json/regression-tests/data/radio-missing-noneOfTheAbove.json create mode 100644 packages/perseus/src/util/parse-perseus-json/regression-tests/parse-perseus-json-regression.test.ts diff --git a/.changeset/mean-pens-grow.md b/.changeset/mean-pens-grow.md new file mode 100644 index 0000000000..771d67c9d4 --- /dev/null +++ b/.changeset/mean-pens-grow.md @@ -0,0 +1,5 @@ +--- +"@khanacademy/perseus": patch +--- + +Internal: Add regression tests for PerseusItem parser diff --git a/packages/perseus/src/util/parse-perseus-json/perseus-parsers/radio-widget.ts b/packages/perseus/src/util/parse-perseus-json/perseus-parsers/radio-widget.ts index b1ad09b5ed..4678fdaf8b 100644 --- a/packages/perseus/src/util/parse-perseus-json/perseus-parsers/radio-widget.ts +++ b/packages/perseus/src/util/parse-perseus-json/perseus-parsers/radio-widget.ts @@ -43,6 +43,6 @@ export const parseRadioWidget: Parser = parseWidget( displayCount: optional(any), // v0 props // `noneOfTheAbove` is still in use (but only set to `false`). - noneOfTheAbove: constant(false), + noneOfTheAbove: optional(constant(false)), }), ); diff --git a/packages/perseus/src/util/parse-perseus-json/regression-tests/data/radio-missing-noneOfTheAbove.json b/packages/perseus/src/util/parse-perseus-json/regression-tests/data/radio-missing-noneOfTheAbove.json new file mode 100644 index 0000000000..ed463b51a8 --- /dev/null +++ b/packages/perseus/src/util/parse-perseus-json/regression-tests/data/radio-missing-noneOfTheAbove.json @@ -0,0 +1,48 @@ +{ + "question": { + "content": "$\\begin{align} \nA &= \\{x: x \\text{ is a rectangle in the plane P} \\} \\\\\\\\\nB &= \\{x: x \\text{ is a square in the plane P} \\}\n\\end{align}$\n\n**Is $A \\subset B$ ?**\n\n[[☃ radio 1]]\n", + "images": {}, + "widgets": { + "radio 1": { + "type": "radio", + "alignment": "default", + "static": false, + "graded": true, + "options": { + "choices": [ + { + "content": "Yes", + "correct": false + }, + { + "content": "No", + "correct": true + } + ], + "randomize": false, + "multipleSelect": false, + "countChoices": false, + "displayCount": null, + "hasNoneOfTheAbove": false, + "deselectEnabled": false + }, + "version": { + "major": 1, + "minor": 0 + } + } + } + }, + "answerArea": { + "calculator": false, + "chi2Table": false, + "periodicTable": false, + "tTable": false, + "zTable": false + }, + "itemDataVersion": { + "major": 0, + "minor": 1 + }, + "hints": [] +} diff --git a/packages/perseus/src/util/parse-perseus-json/regression-tests/parse-perseus-json-regression.test.ts b/packages/perseus/src/util/parse-perseus-json/regression-tests/parse-perseus-json-regression.test.ts new file mode 100644 index 0000000000..96a2b1b32d --- /dev/null +++ b/packages/perseus/src/util/parse-perseus-json/regression-tests/parse-perseus-json-regression.test.ts @@ -0,0 +1,20 @@ +import * as fs from "fs"; +import {join} from "path"; + +import {parseAndTypecheckPerseusItem} from "../index"; + +const dataFiles = fs.readdirSync(join(__dirname, "data")); + +describe("parseAndTypecheckPerseusItem", () => { + it.each(dataFiles)("accepts data/%s", (filename) => { + const json = fs.readFileSync( + join(__dirname, "data", filename), + "utf-8", + ); + const result = parseAndTypecheckPerseusItem(json); + + // This strange-looking assertion style results in the failure message + // being printed if parsing fails, so the test is easier to debug. + expect(result).toEqual(expect.objectContaining({type: "success"})); + }); +});