From d00202a68b6d3a7818c8a9190b46da2f15f6d123 Mon Sep 17 00:00:00 2001 From: Alec <135935598+AlePam109@users.noreply.github.com> Date: Wed, 4 Jun 2025 10:12:57 -0700 Subject: [PATCH] Export simpleHash and add tests --- README.md | 10 +++++++++- script.js | 5 ++++- test/simpleHash.test.js | 23 +++++++++++++++++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 test/simpleHash.test.js diff --git a/README.md b/README.md index ed05863..1116275 100644 --- a/README.md +++ b/README.md @@ -7,4 +7,12 @@ The game will include the following activities:
1. Breaking the Substitution Cipher 2. Reverse engineering a hash and finding a collision -3. Launching a DDoS attack / Rate limiting a DDoS attack \ No newline at end of file +3. Launching a DDoS attack / Rate limiting a DDoS attack + +## Running Tests + +To verify the hash function works correctly run: + +```bash +node test/simpleHash.test.js +``` diff --git a/script.js b/script.js index a4030ed..cdf85d5 100644 --- a/script.js +++ b/script.js @@ -588,4 +588,7 @@ function clearPythonOutput() { // Initialize Pyodide when the sandbox is first interacted with document.getElementById('pythonCode').addEventListener('focus', initPyodide); -document.querySelector('.sandbox-controls button').addEventListener('click', initPyodide); \ No newline at end of file +document.querySelector('.sandbox-controls button').addEventListener('click', initPyodide); + +// Export simpleHash for Node.js environments +if (typeof module !== 'undefined') module.exports = { simpleHash }; diff --git a/test/simpleHash.test.js b/test/simpleHash.test.js new file mode 100644 index 0000000..e41282c --- /dev/null +++ b/test/simpleHash.test.js @@ -0,0 +1,23 @@ +const assert = require('assert'); + +// Minimal DOM stubs so script.js can be loaded under Node +const dummy = new Proxy(function () {}, { + get: (target, prop) => { + if (prop === 'width' || prop === 'height') return 0; + return dummy; + }, + apply: () => dummy, + set: () => true, +}); +global.window = dummy; +global.document = dummy; + +const { simpleHash } = require('../script.js'); + +// Test that simpleHash produces expected hash for known input +assert.strictEqual(simpleHash('AaBb'), 'A2B2'); + +// Two different inputs with identical character counts yield the same hash +assert.strictEqual(simpleHash('abc'), simpleHash('cba')); + +console.log('All tests passed.');