Prepare TUI lifecycle for scenario tests (#28258)

This commit is contained in:
Sebastian
2026-05-26 21:02:02 +02:00
committed by GitHub
parent fdfd0afed7
commit 848d763d08
9 changed files with 596 additions and 280 deletions

View File

@@ -6,6 +6,7 @@ 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"
import { createEventSource, createFetch, directory } from "../../fixture/tui-sdk"
const projectID = "proj_test"
@@ -46,35 +47,11 @@ function update(version: string): Event {
}
}
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 events = createEventSource()
const calls = createFetch()
const seen: Event[] = []
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) => {
@@ -82,7 +59,7 @@ async function mount() {
})
const app = await testRender(() => (
<SDKProvider url="http://test" directory="/tmp/root" events={source.source} fetch={fetch}>
<SDKProvider url="http://test" directory={directory} events={events.source} fetch={calls.fetch}>
<ProjectProvider>
<Probe
onReady={async (ctx) => {
@@ -98,7 +75,7 @@ async function mount() {
))
await ready
return { app, emit: source.emit, project, seen, workspaces }
return { app, emit: events.emit, project, seen, workspaces }
}
function Probe(props: {
@@ -140,7 +117,7 @@ describe("useEvent", () => {
const { app, emit, seen } = await mount()
try {
emit(event(vcs("other"), { directory: "/tmp/root", project: "proj_other" }))
emit(event(vcs("other"), { directory, project: "proj_other" }))
await Bun.sleep(30)
expect(seen).toHaveLength(0)