opencode/packages/opencode/test/cli/cmd/tui/sync.test.tsx
2026-05-23 14:14:30 +10:00

112 lines
3.4 KiB
TypeScript

/** @jsxImportSource @opentui/solid */
import { describe, expect, test } from "bun:test"
import { Global } from "@opencode-ai/core/global"
import { tmpdir } from "../../../fixture/fixture"
import { json, mount, wait } from "./sync-fixture"
import type { GlobalEvent } from "@opencode-ai/sdk/v2"
function branchEvent(branch: string, workspace?: string): GlobalEvent {
return {
directory: "/tmp/other",
project: "proj_test",
workspace,
payload: {
id: `evt_vcs_${branch}`,
type: "vcs.branch.updated",
properties: { branch },
},
}
}
function mcpStatusEvent(status: "connecting" | "connected", workspace?: string): GlobalEvent {
return {
directory: "/tmp/other",
project: "proj_test",
workspace,
payload: {
id: `evt_mcp_${status}`,
type: "mcp.status.changed",
properties: { name: "playwright", status: { status } },
},
}
}
describe("tui sync", () => {
test("refresh scopes sessions by default and lists project sessions when disabled", async () => {
const previous = Global.Path.state
await using tmp = await tmpdir()
Global.Path.state = tmp.path
await Bun.write(`${tmp.path}/kv.json`, "{}")
const { app, kv, sync, session } = await mount()
try {
expect(kv.get("session_directory_filter_enabled", true)).toBe(true)
expect(session.at(-1)?.searchParams.get("scope")).toBeNull()
expect(session.at(-1)?.searchParams.get("path")).toBe("packages/opencode")
kv.set("session_directory_filter_enabled", false)
await sync.session.refresh()
expect(session.at(-1)?.searchParams.get("scope")).toBe("project")
expect(session.at(-1)?.searchParams.get("path")).toBeNull()
} finally {
app.renderer.destroy()
Global.Path.state = previous
}
})
test("vcs branch updates only apply for the active workspace", async () => {
const previous = Global.Path.state
await using tmp = await tmpdir()
Global.Path.state = tmp.path
await Bun.write(`${tmp.path}/kv.json`, "{}")
const { app, emit, project, sync } = await mount()
try {
expect(sync.data.vcs?.branch).toBe("main")
project.workspace.set("ws_a")
emit(branchEvent("other", "ws_b"))
await Bun.sleep(30)
expect(sync.data.vcs?.branch).toBe("main")
emit(branchEvent("feature", "ws_a"))
await wait(() => sync.data.vcs?.branch === "feature")
expect(sync.data.vcs?.branch).toBe("feature")
} finally {
app.renderer.destroy()
Global.Path.state = previous
}
})
test("mcp status changes update the active workspace", async () => {
const previous = Global.Path.state
await using tmp = await tmpdir()
Global.Path.state = tmp.path
await Bun.write(`${tmp.path}/kv.json`, "{}")
const { app, emit, project, sync } = await mount((url) => {
if (url.pathname === "/mcp") return json({ playwright: { status: "connecting" } })
return undefined
})
try {
expect(sync.data.mcp.playwright?.status).toBe("connecting")
project.workspace.set("ws_a")
emit(mcpStatusEvent("connected", "ws_b"))
await Bun.sleep(30)
expect(sync.data.mcp.playwright?.status).toBe("connecting")
emit(mcpStatusEvent("connected", "ws_a"))
await wait(() => sync.data.mcp.playwright?.status === "connected")
expect(sync.data.mcp.playwright?.status).toBe("connected")
} finally {
app.renderer.destroy()
Global.Path.state = previous
}
})
})