import type { DevframeNodeContext } from 'devframe' import type { StartedServer } from 'devframe/internal' import type { GitDevframeOptions } from '../src/node/index' import { tmpdir } from 'node:os' import { DEVFRAME_CONNECTION_META_FILENAME } from 'devframe/constants' import { createH3DevframeHost } from 'devframe/internal' import { createHostContext } from 'devframe/node' import { getPort } from 'devframe/utils/get-port' import { resolveStaticAssetsSource } from 'devframe/utils/remote-assets' import { mountStaticHandler } from 'devframe/utils/serve-static' import { H3 } from 'h3' import { resolve } from 'pathe' import { serveTestContext } from '../../../tests/helpers/serve-test-context' import { createGitDevframe } from '../src/node/index' export interface DashboardServer extends StartedServer { basePath: string } /** * Build a node context rooted at `cwd`. Used directly by the build-dump test * and indirectly by {@link startDashboardServer}. */ export async function createDashboardContext( cwd: string, mode: 'dev' | 'build' = 'dev', options: GitDevframeOptions = {}, app: H3 = new H3(), ): Promise { const devframe = createGitDevframe(options) const h3Host = createH3DevframeHost({ origin: 'http://127.0.0.1', appName: devframe.id, mount: (base, dir) => mountStaticHandler(app, base, dir), }) const ctx = await createHostContext({ cwd, mode, host: h3Host }) // Mirror the adapters: install the declared wire services and ready them // before setup, so `devframes:service:git:*` is registered. for (const input of devframe.services ?? []) void ctx.services.install(input, { resolveFrom: devframe.importMetaUrl }) await ctx.services.ready() await devframe.setup(ctx) return ctx } /** * Boot the dashboard server in-process against `cwd`, mirroring the CLI * adapter's WS+HTTP wiring on a random free port. Bound to 127.0.0.1 to * avoid the IPv4/IPv6 race documented in devframe's ws transport tests. */ export async function startDashboardServer( cwd: string, options: GitDevframeOptions = {}, ): Promise { const devframe = createGitDevframe(options) // The client SPA ships in the workspace-linked `--assets` package in dev. const distDir = resolveStaticAssetsSource(devframe.clientAssets!, resolve(tmpdir(), 'devframes_plugin_git-test'), devframe.importMetaUrl) if (typeof distDir !== 'string') throw new TypeError('these tests serve the local client SPA; build the plugin first') // The factory leaves basePath adapter-resolved; standalone defaults to '/'. const basePath = devframe.basePath ?? '/' const host = '127.0.0.1' const port = await getPort({ host, random: true }) const app = new H3() const h3Host = createH3DevframeHost({ origin: `http://${host}:${port}`, appName: devframe.id, mount: (base, dir) => mountStaticHandler(app, base, dir), }) const ctx = await createHostContext({ cwd, mode: 'dev', host: h3Host }) for (const input of devframe.services ?? []) void ctx.services.install(input, { resolveFrom: devframe.importMetaUrl }) await ctx.services.ready() await devframe.setup(ctx) const metaPath = `${basePath}${DEVFRAME_CONNECTION_META_FILENAME}` app.use(metaPath, () => ({ backend: 'websocket', websocket: port })) mountStaticHandler(app, basePath, resolve(distDir)) const server = await serveTestContext({ context: ctx, host, port, app, auth: false, }) return Object.assign(server, { basePath }) }