Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move scoring out of general Util into scoring util file #1962

Merged
merged 6 commits into from
Dec 6, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/clever-cars-eat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@khanacademy/perseus": minor
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd argue this is a major change. Util is exported from index.ts:

export {default as Util} from "./util";

You'll see that this breaks Webapp because I used Util.keScoreFromPerseusScore in my recent change. So:

  1. Probably a major change
  2. I'd double check the utils that you moved to make sure they're not used in Webapp (and export the ones that are)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. I didn't catch the one usage of keScoreFromPerseusScore outside of Perseus. Fixing.

---

Move scoring utility functions out of `Util` object into their own file
2 changes: 1 addition & 1 deletion dev/flipbook.tsx
Original file line number Diff line number Diff line change
@@ -20,7 +20,7 @@ import {Renderer} from "../packages/perseus/src";
import {SvgImage} from "../packages/perseus/src/components";
import {scorePerseusItem} from "../packages/perseus/src/renderer-util";
import {mockStrings} from "../packages/perseus/src/strings";
import {isCorrect} from "../packages/perseus/src/util";
import {isCorrect} from "../packages/perseus/src/util/scoring";
import {trueForAllMafsSupportedGraphTypes} from "../packages/perseus/src/widgets/interactive-graphs/mafs-supported-graph-types";

import {EditableControlledInput} from "./editable-controlled-input";
134 changes: 1 addition & 133 deletions packages/perseus/src/__tests__/util.test.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,4 @@
import Util, {isCorrect} from "../util";

describe("isCorrect", () => {
it("is true given a score with all points earned", () => {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All deleted tests here just moved to scoring.test.ts

const score = {type: "points", earned: 3, total: 3} as const;
expect(isCorrect(score)).toBe(true);
});

it("is false given a score with some points unearned", () => {
const score = {type: "points", earned: 2, total: 3} as const;
expect(isCorrect(score)).toBe(false);
});

it("is false given an unanswered / invalid score", () => {
const score = {type: "invalid"} as const;
expect(isCorrect(score)).toBe(false);
});
});
import Util from "../util";

