Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions test/test-extension.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ DIR="$(cd "$(dirname "$0")" && pwd)"

echo "==> Running idstack Chrome Extension test suite..."
node "$DIR/test-manifest.js"
node "$DIR/test-parser-helper.js"
node "$DIR/test-prompts.js"
node "$DIR/test-extractor.js"
node "$DIR/test-crawler.js"
Expand Down
48 changes: 48 additions & 0 deletions test/test-parser-helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const assert = require('assert');
const { cleanJsonResponse } = require('../extension/background/parser-helper.cjs');

console.log("==> Running parser-helper tests...");

// Test 1: Plain JSON object
const plainObj = '{"key": "value"}';
const parsedPlainObj = cleanJsonResponse(plainObj);
assert.deepStrictEqual(parsedPlainObj, { key: 'value' });

// Test 2: Plain JSON array
const plainArr = '["item1", "item2"]';
const parsedPlainArr = cleanJsonResponse(plainArr);
assert.deepStrictEqual(parsedPlainArr, ["item1", "item2"]);

// Test 3: Markdown fenced JSON with ```json
const fencedJson = '```json\n{"summary": {"bloomsLevel": "Remember"}}\n```';
const parsedFencedJson = cleanJsonResponse(fencedJson);
assert.deepStrictEqual(parsedFencedJson, { summary: { bloomsLevel: "Remember" } });

// Test 4: Markdown fenced JSON with ```
const fencedPlain = '```\n{"summary": {"bloomsLevel": "Analyze"}}\n```';
const parsedFencedPlain = cleanJsonResponse(fencedPlain);
assert.deepStrictEqual(parsedFencedPlain, { summary: { bloomsLevel: "Analyze" } });

// Test 5: JSON with leading and trailing whitespace
const whitespaceJson = ' \n\t{"key": "value"} \n ';
const parsedWhitespace = cleanJsonResponse(whitespaceJson);
assert.deepStrictEqual(parsedWhitespace, { key: "value" });

// Test 6: Fenced JSON with leading and trailing whitespace
const whitespaceFenced = ' \n```json\n{"key": "value"}\n```\n ';
const parsedWhitespaceFenced = cleanJsonResponse(whitespaceFenced);
assert.deepStrictEqual(parsedWhitespaceFenced, { key: "value" });

// Test 7: Invalid JSON should throw SyntaxError
const invalidJson = '```json\n{"key": "value",}\n```';
let threwError = false;
try {
cleanJsonResponse(invalidJson);
} catch (e) {
if (e instanceof SyntaxError) {
threwError = true;
}
}
assert.strictEqual(threwError, true, "Invalid JSON should throw SyntaxError");

console.log("==> parser-helper tests passed!");
Loading