Skip to content

R50-T002: Define the supported boundaries of checkKeywordValidity (KeywordShiftedAlphabet.js) [Ciphers/KeywordShiftedAlphabet.js] - #100

Open
stavansomaiya wants to merge 1 commit into
S-AI-Projects:masterfrom
stavansomaiya:student/e26b07f1064/R50-T002
Open

stavansomaiya wants to merge 1 commit into
S-AI-Projects:masterfrom
stavansomaiya:student/e26b07f1064/R50-T002

Conversation

@stavansomaiya

@stavansomaiya stavansomaiya commented Nov 23, 2026 •

Copy link
Copy Markdown

Description

Task R50-T002 · task 2 of 10 · Boundary cases
Issue #2 · Define the supported boundaries of checkKeywordValidity
Student Stavan Somaiya · URN e26b07f1064
File → function Ciphers/KeywordShiftedAlphabet.js → checkKeywordValidity (line 48 at the pinned commit, line 65 after this PR)
Pinned source TheAlgorithms/JavaScript @ 5c39e87 · KeywordShiftedAlphabet.js#L48
Branch stavansomaiya:student/e26b07f1064/R50-T002 → S-AI-Projects:master (one commit on 5c39e87)
Outcome Defect found and fixed: repeated-letter keywords were accepted because return false inside forEach was ignored (forEach → every).

Summary

Makes the supported boundary of checkKeywordValidity explicit, and fixes the defect that the boundary tests exposed. The file states "Keyword must be a word where each character can occur only once" (line 3). But return false inside forEach only returned from the callback, so the function always returned true. Repeated-letter keywords were accepted and the cipher no longer round-tripped: on the original code, decrypt('hello', encrypt('hello', 'd')) returned 'c'.

Files changed

File Lines What changed
Ciphers/KeywordShiftedAlphabet.js +25 / −8 forEach → every fix, boundary JSDoc, export, two header typos fixed
Ciphers/test/KeywordShiftedAlphabet.test.js +53 / −1 7 boundary tests (each side of the boundary + unsupported empty keyword)
work/R50-T002/ (10 files) evidence check.py runner, README.md write-up, evidence.mjs, results.json and logs; coursework only, kept out of any upstream PR

Related Issue

Fixes: #2

This PR covers only R50-T002. Each of the 10 tasks has its own branch, commit and PR; see All 10 tasks under Additional Notes.

Type of Change

  • Bug Fix
  • New Feature
  • UI/UX Improvement
  • Performance Improvement
  • Tests
  • Documentation
  • Refactoring / Maintenance
  • CI/CD

Changes Made

  • Ciphers/KeywordShiftedAlphabet.js: forEach replaced by every, which returns false on the first repeated character. Same algorithm and same case-sensitive comparison; no other guard changed. JSDoc documents the supported boundaries. checkKeywordValidity is now also exported for testing; encrypt and decrypt are unchanged. Two typos in the file header are fixed: the table row listed W twice and omitted X, and the decryption example was missing a letter (Aogg → Aoggj).
  • Ciphers/test/KeywordShiftedAlphabet.test.js: a test on each side of the boundary. Valid: 'a' (smallest), 'keyword' (ordinary), all 26 letters (largest). Invalid: 'aa', 'hello' and 27 letters, which return false and make encrypt/decrypt throw Invalid keyword!. Unsupported: '', which checkInputs rejects first.
  • work/R50-T002/: check.py, the boundary table (README), baseline.txt (original behaviour), negative-control.txt, evidence.mjs and results.

Screenshots / Recordings

There is no UI, so the recording is the terminal output of a fresh run on 23 Nov 2026. The run used a clean checkout of this branch with a new npm ci from the lockfile. The output is copied verbatim; only the temporary checkout path is replaced by a label.

Focused tests: npx vitest run … --reporter=verbose, 9/9 passed
$ npx vitest run Ciphers/test/KeywordShiftedAlphabet.test.js --reporter=verbose
 RUN  v1.2.1 <clean checkout of student/e26b07f1064/R50-T002>
 ✓ Ciphers/test/KeywordShiftedAlphabet.test.js > Hello world! === decrypt(encrypt(Hello world!))
 ✓ Ciphers/test/KeywordShiftedAlphabet.test.js > The Algorithms === decrypt(encrypt(The Algorithms))
 ✓ Ciphers/test/KeywordShiftedAlphabet.test.js > checkKeywordValidity boundaries > valid side > smallest valid keyword 'a' is accepted
 ✓ Ciphers/test/KeywordShiftedAlphabet.test.js > checkKeywordValidity boundaries > valid side > ordinary keyword 'keyword' is accepted
 ✓ Ciphers/test/KeywordShiftedAlphabet.test.js > checkKeywordValidity boundaries > valid side > largest valid keyword uses all 26 letters exactly once
 ✓ Ciphers/test/KeywordShiftedAlphabet.test.js > checkKeywordValidity boundaries > invalid side > smallest repeated keyword 'aa' is rejected
 ✓ Ciphers/test/KeywordShiftedAlphabet.test.js > checkKeywordValidity boundaries > invalid side > ordinary repeated keyword 'hello' is rejected
 ✓ Ciphers/test/KeywordShiftedAlphabet.test.js > checkKeywordValidity boundaries > invalid side > a 27th letter must repeat one of the 26 and is rejected
 ✓ Ciphers/test/KeywordShiftedAlphabet.test.js > checkKeywordValidity boundaries > invalid side > empty keyword is rejected by checkInputs before validity
 Test Files  1 passed (1)
      Tests  9 passed (9)