describe("#constrainedTickStepsFromTickSteps", () => {
it("should not changes the tick steps if there are fewer than (or exactly) 10 steps", () => {
@@ -79,118 +62,3 @@ describe("deepClone", () => {
expect(result[0]).not.toBe(input[0]);
});
});

describe("flattenScores", () => {
it("defaults to an empty score", () => {
const result = Util.flattenScores({});

expect(result).toHaveBeenAnsweredCorrectly({shouldHavePoints: false});
expect(result).toEqual({
type: "points",
total: 0,
earned: 0,
message: null,
});
});

it("defaults to single score if there is only one", () => {
const result = Util.flattenScores({
"radio 1": {
type: "points",
total: 1,
earned: 1,
message: null,
},
});

expect(result).toHaveBeenAnsweredCorrectly();
expect(result).toEqual({
type: "points",
total: 1,
earned: 1,
message: null,
});
});

it("returns an invalid score if any are invalid", () => {
const result = Util.flattenScores({
"radio 1": {
type: "points",
total: 1,
earned: 1,
message: null,
},
"radio 2": {
type: "invalid",
message: null,
},
});

expect(result).toHaveInvalidInput();
expect(result).toEqual({
type: "invalid",
message: null,
});
});

it("tallies scores if multiple widgets have points", () => {
const result = Util.flattenScores({
"radio 1": {
type: "points",
total: 1,
earned: 1,
message: null,
},
"radio 2": {
type: "points",
total: 1,
earned: 1,
message: null,
},
"radio 3": {
type: "points",
total: 1,
earned: 1,
message: null,
},
});

expect(result).toHaveBeenAnsweredCorrectly();
expect(result).toEqual({
type: "points",
total: 3,
earned: 3,
message: null,
});
});

it("doesn't count incorrect widgets", () => {
const result = Util.flattenScores({
"radio 1": {
type: "points",
total: 1,
earned: 1,
message: null,
},
"radio 2": {
type: "points",
total: 1,
earned: 1,
message: null,
},
"radio 3": {
type: "points",
total: 1,
earned: 0,
message: null,
},
});

expect(result).toEqual({
type: "points",
total: 3,
earned: 2,
message: null,
});
});
});
8 changes: 4 additions & 4 deletions packages/perseus/src/multi-items/multi-renderer.tsx
Original file line number Diff line number Diff line change
@@ -47,7 +47,7 @@ import {DependenciesContext} from "../dependencies";
import HintsRenderer from "../hints-renderer";
import {Log} from "../logging/log";
import Renderer from "../renderer";
import Util from "../util";
import {combineScores, keScoreFromPerseusScore} from "../util/scoring";
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also switched to ES6 exports in the new file.


import {itemToTree} from "./items";
import {buildMapper} from "./trees";
@@ -404,7 +404,7 @@ class MultiRenderer extends React.Component<Props, State> {
if (ref.getSerializedState) {
state = ref.getSerializedState();
}
return Util.keScoreFromPerseusScore(score, guess, state);
return keScoreFromPerseusScore(score, guess, state);
}

/**
@@ -441,9 +441,9 @@ class MultiRenderer extends React.Component<Props, State> {
return data.ref?.getUserInput();
});

const combinedScore = scores.reduce(Util.combineScores);
const combinedScore = scores.reduce(combineScores);

return Util.keScoreFromPerseusScore(combinedScore, guess, state);
return keScoreFromPerseusScore(combinedScore, guess, state);
}

/**
8 changes: 4 additions & 4 deletions packages/perseus/src/renderer-util.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {mapObject} from "./interactive2/objective_";
import Util from "./util";
import {scoreIsEmpty, flattenScores} from "./util/scoring";
import {getWidgetIdsFromContent} from "./widget-type-utils";
import {getWidgetScorer, upgradeWidgetInfoToLatestVersion} from "./widgets";

@@ -59,8 +59,8 @@ export function emptyWidgetsFunctional(
locale,
);

if (score) {
return Util.scoreIsEmpty(score);
if (score != null) {
return scoreIsEmpty(score);
}
});
}
@@ -86,7 +86,7 @@ export function scorePerseusItem(
strings,
locale,
);
return Util.flattenScores(scores);
return flattenScores(scores);
}

export function scoreWidgetsFunctional(
3 changes: 2 additions & 1 deletion packages/perseus/src/renderer.tsx
Original file line number Diff line number Diff line change
@@ -32,6 +32,7 @@ import {
} from "./renderer-util";
import TranslationLinter from "./translation-linter";
import Util from "./util";
import {flattenScores} from "./util/scoring";
import preprocessTex from "./util/tex-preprocess";
import WidgetContainer from "./widget-container";
import * as Widgets from "./widgets";
@@ -1737,7 +1738,7 @@ class Renderer
this.props.strings,
this.context.locale,
);
const combinedScore = Util.flattenScores(scores);
const combinedScore = flattenScores(scores);
return combinedScore;
}

3 changes: 2 additions & 1 deletion packages/perseus/src/server-item-renderer.tsx
Original file line number Diff line number Diff line change
@@ -21,6 +21,7 @@ import {ApiOptions} from "./perseus-api";
import Renderer from "./renderer";
import {scorePerseusItem} from "./renderer-util";
import Util from "./util";
import {keScoreFromPerseusScore} from "./util/scoring";

import type {PerseusItem, ShowSolutions} from "./perseus-types";
import type {
@@ -366,7 +367,7 @@ export class ServerItemRenderer
// analyzing ProblemLogs. If not, remove this layer.
const maxCompatGuess = [this.questionRenderer.getUserInput(), []];

const keScore = Util.keScoreFromPerseusScore(
const keScore = keScoreFromPerseusScore(
score,
maxCompatGuess,
this.questionRenderer.getSerializedState(),
Loading