Files
orca/src/relay/git-handler-test-setup.ts
T
Trevin ChowandJinjing 3ec1a36ba9 feat: add safe fork upstream sync setting (#5408)
* add safe fork upstream sync setting

* fix fork sync review feedback

* Review safe fork sync setting

* Fix local preflight env typing

* Fix onboarding flow SSR test tooltip provider

---------

Co-authored-by: Jinjing <6427696+AmethystLiang@users.noreply.github.com>
2026-06-15 19:41:45 -07:00

91 lines
2.9 KiB
TypeScript

/**
* Shared test utilities for git-handler tests.
*
* Why: oxlint max-lines (300) requires splitting large test suites.
* This module exports the mock dispatcher factory and git helpers
* so multiple test files can reuse them without duplication.
*/
import { vi } from 'vitest'
import { execFileSync } from 'child_process'
import type { RelayDispatcher } from './dispatcher'
// Why: declare an explicit type so the inferred return type of
// createMockDispatcher doesn't transitively reference `@vitest/spy`'s
// internal `Procedure` type (from `vi.fn(...)`). Without this annotation,
// TS2883 fires under `pnpm run tc:node` because the generated .d.ts would
// need to name a type that isn't portably resolvable from this module.
export type MockDispatcher = {
onRequest: (
method: string,
handler: (
params: Record<string, unknown>,
context: { isStale: () => boolean; signal?: AbortSignal }
) => Promise<unknown>
) => void
onNotification: (method: string, handler: (params: Record<string, unknown>) => void) => void
notify: (method: string, params?: Record<string, unknown>) => void
_requestHandlers: Map<
string,
(
params: Record<string, unknown>,
context: { isStale: () => boolean; signal?: AbortSignal }
) => Promise<unknown>
>
callRequest(
method: string,
params?: Record<string, unknown>,
context?: { isStale: () => boolean; signal?: AbortSignal }
): Promise<unknown>
}
export function createMockDispatcher(): MockDispatcher {
const requestHandlers = new Map<
string,
(
params: Record<string, unknown>,
context: { isStale: () => boolean; signal?: AbortSignal }
) => Promise<unknown>
>()
return {
onRequest: vi.fn(
(
method: string,
handler: (
params: Record<string, unknown>,
context: { isStale: () => boolean; signal?: AbortSignal }
) => Promise<unknown>
) => {
requestHandlers.set(method, handler)
}
),
onNotification: vi.fn(),
notify: vi.fn(),
_requestHandlers: requestHandlers,
async callRequest(
method: string,
params: Record<string, unknown> = {},
context: { isStale: () => boolean; signal?: AbortSignal } = { isStale: () => false }
) {
const handler = requestHandlers.get(method)
if (!handler) {
throw new Error(`No handler for ${method}`)
}
return handler(params, context)
}
}
}
export function gitInit(dir: string): void {
execFileSync('git', ['init'], { cwd: dir, stdio: 'pipe' })
execFileSync('git', ['config', 'user.email', 'test@test.com'], { cwd: dir, stdio: 'pipe' })
execFileSync('git', ['config', 'user.name', 'Test'], { cwd: dir, stdio: 'pipe' })
}
export function gitCommit(dir: string, message: string): void {
execFileSync('git', ['add', '.'], { cwd: dir, stdio: 'pipe' })
execFileSync('git', ['commit', '-m', message, '--allow-empty'], { cwd: dir, stdio: 'pipe' })
}
export type { RelayDispatcher }