config-v2.test.tsx 2.3 KB

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