|
| 1 | +import { describe, expect, test } from "bun:test"; |
| 2 | +import { __test } from "../index"; |
| 3 | + |
| 4 | +const { ansi256FromRgb, colorToEscape, detectColorMode, detectLanguage, tokenize } = __test; |
| 5 | + |
| 6 | +describe("ansi256FromRgb", () => { |
| 7 | + test("black maps to index 16", () => { |
| 8 | + expect(ansi256FromRgb(0, 0, 0)).toBe(16); |
| 9 | + }); |
| 10 | + |
| 11 | + test("pure red maps to cube red", () => { |
| 12 | + expect(ansi256FromRgb(255, 0, 0)).toBe(196); |
| 13 | + }); |
| 14 | + |
| 15 | + test("pure green maps to cube green", () => { |
| 16 | + expect(ansi256FromRgb(0, 255, 0)).toBe(46); |
| 17 | + }); |
| 18 | + |
| 19 | + test("pure blue maps to cube blue", () => { |
| 20 | + expect(ansi256FromRgb(0, 0, 255)).toBe(21); |
| 21 | + }); |
| 22 | + |
| 23 | + test("grey values map to grey ramp", () => { |
| 24 | + const idx = ansi256FromRgb(128, 128, 128); |
| 25 | + // Should be in the grey ramp range (232-255) |
| 26 | + expect(idx).toBeGreaterThanOrEqual(232); |
| 27 | + expect(idx).toBeLessThanOrEqual(255); |
| 28 | + }); |
| 29 | +}); |
| 30 | + |
| 31 | +describe("colorToEscape", () => { |
| 32 | + test("palette index < 8 uses standard ANSI codes", () => { |
| 33 | + const color = { r: 1, g: 0, b: 0, a: 0 }; // palette index 1 |
| 34 | + expect(colorToEscape(color, true, "truecolor")).toBe("\x1b[31m"); // fg red |
| 35 | + expect(colorToEscape(color, false, "truecolor")).toBe("\x1b[41m"); // bg red |
| 36 | + }); |
| 37 | + |
| 38 | + test("palette index 8-15 uses bright ANSI codes", () => { |
| 39 | + const color = { r: 9, g: 0, b: 0, a: 0 }; // bright red |
| 40 | + expect(colorToEscape(color, true, "truecolor")).toBe("\x1b[91m"); |
| 41 | + }); |
| 42 | + |
| 43 | + test("alpha=1 returns terminal default", () => { |
| 44 | + const color = { r: 0, g: 0, b: 0, a: 1 }; |
| 45 | + expect(colorToEscape(color, true, "truecolor")).toBe("\x1b[39m"); |
| 46 | + expect(colorToEscape(color, false, "truecolor")).toBe("\x1b[49m"); |
| 47 | + }); |
| 48 | + |
| 49 | + test("truecolor uses RGB escape", () => { |
| 50 | + const color = { r: 100, g: 150, b: 200, a: 255 }; |
| 51 | + expect(colorToEscape(color, true, "truecolor")).toBe("\x1b[38;2;100;150;200m"); |
| 52 | + }); |
| 53 | + |
| 54 | + test("color256 uses 256-color escape", () => { |
| 55 | + const color = { r: 100, g: 150, b: 200, a: 255 }; |
| 56 | + const result = colorToEscape(color, true, "color256"); |
| 57 | + expect(result).toMatch(/^\x1b\[38;5;\d+m$/); |
| 58 | + }); |
| 59 | +}); |
| 60 | + |
| 61 | +describe("detectColorMode", () => { |
| 62 | + test("returns ansi for ansi-containing theme names", () => { |
| 63 | + expect(detectColorMode("ansi")).toBe("ansi"); |
| 64 | + expect(detectColorMode("base16-ansi-dark")).toBe("ansi"); |
| 65 | + }); |
| 66 | + |
| 67 | + test("returns truecolor or color256 for non-ansi themes", () => { |
| 68 | + const mode = detectColorMode("monokai"); |
| 69 | + expect(["truecolor", "color256"]).toContain(mode); |
| 70 | + }); |
| 71 | +}); |
| 72 | + |
| 73 | +describe("detectLanguage", () => { |
| 74 | + test("detects language from file extension", () => { |
| 75 | + expect(detectLanguage("index.ts")).toBe("ts"); |
| 76 | + expect(detectLanguage("main.py")).toBe("py"); |
| 77 | + expect(detectLanguage("style.css")).toBe("css"); |
| 78 | + }); |
| 79 | + |
| 80 | + test("detects language from known filenames", () => { |
| 81 | + expect(detectLanguage("Makefile")).toBe("makefile"); |
| 82 | + expect(detectLanguage("Dockerfile")).toBe("dockerfile"); |
| 83 | + }); |
| 84 | + |
| 85 | + test("returns null for unknown extensions", () => { |
| 86 | + expect(detectLanguage("file.xyz123")).toBeNull(); |
| 87 | + }); |
| 88 | +}); |
| 89 | + |
| 90 | +describe("tokenize", () => { |
| 91 | + test("returns array of tokens", () => { |
| 92 | + const result = tokenize("hello world"); |
| 93 | + expect(Array.isArray(result)).toBe(true); |
| 94 | + expect(result.length).toBeGreaterThan(0); |
| 95 | + }); |
| 96 | + |
| 97 | + test("preserves original text when joined", () => { |
| 98 | + const text = "foo bar baz"; |
| 99 | + const tokens = tokenize(text); |
| 100 | + expect(tokens.join("")).toBe(text); |
| 101 | + }); |
| 102 | +}); |
0 commit comments