Automated check: python3 work/R50-T002/check.py, exit 0
$ python3 work/R50-T002/check.py
[PASS] checkKeywordValidity valid boundary: smallest keyword
       input:    "a"
       expected: true
       actual:   true
       source:   KeywordShiftedAlphabet.js:3 each character can occur only once
[PASS] checkKeywordValidity valid interior: ordinary keyword
       input:    "keyword"
       expected: true
       actual:   true
       source:   KeywordShiftedAlphabet.js:3 each character can occur only once
[PASS] checkKeywordValidity valid boundary: all 26 letters
       input:    "zyxwvutsrqponmlkjihgfedcba"
       expected: true
       actual:   true
       source:   KeywordShiftedAlphabet.js:3 each character can occur only once
[PASS] checkKeywordValidity invalid boundary: smallest repeat
       input:    "aa"
       expected: false
       actual:   false
       source:   KeywordShiftedAlphabet.js:3 each character can occur only once
[PASS] checkKeywordValidity invalid interior: ordinary repeat
       input:    "hello"
       expected: false
       actual:   false
       source:   KeywordShiftedAlphabet.js:3 each character can occur only once
[PASS] checkKeywordValidity invalid: 27 letters
       input:    "zyxwvutsrqponmlkjihgfedcbaa"
       expected: false
       actual:   false
       source:   KeywordShiftedAlphabet.js:3 each character can occur only once
[PASS] encrypt with smallest keyword
       input:    ["a", "abc"]
       expected: "abc"
       actual:   "abc"
       source:   KeywordShiftedAlphabet.js:3 each character can occur only once
[PASS] encrypt with ordinary keyword
       input:    ["keyword", "Hello world!"]
       expected: "Aoggj ujngw!"
       actual:   "Aoggj ujngw!"
       source:   KeywordShiftedAlphabet.js:8 translation table
[PASS] encrypt rejects repeated keyword
       input:    ["aa", "abc"]
       expected: "throws: Invalid keyword!"
       actual:   "throws: Invalid keyword!"
       source:   KeywordShiftedAlphabet.js:103-104 checkInputs -> Invalid keyword!
[PASS] encrypt rejects empty keyword
       input:    ["", "abc"]
       expected: "throws: Both keyword and message must be specified"
       actual:   "throws: Both keyword and message must be specified"
       source:   KeywordShiftedAlphabet.js:99-100 checkInputs
[KILLED] mutant original defect restored (every character accepted): '    return rest.indexOf(char) === -1\n' -> '    return true\n'
       failed:  checkKeywordValidity boundaries invalid side smallest repeated keyword 'aa' is rejected
       failed:  checkKeywordValidity boundaries invalid side ordinary repeated keyword 'hello' is rejected
       failed:  checkKeywordValidity boundaries invalid side a 27th letter must repeat one of the 26 and is rejected

focused tests: 9/9 passed  (node_modules/.bin/vitest run Ciphers/test/KeywordShiftedAlphabet.test.js --reporter=json --outputFile=work/R50-T002/vitest-focused.raw.json)
npm test:      11977/11977 passed  (npm test -- --reporter=json --outputFile=work/R50-T002/vitest-full.raw.json)

R50-T002: all checks passed
(exit 0)

Testing

Automated check: python3 work/R50-T002/check.py installs from the lockfile and runs the focused tests. It then prints input / expected / actual for every fixture row, runs each deliberate mutant in a temporary copy, and finally runs the repository test script in non-watch mode. It exits non-zero if any assertion or step fails, and records every command in commands.log.

To reproduce:

git fetch https://github.com/stavansomaiya/TheAlgorithms-JavaScript.git student/e26b07f1064/R50-T002
git checkout FETCH_HEAD
python3 work/R50-T002/check.py

Recorded results (committed in results.json; tool versions in versions.txt):

