Skip to content

R50-T008: Cover the failure behavior of TrieNode (Trie.js) [Data-Structures/Tree/Trie.js] - #106

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

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

Conversation

@stavansomaiya

@stavansomaiya stavansomaiya commented Nov 23, 2026 •

Copy link
Copy Markdown

Description

Task R50-T008 · task 8 of 10 · Failure-path coverage
Issue #8 · Cover the failure behavior of TrieNode
Student Stavan Somaiya · URN e26b07f1064
File → function Data-Structures/Tree/Trie.js → TrieNode (line 1 at the pinned commit, line 16 after this PR)
Pinned source TheAlgorithms/JavaScript @ 5c39e87 · Trie.js#L1
Branch stavansomaiya:student/e26b07f1064/R50-T008 → S-AI-Projects:master (one commit on 5c39e87)
Outcome The absent-result branches are covered and proven to be reached. No defect found; the implementation is unchanged.

Summary

The Trie in Data-Structures/Tree/Trie.js is total: it never throws for a missing word. This PR tests its absent-result paths, which return null, false, 0 or [], instead of inventing an exception. It documents them on TrieNode and proves the fixture inputs actually reach the named branches.

Files changed

File Lines What changed
Data-Structures/Tree/Trie.js +16 / −1 JSDoc on TrieNode describing the absent-result paths; TrieNode exported (no logic change)
Data-Structures/Tree/test/Trie.test.js +94 / −0 new test file: 7 tests on missing / prefix-only / non-string / repeated inputs
work/R50-T008/ (9 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: #8

This PR covers only R50-T008. 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

  • Data-Structures/Tree/test/Trie.test.js (new). Fixture: insert car, cat and dog. 'cab' reaches findPrefix's return null // No such prefix exists (line 76), so contains is false, findOccurrences is 0 and findAllWords is []. Prefix 'ca' exists with count === 0, so it is not a word, while 'car' is (line 135). Non-string input is ignored (lines 52, 70). Failed lookups leave the trie unchanged and create no nodes, and a successful insert('cab') follows. Repeated insertion of 'car' reuses the node, giving count 2.
  • Data-Structures/Tree/Trie.js: JSDoc on TrieNode describing the absent-result paths. TrieNode is now also exported. No logic change.
  • work/R50-T008/: check.py, baseline.txt, and evidence.mjs, which loads an instrumented in-memory copy proving contains('cab') hits line 76 at index 2 ('b') and findPrefix(5) hits line 70. Plus 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, 7/7 passed
$ npx vitest run Data-Structures/Tree/test/Trie.test.js --reporter=verbose
 RUN  v1.2.1 <clean checkout of student/e26b07f1064/R50-T008>
 ✓ Data-Structures/Tree/test/Trie.test.js > TrieNode > a node created without a parent has parent null and count 0
 ✓ Data-Structures/Tree/test/Trie.test.js > TrieNode > a child node keeps a reference to its parent
 ✓ Data-Structures/Tree/test/Trie.test.js > Trie absent-result paths (no exceptions are thrown) > missing word 'cab' reaches the null branch of findPrefix
 ✓ Data-Structures/Tree/test/Trie.test.js > Trie absent-result paths (no exceptions are thrown) > prefix 'ca' is not a complete word, but 'car' is
 ✓ Data-Structures/Tree/test/Trie.test.js > Trie absent-result paths (no exceptions are thrown) > non-string input is ignored, not thrown (lines 52 and 70)
 ✓ Data-Structures/Tree/test/Trie.test.js > Trie absent-result paths (no exceptions are thrown) > failed lookups leave the trie unchanged and later calls succeed
 ✓ Data-Structures/Tree/test/Trie.test.js > Trie absent-result paths (no exceptions are thrown) > repeated insertion increments the count of the same node
 Test Files  1 passed (1)
      Tests  7 passed (7)
Automated check: python3 work/R50-T008/check.py, exit 0
$ python3 work/R50-T008/check.py
[PASS] TrieNode without parent
       input:    "new TrieNode('c')"
       expected: {"parent": null, "count": 0}
       actual:   {"parent": null, "count": 0}
       source:   Trie.js:21-22 undefined parent -> null
[PASS] findPrefix('cab') hits the no-such-prefix branch
       input:    "cab"
       expected: null
       actual:   null
       source:   Trie.js:76 return null // No such prefix exists
[PASS] contains / findOccurrences / findAllWords for 'cab'
       input:    "cab"
       expected: {"contains": false, "occurrences": 0, "words": []}
       actual:   {"contains": false, "occurrences": 0, "words": []}
       source:   Trie.js:126,135,142 null node -> empty result
[PASS] prefix 'ca' vs complete word 'car'
       input:    ["ca", "car"]
       expected: {"ca": [false, 0], "car": [true, 1]}
       actual:   {"ca": [false, 0], "car": [true, 1]}
       source:   Trie.js:135 node.count !== 0
[PASS] trie unchanged after failed lookups
       input:    "findAllWords(\"\") after the failed lookups"
       expected: [{"word": "car", "count": 1}, {"word": "cat", "count": 1}, {"word": "dog", "count": 1}]
       actual:   [{"word": "car", "count": 1}, {"word": "cat", "count": 1}, {"word": "dog", "count": 1}]
       source:   Trie.js:69-80 findPrefix only reads children
[PASS] successful call after the failure: insert('cab')
       input:    "cab"
       expected: {"contains": true, "occurrences": 1}
       actual:   {"contains": true, "occurrences": 1}
       source:   Trie.js:51-66 insert creates missing nodes
[PASS] repeated insertion of 'car'
       input:    ["car", "car"]
       expected: 2
       actual:   2
       source:   Trie.js:66 node.count += 1
[PASS] branch reach (instrumented copy): which return null fired
       input:    ["findPrefix('ca')", "contains('cab')", "findPrefix(5)"]
       expected: [{"line": 76, "word": "cab", "i": 2}, {"line": 70, "word": 5}]
       actual:   [{"line": 76, "word": "cab", "i": 2}, {"line": 70, "word": 5}]
       source:   Trie.js:76 missing child at index 2 ('b'); Trie.js:70 non-string guard
[KILLED] mutant prefix-only node counted as a word (drop count check): '    return node !== null && node.count !== 0\n' -> '    return node !== null\n'
       failed:  Trie absent-result paths (no exceptions are thrown) prefix 'ca' is not a complete word, but 'car' is
[KILLED] mutant missing-child branch returns the partial prefix instead of null: '=== undefined) return null // No such prefix exists\n' -> '=== undefined) return node // No such prefix exists\n'
       failed:  Trie absent-result paths (no exceptions are thrown) missing word 'cab' reaches the null branch of findPrefix
       failed:  Trie absent-result paths (no exceptions are thrown) failed lookups leave the trie unchanged and later calls succeed

focused tests: 7/7 passed  (node_modules/.bin/vitest run Data-Structures/Tree/test/Trie.test.js --reporter=json --outputFile=work/R50-T008/vitest-focused.raw.json)
npm test:      11977/11977 passed  (npm test -- --reporter=json --outputFile=work/R50-T008/vitest-full.raw.json)

R50-T008: all checks passed
(exit 0)

Testing

Automated check: python3 work/R50-T008/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-T008
git checkout FETCH_HEAD
python3 work/R50-T008/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 Data-Structures/Tree/test/Trie.test.js ✅ 7/7 passed
Fixture evidence (input / expected / actual / source line) node work/R50-T008/evidence.mjs ✅ 8/8 rows match
Mutant (temporary copy): prefix-only node counted as a word (drop count check) applied by check.py ✅ caught by prefix 'ca' is not a complete word, but 'car' is
Mutant (temporary copy): missing-child branch returns the partial prefix instead of null applied by check.py ✅ caught by missing word 'cab' reaches the null branch of findPrefix
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 #8) Evidence in this PR
1 The checked behavior is tied to TrieNode in Data-Structures/Tree/Trie.js; every assertion has an explicit expected value. ✅ Assertions use literals: null, false, true, 0, [], counts 1 and 2, and the literal word list after the failed lookups.
2 Insert "car", "cat" and "dog"; distinguish complete "car" from prefix "ca"; check repeated insertion using the source API. ✅ prefix 'ca' is not a complete word, but 'car' is inserts car, cat and dog: contains('car') === true, contains('ca') === false. repeated insertion increments the count of the same node inserts 'car' again; the same node then has count 2.
3 Existing supported behavior and the public interface are preserved unless a failing regression demonstrates and documents a defect. ✅ No logic change (JSDoc, and TrieNode exported). Every Trie method is unchanged. The full suite passes (11977/11977).
4 The tested branch is named by source line and the input actually reaches it. ✅ Branch: findPrefix line 76, return null // No such prefix exists. An instrumented in-memory copy proves contains('cab') hits line 76 with i = 2 (missing child 'b'), and findPrefix(5) hits line 70. The other branches are named at lines 135, 142, 126 and 52.
5 An ordinary valid call still succeeds after the failure case. ✅ failed lookups leave the trie unchanged and later calls succeed: after the failed lookups, insert('cab') succeeds and contains('cab') === true.

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

Two temporary-copy mutants are each caught by their named test: dropping && node.count !== 0 (line 135) and returning the partial node instead of null at line 76. Side observation, not changed: remove never prunes nodes, because children has no prototype. Answers are still correct; details are in the task README.

Scope: the PR changes only Data-Structures/Tree/Trie.js, its test file and work/R50-T008/. work/R50-T008/ 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
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 (this PR)
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-T008: Cover the failure behavior of TrieNode (Trie.js) [Data-Structures/Tree/Trie.js]

1 participant