use-event.test.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /** @jsxImportSource @opentui/solid */
  2. import { describe, expect, test } from "bun:test"
  3. import { testRender } from "@opentui/solid"
  4. import type { Event, GlobalEvent } from "@opencode-ai/sdk/v2"
  5. import { onMount } from "solid-js"
  6. import { ProjectProvider, useProject } from "../../../src/cli/cmd/tui/context/project"
  7. import { SDKProvider } from "../../../src/cli/cmd/tui/context/sdk"
  8. import { useEvent } from "../../../src/cli/cmd/tui/context/event"
  9. const projectID = "proj_test"
  10. async function wait(fn: () => boolean, timeout = 2000) {
  11. const start = Date.now()
  12. while (!fn()) {
  13. if (Date.now() - start > timeout) throw new Error("timed out waiting for condition")
  14. await Bun.sleep(10)
  15. }
  16. }
  17. function event(payload: Event, input: { directory: string; project?: string; workspace?: string }): GlobalEvent {
  18. return {
  19. directory: input.directory,
  20. project: input.project,
  21. workspace: input.workspace,
  22. payload,
  23. }
  24. }
  25. function vcs(branch: string): Event {
  26. return {
  27. id: `evt_vcs_${branch}`,
  28. type: "vcs.branch.updated",
  29. properties: {
  30. branch,
  31. },
  32. }
  33. }
  34. function update(version: string): Event {
  35. return {
  36. id: `evt_update_${version}`,
  37. type: "installation.update-available",
  38. properties: {
  39. version,
  40. },
  41. }
  42. }
  43. function createSource() {
  44. let fn: ((event: GlobalEvent) => void) | undefined
  45. return {
  46. source: {
  47. subscribe: async (handler: (event: GlobalEvent) => void) => {
  48. fn = handler
  49. return () => {
  50. if (fn === handler) fn = undefined
  51. }
  52. },
  53. },
  54. emit(evt: GlobalEvent) {
  55. if (!fn) throw new Error("event source not ready")
  56. fn(evt)
  57. },
  58. }
  59. }
  60. async function mount() {
  61. const source = createSource()
  62. const seen: Event[] = []
  63. const workspaces: Array<string | undefined> = []
  64. const fetch = (async (input: RequestInfo | URL) => {
  65. const url = new URL(input instanceof Request ? input.url : String(input))
  66. if (url.pathname === "/path") return Response.json({ home: "", state: "", config: "", directory: "/tmp/root" })
  67. if (url.pathname === "/project/current") return Response.json({ id: projectID })
  68. throw new Error(`unexpected request: ${url.pathname}`)
  69. }) as typeof globalThis.fetch
  70. let project!: ReturnType<typeof useProject>
  71. let done!: () => void
  72. const ready = new Promise<void>((resolve) => {
  73. done = resolve
  74. })
  75. const app = await testRender(() => (
  76. <SDKProvider url="http://test" directory="/tmp/root" events={source.source} fetch={fetch}>
  77. <ProjectProvider>
  78. <Probe
  79. onReady={async (ctx) => {
  80. project = ctx.project
  81. await project.sync()
  82. done()
  83. }}
  84. seen={seen}
  85. workspaces={workspaces}
  86. />
  87. </ProjectProvider>
  88. </SDKProvider>
  89. ))
  90. await ready
  91. return { app, emit: source.emit, project, seen, workspaces }
  92. }
  93. function Probe(props: {
  94. seen: Event[]
  95. workspaces: Array<string | undefined>
  96. onReady: (ctx: { project: ReturnType<typeof useProject> }) => void
  97. }) {
  98. const project = useProject()
  99. const event = useEvent()
  100. onMount(() => {
  101. event.subscribe((evt, { workspace }) => {
  102. props.seen.push(evt)
  103. props.workspaces.push(workspace)
  104. })
  105. props.onReady({ project })
  106. })
  107. return <box />
  108. }
  109. describe("useEvent", () => {
  110. test("delivers events for the current project", async () => {
  111. const { app, emit, seen, workspaces } = await mount()
  112. try {
  113. emit(event(vcs("main"), { directory: "/tmp/other", project: projectID, workspace: "ws_a" }))
  114. await wait(() => seen.length === 1)
  115. expect(seen).toEqual([vcs("main")])
  116. expect(workspaces).toEqual(["ws_a"])
  117. } finally {
  118. app.renderer.destroy()
  119. }
  120. })
  121. test("ignores events for other projects", async () => {
  122. const { app, emit, seen } = await mount()
  123. try {
  124. emit(event(vcs("other"), { directory: "/tmp/root", project: "proj_other" }))
  125. await Bun.sleep(30)
  126. expect(seen).toHaveLength(0)
  127. } finally {
  128. app.renderer.destroy()
  129. }
  130. })
  131. test("delivers current project events regardless of active workspace", async () => {
  132. const { app, emit, project, seen } = await mount()
  133. try {
  134. project.workspace.set("ws_a")
  135. emit(event(vcs("ws"), { directory: "/tmp/other", project: projectID, workspace: "ws_b" }))
  136. await wait(() => seen.length === 1)
  137. expect(seen).toEqual([vcs("ws")])
  138. } finally {
  139. app.renderer.destroy()
  140. }
  141. })
  142. test("delivers truly global events even when a workspace is active", async () => {
  143. const { app, emit, project, seen } = await mount()
  144. try {
  145. project.workspace.set("ws_a")
  146. emit(event(update("1.2.3"), { directory: "global" }))
  147. await wait(() => seen.length === 1)
  148. expect(seen).toEqual([update("1.2.3")])
  149. } finally {
  150. app.renderer.destroy()
  151. }
  152. })
  153. })