mirror of
https://github.com/jackwener/OpenCLI.git
synced 2026-09-14 18:25:42 +08:00
34a9bff2b3
- NEW: src/interceptor.ts — unified XHR/Fetch interceptor (was duplicated 3x) - NEW: src/version.ts — centralized PKG_VERSION (was duplicated 2x) - NEW: src/constants.ts — shared VOLATILE_PARAMS, FIELD_ROLES etc. - NEW: src/engine.test.ts, src/registry.test.ts — 14 new unit tests - browser.ts: use shared normalizeEval, interceptor, withTimeoutMs, PKG_VERSION - intercept.ts, tap.ts: use shared interceptor generators - cascade.ts: extract shared buildFetchProbeJs (90% dedup) - engine.ts: use InternalCliCommand (no more 'as any' casts) - executor.ts: remove all 15 'as StepHandler' type casts - runtime.ts: add withTimeoutMs, IBrowserFactory interface - registry.ts: add InternalCliCommand type for internal fields - validate.ts: add pipeline step name validation - output.ts: remove 'null as any' and '.filter(() => true)' hacks - explore.ts, synthesize.ts: use shared constants - docs: fix V2EX commands (3→6), SKILL.md version, verify example Tests: 88 passed (was 74), tsc --noEmit: 0 errors
78 lines
2.2 KiB
TypeScript
78 lines
2.2 KiB
TypeScript
/**
|
|
* Tests for engine.ts: CLI discovery and command execution.
|
|
*/
|
|
|
|
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
import { discoverClis, executeCommand } from './engine.js';
|
|
import { getRegistry, cli, Strategy } from './registry.js';
|
|
|
|
describe('discoverClis', () => {
|
|
it('handles non-existent directories gracefully', async () => {
|
|
// Should not throw for missing directories
|
|
await expect(discoverClis('/tmp/nonexistent-opencli-test-dir')).resolves.not.toThrow();
|
|
});
|
|
});
|
|
|
|
describe('executeCommand', () => {
|
|
it('executes a command with func', async () => {
|
|
const cmd = cli({
|
|
site: 'test-engine',
|
|
name: 'func-test',
|
|
description: 'test command with func',
|
|
browser: false,
|
|
strategy: Strategy.PUBLIC,
|
|
func: async (_page, kwargs) => {
|
|
return [{ title: kwargs.query ?? 'default' }];
|
|
},
|
|
});
|
|
|
|
const result = await executeCommand(cmd, null, { query: 'hello' });
|
|
expect(result).toEqual([{ title: 'hello' }]);
|
|
});
|
|
|
|
it('executes a command with pipeline', async () => {
|
|
const cmd = cli({
|
|
site: 'test-engine',
|
|
name: 'pipe-test',
|
|
description: 'test command with pipeline',
|
|
browser: false,
|
|
strategy: Strategy.PUBLIC,
|
|
pipeline: [
|
|
{ evaluate: '() => [{ n: 1 }, { n: 2 }, { n: 3 }]' },
|
|
{ limit: '2' },
|
|
],
|
|
});
|
|
|
|
// Pipeline commands require page for evaluate step, so we'll test the error path
|
|
await expect(executeCommand(cmd, null, {})).rejects.toThrow();
|
|
});
|
|
|
|
it('throws for command with no func or pipeline', async () => {
|
|
const cmd = cli({
|
|
site: 'test-engine',
|
|
name: 'empty-test',
|
|
description: 'empty command',
|
|
browser: false,
|
|
});
|
|
|
|
await expect(executeCommand(cmd, null, {})).rejects.toThrow('has no func or pipeline');
|
|
});
|
|
|
|
it('passes debug flag to func', async () => {
|
|
let receivedDebug = false;
|
|
const cmd = cli({
|
|
site: 'test-engine',
|
|
name: 'debug-test',
|
|
description: 'debug test',
|
|
browser: false,
|
|
func: async (_page, _kwargs, debug) => {
|
|
receivedDebug = debug ?? false;
|
|
return [];
|
|
},
|
|
});
|
|
|
|
await executeCommand(cmd, null, {}, true);
|
|
expect(receivedDebug).toBe(true);
|
|
});
|
|
});
|