|
| 1 | +import { afterEach, describe, expect, test } from "bun:test"; |
| 2 | + |
| 3 | +import { |
| 4 | + BRAND, |
| 5 | + configureTransparentBackground, |
| 6 | + corbitsDark, |
| 7 | + corbitsLight, |
| 8 | + resetTransparentBackgroundLogForTests, |
| 9 | + resolveGround, |
| 10 | + setTheme, |
| 11 | + TRANSPARENT_BACKGROUND, |
| 12 | + UI, |
| 13 | + type Theme, |
| 14 | + type TransparentBackgroundEnv, |
| 15 | +} from "./theme.js"; |
| 16 | + |
| 17 | +const SUPPORTED: TransparentBackgroundEnv = { |
| 18 | + CORBITS_TRANSPARENT_BACKGROUND: "1", |
| 19 | + COLORTERM: "truecolor", |
| 20 | + TERM_PROGRAM: "kitty", |
| 21 | +}; |
| 22 | + |
| 23 | +const NO_TRUECOLOR: TransparentBackgroundEnv = { |
| 24 | + CORBITS_TRANSPARENT_BACKGROUND: "1", |
| 25 | + TERM_PROGRAM: "kitty", |
| 26 | +}; |
| 27 | + |
| 28 | +const UNKNOWN_TERMINAL: TransparentBackgroundEnv = { |
| 29 | + CORBITS_TRANSPARENT_BACKGROUND: "true", |
| 30 | + COLORTERM: "truecolor", |
| 31 | + TERM: "xterm-256color", |
| 32 | +}; |
| 33 | + |
| 34 | +afterEach(() => { |
| 35 | + configureTransparentBackground({}); |
| 36 | + setTheme("corbits-dark"); |
| 37 | + resetTransparentBackgroundLogForTests(); |
| 38 | +}); |
| 39 | + |
| 40 | +describe("resolveGround", () => { |
| 41 | + test("default stays opaque without logging", () => { |
| 42 | + let logged = 0; |
| 43 | + for (const theme of [corbitsDark, corbitsLight]) { |
| 44 | + expect(resolveGround(theme, {}, () => logged++)).toBe(theme.ground); |
| 45 | + } |
| 46 | + expect(logged).toBe(0); |
| 47 | + }); |
| 48 | + |
| 49 | + test("requested and supported resolves transparent for both themes", () => { |
| 50 | + let logged = 0; |
| 51 | + const onFallback = () => logged++; |
| 52 | + expect(resolveGround(corbitsDark, SUPPORTED, onFallback)).toBe( |
| 53 | + TRANSPARENT_BACKGROUND, |
| 54 | + ); |
| 55 | + expect(resolveGround(corbitsLight, SUPPORTED, onFallback)).toBe( |
| 56 | + TRANSPARENT_BACKGROUND, |
| 57 | + ); |
| 58 | + expect(logged).toBe(0); |
| 59 | + }); |
| 60 | + |
| 61 | + test("truthy env spellings opt in when supported", () => { |
| 62 | + for (const value of ["1", "true", "yes", "on", " TRUE "]) { |
| 63 | + const env = { ...SUPPORTED, CORBITS_TRANSPARENT_BACKGROUND: value }; |
| 64 | + expect(resolveGround(corbitsDark, env)).toBe(TRANSPARENT_BACKGROUND); |
| 65 | + } |
| 66 | + }); |
| 67 | + |
| 68 | + test("falsy env spellings stay opaque without logging", () => { |
| 69 | + let logged = 0; |
| 70 | + for (const value of ["0", "false", "off", "", "no"]) { |
| 71 | + const env = { ...SUPPORTED, CORBITS_TRANSPARENT_BACKGROUND: value }; |
| 72 | + expect(resolveGround(corbitsDark, env, () => logged++)).toBe( |
| 73 | + corbitsDark.ground, |
| 74 | + ); |
| 75 | + } |
| 76 | + expect(logged).toBe(0); |
| 77 | + }); |
| 78 | + |
| 79 | + test("requested without truecolor falls back to opaque with one log line", () => { |
| 80 | + const lines: string[] = []; |
| 81 | + expect(resolveGround(corbitsDark, NO_TRUECOLOR, (m) => lines.push(m))).toBe( |
| 82 | + corbitsDark.ground, |
| 83 | + ); |
| 84 | + expect( |
| 85 | + resolveGround(corbitsLight, NO_TRUECOLOR, (m) => lines.push(m)), |
| 86 | + ).toBe(corbitsLight.ground); |
| 87 | + expect(lines).toHaveLength(1); |
| 88 | + }); |
| 89 | + |
| 90 | + test("requested on an unknown terminal falls back to opaque", () => { |
| 91 | + const lines: string[] = []; |
| 92 | + expect( |
| 93 | + resolveGround(corbitsDark, UNKNOWN_TERMINAL, (m) => lines.push(m)), |
| 94 | + ).toBe(corbitsDark.ground); |
| 95 | + expect(lines).toHaveLength(1); |
| 96 | + }); |
| 97 | + |
| 98 | + test("24bit colorterm with a TERM hint counts as supported", () => { |
| 99 | + const env: TransparentBackgroundEnv = { |
| 100 | + CORBITS_TRANSPARENT_BACKGROUND: "on", |
| 101 | + COLORTERM: "24bit", |
| 102 | + TERM: "xterm-ghostty", |
| 103 | + }; |
| 104 | + expect(resolveGround(corbitsDark, env)).toBe(TRANSPARENT_BACKGROUND); |
| 105 | + }); |
| 106 | +}); |
| 107 | + |
| 108 | +describe("configureTransparentBackground", () => { |
| 109 | + test("default leaves UI opaque", () => { |
| 110 | + const lines: string[] = []; |
| 111 | + expect(configureTransparentBackground({}, (m) => lines.push(m))).toBe( |
| 112 | + false, |
| 113 | + ); |
| 114 | + expect(UI.ground).toBe(corbitsDark.ground); |
| 115 | + expect(lines).toHaveLength(0); |
| 116 | + }); |
| 117 | + |
| 118 | + test("supported request publishes transparent on UI", () => { |
| 119 | + expect(configureTransparentBackground(SUPPORTED)).toBe(true); |
| 120 | + expect(UI.ground).toBe(TRANSPARENT_BACKGROUND); |
| 121 | + }); |
| 122 | + |
| 123 | + test("default restores opaque ground after a transparent configuration", () => { |
| 124 | + expect(configureTransparentBackground(SUPPORTED)).toBe(true); |
| 125 | + expect(configureTransparentBackground({})).toBe(false); |
| 126 | + expect(UI.ground).toBe(corbitsDark.ground); |
| 127 | + }); |
| 128 | + |
| 129 | + test("unsupported request keeps UI opaque and logs once", () => { |
| 130 | + const lines: string[] = []; |
| 131 | + const onFallback = (m: string) => lines.push(m); |
| 132 | + expect(configureTransparentBackground(NO_TRUECOLOR, onFallback)).toBe( |
| 133 | + false, |
| 134 | + ); |
| 135 | + expect(configureTransparentBackground(NO_TRUECOLOR, onFallback)).toBe( |
| 136 | + false, |
| 137 | + ); |
| 138 | + expect(UI.ground).toBe(corbitsDark.ground); |
| 139 | + expect(lines).toHaveLength(1); |
| 140 | + }); |
| 141 | + |
| 142 | + test("never mutates the dark theme's own ground", () => { |
| 143 | + configureTransparentBackground(SUPPORTED); |
| 144 | + expect(corbitsDark.ground).toBe(BRAND.ground); |
| 145 | + }); |
| 146 | +}); |
| 147 | + |
| 148 | +describe("theme and transparency composition", () => { |
| 149 | + const matrix: [string, Theme, TransparentBackgroundEnv, string][] = [ |
| 150 | + ["corbits-dark", corbitsDark, {}, corbitsDark.ground], |
| 151 | + ["corbits-dark", corbitsDark, SUPPORTED, TRANSPARENT_BACKGROUND], |
| 152 | + ["corbits-light", corbitsLight, {}, corbitsLight.ground], |
| 153 | + ["corbits-light", corbitsLight, SUPPORTED, TRANSPARENT_BACKGROUND], |
| 154 | + ]; |
| 155 | + |
| 156 | + for (const [name, theme, env, expectedGround] of matrix) { |
| 157 | + const background = |
| 158 | + expectedGround === TRANSPARENT_BACKGROUND ? "transparent" : "default"; |
| 159 | + test(`${name} with ${background} background`, () => { |
| 160 | + setTheme(name); |
| 161 | + configureTransparentBackground(env); |
| 162 | + expect(UI.name).toBe(theme.name); |
| 163 | + expect(UI.text).toBe(theme.text); |
| 164 | + expect(UI.ground).toBe(expectedGround); |
| 165 | + }); |
| 166 | + } |
| 167 | + |
| 168 | + test("sync setTheme preserves the transparency overlay", () => { |
| 169 | + configureTransparentBackground(SUPPORTED); |
| 170 | + setTheme("corbits-light"); |
| 171 | + expect(UI.name).toBe("corbits-light"); |
| 172 | + expect(UI.text).toBe(corbitsLight.text); |
| 173 | + expect(UI.ground).toBe(TRANSPARENT_BACKGROUND); |
| 174 | + }); |
| 175 | + |
| 176 | + test("configure after setTheme uses the selected theme fallback", () => { |
| 177 | + setTheme("corbits-light"); |
| 178 | + configureTransparentBackground({}); |
| 179 | + expect(UI.ground).toBe(corbitsLight.ground); |
| 180 | + }); |
| 181 | + |
| 182 | + test("async-equivalent theme transition preserves transparency", async () => { |
| 183 | + configureTransparentBackground(SUPPORTED); |
| 184 | + await Promise.resolve(); |
| 185 | + setTheme("corbits-light"); |
| 186 | + expect(UI.name).toBe("corbits-light"); |
| 187 | + expect(UI.text).toBe(corbitsLight.text); |
| 188 | + expect(UI.ground).toBe(TRANSPARENT_BACKGROUND); |
| 189 | + }); |
| 190 | + |
| 191 | + test("disabling transparency restores the selected light ground", () => { |
| 192 | + setTheme("corbits-light"); |
| 193 | + configureTransparentBackground(SUPPORTED); |
| 194 | + configureTransparentBackground({}); |
| 195 | + expect(UI.name).toBe("corbits-light"); |
| 196 | + expect(UI.ground).toBe(corbitsLight.ground); |
| 197 | + }); |
| 198 | +}); |
0 commit comments