-
Notifications
You must be signed in to change notification settings - Fork 16
Added Node.js unit tests for core JavaScript utilities #636
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
1da3a5d
Added Node.js unit tests for core JavaScript utilities
xr843 767397e
Merge branch 'main' into feat/node-tests
xr843 d2317a7
Address review feedback: relocate tests, improve assertions, add cove…
xr843 13d2f57
Merge remote-tracking branch 'upstream/main' into feat/node-tests
xr843 0815a59
refactor: load search_functions.js directly, add handleSearchMessage …
xr843 347f68c
test: add +-, --, -+ prefix combination tests for handleSearchMessage
xr843 11747da
test: address review feedback — add html-validate and missing test cases
xr843 bc8f37c
fix(test): add HTML validation in categoryName loop + flip assertion …
xr843 8a1e6d5
fix(search): use numeric for-loop instead of for-in on positions array
xr843 e5bd70e
test(search): flip test data to catch greedy blurb selection
xr843 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -88,6 +88,7 @@ exclude: | |
| - scripts/ | ||
| - .github/ | ||
| - .obsidian/ | ||
| - assets/js/tests/ | ||
|
|
||
| permalink: pretty | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| const { describe, it } = require('node:test'); | ||
| const assert = require('node:assert/strict'); | ||
| const vm = require('node:vm'); | ||
| const fs = require('node:fs'); | ||
| const path = require('node:path'); | ||
|
|
||
| // Load only the cyrb53 function from buggytrack.js | ||
| // The file also creates a BuggyTracker that needs `document`, so we | ||
| // provide minimal DOM stubs to let the file evaluate without errors. | ||
| const src = fs.readFileSync( | ||
| path.join(__dirname, '..', 'buggytrack.js'), | ||
| 'utf-8' | ||
| ); | ||
|
|
||
| // vm.createContext() creates a bare sandbox with no built-in globals. | ||
| // Unlike the main Node.js runtime, Math, Array, etc. must be provided explicitly. | ||
| const sandbox = { | ||
| Math, | ||
|
khemarato marked this conversation as resolved.
|
||
| console, | ||
| localStorage: { | ||
| getItem() { return null; }, | ||
| setItem() {}, | ||
| }, | ||
| window: { WEBSITE_SECTION: 'test' }, | ||
| gtag() {}, | ||
| document: { | ||
| createElement() { | ||
| return { href: '', host: '', hostname: '', pathname: '' }; | ||
| }, | ||
| location: { host: 'example.com', hostname: 'example.com', pathname: '/', search: '', href: 'https://example.com/' }, | ||
| addEventListener() {}, | ||
| referrer: '', | ||
| cookie: '', | ||
| }, | ||
| }; | ||
| // BuggyTracker references `document` as a bare global | ||
| sandbox.self = sandbox; | ||
| vm.createContext(sandbox); | ||
| vm.runInContext(src + '\nthis.cyrb53 = cyrb53;\n', sandbox); | ||
|
|
||
| const { cyrb53 } = sandbox; | ||
|
|
||
| describe('cyrb53', () => { | ||
| it('returns a number', () => { | ||
| assert.equal(typeof cyrb53('hello'), 'number'); | ||
| }); | ||
|
|
||
| it('is deterministic (same input gives same output)', () => { | ||
| const a = cyrb53('test string'); | ||
| const b = cyrb53('test string'); | ||
| assert.equal(a, b); | ||
| }); | ||
|
|
||
| it('produces different hashes for different inputs', () => { | ||
| const a = cyrb53('hello'); | ||
| const b = cyrb53('world'); | ||
| assert.notEqual(a, b); | ||
| }); | ||
|
|
||
| it('handles empty string', () => { | ||
| const h = cyrb53(''); | ||
| assert.equal(typeof h, 'number'); | ||
| assert.ok(Number.isFinite(h)); | ||
| }); | ||
|
|
||
| it('handles unicode strings', () => { | ||
| const h = cyrb53('āgama nibbāna'); | ||
| assert.equal(typeof h, 'number'); | ||
| assert.ok(Number.isFinite(h)); | ||
| }); | ||
|
|
||
| it('returns non-negative values', () => { | ||
| for (const s of ['a', 'bb', 'ccc', '', 'test123']) { | ||
| assert.ok(cyrb53(s) >= 0, `Expected non-negative hash for "${s}"`); | ||
| } | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Excellent! This is why I asked for the flipped test. I had a feeling there was a bug! 😁