Docs/Skills and code/tests/

tests/llm-parse-json.test.js

Code28 lines1.3 KBtests/

import test from "node:test";
import assert from "node:assert/strict";
import { parseJson } from "../src/llm.js";

test("parseJson keeps only the balanced object when junk follows it inside a code fence", () => {
  assert.deepEqual(parseJson('```json\n{"results":[{"channelUrl":"x"}]}"\n```'), { results: [{ channel_url: "x" }] });
});

test("parseJson repairs a full-width colon or dropped quote after a key, only after a normal parse fails", () => {
  assert.deepEqual(parseJson('{"results":[{"confidence:0.95,"tags":["a"]}]}'), { results: [{ confidence: 0.95, tags: ["a"] }] });
  assert.deepEqual(parseJson('{"results":[{"confidence:0.95,"name":"x"}]}'), { results: [{ confidence: 0.95, name: "x" }] });
});

test("parseJson leaves valid JSON strings that contain colons alone", () => {
  assert.deepEqual(parseJson('{"rationale":"Note: \\"a:1\\" is a ratio","confidence":0.5}'), {
    rationale: 'Note: "a:1" is a ratio',
    confidence: 0.5
  });
});

test("parseJson still rejects output that is not JSON", () => {
  assert.throws(() => parseJson("I cannot help with that."), /Unable to parse JSON/);
});

test("parseJson repairs a dropped opening quote on a key", () => {
  assert.deepEqual(parseJson('{"results":[{"a":["x"],topic_tags":["y"],"b":1}]}'), { results: [{ a: ["x"], topic_tags: ["y"], b: 1 }] });
});

Shown as it is in the repository at build time (2026-11-03); the home directory appears as <home>. If this page and the file differ, the file is right. Back to the catalogue.