refactor(cli): extract Query CLI Adapter Module seam (#3074)
* refactor(cli): extract query adapter seam from cli entrypoint * test: update ws forwarding guard for query-cli-adapter seam * fix(query): close remaining CodeRabbit findings on cli adapter * test: address remaining CodeRabbit nitpicks on ws forwarding coverage
This commit is contained in:
58
sdk/src/query/query-cli-adapter.test.ts
Normal file
58
sdk/src/query/query-cli-adapter.test.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
const dispatchSpy = vi.hoisted(() => vi.fn());
|
||||
const runQueryDispatchSpy = vi.hoisted(() => vi.fn());
|
||||
|
||||
vi.mock('./helpers.js', () => ({
|
||||
findProjectRoot: (projectDir: string) => projectDir,
|
||||
}));
|
||||
|
||||
vi.mock('./index.js', () => ({
|
||||
createRegistry: () => ({ dispatch: dispatchSpy }),
|
||||
}));
|
||||
|
||||
vi.mock('./query-dispatch.js', () => ({
|
||||
runQueryDispatch: (...args: unknown[]) => runQueryDispatchSpy(...args),
|
||||
}));
|
||||
|
||||
import { runQueryCliCommand } from './query-cli-adapter.js';
|
||||
|
||||
describe('query-cli-adapter', () => {
|
||||
beforeEach(() => {
|
||||
dispatchSpy.mockReset();
|
||||
runQueryDispatchSpy.mockReset();
|
||||
});
|
||||
|
||||
it('returns validation failure for missing query command', async () => {
|
||||
runQueryDispatchSpy.mockResolvedValueOnce({
|
||||
ok: false,
|
||||
exit_code: 10,
|
||||
stdout: '',
|
||||
stderr: [],
|
||||
error: { kind: 'validation_error', message: 'query requires a command', details: {} },
|
||||
});
|
||||
|
||||
const out = await runQueryCliCommand({
|
||||
projectDir: process.cwd(),
|
||||
queryArgv: [],
|
||||
});
|
||||
|
||||
expect(out.exitCode).toBe(10);
|
||||
expect(out.stderrLines.join('\n')).toContain('requires a command');
|
||||
});
|
||||
|
||||
it('forwards ws to registry.dispatch via dispatchNative', async () => {
|
||||
runQueryDispatchSpy.mockImplementationOnce(async (input: any) => {
|
||||
await input.dispatchNative('state', ['show']);
|
||||
return { ok: true, exit_code: 0, stdout: '', stderr: [] };
|
||||
});
|
||||
|
||||
await runQueryCliCommand({
|
||||
projectDir: process.cwd(),
|
||||
ws: 'alpha',
|
||||
queryArgv: ['state', 'show'],
|
||||
});
|
||||
|
||||
expect(dispatchSpy).toHaveBeenCalledWith('state', ['show'], process.cwd(), 'alpha');
|
||||
});
|
||||
});
|
||||
71
sdk/src/query/query-cli-adapter.ts
Normal file
71
sdk/src/query/query-cli-adapter.ts
Normal file
@@ -0,0 +1,71 @@
|
||||
import { findProjectRoot } from './helpers.js';
|
||||
import { createRegistry } from './index.js';
|
||||
import { runQueryDispatch } from './query-dispatch.js';
|
||||
import { resolveGsdToolsPath, GSDToolsError } from '../gsd-tools.js';
|
||||
import { GSDError, exitCodeFor } from '../errors.js';
|
||||
import { validateWorkstreamName } from '../workstream-utils.js';
|
||||
|
||||
export interface QueryCliAdapterInput {
|
||||
projectDir: string;
|
||||
ws?: string;
|
||||
queryArgv?: string[];
|
||||
}
|
||||
|
||||
export interface QueryCliAdapterOutput {
|
||||
exitCode: number;
|
||||
stdoutChunks: string[];
|
||||
stderrLines: string[];
|
||||
}
|
||||
|
||||
function queryFallbackToCjsEnabled(): boolean {
|
||||
const v = process.env.GSD_QUERY_FALLBACK?.toLowerCase();
|
||||
if (v === 'off' || v === 'never' || v === 'false' || v === '0') return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
function resolveQueryWorkstream(ws: string | undefined): string | undefined {
|
||||
if (ws !== undefined) {
|
||||
return validateWorkstreamName(ws) ? ws : undefined;
|
||||
}
|
||||
const envWs = process.env.GSD_WORKSTREAM;
|
||||
if (!envWs) return undefined;
|
||||
return validateWorkstreamName(envWs) ? envWs : undefined;
|
||||
}
|
||||
|
||||
export async function runQueryCliCommand(input: QueryCliAdapterInput): Promise<QueryCliAdapterOutput> {
|
||||
const stderrLines: string[] = [];
|
||||
const stdoutChunks: string[] = [];
|
||||
const ws = resolveQueryWorkstream(input.ws);
|
||||
|
||||
try {
|
||||
const projectDir = findProjectRoot(input.projectDir);
|
||||
const registry = createRegistry();
|
||||
const out = await runQueryDispatch({
|
||||
registry,
|
||||
projectDir,
|
||||
ws,
|
||||
cjsFallbackEnabled: queryFallbackToCjsEnabled(),
|
||||
resolveGsdToolsPath,
|
||||
dispatchNative: (cmd, argv) => registry.dispatch(cmd, argv, projectDir, ws),
|
||||
}, input.queryArgv ?? []);
|
||||
|
||||
stderrLines.push(...out.stderr);
|
||||
if (!out.ok) {
|
||||
stderrLines.push(out.error.message);
|
||||
return { exitCode: out.exit_code, stdoutChunks, stderrLines };
|
||||
}
|
||||
if (out.stdout) stdoutChunks.push(out.stdout);
|
||||
return { exitCode: 0, stdoutChunks, stderrLines };
|
||||
} catch (err) {
|
||||
if (err instanceof GSDError) {
|
||||
stderrLines.push(`Error: ${err.message}`);
|
||||
return { exitCode: exitCodeFor(err.classification), stdoutChunks, stderrLines };
|
||||
}
|
||||
if (err instanceof GSDToolsError) {
|
||||
stderrLines.push(`Error: ${err.message}`);
|
||||
return { exitCode: err.exitCode ?? 1, stdoutChunks, stderrLines };
|
||||
}
|
||||
stderrLines.push(`Error: ${err instanceof Error ? err.message : String(err)}`);
|
||||
return { exitCode: 1, stdoutChunks, stderrLines };
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user