|
3 | 3 | import type { DomainEvent, Terminal } from "@coder-studio/core"; |
4 | 4 | import { beforeEach, describe, expect, it, type Mock, vi } from "vitest"; |
5 | 5 | import { EventBus } from "../bus/event-bus"; |
6 | | -import { TerminalManager } from "./manager"; |
| 6 | +import { computeColorFgBg, TerminalManager } from "./manager"; |
7 | 7 | import { RingBuffer } from "./ring-buffer"; |
8 | 8 | import * as snapshotBufferModule from "./terminal-snapshot-buffer"; |
9 | 9 | import type { PtyHost, PtyProcess, TerminalDatabase, TerminalSpec } from "./types"; |
@@ -164,6 +164,116 @@ describe("TerminalManager", () => { |
164 | 164 | expect(spawnOptions.env.FORCE_COLOR).toBe("0"); |
165 | 165 | }); |
166 | 166 |
|
| 167 | + it("injects COLORFGBG=0;15 for a light themeBackground", () => { |
| 168 | + const spec: TerminalSpec = { |
| 169 | + workspaceId: "ws-123", |
| 170 | + kind: "shell", |
| 171 | + argv: ["bash"], |
| 172 | + cwd: "/home/user", |
| 173 | + themeBackground: "#fcfffd", |
| 174 | + }; |
| 175 | + |
| 176 | + manager.create(spec); |
| 177 | + |
| 178 | + const spawnOptions = (mockPtyHost.spawn as Mock).mock.calls[0][1]; |
| 179 | + expect(spawnOptions.env.COLORFGBG).toBe("0;15"); |
| 180 | + }); |
| 181 | + |
| 182 | + it("injects COLORFGBG=15;0 for a dark themeBackground", () => { |
| 183 | + const spec: TerminalSpec = { |
| 184 | + workspaceId: "ws-123", |
| 185 | + kind: "shell", |
| 186 | + argv: ["bash"], |
| 187 | + cwd: "/home/user", |
| 188 | + themeBackground: "#0b1218", |
| 189 | + }; |
| 190 | + |
| 191 | + manager.create(spec); |
| 192 | + |
| 193 | + const spawnOptions = (mockPtyHost.spawn as Mock).mock.calls[0][1]; |
| 194 | + expect(spawnOptions.env.COLORFGBG).toBe("15;0"); |
| 195 | + }); |
| 196 | + |
| 197 | + it("omits COLORFGBG when themeBackground is not provided", () => { |
| 198 | + const spec: TerminalSpec = { |
| 199 | + workspaceId: "ws-123", |
| 200 | + kind: "shell", |
| 201 | + argv: ["bash"], |
| 202 | + cwd: "/home/user", |
| 203 | + }; |
| 204 | + |
| 205 | + manager.create(spec); |
| 206 | + |
| 207 | + const spawnOptions = (mockPtyHost.spawn as Mock).mock.calls[0][1]; |
| 208 | + expect(spawnOptions.env.COLORFGBG).toBeUndefined(); |
| 209 | + }); |
| 210 | + |
| 211 | + it("omits COLORFGBG when themeBackground is malformed", () => { |
| 212 | + const spec: TerminalSpec = { |
| 213 | + workspaceId: "ws-123", |
| 214 | + kind: "shell", |
| 215 | + argv: ["bash"], |
| 216 | + cwd: "/home/user", |
| 217 | + themeBackground: "not-a-color", |
| 218 | + }; |
| 219 | + |
| 220 | + manager.create(spec); |
| 221 | + |
| 222 | + const spawnOptions = (mockPtyHost.spawn as Mock).mock.calls[0][1]; |
| 223 | + expect(spawnOptions.env.COLORFGBG).toBeUndefined(); |
| 224 | + }); |
| 225 | + |
| 226 | + it("lets spec.env override the derived COLORFGBG when explicitly set", () => { |
| 227 | + const spec: TerminalSpec = { |
| 228 | + workspaceId: "ws-123", |
| 229 | + kind: "shell", |
| 230 | + argv: ["bash"], |
| 231 | + cwd: "/home/user", |
| 232 | + themeBackground: "#fcfffd", |
| 233 | + env: { |
| 234 | + COLORFGBG: "7;0", |
| 235 | + }, |
| 236 | + }; |
| 237 | + |
| 238 | + manager.create(spec); |
| 239 | + |
| 240 | + const spawnOptions = (mockPtyHost.spawn as Mock).mock.calls[0][1]; |
| 241 | + expect(spawnOptions.env.COLORFGBG).toBe("7;0"); |
| 242 | + }); |
| 243 | + }); |
| 244 | + |
| 245 | + describe("computeColorFgBg", () => { |
| 246 | + it.each([ |
| 247 | + ["#ffffff", "0;15"], |
| 248 | + ["#fcfffd", "0;15"], |
| 249 | + ["#f5f7fa", "0;15"], |
| 250 | + ["#fff", "0;15"], |
| 251 | + ])("returns 0;15 (light bg) for %s", (input, expected) => { |
| 252 | + expect(computeColorFgBg(input)).toBe(expected); |
| 253 | + }); |
| 254 | + |
| 255 | + it.each([ |
| 256 | + ["#000000", "15;0"], |
| 257 | + ["#0b1218", "15;0"], |
| 258 | + ["#2e3440", "15;0"], |
| 259 | + ["#000", "15;0"], |
| 260 | + ])("returns 15;0 (dark bg) for %s", (input, expected) => { |
| 261 | + expect(computeColorFgBg(input)).toBe(expected); |
| 262 | + }); |
| 263 | + |
| 264 | + it("accepts #RRGGBBAA and ignores the alpha channel", () => { |
| 265 | + expect(computeColorFgBg("#ffffff80")).toBe("0;15"); |
| 266 | + expect(computeColorFgBg("#00000080")).toBe("15;0"); |
| 267 | + }); |
| 268 | + |
| 269 | + it("returns undefined for malformed input", () => { |
| 270 | + expect(computeColorFgBg("")).toBeUndefined(); |
| 271 | + expect(computeColorFgBg("white")).toBeUndefined(); |
| 272 | + expect(computeColorFgBg("#xyz")).toBeUndefined(); |
| 273 | + expect(computeColorFgBg("#12")).toBeUndefined(); |
| 274 | + expect(computeColorFgBg("rgb(0,0,0)")).toBeUndefined(); |
| 275 | + }); |
| 276 | + |
167 | 277 | it("should throw error on spawn failure", () => { |
168 | 278 | const spawnError = new Error("Command not found"); |
169 | 279 | mockPtyHost.spawn = vi.fn().mockImplementation(() => { |
|
0 commit comments