mirror of
https://github.com/anomalyco/opencode.git
synced 2026-05-24 05:14:44 +00:00
185 lines
4.9 KiB
TypeScript
185 lines
4.9 KiB
TypeScript
/** @jsxImportSource @opentui/solid */
|
|
import { describe, expect, test } from "bun:test"
|
|
import { testRender } from "@opentui/solid"
|
|
import type { GlobalEvent } from "@opencode-ai/sdk/v2"
|
|
import { onMount } from "solid-js"
|
|
import { ProjectProvider, useProject } from "../../../src/cli/cmd/tui/context/project"
|
|
import { SDKProvider } from "../../../src/cli/cmd/tui/context/sdk"
|
|
import { useEvent } from "../../../src/cli/cmd/tui/context/event"
|
|
|
|
const projectID = "proj_test"
|
|
|
|
async function wait(fn: () => boolean, timeout = 2000) {
|
|
const start = Date.now()
|
|
while (!fn()) {
|
|
if (Date.now() - start > timeout) throw new Error("timed out waiting for condition")
|
|
await Bun.sleep(10)
|
|
}
|
|
}
|
|
|
|
function event(
|
|
payload: GlobalEvent["payload"],
|
|
input: { directory: string; project?: string; workspace?: string },
|
|
): GlobalEvent {
|
|
return {
|
|
directory: input.directory,
|
|
project: input.project,
|
|
workspace: input.workspace,
|
|
payload,
|
|
}
|
|
}
|
|
|
|
function vcs(branch: string): GlobalEvent["payload"] {
|
|
return {
|
|
id: `evt_vcs_${branch}`,
|
|
type: "vcs.branch.updated",
|
|
properties: {
|
|
branch,
|
|
},
|
|
}
|
|
}
|
|
|
|
function update(version: string): GlobalEvent["payload"] {
|
|
return {
|
|
id: `evt_update_${version}`,
|
|
type: "installation.update-available",
|
|
properties: {
|
|
version,
|
|
},
|
|
}
|
|
}
|
|
|
|
function createSource() {
|
|
let fn: ((event: GlobalEvent) => void) | undefined
|
|
|
|
return {
|
|
source: {
|
|
subscribe: async (handler: (event: GlobalEvent) => void) => {
|
|
fn = handler
|
|
return () => {
|
|
if (fn === handler) fn = undefined
|
|
}
|
|
},
|
|
},
|
|
emit(evt: GlobalEvent) {
|
|
if (!fn) throw new Error("event source not ready")
|
|
fn(evt)
|
|
},
|
|
}
|
|
}
|
|
|
|
async function mount() {
|
|
const source = createSource()
|
|
const seen: GlobalEvent["payload"][] = []
|
|
const workspaces: Array<string | undefined> = []
|
|
const fetch = (async (input: RequestInfo | URL) => {
|
|
const url = new URL(input instanceof Request ? input.url : String(input))
|
|
if (url.pathname === "/path") return Response.json({ home: "", state: "", config: "", directory: "/tmp/root" })
|
|
if (url.pathname === "/project/current") return Response.json({ id: projectID })
|
|
throw new Error(`unexpected request: ${url.pathname}`)
|
|
}) as typeof globalThis.fetch
|
|
let project!: ReturnType<typeof useProject>
|
|
let done!: () => void
|
|
const ready = new Promise<void>((resolve) => {
|
|
done = resolve
|
|
})
|
|
|
|
const app = await testRender(() => (
|
|
<SDKProvider url="http://test" directory="/tmp/root" events={source.source} fetch={fetch}>
|
|
<ProjectProvider>
|
|
<Probe
|
|
onReady={async (ctx) => {
|
|
project = ctx.project
|
|
await project.sync()
|
|
done()
|
|
}}
|
|
seen={seen}
|
|
workspaces={workspaces}
|
|
/>
|
|
</ProjectProvider>
|
|
</SDKProvider>
|
|
))
|
|
|
|
await ready
|
|
return { app, emit: source.emit, project, seen, workspaces }
|
|
}
|
|
|
|
function Probe(props: {
|
|
seen: GlobalEvent["payload"][]
|
|
workspaces: Array<string | undefined>
|
|
onReady: (ctx: { project: ReturnType<typeof useProject> }) => void
|
|
}) {
|
|
const project = useProject()
|
|
const event = useEvent()
|
|
|
|
onMount(() => {
|
|
event.subscribe((evt, { workspace }) => {
|
|
props.seen.push(evt as GlobalEvent["payload"])
|
|
props.workspaces.push(workspace)
|
|
})
|
|
props.onReady({ project })
|
|
})
|
|
|
|
return <box />
|
|
}
|
|
|
|
describe("useEvent", () => {
|
|
test("delivers events for the current project", async () => {
|
|
const { app, emit, seen, workspaces } = await mount()
|
|
|
|
try {
|
|
emit(event(vcs("main"), { directory: "/tmp/other", project: projectID, workspace: "ws_a" }))
|
|
|
|
await wait(() => seen.length === 1)
|
|
|
|
expect(seen).toEqual([vcs("main")])
|
|
expect(workspaces).toEqual(["ws_a"])
|
|
} finally {
|
|
app.renderer.destroy()
|
|
}
|
|
})
|
|
|
|
test("ignores events for other projects", async () => {
|
|
const { app, emit, seen } = await mount()
|
|
|
|
try {
|
|
emit(event(vcs("other"), { directory: "/tmp/root", project: "proj_other" }))
|
|
await Bun.sleep(30)
|
|
|
|
expect(seen).toHaveLength(0)
|
|
} finally {
|
|
app.renderer.destroy()
|
|
}
|
|
})
|
|
|
|
test("delivers current project events regardless of active workspace", async () => {
|
|
const { app, emit, project, seen } = await mount()
|
|
|
|
try {
|
|
project.workspace.set("ws_a")
|
|
emit(event(vcs("ws"), { directory: "/tmp/other", project: projectID, workspace: "ws_b" }))
|
|
|
|
await wait(() => seen.length === 1)
|
|
|
|
expect(seen).toEqual([vcs("ws")])
|
|
} finally {
|
|
app.renderer.destroy()
|
|
}
|
|
})
|
|
|
|
test("delivers truly global events even when a workspace is active", async () => {
|
|
const { app, emit, project, seen } = await mount()
|
|
|
|
try {
|
|
project.workspace.set("ws_a")
|
|
emit(event(update("1.2.3"), { directory: "global" }))
|
|
|
|
await wait(() => seen.length === 1)
|
|
|
|
expect(seen).toEqual([update("1.2.3")])
|
|
} finally {
|
|
app.renderer.destroy()
|
|
}
|
|
})
|
|
})
|