config-v2.test.tsx 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /** @jsxImportSource @opentui/solid */
  2. import { testRender } from "@opentui/solid"
  3. import { expect, test } from "bun:test"
  4. import { Schema } from "effect"
  5. import { resolve, ConfigProvider, Info, useConfig, type Interface } from "../src/config"
  6. test("validates mini replay settings", () => {
  7. const decode = Schema.decodeUnknownSync(Info)
  8. expect(decode({ mini: { replay: false, replay_limit: 50 } })).toEqual({
  9. mini: { replay: false, replay_limit: 50 },
  10. })
  11. expect(() => decode({ mini: { replay_limit: 0 } })).toThrow()
  12. expect(() => decode({ mini: { replay_limit: 1.5 } })).toThrow()
  13. })
  14. test("validates the session tabs setting", () => {
  15. const decode = Schema.decodeUnknownSync(Info)
  16. expect(decode({ tabs: { enabled: true } })).toEqual({ tabs: { enabled: true } })
  17. expect(() => decode({ tabs: { enabled: "on" } })).toThrow()
  18. })
  19. test("resolves nested config and keybind defaults", () => {
  20. const config = resolve(
  21. {
  22. keybinds: { leader: "ctrl+o" },
  23. leader: { timeout: 500 },
  24. scroll: { speed: 2, acceleration: true },
  25. diffs: { view: "split" },
  26. debug: { devtools: true },
  27. },
  28. { terminalSuspend: true },
  29. )
  30. expect(config.leader.timeout).toBe(500)
  31. expect(config.keybinds.get("leader")?.[0]?.key).toBe("ctrl+o")
  32. expect(config.scroll).toEqual({ speed: 2, acceleration: true })
  33. expect(config.diffs).toEqual({ view: "split" })
  34. expect(config.debug).toEqual({ devtools: true })
  35. })
  36. test("provides config and its host interface", async () => {
  37. const config = resolve({}, { terminalSuspend: true })
  38. let current = {}
  39. const service: Interface = {
  40. get: async () => current,
  41. update: async (update) => {
  42. const draft: Record<string, any> = { ...current }
  43. update(draft)
  44. current = draft
  45. return draft
  46. },
  47. }
  48. let context: ReturnType<typeof useConfig> | undefined
  49. function Consumer() {
  50. context = useConfig()
  51. return <text>{`${context.data.mouse ? "mouse" : "none"} ${context.data.keybinds.get("leader")?.[0]?.key}`}</text>
  52. }
  53. const app = await testRender(() => (
  54. <ConfigProvider config={config} service={service}>
  55. <Consumer />
  56. </ConfigProvider>
  57. ))
  58. try {
  59. await app.renderOnce()
  60. expect(app.captureCharFrame()).toContain("mouse ctrl+x")
  61. if (!context) throw new Error("Config context was not provided")
  62. await context.update((draft) => {
  63. draft.mouse = false
  64. draft.keybinds = { leader: "ctrl+o" }
  65. })
  66. await app.renderOnce()
  67. expect(app.captureCharFrame()).toContain("none ctrl+o")
  68. } finally {
  69. app.renderer.destroy()
  70. }
  71. })