mirror of
https://github.com/jackwener/OpenCLI.git
synced 2026-09-14 18:25:42 +08:00
c0a49e4b44
* feat(weixin): add publish (create draft with cover) and drafts (list drafts) Closes #441 * fix(weixin): rename publish to create-draft to match issue #441 proposal * fix(weixin): fail fast on draft auth and empty states * test(weixin): align adapter imports with repo style --------- Co-authored-by: jackwener <jakevingoo@gmail.com>
66 lines
2.6 KiB
JavaScript
66 lines
2.6 KiB
JavaScript
import { describe, expect, it, vi } from 'vitest';
|
|
import { AuthRequiredError, EmptyResultError } from '@jackwener/opencli/errors';
|
|
import { getRegistry } from '@jackwener/opencli/registry';
|
|
import './create-draft.js';
|
|
import './drafts.js';
|
|
|
|
function createPageMock(overrides = {}) {
|
|
return {
|
|
goto: vi.fn().mockResolvedValue(undefined),
|
|
wait: vi.fn().mockResolvedValue(undefined),
|
|
evaluate: overrides.evaluate ?? vi.fn().mockResolvedValue(undefined),
|
|
setFileInput: vi.fn().mockResolvedValue(undefined),
|
|
};
|
|
}
|
|
|
|
describe('weixin command registration', () => {
|
|
it('registers create-draft and drafts commands', () => {
|
|
const registry = getRegistry();
|
|
const values = [...registry.values()];
|
|
expect(values.find(c => c.site === 'weixin' && c.name === 'create-draft')).toBeDefined();
|
|
expect(values.find(c => c.site === 'weixin' && c.name === 'drafts')).toBeDefined();
|
|
});
|
|
});
|
|
|
|
describe('weixin drafts command', () => {
|
|
it('throws AuthRequiredError when no session token is available', async () => {
|
|
const command = getRegistry().get('weixin/drafts');
|
|
const page = createPageMock({
|
|
evaluate: vi.fn().mockResolvedValueOnce(undefined),
|
|
});
|
|
|
|
await expect(command.func(page, { limit: 10 })).rejects.toBeInstanceOf(AuthRequiredError);
|
|
});
|
|
|
|
it('fails instead of scraping arbitrary body text when structured selectors miss', async () => {
|
|
const command = getRegistry().get('weixin/drafts');
|
|
const evaluate = vi.fn()
|
|
.mockResolvedValueOnce('123456')
|
|
.mockImplementationOnce(async (script) => {
|
|
expect(script).not.toContain('document.body.innerText');
|
|
return [];
|
|
});
|
|
const page = createPageMock({ evaluate });
|
|
|
|
await expect(command.func(page, { limit: 10 })).rejects.toBeInstanceOf(EmptyResultError);
|
|
});
|
|
|
|
it('returns structured drafts and respects the requested limit', async () => {
|
|
const command = getRegistry().get('weixin/drafts');
|
|
const page = createPageMock({
|
|
evaluate: vi.fn()
|
|
.mockResolvedValueOnce('123456')
|
|
.mockResolvedValueOnce([
|
|
{ Index: 1, Title: '第一篇草稿', Time: '2026-04-24 10:00' },
|
|
{ Index: 2, Title: '第二篇草稿', Time: '2026-04-24 11:00' },
|
|
]),
|
|
});
|
|
|
|
const result = await command.func(page, { limit: 1 });
|
|
|
|
expect(result).toEqual([
|
|
{ Index: 1, Title: '第一篇草稿', Time: '2026-04-24 10:00' },
|
|
]);
|
|
});
|
|
});
|