mirror of
https://github.com/jackwener/OpenCLI.git
synced 2026-09-14 18:25:42 +08:00
12d88e4b23
* refactor(runtime): unify command timeout into a single --timeout arg Drop the cli-level `timeoutSeconds` build-time ceiling field. A command now opts into runtime-enforced timeouts purely by declaring an arg named `timeout`; the user-facing `--timeout` value (its default or override) is the single authoritative knob, used both by the adapter polling loop and by the runtime ceiling (with a 30s padding for return + closeWindow + trace export). Behavior: - Browser commands without a `--timeout` arg fall back to OPENCLI_BROWSER_COMMAND_TIMEOUT (default 60s, unchanged). - Non-browser commands without a `--timeout` arg now run unbounded rather than against the previously implicit `timeoutSeconds` cap. Affected commands keep their old caps via newly added `--timeout` args. - LLM adapters (gemini/claude/deepseek/doubao/qwen/yuanbao ask) keep their current `--timeout` defaults; the runtime ceiling is now strictly more generous (userTimeout + 30s vs. the previous 180s cap), so `--timeout 600` actually buys 600s of polling rather than dying at 180s. Closes the design discussion that started from PR #1227, which proposed a per-site `OPENCLI_GEMINI_ASK_TIMEOUT` env var to work around the same underlying mismatch. * fix(timeout): wire --timeout arg into chatgpt/gemini image adapter polling codex-coder review on PR #1364 caught that the new --timeout arg I added to chatgpt/image and gemini/image only drove the runtime ceiling — the adapter still hardcoded `const timeout = 120`, so users passing --timeout 240/600 saw runtime allow 270s/630s but the adapter stop polling at 120s. That recreated the same single-knob mismatch this PR was meant to delete. Also add the browser-path runWithTimeout assertion codex-coder flagged as missing: a browser command with --timeout default=5 must call runWithTimeout with timeout: 35; a browser command without --timeout arg must fall back to DEFAULT_BROWSER_COMMAND_TIMEOUT. Image adapters now read kwargs.timeout and reject non-positive-integer values with ArgumentError (no silent fallback). chatgpt/image.test.js updated to pass an explicit timeout when calling .func directly (the test bypasses arg coercion). * fix(runtime): reject invalid timeout ceilings * fix(timeout): normalize timeout args to integer values * fix(timeout): preserve remaining command ceilings * fix(runtime): validate timeout before browser setup
156 lines
9.1 KiB
JavaScript
156 lines
9.1 KiB
JavaScript
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
const { mockClickGeminiConversationByTitle, mockExportGeminiDeepResearchReport, mockGetGeminiConversationList, mockGetGeminiPageState, mockGetLatestGeminiAssistantResponse, mockReadGeminiSnapshot, mockResolveGeminiConversationForQuery, mockWaitForGeminiTranscript, } = vi.hoisted(() => ({
|
|
mockClickGeminiConversationByTitle: vi.fn(),
|
|
mockExportGeminiDeepResearchReport: vi.fn(),
|
|
mockGetGeminiConversationList: vi.fn(),
|
|
mockGetGeminiPageState: vi.fn(),
|
|
mockGetLatestGeminiAssistantResponse: vi.fn(),
|
|
mockReadGeminiSnapshot: vi.fn(),
|
|
mockResolveGeminiConversationForQuery: vi.fn(),
|
|
mockWaitForGeminiTranscript: vi.fn(),
|
|
}));
|
|
vi.mock('./utils.js', () => ({
|
|
GEMINI_DOMAIN: 'gemini.google.com',
|
|
clickGeminiConversationByTitle: mockClickGeminiConversationByTitle,
|
|
exportGeminiDeepResearchReport: mockExportGeminiDeepResearchReport,
|
|
getGeminiConversationList: mockGetGeminiConversationList,
|
|
getGeminiPageState: mockGetGeminiPageState,
|
|
getLatestGeminiAssistantResponse: mockGetLatestGeminiAssistantResponse,
|
|
readGeminiSnapshot: mockReadGeminiSnapshot,
|
|
parseGeminiConversationUrl: (value) => {
|
|
const raw = String(value ?? '').trim();
|
|
return raw.startsWith('https://gemini.google.com/app/') ? raw : null;
|
|
},
|
|
parseGeminiTitleMatchMode: (value) => {
|
|
const raw = String(value ?? 'contains').trim().toLowerCase();
|
|
if (raw === 'contains' || raw === 'exact')
|
|
return raw;
|
|
return null;
|
|
},
|
|
resolveGeminiConversationForQuery: mockResolveGeminiConversationForQuery,
|
|
waitForGeminiTranscript: mockWaitForGeminiTranscript,
|
|
}));
|
|
import { deepResearchResultCommand } from './deep-research-result.js';
|
|
describe('gemini/deep-research-result', () => {
|
|
const page = {
|
|
goto: vi.fn().mockResolvedValue(undefined),
|
|
wait: vi.fn().mockResolvedValue(undefined),
|
|
};
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
mockGetGeminiPageState.mockResolvedValue({ isSignedIn: true });
|
|
mockGetGeminiConversationList.mockResolvedValue([{ Title: 'A title', Url: 'https://gemini.google.com/app/abc' }]);
|
|
mockResolveGeminiConversationForQuery.mockReturnValue({ Title: 'A title', Url: 'https://gemini.google.com/app/abc' });
|
|
mockClickGeminiConversationByTitle.mockResolvedValue(true);
|
|
mockWaitForGeminiTranscript.mockResolvedValue(['line']);
|
|
mockExportGeminiDeepResearchReport.mockResolvedValue({ url: 'https://files.example.com/report.md', source: 'network' });
|
|
mockGetLatestGeminiAssistantResponse.mockResolvedValue('Final answer');
|
|
mockReadGeminiSnapshot.mockResolvedValue({
|
|
turns: [],
|
|
transcriptLines: [],
|
|
composerHasText: false,
|
|
isGenerating: false,
|
|
structuredTurnsTrusted: true,
|
|
});
|
|
});
|
|
const runCommand = (kwargs) => deepResearchResultCommand.func(page, { timeout: 120, ...kwargs });
|
|
it('uses latest conversation when query is empty', async () => {
|
|
const result = await runCommand({ query: ' ' });
|
|
expect(page.goto).toHaveBeenCalledWith('https://gemini.google.com/app/abc', { waitUntil: 'load', settleMs: 2500 });
|
|
expect(result).toEqual([{ response: 'https://files.example.com/report.md' }]);
|
|
});
|
|
it('falls back to current page response when query is empty and sidebar has no conversations', async () => {
|
|
mockGetGeminiConversationList.mockResolvedValue([]);
|
|
mockResolveGeminiConversationForQuery.mockReturnValue(null);
|
|
const result = await runCommand({ query: '' });
|
|
expect(page.goto).not.toHaveBeenCalled();
|
|
expect(result).toEqual([{ response: 'https://files.example.com/report.md' }]);
|
|
});
|
|
it('returns a validation message when match mode is invalid', async () => {
|
|
const result = await runCommand({ query: 'A', match: 'prefix' });
|
|
expect(result).toEqual([{ response: 'Invalid match mode. Use contains or exact.' }]);
|
|
});
|
|
it('returns a signed-out message when Gemini page state indicates logged out', async () => {
|
|
mockGetGeminiPageState.mockResolvedValue({ isSignedIn: false });
|
|
const result = await runCommand({ query: 'A' });
|
|
expect(result).toEqual([{ response: 'Not signed in to Gemini.' }]);
|
|
});
|
|
it('opens matched conversation by URL and returns exported report url', async () => {
|
|
const result = await runCommand({ query: 'A title', match: 'exact' });
|
|
expect(page.goto).toHaveBeenCalledWith('https://gemini.google.com/app/abc', { waitUntil: 'load', settleMs: 2500 });
|
|
expect(result).toEqual([{ response: 'https://files.example.com/report.md' }]);
|
|
});
|
|
it('accepts a direct conversation URL and reads response from that page', async () => {
|
|
const url = 'https://gemini.google.com/app/direct-id';
|
|
const result = await runCommand({ query: url, match: 'contains' });
|
|
expect(page.goto).toHaveBeenCalledWith(url, { waitUntil: 'load', settleMs: 2500 });
|
|
expect(result).toEqual([{ response: 'https://files.example.com/report.md' }]);
|
|
});
|
|
it('passes query and mode into resolveGeminiConversationForQuery', async () => {
|
|
const result = await runCommand({ query: 'title', match: 'contains' });
|
|
expect(mockResolveGeminiConversationForQuery).toHaveBeenCalledWith([{ Title: 'A title', Url: 'https://gemini.google.com/app/abc' }], 'title', 'contains');
|
|
expect(result).toEqual([{ response: 'https://files.example.com/report.md' }]);
|
|
});
|
|
it('falls back to click-by-title and returns not-found when click fails', async () => {
|
|
mockResolveGeminiConversationForQuery.mockReturnValue(null);
|
|
mockClickGeminiConversationByTitle.mockResolvedValue(false);
|
|
const result = await runCommand({ query: 'missing', match: 'contains' });
|
|
expect(result).toEqual([{ response: 'No conversation matched: missing' }]);
|
|
});
|
|
it('returns pending message when export url is unavailable and completion is not confirmed', async () => {
|
|
mockExportGeminiDeepResearchReport.mockResolvedValue({ url: '', source: 'none' });
|
|
const result = await runCommand({ query: 'A title' });
|
|
expect(result).toEqual([{ response: 'Deep Research may still be running or preparing export. Please wait and retry later.' }]);
|
|
});
|
|
it('returns waiting message when deep research is still generating', async () => {
|
|
mockExportGeminiDeepResearchReport.mockResolvedValue({ url: '', source: 'none' });
|
|
mockReadGeminiSnapshot.mockResolvedValue({
|
|
turns: [],
|
|
transcriptLines: [],
|
|
composerHasText: false,
|
|
isGenerating: true,
|
|
structuredTurnsTrusted: true,
|
|
});
|
|
const result = await runCommand({ query: 'A title' });
|
|
expect(result).toEqual([{ response: 'Deep Research is still running. Please wait and retry later.' }]);
|
|
});
|
|
it('returns waiting message when assistant response indicates research in progress', async () => {
|
|
mockExportGeminiDeepResearchReport.mockResolvedValue({ url: '', source: 'none' });
|
|
mockGetLatestGeminiAssistantResponse.mockResolvedValue('正在研究中,请稍候。');
|
|
const result = await runCommand({ query: 'A title' });
|
|
expect(result).toEqual([{ response: 'Deep Research is still running. Please wait and retry later.' }]);
|
|
});
|
|
it('returns waiting message when transcript indicates in-progress status', async () => {
|
|
mockExportGeminiDeepResearchReport.mockResolvedValue({ url: '', source: 'none' });
|
|
mockGetLatestGeminiAssistantResponse.mockResolvedValue('');
|
|
mockReadGeminiSnapshot.mockResolvedValue({
|
|
turns: [],
|
|
transcriptLines: ['生成研究计划中,请稍候。'],
|
|
composerHasText: false,
|
|
isGenerating: false,
|
|
structuredTurnsTrusted: true,
|
|
});
|
|
const result = await runCommand({ query: 'A title' });
|
|
expect(result).toEqual([{ response: 'Deep Research is still running. Please wait and retry later.' }]);
|
|
});
|
|
it('returns no-docs message when text indicates completed state', async () => {
|
|
mockExportGeminiDeepResearchReport.mockResolvedValue({ url: '', source: 'none' });
|
|
mockGetLatestGeminiAssistantResponse.mockResolvedValue('Researching websites... Completed');
|
|
mockReadGeminiSnapshot.mockResolvedValue({
|
|
turns: [],
|
|
transcriptLines: [],
|
|
composerHasText: false,
|
|
isGenerating: false,
|
|
structuredTurnsTrusted: true,
|
|
});
|
|
const result = await runCommand({ query: 'A title' });
|
|
expect(result).toEqual([{ response: 'No Docs URL found. Please check Share & Export -> Export to Docs in Gemini UI.' }]);
|
|
});
|
|
it('returns pending message when assistant response is empty', async () => {
|
|
mockExportGeminiDeepResearchReport.mockResolvedValue({ url: '', source: 'none' });
|
|
mockGetLatestGeminiAssistantResponse.mockResolvedValue('');
|
|
const result = await runCommand({ query: 'A title' });
|
|
expect(result).toEqual([{ response: 'Deep Research may still be running or preparing export. Please wait and retry later.' }]);
|
|
});
|
|
});
|