From e452eb9dce5f3ab14b90cf9386247fbcb3c4ac92 Mon Sep 17 00:00:00 2001 From: farmer <101955570+farmer-data@users.noreply.github.com> Date: Tue, 23 Jun 2026 05:41:41 +0800 Subject: [PATCH] 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 Co-authored-by: Claude Opus 4.8 --- test/format-turns.test.ts | 48 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/test/format-turns.test.ts b/test/format-turns.test.ts index bb26f2e5..e6ac058b 100644 --- a/test/format-turns.test.ts +++ b/test/format-turns.test.ts @@ -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"); + }); +});