|
| 1 | +import { afterEach, describe, expect, test } from "bun:test"; |
| 2 | + |
| 3 | +import { |
| 4 | + BRAND, |
| 5 | + configureTransparentBackground, |
| 6 | + corbitsDark, |
| 7 | + resetTransparentBackgroundLogForTests, |
| 8 | + resolveGround, |
| 9 | + TRANSPARENT_BACKGROUND, |
| 10 | + UI, |
| 11 | + type Theme, |
| 12 | + type TransparentBackgroundEnv, |
| 13 | +} from "./theme.js"; |
| 14 | + |
| 15 | +const SUPPORTED: TransparentBackgroundEnv = { |
| 16 | + CORBITS_TRANSPARENT_BACKGROUND: "1", |
| 17 | + COLORTERM: "truecolor", |
| 18 | + TERM_PROGRAM: "kitty", |
| 19 | +}; |
| 20 | + |
| 21 | +const NO_TRUECOLOR: TransparentBackgroundEnv = { |
| 22 | + CORBITS_TRANSPARENT_BACKGROUND: "1", |
| 23 | + TERM_PROGRAM: "kitty", |
| 24 | +}; |
| 25 | + |
| 26 | +const UNKNOWN_TERMINAL: TransparentBackgroundEnv = { |
| 27 | + CORBITS_TRANSPARENT_BACKGROUND: "true", |
| 28 | + COLORTERM: "truecolor", |
| 29 | + TERM: "xterm-256color", |
| 30 | +}; |
| 31 | + |
| 32 | +// Stand-in for the future light theme: the resolver must honor whatever |
| 33 | +// ground the active theme carries, not just the dark hex. |
| 34 | +const light: Theme = { |
| 35 | + ...corbitsDark, |
| 36 | + name: "corbits-light", |
| 37 | + ground: "#f7ead5", |
| 38 | +}; |
| 39 | + |
| 40 | +afterEach(() => { |
| 41 | + (UI as { ground: string }).ground = corbitsDark.ground; |
| 42 | + resetTransparentBackgroundLogForTests(); |
| 43 | +}); |
| 44 | + |
| 45 | +describe("resolveGround", () => { |
| 46 | + test("default stays opaque without logging", () => { |
| 47 | + let logged = 0; |
| 48 | + for (const theme of [corbitsDark, light]) { |
| 49 | + expect(resolveGround(theme, {}, () => logged++)).toBe(theme.ground); |
| 50 | + } |
| 51 | + expect(logged).toBe(0); |
| 52 | + }); |
| 53 | + |
| 54 | + test("requested and supported resolves transparent for both themes", () => { |
| 55 | + let logged = 0; |
| 56 | + const onFallback = () => logged++; |
| 57 | + expect(resolveGround(corbitsDark, SUPPORTED, onFallback)).toBe( |
| 58 | + TRANSPARENT_BACKGROUND, |
| 59 | + ); |
| 60 | + expect(resolveGround(light, SUPPORTED, onFallback)).toBe( |
| 61 | + TRANSPARENT_BACKGROUND, |
| 62 | + ); |
| 63 | + expect(logged).toBe(0); |
| 64 | + }); |
| 65 | + |
| 66 | + test("truthy env spellings opt in when supported", () => { |
| 67 | + for (const value of ["1", "true", "yes", "on", " TRUE "]) { |
| 68 | + const env = { ...SUPPORTED, CORBITS_TRANSPARENT_BACKGROUND: value }; |
| 69 | + expect(resolveGround(corbitsDark, env)).toBe(TRANSPARENT_BACKGROUND); |
| 70 | + } |
| 71 | + }); |
| 72 | + |
| 73 | + test("falsy env spellings stay opaque without logging", () => { |
| 74 | + let logged = 0; |
| 75 | + for (const value of ["0", "false", "off", "", "no"]) { |
| 76 | + const env = { ...SUPPORTED, CORBITS_TRANSPARENT_BACKGROUND: value }; |
| 77 | + expect(resolveGround(corbitsDark, env, () => logged++)).toBe( |
| 78 | + corbitsDark.ground, |
| 79 | + ); |
| 80 | + } |
| 81 | + expect(logged).toBe(0); |
| 82 | + }); |
| 83 | + |
| 84 | + test("requested without truecolor falls back to opaque with one log line", () => { |
| 85 | + const lines: string[] = []; |
| 86 | + expect(resolveGround(corbitsDark, NO_TRUECOLOR, (m) => lines.push(m))).toBe( |
| 87 | + corbitsDark.ground, |
| 88 | + ); |
| 89 | + expect(resolveGround(light, NO_TRUECOLOR, (m) => lines.push(m))).toBe( |
| 90 | + light.ground, |
| 91 | + ); |
| 92 | + expect(lines).toHaveLength(1); |
| 93 | + }); |
| 94 | + |
| 95 | + test("requested on an unknown terminal falls back to opaque", () => { |
| 96 | + const lines: string[] = []; |
| 97 | + expect( |
| 98 | + resolveGround(corbitsDark, UNKNOWN_TERMINAL, (m) => lines.push(m)), |
| 99 | + ).toBe(corbitsDark.ground); |
| 100 | + expect(lines).toHaveLength(1); |
| 101 | + }); |
| 102 | + |
| 103 | + test("24bit colorterm with a TERM hint counts as supported", () => { |
| 104 | + const env: TransparentBackgroundEnv = { |
| 105 | + CORBITS_TRANSPARENT_BACKGROUND: "on", |
| 106 | + COLORTERM: "24bit", |
| 107 | + TERM: "xterm-ghostty", |
| 108 | + }; |
| 109 | + expect(resolveGround(corbitsDark, env)).toBe(TRANSPARENT_BACKGROUND); |
| 110 | + }); |
| 111 | +}); |
| 112 | + |
| 113 | +describe("configureTransparentBackground", () => { |
| 114 | + test("default leaves UI opaque", () => { |
| 115 | + const lines: string[] = []; |
| 116 | + expect(configureTransparentBackground({}, (m) => lines.push(m))).toBe( |
| 117 | + false, |
| 118 | + ); |
| 119 | + expect(UI.ground).toBe(corbitsDark.ground); |
| 120 | + expect(lines).toHaveLength(0); |
| 121 | + }); |
| 122 | + |
| 123 | + test("supported request publishes transparent on UI", () => { |
| 124 | + expect(configureTransparentBackground(SUPPORTED)).toBe(true); |
| 125 | + expect(UI.ground).toBe(TRANSPARENT_BACKGROUND); |
| 126 | + }); |
| 127 | + |
| 128 | + test("default restores opaque ground after a transparent configuration", () => { |
| 129 | + expect(configureTransparentBackground(SUPPORTED)).toBe(true); |
| 130 | + expect(configureTransparentBackground({})).toBe(false); |
| 131 | + expect(UI.ground).toBe(corbitsDark.ground); |
| 132 | + }); |
| 133 | + |
| 134 | + test("unsupported request keeps UI opaque and logs once", () => { |
| 135 | + const lines: string[] = []; |
| 136 | + const onFallback = (m: string) => lines.push(m); |
| 137 | + expect(configureTransparentBackground(NO_TRUECOLOR, onFallback)).toBe( |
| 138 | + false, |
| 139 | + ); |
| 140 | + expect(configureTransparentBackground(NO_TRUECOLOR, onFallback)).toBe( |
| 141 | + false, |
| 142 | + ); |
| 143 | + expect(UI.ground).toBe(corbitsDark.ground); |
| 144 | + expect(lines).toHaveLength(1); |
| 145 | + }); |
| 146 | + |
| 147 | + test("never mutates the dark theme's own ground", () => { |
| 148 | + configureTransparentBackground(SUPPORTED); |
| 149 | + expect(corbitsDark.ground).toBe(BRAND.ground); |
| 150 | + }); |
| 151 | +}); |
0 commit comments