phase-1: optimize code
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
import { test } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
|
||||
// Test the markdown fence stripping logic in isolation —
|
||||
// we don't instantiate LLMAnalyst (requires Anthropic SDK + API key).
|
||||
// The regex is: raw.replace(/^```(?:json)?\s*/i, '').replace(/```\s*$/i, '').trim()
|
||||
|
||||
function stripFences(raw) {
|
||||
return raw
|
||||
.replace(/^```(?:json)?\s*/i, '')
|
||||
.replace(/```\s*$/i, '')
|
||||
.trim();
|
||||
}
|
||||
|
||||
const VALID_JSON =
|
||||
'{"summary":"test","sentiment":"BULLISH","affectedIndustries":[],"relatedTickers":[]}';
|
||||
|
||||
test('stripFences: passes clean JSON through unchanged', () => {
|
||||
assert.equal(stripFences(VALID_JSON), VALID_JSON);
|
||||
});
|
||||
|
||||
test('stripFences: strips ```json ... ``` fences', () => {
|
||||
const wrapped = '```json\n' + VALID_JSON + '\n```';
|
||||
assert.equal(stripFences(wrapped), VALID_JSON);
|
||||
});
|
||||
|
||||
test('stripFences: strips ``` ... ``` fences (no language tag)', () => {
|
||||
const wrapped = '```\n' + VALID_JSON + '\n```';
|
||||
assert.equal(stripFences(wrapped), VALID_JSON);
|
||||
});
|
||||
|
||||
test('stripFences: result is valid parseable JSON', () => {
|
||||
const wrapped = '```json\n' + VALID_JSON + '\n```';
|
||||
const parsed = JSON.parse(stripFences(wrapped));
|
||||
assert.equal(parsed.sentiment, 'BULLISH');
|
||||
assert.equal(parsed.summary, 'test');
|
||||
});
|
||||
|
||||
test('stripFences: handles no trailing newline before closing fence', () => {
|
||||
const wrapped = '```json\n' + VALID_JSON + '```';
|
||||
assert.equal(stripFences(wrapped), VALID_JSON);
|
||||
});
|
||||
|
||||
test('stripFences: case-insensitive fence tag', () => {
|
||||
const wrapped = '```JSON\n' + VALID_JSON + '\n```';
|
||||
assert.equal(stripFences(wrapped), VALID_JSON);
|
||||
});
|
||||
Reference in New Issue
Block a user