From c20a5fb879a4f8cabf553fc2885d379e5de8f40b Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Tue, 10 Mar 2026 12:34:30 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=AA=20[testing=20improvement]=20add=20?= =?UTF-8?q?unit=20tests=20for=20GitHub=20API=20header=20generation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Added `tests/test_api.js` to verify header generation in `src/api.js`. - Included test cases for `githubHeaders` and `apiHeaders`. - Followed the project's existing testing pattern using Node.js `vm` module. Co-authored-by: TauNeutrino <1600410+TauNeutrino@users.noreply.github.com> --- tests/test_api.js | 72 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 tests/test_api.js diff --git a/tests/test_api.js b/tests/test_api.js new file mode 100644 index 0000000..dce3402 --- /dev/null +++ b/tests/test_api.js @@ -0,0 +1,72 @@ +const fs = require('fs'); +const vm = require('vm'); +const path = require('path'); + +console.log("=== Running API Unit Tests ==="); + +// 1. Load Source Code +const apiPath = path.join(__dirname, '..', 'src', 'api.js'); +const constantsPath = path.join(__dirname, '..', 'src', 'constants.js'); + +let apiCode = fs.readFileSync(apiPath, 'utf8'); +let constantsCode = fs.readFileSync(constantsPath, 'utf8'); + +// Strip exports and imports for VM +apiCode = apiCode.replace(/export /g, '').replace(/import .*? from .*?;/g, ''); +constantsCode = constantsCode.replace(/export /g, ''); + +// 2. Setup Mock Environment +const sandbox = { + console: console, +}; + +try { + vm.createContext(sandbox); + // Load constants first as api.js might depend on them + vm.runInContext(constantsCode, sandbox); + vm.runInContext(apiCode, sandbox); + + console.log("--- Testing githubHeaders ---"); + const ghHeaders = sandbox.githubHeaders(); + console.log("Result:", JSON.stringify(ghHeaders)); + + if (ghHeaders['Accept'] !== 'application/vnd.github.v3+json') { + throw new Error(`Expected Accept header 'application/vnd.github.v3+json', but got '${ghHeaders['Accept']}'`); + } + console.log("✅ githubHeaders Test Passed"); + + console.log("--- Testing apiHeaders ---"); + + // Test with token + const token = 'test-token'; + const headersWithToken = sandbox.apiHeaders(token); + console.log("With token:", JSON.stringify(headersWithToken)); + if (headersWithToken['Authorization'] !== `Token ${token}`) { + throw new Error(`Expected Authorization header 'Token ${token}', but got '${headersWithToken['Authorization']}'`); + } + + // Test without token (should use GUEST_TOKEN) + const headersWithoutToken = sandbox.apiHeaders(); + console.log("Without token:", JSON.stringify(headersWithoutToken)); + const guestToken = vm.runInContext('GUEST_TOKEN', sandbox); + if (headersWithoutToken['Authorization'] !== `Token ${guestToken}`) { + throw new Error(`Expected Authorization header 'Token ${guestToken}', but got '${headersWithoutToken['Authorization']}'`); + } + + if (headersWithoutToken['Accept'] !== 'application/json') { + throw new Error(`Expected Accept header 'application/json', but got '${headersWithoutToken['Accept']}'`); + } + + const clientVersion = vm.runInContext('CLIENT_VERSION', sandbox); + if (headersWithoutToken['X-Client-Version'] !== clientVersion) { + throw new Error(`Expected X-Client-Version header '${clientVersion}', but got '${headersWithoutToken['X-Client-Version']}'`); + } + + console.log("✅ apiHeaders Test Passed"); + + console.log("ALL API TESTS PASSED ✅"); + +} catch (e) { + console.error("❌ API Unit Test Error:", e); + process.exit(1); +}