|
| 1 | +import type { SimpleHighlight } from "@opentui/core" |
| 2 | +import { describe, expect, test } from "bun:test" |
| 3 | +import { |
| 4 | + OUTPUT_LANGUAGE_LIMIT, |
| 5 | + detectOutputLanguage, |
| 6 | + outputLanguage, |
| 7 | + previewHighlights, |
| 8 | +} from "../../src/util/tool-output" |
| 9 | + |
| 10 | +const json = '{"p": "/console/invite", "els": ["- textbox \\"[email protected]\\" [ref=e174]"]}' |
| 11 | + |
| 12 | +describe("outputLanguage", () => { |
| 13 | + test("leaves output plain when nothing is configured", () => { |
| 14 | + expect(outputLanguage(undefined, "browser", json)).toBeUndefined() |
| 15 | + expect(outputLanguage({}, "browser", json)).toBeUndefined() |
| 16 | + }) |
| 17 | + |
| 18 | + test("takes the exact tool name over any glob", () => { |
| 19 | + const settings = { "*": "auto", "browser*": "yaml", browser: "json" } |
| 20 | + expect(outputLanguage(settings, "browser", "x")).toBe("json") |
| 21 | + }) |
| 22 | + |
| 23 | + test("takes the glob with the most literal characters", () => { |
| 24 | + const settings = { "*": "markdown", "github_*": "json", "github_create_*": "yaml", "gh?b": "toml" } |
| 25 | + expect(outputLanguage(settings, "github_create_issue", "x")).toBe("yaml") |
| 26 | + expect(outputLanguage(settings, "github_list", "x")).toBe("json") |
| 27 | + expect(outputLanguage(settings, "gh_b", "x")).toBe("toml") |
| 28 | + expect(outputLanguage(settings, "context7_query", "x")).toBe("markdown") |
| 29 | + }) |
| 30 | + |
| 31 | + test("treats regex characters in a name literally", () => { |
| 32 | + expect(outputLanguage({ "a.b*": "json" }, "axb1", "x")).toBeUndefined() |
| 33 | + expect(outputLanguage({ "a.b*": "json" }, "a.b1", "x")).toBe("json") |
| 34 | + }) |
| 35 | + |
| 36 | + test("none switches a tool off under a broader glob", () => { |
| 37 | + expect(outputLanguage({ "*": "json", todo: "none" }, "todo", json)).toBeUndefined() |
| 38 | + }) |
| 39 | + |
| 40 | + test("auto colors only output recognized as JSON", () => { |
| 41 | + expect(outputLanguage({ "*": "auto" }, "browser", json)).toBe("json") |
| 42 | + expect(outputLanguage({ "*": "auto" }, "browser", "Navigated to https://x")).toBeUndefined() |
| 43 | + }) |
| 44 | + |
| 45 | + test("never colors output past the size limit", () => { |
| 46 | + const big = `[${'"x",'.repeat(OUTPUT_LANGUAGE_LIMIT / 4)}"x"]` |
| 47 | + expect(outputLanguage({ browser: "json" }, "browser", big)).toBeUndefined() |
| 48 | + }) |
| 49 | +}) |
| 50 | + |
| 51 | +describe("detectOutputLanguage", () => { |
| 52 | + test("recognizes objects and arrays that parse", () => { |
| 53 | + expect(detectOutputLanguage(` ${json}\n`)).toBe("json") |
| 54 | + expect(detectOutputLanguage("[1, 2, 3]")).toBe("json") |
| 55 | + }) |
| 56 | + |
| 57 | + test("rejects text that only looks like JSON", () => { |
| 58 | + expect(detectOutputLanguage("{not json}")).toBeUndefined() |
| 59 | + expect(detectOutputLanguage("[ref=e174] clicked")).toBeUndefined() |
| 60 | + expect(detectOutputLanguage('{"a": 1} trailing')).toBeUndefined() |
| 61 | + expect(detectOutputLanguage('"a string"')).toBeUndefined() |
| 62 | + expect(detectOutputLanguage("42")).toBeUndefined() |
| 63 | + expect(detectOutputLanguage("")).toBeUndefined() |
| 64 | + }) |
| 65 | + |
| 66 | + test("answers the same content again from its cache", () => { |
| 67 | + const text = `{"n": ${Date.now()}}` |
| 68 | + expect(detectOutputLanguage(text)).toBe("json") |
| 69 | + expect(detectOutputLanguage(text)).toBe("json") |
| 70 | + }) |
| 71 | +}) |
| 72 | + |
| 73 | +describe("previewHighlights", () => { |
| 74 | + const output = '{\n "p": "/x",\n "els": [1, 2]\n}' |
| 75 | + const highlights: SimpleHighlight[] = [ |
| 76 | + [4, 7, "property"], |
| 77 | + [9, 13, "string"], |
| 78 | + [17, 22, "property"], |
| 79 | + [25, 26, "number"], |
| 80 | + ] |
| 81 | + |
| 82 | + test("keeps every highlight when the whole output is shown", () => { |
| 83 | + expect(previewHighlights(output, output, highlights)).toBe(highlights) |
| 84 | + }) |
| 85 | + |
| 86 | + test("keeps the highlights inside a collapsed preview and cuts the one it splits", () => { |
| 87 | + const shown = output.slice(0, 20) + "…" |
| 88 | + expect(previewHighlights(shown, output, highlights)).toEqual([ |
| 89 | + [4, 7, "property"], |
| 90 | + [9, 13, "string"], |
| 91 | + [17, 20, "property"], |
| 92 | + ]) |
| 93 | + }) |
| 94 | + |
| 95 | + test("gives a line-collapsed preview the colors of the lines it keeps", () => { |
| 96 | + const shown = output.split("\n").slice(0, 2).join("\n") + "\n…" |
| 97 | + expect(previewHighlights(shown, output, highlights)).toEqual([ |
| 98 | + [4, 7, "property"], |
| 99 | + [9, 13, "string"], |
| 100 | + ]) |
| 101 | + }) |
| 102 | +}) |
0 commit comments