settings.spec.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. import { test, expect, settingsKey } from "../fixtures"
  2. import { closeDialog, openSettings } from "../actions"
  3. import {
  4. settingsColorSchemeSelector,
  5. settingsFontSelector,
  6. settingsLanguageSelectSelector,
  7. settingsNotificationsAgentSelector,
  8. settingsNotificationsErrorsSelector,
  9. settingsNotificationsPermissionsSelector,
  10. settingsReleaseNotesSelector,
  11. settingsSoundsAgentSelector,
  12. settingsSoundsErrorsSelector,
  13. settingsSoundsPermissionsSelector,
  14. settingsThemeSelector,
  15. settingsUpdatesStartupSelector,
  16. } from "../selectors"
  17. test("smoke settings dialog opens, switches tabs, closes", async ({ page, gotoSession }) => {
  18. await gotoSession()
  19. const dialog = await openSettings(page)
  20. await dialog.getByRole("tab", { name: "Shortcuts" }).click()
  21. await expect(dialog.getByRole("button", { name: "Reset to defaults" })).toBeVisible()
  22. await expect(dialog.getByPlaceholder("Search shortcuts")).toBeVisible()
  23. await closeDialog(page, dialog)
  24. })
  25. test("changing language updates settings labels", async ({ page, gotoSession }) => {
  26. await page.addInitScript(() => {
  27. localStorage.setItem("opencode.global.dat:language", JSON.stringify({ locale: "en" }))
  28. })
  29. await gotoSession()
  30. const dialog = await openSettings(page)
  31. const heading = dialog.getByRole("heading", { level: 2 })
  32. await expect(heading).toHaveText("General")
  33. const select = dialog.locator(settingsLanguageSelectSelector)
  34. await expect(select).toBeVisible()
  35. await select.locator('[data-slot="select-select-trigger"]').click()
  36. await page.locator('[data-slot="select-select-item"]').filter({ hasText: "Deutsch" }).click()
  37. await expect(heading).toHaveText("Allgemein")
  38. await select.locator('[data-slot="select-select-trigger"]').click()
  39. await page.locator('[data-slot="select-select-item"]').filter({ hasText: "English" }).click()
  40. await expect(heading).toHaveText("General")
  41. })
  42. test("changing color scheme persists in localStorage", async ({ page, gotoSession }) => {
  43. await gotoSession()
  44. const dialog = await openSettings(page)
  45. const select = dialog.locator(settingsColorSchemeSelector)
  46. await expect(select).toBeVisible()
  47. await select.locator('[data-slot="select-select-trigger"]').click()
  48. await page.locator('[data-slot="select-select-item"]').filter({ hasText: "Dark" }).click()
  49. const colorScheme = await page.evaluate(() => {
  50. return document.documentElement.getAttribute("data-color-scheme")
  51. })
  52. expect(colorScheme).toBe("dark")
  53. await select.locator('[data-slot="select-select-trigger"]').click()
  54. await page.locator('[data-slot="select-select-item"]').filter({ hasText: "Light" }).click()
  55. const lightColorScheme = await page.evaluate(() => {
  56. return document.documentElement.getAttribute("data-color-scheme")
  57. })
  58. expect(lightColorScheme).toBe("light")
  59. })
  60. test("changing theme persists in localStorage", async ({ page, gotoSession }) => {
  61. await gotoSession()
  62. const dialog = await openSettings(page)
  63. const select = dialog.locator(settingsThemeSelector)
  64. await expect(select).toBeVisible()
  65. const currentThemeId = await page.evaluate(() => {
  66. return document.documentElement.getAttribute("data-theme")
  67. })
  68. const currentTheme = (await select.locator('[data-slot="select-select-trigger-value"]').textContent())?.trim() ?? ""
  69. await select.locator('[data-slot="select-select-trigger"]').click()
  70. const items = page.locator('[data-slot="select-select-item"]')
  71. const count = await items.count()
  72. expect(count).toBeGreaterThan(1)
  73. const nextTheme = (await items.locator('[data-slot="select-select-item-label"]').allTextContents())
  74. .map((x) => x.trim())
  75. .find((x) => x && x !== currentTheme)
  76. expect(nextTheme).toBeTruthy()
  77. await items.filter({ hasText: nextTheme! }).first().click()
  78. await page.keyboard.press("Escape")
  79. const storedThemeId = await page.evaluate(() => {
  80. return localStorage.getItem("opencode-theme-id")
  81. })
  82. expect(storedThemeId).not.toBeNull()
  83. expect(storedThemeId).not.toBe(currentThemeId)
  84. const dataTheme = await page.evaluate(() => {
  85. return document.documentElement.getAttribute("data-theme")
  86. })
  87. expect(dataTheme).toBe(storedThemeId)
  88. })
  89. test("legacy oc-1 theme migrates to oc-2", async ({ page, gotoSession }) => {
  90. await page.addInitScript(() => {
  91. localStorage.setItem("opencode-theme-id", "oc-1")
  92. localStorage.setItem("opencode-theme-css-light", "--background-base:#fff;")
  93. localStorage.setItem("opencode-theme-css-dark", "--background-base:#000;")
  94. })
  95. await gotoSession()
  96. await expect(page.locator("html")).toHaveAttribute("data-theme", "oc-2")
  97. await expect
  98. .poll(async () => {
  99. return await page.evaluate(() => {
  100. return localStorage.getItem("opencode-theme-id")
  101. })
  102. })
  103. .toBe("oc-2")
  104. await expect
  105. .poll(async () => {
  106. return await page.evaluate(() => {
  107. return localStorage.getItem("opencode-theme-css-light")
  108. })
  109. })
  110. .toBeNull()
  111. await expect
  112. .poll(async () => {
  113. return await page.evaluate(() => {
  114. return localStorage.getItem("opencode-theme-css-dark")
  115. })
  116. })
  117. .toBeNull()
  118. })
  119. test("changing font persists in localStorage and updates CSS variable", async ({ page, gotoSession }) => {
  120. await gotoSession()
  121. const dialog = await openSettings(page)
  122. const select = dialog.locator(settingsFontSelector)
  123. await expect(select).toBeVisible()
  124. const initialFontFamily = await page.evaluate(() => {
  125. return getComputedStyle(document.documentElement).getPropertyValue("--font-family-mono")
  126. })
  127. expect(initialFontFamily).toContain("IBM Plex Mono")
  128. await select.locator('[data-slot="select-select-trigger"]').click()
  129. const items = page.locator('[data-slot="select-select-item"]')
  130. await items.nth(2).click()
  131. await page.waitForTimeout(100)
  132. const stored = await page.evaluate((key) => {
  133. const raw = localStorage.getItem(key)
  134. return raw ? JSON.parse(raw) : null
  135. }, settingsKey)
  136. expect(stored?.appearance?.font).not.toBe("ibm-plex-mono")
  137. const newFontFamily = await page.evaluate(() => {
  138. return getComputedStyle(document.documentElement).getPropertyValue("--font-family-mono")
  139. })
  140. expect(newFontFamily).not.toBe(initialFontFamily)
  141. })
  142. test("color scheme and font rehydrate after reload", async ({ page, gotoSession }) => {
  143. await gotoSession()
  144. const dialog = await openSettings(page)
  145. const colorSchemeSelect = dialog.locator(settingsColorSchemeSelector)
  146. await expect(colorSchemeSelect).toBeVisible()
  147. await colorSchemeSelect.locator('[data-slot="select-select-trigger"]').click()
  148. await page.locator('[data-slot="select-select-item"]').filter({ hasText: "Dark" }).click()
  149. await expect(page.locator("html")).toHaveAttribute("data-color-scheme", "dark")
  150. const fontSelect = dialog.locator(settingsFontSelector)
  151. await expect(fontSelect).toBeVisible()
  152. const initialFontFamily = await page.evaluate(() => {
  153. return getComputedStyle(document.documentElement).getPropertyValue("--font-family-mono").trim()
  154. })
  155. const initialSettings = await page.evaluate((key) => {
  156. const raw = localStorage.getItem(key)
  157. return raw ? JSON.parse(raw) : null
  158. }, settingsKey)
  159. const currentFont =
  160. (await fontSelect.locator('[data-slot="select-select-trigger-value"]').textContent())?.trim() ?? ""
  161. await fontSelect.locator('[data-slot="select-select-trigger"]').click()
  162. const fontItems = page.locator('[data-slot="select-select-item"]')
  163. expect(await fontItems.count()).toBeGreaterThan(1)
  164. if (currentFont) {
  165. await fontItems.filter({ hasNotText: currentFont }).first().click()
  166. }
  167. if (!currentFont) {
  168. await fontItems.nth(1).click()
  169. }
  170. await expect
  171. .poll(async () => {
  172. return await page.evaluate((key) => {
  173. const raw = localStorage.getItem(key)
  174. return raw ? JSON.parse(raw) : null
  175. }, settingsKey)
  176. })
  177. .toMatchObject({
  178. appearance: {
  179. font: expect.any(String),
  180. },
  181. })
  182. const updatedSettings = await page.evaluate((key) => {
  183. const raw = localStorage.getItem(key)
  184. return raw ? JSON.parse(raw) : null
  185. }, settingsKey)
  186. const updatedFontFamily = await page.evaluate(() => {
  187. return getComputedStyle(document.documentElement).getPropertyValue("--font-family-mono").trim()
  188. })
  189. expect(updatedFontFamily).not.toBe(initialFontFamily)
  190. expect(updatedSettings?.appearance?.font).not.toBe(initialSettings?.appearance?.font)
  191. await closeDialog(page, dialog)
  192. await page.reload()
  193. await expect(page.locator("html")).toHaveAttribute("data-color-scheme", "dark")
  194. await expect
  195. .poll(async () => {
  196. return await page.evaluate((key) => {
  197. const raw = localStorage.getItem(key)
  198. return raw ? JSON.parse(raw) : null
  199. }, settingsKey)
  200. })
  201. .toMatchObject({
  202. appearance: {
  203. font: updatedSettings?.appearance?.font,
  204. },
  205. })
  206. const rehydratedSettings = await page.evaluate((key) => {
  207. const raw = localStorage.getItem(key)
  208. return raw ? JSON.parse(raw) : null
  209. }, settingsKey)
  210. await expect
  211. .poll(async () => {
  212. return await page.evaluate(() => {
  213. return getComputedStyle(document.documentElement).getPropertyValue("--font-family-mono").trim()
  214. })
  215. })
  216. .not.toBe(initialFontFamily)
  217. const rehydratedFontFamily = await page.evaluate(() => {
  218. return getComputedStyle(document.documentElement).getPropertyValue("--font-family-mono").trim()
  219. })
  220. expect(rehydratedFontFamily).not.toBe(initialFontFamily)
  221. expect(rehydratedSettings?.appearance?.font).toBe(updatedSettings?.appearance?.font)
  222. })
  223. test("toggling notification agent switch updates localStorage", async ({ page, gotoSession }) => {
  224. await gotoSession()
  225. const dialog = await openSettings(page)
  226. const switchContainer = dialog.locator(settingsNotificationsAgentSelector)
  227. await expect(switchContainer).toBeVisible()
  228. const toggleInput = switchContainer.locator('[data-slot="switch-input"]')
  229. const initialState = await toggleInput.evaluate((el: HTMLInputElement) => el.checked)
  230. expect(initialState).toBe(true)
  231. await switchContainer.locator('[data-slot="switch-control"]').click()
  232. await page.waitForTimeout(100)
  233. const newState = await toggleInput.evaluate((el: HTMLInputElement) => el.checked)
  234. expect(newState).toBe(false)
  235. const stored = await page.evaluate((key) => {
  236. const raw = localStorage.getItem(key)
  237. return raw ? JSON.parse(raw) : null
  238. }, settingsKey)
  239. expect(stored?.notifications?.agent).toBe(false)
  240. })
  241. test("toggling notification permissions switch updates localStorage", async ({ page, gotoSession }) => {
  242. await gotoSession()
  243. const dialog = await openSettings(page)
  244. const switchContainer = dialog.locator(settingsNotificationsPermissionsSelector)
  245. await expect(switchContainer).toBeVisible()
  246. const toggleInput = switchContainer.locator('[data-slot="switch-input"]')
  247. const initialState = await toggleInput.evaluate((el: HTMLInputElement) => el.checked)
  248. expect(initialState).toBe(true)
  249. await switchContainer.locator('[data-slot="switch-control"]').click()
  250. await page.waitForTimeout(100)
  251. const newState = await toggleInput.evaluate((el: HTMLInputElement) => el.checked)
  252. expect(newState).toBe(false)
  253. const stored = await page.evaluate((key) => {
  254. const raw = localStorage.getItem(key)
  255. return raw ? JSON.parse(raw) : null
  256. }, settingsKey)
  257. expect(stored?.notifications?.permissions).toBe(false)
  258. })
  259. test("toggling notification errors switch updates localStorage", async ({ page, gotoSession }) => {
  260. await gotoSession()
  261. const dialog = await openSettings(page)
  262. const switchContainer = dialog.locator(settingsNotificationsErrorsSelector)
  263. await expect(switchContainer).toBeVisible()
  264. const toggleInput = switchContainer.locator('[data-slot="switch-input"]')
  265. const initialState = await toggleInput.evaluate((el: HTMLInputElement) => el.checked)
  266. expect(initialState).toBe(false)
  267. await switchContainer.locator('[data-slot="switch-control"]').click()
  268. await page.waitForTimeout(100)
  269. const newState = await toggleInput.evaluate((el: HTMLInputElement) => el.checked)
  270. expect(newState).toBe(true)
  271. const stored = await page.evaluate((key) => {
  272. const raw = localStorage.getItem(key)
  273. return raw ? JSON.parse(raw) : null
  274. }, settingsKey)
  275. expect(stored?.notifications?.errors).toBe(true)
  276. })
  277. test("changing sound agent selection persists in localStorage", async ({ page, gotoSession }) => {
  278. await gotoSession()
  279. const dialog = await openSettings(page)
  280. const select = dialog.locator(settingsSoundsAgentSelector)
  281. await expect(select).toBeVisible()
  282. await select.locator('[data-slot="select-select-trigger"]').click()
  283. const items = page.locator('[data-slot="select-select-item"]')
  284. await items.nth(2).click()
  285. const stored = await page.evaluate((key) => {
  286. const raw = localStorage.getItem(key)
  287. return raw ? JSON.parse(raw) : null
  288. }, settingsKey)
  289. expect(stored?.sounds?.agent).not.toBe("staplebops-01")
  290. })
  291. test("selecting none disables agent sound", async ({ page, gotoSession }) => {
  292. await gotoSession()
  293. const dialog = await openSettings(page)
  294. const select = dialog.locator(settingsSoundsAgentSelector)
  295. const trigger = select.locator('[data-slot="select-select-trigger"]')
  296. await expect(select).toBeVisible()
  297. await expect(trigger).toBeEnabled()
  298. await trigger.click()
  299. const items = page.locator('[data-slot="select-select-item"]')
  300. await expect(items.first()).toBeVisible()
  301. await items.first().click()
  302. const stored = await page.evaluate((key) => {
  303. const raw = localStorage.getItem(key)
  304. return raw ? JSON.parse(raw) : null
  305. }, settingsKey)
  306. expect(stored?.sounds?.agentEnabled).toBe(false)
  307. })
  308. test("changing permissions and errors sounds updates localStorage", async ({ page, gotoSession }) => {
  309. await gotoSession()
  310. const dialog = await openSettings(page)
  311. const permissionsSelect = dialog.locator(settingsSoundsPermissionsSelector)
  312. const errorsSelect = dialog.locator(settingsSoundsErrorsSelector)
  313. await expect(permissionsSelect).toBeVisible()
  314. await expect(errorsSelect).toBeVisible()
  315. const initial = await page.evaluate((key) => {
  316. const raw = localStorage.getItem(key)
  317. return raw ? JSON.parse(raw) : null
  318. }, settingsKey)
  319. const permissionsCurrent =
  320. (await permissionsSelect.locator('[data-slot="select-select-trigger-value"]').textContent())?.trim() ?? ""
  321. await permissionsSelect.locator('[data-slot="select-select-trigger"]').click()
  322. const permissionItems = page.locator('[data-slot="select-select-item"]')
  323. expect(await permissionItems.count()).toBeGreaterThan(1)
  324. if (permissionsCurrent) {
  325. await permissionItems.filter({ hasNotText: permissionsCurrent }).first().click()
  326. }
  327. if (!permissionsCurrent) {
  328. await permissionItems.nth(1).click()
  329. }
  330. const errorsCurrent =
  331. (await errorsSelect.locator('[data-slot="select-select-trigger-value"]').textContent())?.trim() ?? ""
  332. await errorsSelect.locator('[data-slot="select-select-trigger"]').click()
  333. const errorItems = page.locator('[data-slot="select-select-item"]')
  334. expect(await errorItems.count()).toBeGreaterThan(1)
  335. if (errorsCurrent) {
  336. await errorItems.filter({ hasNotText: errorsCurrent }).first().click()
  337. }
  338. if (!errorsCurrent) {
  339. await errorItems.nth(1).click()
  340. }
  341. await expect
  342. .poll(async () => {
  343. return await page.evaluate((key) => {
  344. const raw = localStorage.getItem(key)
  345. return raw ? JSON.parse(raw) : null
  346. }, settingsKey)
  347. })
  348. .toMatchObject({
  349. sounds: {
  350. permissions: expect.any(String),
  351. errors: expect.any(String),
  352. },
  353. })
  354. const stored = await page.evaluate((key) => {
  355. const raw = localStorage.getItem(key)
  356. return raw ? JSON.parse(raw) : null
  357. }, settingsKey)
  358. expect(stored?.sounds?.permissions).not.toBe(initial?.sounds?.permissions)
  359. expect(stored?.sounds?.errors).not.toBe(initial?.sounds?.errors)
  360. })
  361. test("toggling updates startup switch updates localStorage", async ({ page, gotoSession }) => {
  362. await gotoSession()
  363. const dialog = await openSettings(page)
  364. const switchContainer = dialog.locator(settingsUpdatesStartupSelector)
  365. await expect(switchContainer).toBeVisible()
  366. const toggleInput = switchContainer.locator('[data-slot="switch-input"]')
  367. const isDisabled = await toggleInput.evaluate((el: HTMLInputElement) => el.disabled)
  368. if (isDisabled) {
  369. test.skip()
  370. return
  371. }
  372. const initialState = await toggleInput.evaluate((el: HTMLInputElement) => el.checked)
  373. expect(initialState).toBe(true)
  374. await switchContainer.locator('[data-slot="switch-control"]').click()
  375. await page.waitForTimeout(100)
  376. const newState = await toggleInput.evaluate((el: HTMLInputElement) => el.checked)
  377. expect(newState).toBe(false)
  378. const stored = await page.evaluate((key) => {
  379. const raw = localStorage.getItem(key)
  380. return raw ? JSON.parse(raw) : null
  381. }, settingsKey)
  382. expect(stored?.updates?.startup).toBe(false)
  383. })
  384. test("toggling release notes switch updates localStorage", async ({ page, gotoSession }) => {
  385. await gotoSession()
  386. const dialog = await openSettings(page)
  387. const switchContainer = dialog.locator(settingsReleaseNotesSelector)
  388. await expect(switchContainer).toBeVisible()
  389. const toggleInput = switchContainer.locator('[data-slot="switch-input"]')
  390. const initialState = await toggleInput.evaluate((el: HTMLInputElement) => el.checked)
  391. expect(initialState).toBe(true)
  392. await switchContainer.locator('[data-slot="switch-control"]').click()
  393. await page.waitForTimeout(100)
  394. const newState = await toggleInput.evaluate((el: HTMLInputElement) => el.checked)
  395. expect(newState).toBe(false)
  396. const stored = await page.evaluate((key) => {
  397. const raw = localStorage.getItem(key)
  398. return raw ? JSON.parse(raw) : null
  399. }, settingsKey)
  400. expect(stored?.general?.releaseNotes).toBe(false)
  401. })