test: cover format-turns content-type fallbacks and system_other handling (#1421)

Adds unit tests for previously-uncovered branches in
src/entrypoints/format-turns.ts:

- detectContentType: malformed-JSON fall-through (objects and arrays)
  and the default python classification for non-python/non-js code
- formatResultContent: non-string inputs (number, plain object)
- groupTurnsNaturally / formatGroupedContent: the system_other path
  for non-init system turns

Tests only; no source changes. format-turns.ts line coverage rises
from ~86% and the file's non-CLI logic is now fully exercised.

Co-authored-by: hk <solanamobilech@gmail.com>
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
farmer 2026-06-23 05:41:41 +08:00 committed by GitHub
parent 6b8063043e
commit e452eb9dce
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -437,3 +437,51 @@ describe("integration tests", () => {
expect(actualOutput).toBe(expectedOutput);
});
});
describe("detectContentType fallbacks", () => {
test("falls back to text for malformed JSON objects", () => {
// Looks like an object (starts with { ends with }) but does not parse.
expect(detectContentType("{not valid json}")).toBe("text");
});
test("falls back to text for malformed JSON arrays", () => {
// Looks like an array (starts with [ ends with ]) but does not parse.
expect(detectContentType("[not, valid, json]")).toBe("text");
});
test("classifies non-python, non-js code keywords as python by default", () => {
// Contains a code keyword ("class ") but matches neither the python-specific
// nor the javascript-specific checks, so it hits the default branch.
expect(detectContentType("class Foo {}")).toBe("python");
});
});
describe("formatResultContent non-string input", () => {
test("handles a numeric (non-string) result value", () => {
const result = formatResultContent(42);
expect(result).toContain("42");
});
test("handles a plain object (non-string, non-text-array) result value", () => {
const result = formatResultContent({ status: "ok" });
expect(typeof result).toBe("string");
expect(result.length).toBeGreaterThan(0);
});
});
describe("system_other handling", () => {
test("groups a non-init system turn as system_other", () => {
const systemTurn: Turn = { type: "system", subtype: "some_other_subtype" };
const grouped = groupTurnsNaturally([systemTurn]);
expect(grouped).toHaveLength(1);
expect(grouped[0]?.type).toBe("system_other");
expect(grouped[0]?.data).toEqual(systemTurn);
});
test("renders a system_other group as a System Message section", () => {
const markdown = formatGroupedContent([
{ type: "system_other", data: { type: "system" } as Turn },
]);
expect(markdown).toContain("## ⚙️ System Message");
});
});