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
70 lines
2.8 KiB
JavaScript
70 lines
2.8 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';
|
|
import './search.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();
|
|
const draftsCommand = values.find(c => c.site === 'weixin' && c.name === 'drafts');
|
|
expect(draftsCommand).toBeDefined();
|
|
expect(draftsCommand.args.find((arg) => arg.name === 'timeout')).toMatchObject({ type: 'int', default: 60 });
|
|
expect(values.find(c => c.site === 'weixin' && c.name === 'search')).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' },
|
|
]);
|
|
});
|
|
});
|