Check Command Result
Install from lockfile npm ci --ignore-scripts --no-audit --no-fund ✅ exit 0
Focused tests vitest run Ciphers/test/KeywordShiftedAlphabet.test.js ✅ 9/9 passed
Fixture evidence (input / expected / actual / source line) node work/R50-T002/evidence.mjs ✅ 10/10 rows match
Mutant (temporary copy): original defect restored (every character accepted) applied by check.py ✅ caught by smallest repeated keyword 'aa' is rejected
Repository test script, non-watch npm test (vitest run) ✅ 11977/11977 passed
Code style npm run check-style (Prettier) ✅ passes
Spelling (CI settings) codespell ✅ no new findings

Acceptance criteria → evidence:

# Done when (quoted from #2) Evidence in this PR
1 The checked behavior is tied to checkKeywordValidity in Ciphers/KeywordShiftedAlphabet.js; every assertion has an explicit expected value. ✅ Tests in describe('checkKeywordValidity boundaries') call the exported checkKeywordValidity and assert literal true / false, plus literal results through encrypt / decrypt ('abc', 'Aoggj ujngw!', 'zyx', 'Invalid keyword!').
2 Use the smallest valid input and one ordinary input for checkKeywordValidity from Ciphers/KeywordShiftedAlphabet.js. Write the literal inputs and independently calculated expected outputs in the tests before editing the implementation. The check must name the source line establishing each precondition. ✅ Smallest valid 'a' → true; ordinary 'keyword' → true with encrypt('keyword', 'Hello world!') === 'Aoggj ujngw!'. Expected values come from the contract at line 3 and were recorded before running; the original's behaviour is saved in baseline.txt. Each boundary-table row names its source line (3, 8, 99-100, 104).
3 Existing supported behavior and the public interface are preserved unless a failing regression demonstrates and documents a defect. ✅ Behaviour changes only where a failing regression proved a defect: negative-control.txt shows check.py exiting 1 on the original logic, with the 3 repeated-keyword tests failing. Defect, cause and fix are documented in the README and JSDoc. encrypt / decrypt are unchanged and the 2 existing round-trip tests pass.
4 The boundary table cites the source contract, and every supported boundary is asserted. ✅ The README boundary table has 7 rows (valid smallest / interior / largest; invalid smallest / interior / 27 letters; unsupported empty). Each cites its source line and each is asserted by one named test.
5 No arbitrary TypeError/RangeError policy is imposed across unrelated functions. ✅ No new exception type or guard. The only logic change is forEach → every inside checkKeywordValidity. The errors asserted are the file's existing Invalid keyword! and Both keyword and message must be specified. Cases the source leaves undefined, such as 'Aa', get no new rule.

Evidence files: check.py · README.md (contract, fixture, baseline, findings) · evidence.mjs · results.json · vitest-focused.json

Checklist

  • I have tested my changes locally.
  • I have added or updated tests, if needed.
  • I have updated documentation, if needed.
  • I have followed the project's coding guidelines.
  • I have run the relevant tests and checks.
  • I have addressed previous review feedback, if applicable. (No review feedback yet.)

Additional Notes

negative-control.txt shows check.py exiting 1 with the original always-true behaviour: the 3 repeated-keyword tests fail and the other 6 pass. check.py also re-applies that defect to a temporary copy on every run and requires smallest repeated keyword 'aa' is rejected to fail on it. No rule was added for cases the source leaves undefined, such as mixed-case repeats like 'Aa', and no TypeError/RangeError policy was introduced.

Scope: the PR changes only Ciphers/KeywordShiftedAlphabet.js, its test file and work/R50-T002/. work/R50-T002/ is coursework evidence and would be removed from any later upstream PR.

All 10 tasks (one PR each)
Task Issue Branch Pull request
R50-T001: Lock down the contract of setBit #1 R50-T001 #99
R50-T002: Define the supported boundaries of checkKeywordValidity #2 R50-T002 #100 (this PR)
R50-T003: Cross-check getParentPosition #3 R50-T003 #101
R50-T004: Test an invariant of swap #4 R50-T004 #102
R50-T005: Make verification repeatable for newGeneration #5 R50-T005 #103
R50-T006: Simplify one responsibility in countLetters #6 R50-T006 #104
R50-T007: Publish a runnable example for DisjointSetTreeNode #7 R50-T007 #105
R50-T008: Cover the failure behavior of TrieNode #8 R50-T008 #106
R50-T009: Measure the work done by fibonacci #9 R50-T009 #107
R50-T010: Strengthen the tests of OpenKnightTour #10 R50-T010 #108

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Stavan Somaiya — R50-T002: Define the supported boundaries of checkKeywordValidity (KeywordShiftedAlphabet.js) [Ciphers/KeywordShiftedAlphabet.js]

1 participant