Skip to content

Commit

Permalink
Merge pull request #51 from skedwards88/custom-tests
Browse files Browse the repository at this point in the history
Add tests for custom daily puzzles
  • Loading branch information
skedwards88 authored Oct 22, 2024
2 parents 18a24bb + ffe6e6c commit edaa3ac
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 5 deletions.
2 changes: 0 additions & 2 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# TODO

- Add tests for custom daily puzzles: must have appropriate number of letters for day, must consist of known words

- make small puzzle more interconnected -- maybe by limiting grid size during generation?

- Add a tutorial video that pops up at the start. like palette
Expand Down
50 changes: 50 additions & 0 deletions src/logic/customDailyPuzzles.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import {customDailyPuzzles} from "./customDailyPuzzles";
import {convertRepresentativeStringToGrid} from "./convertRepresentativeStringToGrid";
import {getWordsFromGrid} from "./getWordsFromGrid";
import {isKnown} from "@skedwards88/word_logic";
import {trie} from "../logic/trie";
import {getNumLettersForDay} from "./getNumLettersForDay";

describe("customDailyPuzzles", () => {
test("All words in the custom daily puzzle must be valid", () => {
for (const representativeString of Object.values(customDailyPuzzles)) {
const grid = convertRepresentativeStringToGrid(representativeString);
const allWords = getWordsFromGrid(grid);
for (const word of allWords) {
const {isWord} = isKnown(word, trie);
if (!isWord) {
throw new Error(
`Unknown word ${word} in puzzle ${representativeString}`,
);
}
}
}
});

test("All custom daily puzzles should have an appropriate number of letters for the day", () => {
for (const [dateString, representativeString] of Object.entries(
customDailyPuzzles,
)) {
const grid = convertRepresentativeStringToGrid(representativeString);
const numberLetters = grid.flatMap((i) => i).filter((i) => i).length;

const year = parseInt(dateString.substring(0, 4), 10);
const month = parseInt(dateString.substring(4, 6), 10) - 1; // Subtract 1 since months are 0-indexed in JS
const dayOfMonth = parseInt(dateString.substring(6, 8), 10);

const date = new Date(year, month, dayOfMonth);
const dayNumber = date.getDay();

const expectedNumberLetters = getNumLettersForDay(dayNumber);

const wiggleRoom = 10;

// Just a warning for now
if (Math.abs(expectedNumberLetters - numberLetters) > wiggleRoom) {
console.warn(
`Custom puzzle for ${dateString} is more than ${wiggleRoom} letters off of the expected number of letters. (Has ${numberLetters}; expected ${expectedNumberLetters} +/ ${wiggleRoom})`,
);
}
}
});
});
8 changes: 5 additions & 3 deletions src/logic/getNumLettersForDay.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export function getNumLettersForDay() {
const today = new Date().getDay();
export function getNumLettersForDay(dayNumber) {
if (dayNumber === undefined) {
dayNumber = new Date().getDay();
}

const wordLengths = [
60, // Sunday
Expand All @@ -11,5 +13,5 @@ export function getNumLettersForDay() {
50,
];

return wordLengths[today];
return wordLengths[dayNumber];
}

0 comments on commit edaa3ac

Please sign in to comment.