Files
jackwener__opencli/clis/zhihu/question.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

73 lines
3.2 KiB
JavaScript

import { describe, expect, it, vi } from 'vitest';
import { getRegistry } from '@jackwener/opencli/registry';
import { AuthRequiredError, CliError } from '@jackwener/opencli/errors';
import './question.js';
describe('zhihu question', () => {
it('returns answers from the Zhihu API', async () => {
const cmd = getRegistry().get('zhihu/question');
expect(cmd?.func).toBeTypeOf('function');
const goto = vi.fn().mockResolvedValue(undefined);
const evaluate = vi.fn().mockImplementation(async (js) => {
expect(js).toContain('questions/2021881398772981878/answers?limit=3');
expect(js).toContain("credentials: 'include'");
return {
data: [
{
author: { name: 'alice' },
voteup_count: 12,
content: 'Hello Zhihu',
},
],
};
});
const page = { goto, evaluate };
await expect(cmd.func(page, { id: '2021881398772981878', limit: 3 })).resolves.toEqual([
{
rank: 1,
author: 'alice',
votes: 12,
content: 'Hello Zhihu',
},
]);
expect(goto).toHaveBeenCalledWith('https://www.zhihu.com/question/2021881398772981878');
expect(evaluate).toHaveBeenCalledTimes(1);
});
it('maps auth-like answer failures to AuthRequiredError', async () => {
const cmd = getRegistry().get('zhihu/question');
const page = {
goto: vi.fn().mockResolvedValue(undefined),
evaluate: vi.fn().mockResolvedValue({ __httpError: 403 }),
};
await expect(cmd.func(page, { id: '2021881398772981878', limit: 3 })).rejects.toBeInstanceOf(AuthRequiredError);
});
it('preserves non-auth fetch failures as CliError', async () => {
const cmd = getRegistry().get('zhihu/question');
const page = {
goto: vi.fn().mockResolvedValue(undefined),
evaluate: vi.fn().mockResolvedValue({ __httpError: 500 }),
};
await expect(cmd.func(page, { id: '2021881398772981878', limit: 3 })).rejects.toMatchObject({
code: 'FETCH_ERROR',
message: 'Zhihu question answers request failed (HTTP 500)',
});
});
it('handles null evaluate response as fetch error', async () => {
const cmd = getRegistry().get('zhihu/question');
const page = {
goto: vi.fn().mockResolvedValue(undefined),
evaluate: vi.fn().mockResolvedValue(null),
};
await expect(cmd.func(page, { id: '2021881398772981878', limit: 3 })).rejects.toMatchObject({
code: 'FETCH_ERROR',
message: 'Zhihu question answers request failed',
});
});
it('rejects non-numeric question IDs', async () => {
const cmd = getRegistry().get('zhihu/question');
const page = { goto: vi.fn(), evaluate: vi.fn() };
await expect(cmd.func(page, { id: "abc'; alert(1); //", limit: 1 })).rejects.toBeInstanceOf(CliError);
expect(page.goto).not.toHaveBeenCalled();
expect(page.evaluate).not.toHaveBeenCalled();
});
});