Files
jackwener__opencli/clis/grok/ask.test.js
jakevin d2974a9ff6 refactor(adapters): convert adapter layer from TypeScript to JavaScript (#928)
* refactor(adapters): convert adapter layer from TypeScript to JavaScript

Core framework stays TypeScript; adapter layer moves to JS-first.
Adapters are essentially "executable config + browser scripts" that
barely use TS features — this simplifies the build/distribution pipeline
by removing the dist/clis/ intermediate compilation step.

Changes:
- Convert all 753 adapter files in clis/ from .ts to .js
- Update tsconfig to exclude clis/ from compilation
- Simplify build-manifest to scan clis/*.js directly (no dist/clis/)
- Update discovery, main, fetch-adapters to load JS adapters from clis/
- Update generate-verified to output .js artifacts
- Update package.json files field: dist/clis/ → clis/
- Fix all test files for the .ts → .js transition

* fix(main): use findPackageRoot for BUILTIN_CLIS path

The previous relative path (../../clis from __dirname) only worked for
dist/src/main.js but broke dev mode (tsx src/main.ts) where __dirname
is <repo>/src — resolving to /clis instead of <repo>/clis.

Use findPackageRoot() which works for both dev and prod paths.
2026-04-10 14:52:18 +08:00

55 lines
3.0 KiB
JavaScript

import { describe, expect, it } from 'vitest';
import { __test__ } from './ask.js';
describe('grok ask helpers', () => {
describe('isOnGrok', () => {
const fakePage = (url) => ({ evaluate: () => url instanceof Error ? Promise.reject(url) : Promise.resolve(url) });
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);
expect(__test__.normalizeBooleanFlag('1')).toBe(true);
expect(__test__.normalizeBooleanFlag('yes')).toBe(true);
expect(__test__.normalizeBooleanFlag('on')).toBe(true);
expect(__test__.normalizeBooleanFlag(false)).toBe(false);
expect(__test__.normalizeBooleanFlag('false')).toBe(false);
expect(__test__.normalizeBooleanFlag(undefined)).toBe(false);
});
it('ignores baseline bubbles and the echoed prompt when choosing the latest assistant candidate', () => {
const candidate = __test__.pickLatestAssistantCandidate(['older assistant answer', 'Prompt text', 'Assistant draft', 'Assistant final'], 1, 'Prompt text');
expect(candidate).toBe('Assistant final');
});
it('returns empty when only the echoed prompt appeared after send', () => {
const candidate = __test__.pickLatestAssistantCandidate(['older assistant answer', 'Prompt text'], 1, 'Prompt text');
expect(candidate).toBe('');
});
it('tracks stabilization by incrementing repeats and resetting on changes', () => {
expect(__test__.updateStableState('', 0, 'First chunk')).toEqual({
previousText: 'First chunk',
stableCount: 0,
});
expect(__test__.updateStableState('First chunk', 0, 'First chunk')).toEqual({
previousText: 'First chunk',
stableCount: 1,
});
expect(__test__.updateStableState('First chunk', 1, 'Second chunk')).toEqual({
previousText: 'Second chunk',
stableCount: 0,
});
});
});