runtime.test.tsx 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import { expect, test } from "bun:test"
  2. import { testRender } from "@opentui/solid"
  3. import {
  4. abbreviateHome,
  5. createTuiBuildInfo,
  6. createTuiEnvironment,
  7. TuiBuildInfoProvider,
  8. TuiEnvironmentProvider,
  9. useTuiBuildInfo,
  10. useTuiEnvironment,
  11. } from "../src/runtime"
  12. test("abbreviates paths within home boundaries", () => {
  13. expect(abbreviateHome("/home/test", "/home/test")).toBe("~")
  14. expect(abbreviateHome("/home/test/project", "/home/test")).toBe("~/project")
  15. expect(abbreviateHome("/home/tester/project", "/home/test")).toBe("/home/tester/project")
  16. expect(abbreviateHome("/tmp/project", "/home/test")).toBe("/tmp/project")
  17. })
  18. test("provides immutable runtime inputs", async () => {
  19. const environment = createTuiEnvironment({
  20. cwd: "/work",
  21. platform: "linux",
  22. paths: { home: "/home/test", state: "/state", worktree: "/data/worktree" },
  23. capabilities: {
  24. mouse: true,
  25. copyOnSelect: true,
  26. terminalTitle: true,
  27. terminalSuspend: true,
  28. workspaces: false,
  29. showTimeToFirstDraw: false,
  30. },
  31. terminal: { multiplexer: "tmux", displayServer: "wayland" },
  32. editor: { command: "vim", port: 4242, zedTerminal: false },
  33. skipInitialLoading: false,
  34. })
  35. const build = createTuiBuildInfo({ version: "1.2.3", channel: "beta" })
  36. function Runtime() {
  37. const runtime = useTuiEnvironment()
  38. const info = useTuiBuildInfo()
  39. return <text>{`${runtime.cwd} ${runtime.editor.command} ${info.version}`}</text>
  40. }
  41. const app = await testRender(
  42. () => (
  43. <TuiEnvironmentProvider value={environment}>
  44. <TuiBuildInfoProvider value={build}>
  45. <Runtime />
  46. </TuiBuildInfoProvider>
  47. </TuiEnvironmentProvider>
  48. ),
  49. { width: 40, height: 3 },
  50. )
  51. try {
  52. await app.renderOnce()
  53. expect(app.captureCharFrame()).toContain("/work vim 1.2.3")
  54. expect(Object.isFrozen(environment)).toBe(true)
  55. expect(Object.isFrozen(environment.paths)).toBe(true)
  56. expect(Object.isFrozen(build)).toBe(true)
  57. } finally {
  58. app.renderer.destroy()
  59. }
  60. })