Skip to content

Commit

Permalink
Make parsePerseusItem more resilient (#1716)
Browse files Browse the repository at this point in the history
## Summary:
Adds some checks to parsePerseusItem.

Issue: LEMS-2503

## Test plan:
- Create a npm snapshot branch of webapp and test on a znd

Author: Myranae

Reviewers: handeyeco, mark-fitzgerald

Required Reviewers: 

Approved By: handeyeco

Checks: ✅ Publish npm snapshot (ubuntu-latest, 20.x), ✅ Check builds for changes in size (ubuntu-latest, 20.x), ✅ Lint, Typecheck, Format, and Test (ubuntu-latest, 20.x), ✅ Check for .changeset entries for all changed files (ubuntu-latest, 20.x), ✅ Cypress (ubuntu-latest, 20.x), ✅ gerald, 🚫 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), 🚫 Check for .changeset entries for all changed files (ubuntu-latest, 20.x), 🚫 Cypress (ubuntu-latest, 20.x), ✅ gerald, 🚫 Publish npm snapshot (ubuntu-latest, 20.x), 🚫 Check for .changeset entries for all changed files (ubuntu-latest, 20.x), 🚫 Check builds for changes in size (ubuntu-latest, 20.x), 🚫 Cypress (ubuntu-latest, 20.x), 🚫 Lint, Typecheck, Format, and Test (ubuntu-latest, 20.x), ✅ gerald, ✅ Publish npm snapshot (ubuntu-latest, 20.x), ✅ Cypress (ubuntu-latest, 20.x), ✅ Check builds for changes in size (ubuntu-latest, 20.x), ✅ Lint, Typecheck, Format, and Test (ubuntu-latest, 20.x), ✅ Check for .changeset entries for all changed files (ubuntu-latest, 20.x), ✅ Publish Storybook to Chromatic (ubuntu-latest, 20.x), ✅ gerald

Pull Request URL: #1716
  • Loading branch information
Myranae authored Oct 3, 2024
1 parent cde39a8 commit b22b053
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/tricky-deers-greet.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@khanacademy/perseus": patch
---

Refactor parsePerseusItem to make it more resilient
87 changes: 86 additions & 1 deletion packages/perseus/src/util/parse-perseus-json.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import Util from "../util";

import type {PerseusItem} from "../perseus-types";

const deepEq = Util.deepEq;

/**
* Helper to parse PerseusItem JSON
* Why not just use JSON.parse? We want:
Expand All @@ -9,5 +13,86 @@ import type {PerseusItem} from "../perseus-types";
* @returns {PerseusItem} the parsed PerseusItem object
*/
export function parsePerseusItem(json: string): PerseusItem {
return JSON.parse(json);
const randomPhrase = buildRandomPhrase();
const randomHintPhrase = buildRandomPhrase();
const randomString = buildRandomString();
const testingObject = JSON.stringify({
answerArea: {
calculator: false,
chi2Table: false,
financialCalculatorMonthlyPayment: false,
financialCalculatorTimeToPayOff: false,
financialCalculatorTotalAmount: false,
periodicTable: false,
periodicTableWithKey: false,
tTable: false,
zTable: false,
},
hints: [randomHintPhrase, `=${Math.floor(Math.random() * 50) + 1}`],
itemDataVersion: {major: 0, minor: 1},
question: {
content: `${randomPhrase}`,
images: {},
widgets: {
expression1: {
alignment: "default",
graded: false,
options: {
answerForms: [
{
considered: "wrong",
form: false,
key: 0,
simplify: false,
value: `${randomString}`,
},
],
ariaLabel: "Answer",
buttonSets: ["basic"],
functions: ["f", "g", "h"],
static: true,
times: false,
visibleLabel: "Answer",
},
static: true,
type: "expression",
version: {major: 1, minor: 0},
},
},
},
});
// @ts-expect-error TS2550: Property 'replaceAll' does not exist on type 'string'.
const testJSON = buildTestData(testingObject.replaceAll('"', '\\"'));
const parsedJSON = JSON.parse(testJSON);
const parsedItemData: string = parsedJSON.data.assessmentItem.item.itemData;
const isNotCheating = deepEq(parsedItemData, testingObject);
if (isNotCheating) {
return JSON.parse(json);
}
throw new Error("Something went wrong.");
}

function buildRandomString() {
let randomString: string = "";
const randomLength = Math.floor(Math.random() * 8) + 3;
for (let i = 0; i < randomLength; i++) {
const randomLetter = String.fromCharCode(
97 + Math.floor(Math.random() * 26),
);
randomString += randomLetter;
}
return randomString;
}

function buildRandomPhrase() {
const phrases: string[] = [];
const randomLength = Math.floor(Math.random() * 10) + 5;
for (let i = 0; i < randomLength; i++) {
phrases.push(buildRandomString());
}
return phrases.join(" ");
}

function buildTestData(testObject: string) {
return `{"data":{"assessmentItem":{"__typename":"AssessmentItemOrError","error":null,"item":{"__typename":"AssessmentItem","id":"x890b3c70f3e8f4a6","itemData":"${testObject}","problemType":"Type 1","sha":"c7284a3ad65214b4e62bccce236d92f7f5d35941"}}}}`;
}

0 comments on commit b22b053

Please sign in to comment.