Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/clis/grok/ask.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,32 @@
import { describe, expect, it } from 'vitest';
import type { IPage } from '../../types.js';
import { __test__ } from './ask.js';

describe('grok ask helpers', () => {
describe('isOnGrok', () => {
const fakePage = (url: string | Error): IPage =>
({ evaluate: () => url instanceof Error ? Promise.reject(url) : Promise.resolve(url) }) as unknown as IPage;

it('returns true for grok.com URLs', async () => {
expect(await __test__.isOnGrok(fakePage('https://grok.com/'))).toBe(true);
expect(await __test__.isOnGrok(fakePage('https://grok.com/chat/abc123'))).toBe(true);
});

it('returns true for grok.com subdomains', async () => {
expect(await __test__.isOnGrok(fakePage('https://api.grok.com/v1'))).toBe(true);
});

it('returns false for non-grok domains', async () => {
expect(await __test__.isOnGrok(fakePage('https://fakegrok.com/'))).toBe(false);
expect(await __test__.isOnGrok(fakePage('https://example.com/?next=grok.com'))).toBe(false);
expect(await __test__.isOnGrok(fakePage('about:blank'))).toBe(false);
});

it('returns false when evaluate throws (detached tab)', async () => {
expect(await __test__.isOnGrok(fakePage(new Error('detached')))).toBe(false);
});
});

it('normalizes boolean flags for explicit web routing', () => {
expect(__test__.normalizeBooleanFlag(true)).toBe(true);
expect(__test__.normalizeBooleanFlag('true')).toBe(true);
Expand Down
37 changes: 25 additions & 12 deletions src/clis/grok/ask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,28 +53,37 @@ function updateStableState(previousText: string, stableCount: number, nextText:
return { previousText: nextText, stableCount: 0 };
}

/** Check whether the tab is already on grok.com (any path). */
async function isOnGrok(page: IPage): Promise<boolean> {
// catch handles blank tabs (about:blank) or detached pages
const url = await page.evaluate('window.location.href').catch(() => '');
if (typeof url !== 'string' || !url) return false;
try {
const hostname = new URL(url).hostname;
return hostname === 'grok.com' || hostname.endsWith('.grok.com');
} catch {
return false;
}
}

async function runDefaultAsk(
page: IPage,
prompt: string,
timeoutMs: number,
newChat: boolean,
) {
if (newChat) {
// Explicitly start a fresh conversation via the homepage
await page.goto(GROK_URL);
await page.wait(2);
await page.evaluate(`(() => {
const btn = [...document.querySelectorAll('a, button')].find(b => {
const t = (b.textContent || '').trim().toLowerCase();
return t.includes('new') || b.getAttribute('href') === '/';
});
if (btn) btn.click();
})()`);
await tryStartFreshChat(page);
await page.wait(2);
} else if (!(await isOnGrok(page))) {
// First invocation or tab was recycled β€” navigate to Grok
await page.goto(GROK_URL);
await page.wait(3);
}

await page.goto(GROK_URL);
await page.wait(3);

const promptJson = JSON.stringify(prompt);
const sendResult = await page.evaluate(`(async () => {
try {
Expand Down Expand Up @@ -249,11 +258,14 @@ async function runExplicitWebAsk(
timeoutMs: number,
newChat: boolean,
) {
await page.goto(GROK_URL, { settleMs: 2000 });

if (newChat) {
// Navigate to homepage and start a fresh conversation
await page.goto(GROK_URL, { settleMs: 2000 });
await tryStartFreshChat(page);
await page.wait(2);
} else if (!(await isOnGrok(page))) {
// First invocation or tab was recycled β€” navigate to Grok
await page.goto(GROK_URL, { settleMs: 2000 });
}

const baselineBubbles = await getBubbleTexts(page);
Expand Down Expand Up @@ -318,4 +330,5 @@ export const __test__ = {
updateStableState,
normalizeBooleanFlag,
normalizeBubbleText,
isOnGrok,
};
1 change: 1 addition & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export default defineConfig({
'src/clis/reddit/**/*.test.ts',
'src/clis/bilibili/**/*.test.ts',
'src/clis/linkedin/**/*.test.ts',
'src/clis/grok/**/*.test.ts',
],
sequence: {
groupOrder: 1,
Expand Down
Loading