9b395f5cc3
E2E Headed Chrome / e2e-headed (macos-15) (push) Has been cancelled
E2E Headed Chrome / e2e-headed (ubuntu-latest) (push) Has been cancelled
E2E Headed Chrome / e2e-headed (windows-latest) (push) Has been cancelled
CI / build (macos-latest) (push) Has been cancelled
CI / build (ubuntu-latest) (push) Has been cancelled
CI / build (windows-latest) (push) Has been cancelled
CI / unit-test (push) Has been cancelled
CI / bun-test (push) Has been cancelled
CI / adapter-test (push) Has been cancelled
CI / smoke-test (macos-latest) (push) Has been cancelled
CI / smoke-test (ubuntu-latest) (push) Has been cancelled
Security Audit / audit (push) Has been cancelled
Build Chrome Extension / build (push) Has been cancelled
Trigger Website Rebuild (Docs Updated) / dispatch (push) Has been cancelled
47 lines
2.1 KiB
JavaScript
47 lines
2.1 KiB
JavaScript
import { describe, expect, it, vi } from 'vitest';
|
|
import { getRegistry } from '@jackwener/opencli/registry';
|
|
import { CliError } from '@jackwener/opencli/errors';
|
|
import './article.js';
|
|
|
|
function makePage(evaluateResult) {
|
|
return {
|
|
installInterceptor: vi.fn().mockResolvedValue(undefined),
|
|
goto: vi.fn().mockResolvedValue(undefined),
|
|
wait: vi.fn().mockResolvedValue(undefined),
|
|
evaluate: vi.fn().mockResolvedValue(evaluateResult),
|
|
};
|
|
}
|
|
|
|
describe('36kr article', () => {
|
|
it('emits empty-string for missing optional author / date instead of a sentinel', async () => {
|
|
const command = getRegistry().get('36kr/article');
|
|
expect(command?.func).toBeDefined();
|
|
const page = makePage({ title: 'Real Title', author: '', date: '', body: 'Real article body' });
|
|
const rows = await command.func(page, { id: '1234567' });
|
|
const byField = Object.fromEntries(rows.map((r) => [r.field, r.value]));
|
|
expect(byField.title).toBe('Real Title');
|
|
expect(byField.author).toBe('');
|
|
expect(byField.date).toBe('');
|
|
expect(byField.body).toBe('Real article body');
|
|
expect(byField.url).toBe('https://36kr.com/p/1234567');
|
|
});
|
|
|
|
it('throws CliError NOT_FOUND when the page exposes no title', async () => {
|
|
const command = getRegistry().get('36kr/article');
|
|
const page = makePage({ title: '', author: 'x', date: 'y', body: 'z' });
|
|
await expect(command.func(page, { id: '1234567' })).rejects.toBeInstanceOf(CliError);
|
|
});
|
|
|
|
it('throws CliError PARSE_ERROR when the page exposes title but no body', async () => {
|
|
const command = getRegistry().get('36kr/article');
|
|
const page = makePage({ title: 'Real Title', author: 'x', date: 'y', body: '' });
|
|
await expect(command.func(page, { id: '1234567' })).rejects.toMatchObject({ code: 'PARSE_ERROR' });
|
|
});
|
|
|
|
it('throws CliError INVALID_ARGUMENT when no numeric id can be parsed', async () => {
|
|
const command = getRegistry().get('36kr/article');
|
|
const page = makePage({});
|
|
await expect(command.func(page, { id: 'not-a-url' })).rejects.toBeInstanceOf(CliError);
|
|
});
|
|
});
|