terminalProfile.test.ts 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. import { execFileSync } from "node:child_process";
  2. import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
  3. import { tmpdir } from "node:os";
  4. import { join } from "node:path";
  5. import { afterEach, describe, expect, it, vi } from "vitest";
  6. import { resolveTerminalFontProfile } from "../src/terminal/terminalProfile.js";
  7. vi.mock("node:child_process", () => ({
  8. execFileSync: vi.fn(),
  9. }));
  10. const originalPlatformDescriptor = Object.getOwnPropertyDescriptor(process, "platform");
  11. const tempDirs: string[] = [];
  12. const execFileSyncMock = vi.mocked(execFileSync);
  13. function makeTempDir(prefix: string): string {
  14. const dir = mkdtempSync(join(tmpdir(), prefix));
  15. tempDirs.push(dir);
  16. return dir;
  17. }
  18. function setProcessPlatform(platform: NodeJS.Platform) {
  19. Object.defineProperty(process, "platform", { value: platform });
  20. }
  21. afterEach(() => {
  22. execFileSyncMock.mockReset();
  23. if (originalPlatformDescriptor) {
  24. Object.defineProperty(process, "platform", originalPlatformDescriptor);
  25. }
  26. while (tempDirs.length > 0) {
  27. const dir = tempDirs.pop();
  28. if (dir) rmSync(dir, { recursive: true, force: true });
  29. }
  30. });
  31. describe("terminalProfile", () => {
  32. it("prefers an explicit terminal font override", () => {
  33. const profile = resolveTerminalFontProfile({
  34. settings: {
  35. terminalFontFamily: "MesloLGS NF",
  36. terminalInheritSystemProfile: true,
  37. },
  38. env: {},
  39. });
  40. expect(profile.source).toBe("custom");
  41. expect(profile.fontFamily).toContain("MesloLGS NF");
  42. expect(profile.fontFamily).toContain("monospace");
  43. });
  44. it("detects the Windows Terminal default profile font", () => {
  45. setProcessPlatform("win32");
  46. const localAppData = makeTempDir("zcode-terminal-profile-localappdata-");
  47. const settingsDir = join(
  48. localAppData,
  49. "Packages",
  50. "Microsoft.WindowsTerminal_8wekyb3d8bbwe",
  51. "LocalState",
  52. );
  53. mkdirSync(settingsDir, { recursive: true });
  54. writeFileSync(
  55. join(settingsDir, "settings.json"),
  56. `{
  57. // JSONC comments and trailing commas are valid in Windows Terminal settings.
  58. "defaultProfile": "{zsh}",
  59. "profiles": {
  60. "list": [
  61. { "guid": "{zsh}", "font": { "face": "CaskaydiaCove Nerd Font" } },
  62. ],
  63. },
  64. }`,
  65. "utf8",
  66. );
  67. const profile = resolveTerminalFontProfile({
  68. settings: {
  69. terminalFontFamily: undefined,
  70. terminalInheritSystemProfile: true,
  71. },
  72. env: { LOCALAPPDATA: localAppData },
  73. });
  74. expect(profile.source).toBe("system");
  75. expect(profile.fontFamily).toContain("CaskaydiaCove Nerd Font");
  76. });
  77. it("falls back when system profile inheritance is disabled", () => {
  78. setProcessPlatform("win32");
  79. const localAppData = makeTempDir("zcode-terminal-profile-localappdata-");
  80. const settingsDir = join(
  81. localAppData,
  82. "Packages",
  83. "Microsoft.WindowsTerminal_8wekyb3d8bbwe",
  84. "LocalState",
  85. );
  86. mkdirSync(settingsDir, { recursive: true });
  87. writeFileSync(
  88. join(settingsDir, "settings.json"),
  89. JSON.stringify({
  90. profiles: {
  91. defaults: { font: { face: "Ignored Nerd Font" } },
  92. },
  93. }),
  94. "utf8",
  95. );
  96. const profile = resolveTerminalFontProfile({
  97. settings: {
  98. terminalFontFamily: undefined,
  99. terminalInheritSystemProfile: false,
  100. },
  101. env: { LOCALAPPDATA: localAppData },
  102. });
  103. expect(profile.source).toBe("fallback");
  104. expect(profile.fontFamily).not.toContain("Ignored Nerd Font");
  105. });
  106. it("detects the VS Code terminal font on Unix-like systems", () => {
  107. setProcessPlatform("linux");
  108. const home = makeTempDir("zcode-terminal-profile-home-");
  109. const settingsDir = join(home, ".config", "Code", "User");
  110. mkdirSync(settingsDir, { recursive: true });
  111. writeFileSync(
  112. join(settingsDir, "settings.json"),
  113. JSON.stringify({ "terminal.integrated.fontFamily": "JetBrainsMono Nerd Font" }),
  114. "utf8",
  115. );
  116. const profile = resolveTerminalFontProfile({
  117. settings: {
  118. terminalFontFamily: undefined,
  119. terminalInheritSystemProfile: true,
  120. },
  121. env: { HOME: home },
  122. });
  123. expect(profile.source).toBe("system");
  124. expect(profile.fontFamily).toContain("JetBrainsMono Nerd Font");
  125. });
  126. it("detects the default iTerm2 profile font as a macOS plugin", () => {
  127. setProcessPlatform("darwin");
  128. const home = makeTempDir("zcode-terminal-profile-home-");
  129. const preferencesDir = join(home, "Library", "Preferences");
  130. mkdirSync(preferencesDir, { recursive: true });
  131. writeFileSync(join(preferencesDir, "com.googlecode.iterm2.plist"), "plist", "utf8");
  132. execFileSyncMock.mockReturnValueOnce(
  133. JSON.stringify({
  134. "New Bookmarks": [
  135. {
  136. Name: "Default",
  137. "Default Bookmark": true,
  138. "Normal Font": "MesloLGS-NF-Regular 13",
  139. "Foreground Color": {
  140. "Red Component": 0.9,
  141. "Green Component": 0.85,
  142. "Blue Component": 0.8,
  143. },
  144. "Background Color": {
  145. "Red Component": 0.1,
  146. "Green Component": 0.12,
  147. "Blue Component": 0.14,
  148. },
  149. "Ansi 1 Color": {
  150. "Red Component": 1,
  151. "Green Component": 0.2,
  152. "Blue Component": 0.1,
  153. },
  154. "Ansi 9 Color": {
  155. "Red Component": 1,
  156. "Green Component": 0.4,
  157. "Blue Component": 0.35,
  158. },
  159. },
  160. ],
  161. }),
  162. );
  163. const profile = resolveTerminalFontProfile({
  164. settings: {
  165. terminalFontFamily: undefined,
  166. terminalInheritSystemProfile: true,
  167. },
  168. env: { HOME: home },
  169. });
  170. expect(profile.source).toBe("system");
  171. expect(profile.fontFamily).toContain("MesloLGS NF Regular");
  172. expect(profile.fontSize).toBe(13);
  173. expect(profile.theme).toMatchObject({
  174. background: "#1a1f24",
  175. foreground: "#e6d9cc",
  176. red: "#ff331a",
  177. brightRed: "#ff6659",
  178. });
  179. });
  180. it("detects macOS Terminal font when iTerm2 is unavailable", () => {
  181. setProcessPlatform("darwin");
  182. const home = makeTempDir("zcode-terminal-profile-home-");
  183. const preferencesDir = join(home, "Library", "Preferences");
  184. mkdirSync(preferencesDir, { recursive: true });
  185. writeFileSync(join(preferencesDir, "com.apple.Terminal.plist"), "plist", "utf8");
  186. execFileSyncMock.mockReturnValueOnce(
  187. JSON.stringify({
  188. "Default Window Settings": "Pro",
  189. Pro: {
  190. FontName: "SF Mono",
  191. FontSize: 14,
  192. TextColor: {
  193. "Red Component": 0.8,
  194. "Green Component": 0.82,
  195. "Blue Component": 0.84,
  196. },
  197. BackgroundColor: {
  198. "Red Component": 0.05,
  199. "Green Component": 0.06,
  200. "Blue Component": 0.07,
  201. },
  202. ANSIBlueColor: {
  203. "Red Component": 0.2,
  204. "Green Component": 0.4,
  205. "Blue Component": 1,
  206. },
  207. },
  208. }),
  209. );
  210. const profile = resolveTerminalFontProfile({
  211. settings: {
  212. terminalFontFamily: undefined,
  213. terminalInheritSystemProfile: true,
  214. },
  215. env: { HOME: home },
  216. });
  217. expect(profile.source).toBe("system");
  218. expect(profile.fontFamily).toContain("SF Mono");
  219. expect(profile.fontSize).toBe(14);
  220. expect(profile.theme).toMatchObject({
  221. background: "#0d0f12",
  222. foreground: "#ccd1d6",
  223. blue: "#3366ff",
  224. });
  225. });
  226. it("keeps macOS profile size and theme when an explicit font override is configured", () => {
  227. setProcessPlatform("darwin");
  228. const home = makeTempDir("zcode-terminal-profile-home-");
  229. const preferencesDir = join(home, "Library", "Preferences");
  230. mkdirSync(preferencesDir, { recursive: true });
  231. writeFileSync(join(preferencesDir, "com.googlecode.iterm2.plist"), "plist", "utf8");
  232. execFileSyncMock.mockReturnValueOnce(
  233. JSON.stringify({
  234. "New Bookmarks": [
  235. {
  236. Name: "Default",
  237. "Default Bookmark": true,
  238. "Normal Font": "Menlo-Regular 15",
  239. "Cursor Color": {
  240. "Red Component": 0.1,
  241. "Green Component": 0.9,
  242. "Blue Component": 0.5,
  243. },
  244. },
  245. ],
  246. }),
  247. );
  248. const profile = resolveTerminalFontProfile({
  249. settings: {
  250. terminalFontFamily: "Custom Mono",
  251. terminalInheritSystemProfile: true,
  252. },
  253. env: { HOME: home },
  254. });
  255. expect(profile.source).toBe("custom");
  256. expect(profile.fontFamily).toContain("Custom Mono");
  257. expect(profile.fontSize).toBe(15);
  258. expect(profile.theme).toMatchObject({ cursor: "#1ae680" });
  259. });
  260. it("skips failing macOS terminal plugins and continues to fallback", () => {
  261. setProcessPlatform("darwin");
  262. const home = makeTempDir("zcode-terminal-profile-home-");
  263. const preferencesDir = join(home, "Library", "Preferences");
  264. mkdirSync(preferencesDir, { recursive: true });
  265. writeFileSync(join(preferencesDir, "com.googlecode.iterm2.plist"), "plist", "utf8");
  266. writeFileSync(join(preferencesDir, "com.apple.Terminal.plist"), "plist", "utf8");
  267. execFileSyncMock.mockImplementation(() => {
  268. throw new Error("plutil failed");
  269. });
  270. const profile = resolveTerminalFontProfile({
  271. settings: {
  272. terminalFontFamily: undefined,
  273. terminalInheritSystemProfile: true,
  274. },
  275. env: { HOME: home },
  276. });
  277. expect(profile.source).toBe("fallback");
  278. expect(profile.fontFamily).toContain("monospace");
  279. });
  280. });