Files
wehub-resource-sync 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
chore: import upstream snapshot with attribution
2026-07-13 12:39:48 +08:00

67 lines
2.3 KiB
JavaScript

import { afterEach, describe, it, expect, vi } from 'vitest';
const { __test__ } = await import('./suggest.js');
const command = __test__.command;
afterEach(() => {
vi.restoreAllMocks();
});
describe('duckduckgo suggest', () => {
it('should register as a valid command', () => {
expect(command).toBeDefined();
expect(command.site).toBe('duckduckgo');
expect(command.name).toBe('suggest');
expect(command.access).toBe('read');
expect(command.browser).toBe(false);
expect(command.strategy).toBe('public');
});
it('should define keyword positional arg', () => {
const kwArg = command.args.find(a => a.name === 'keyword');
expect(kwArg).toBeDefined();
expect(kwArg.positional).toBe(true);
expect(kwArg.required).toBe(true);
});
it('should define limit arg with default 8', () => {
const limitArg = command.args.find(a => a.name === 'limit');
expect(limitArg).toBeDefined();
expect(limitArg.default).toBe(8);
});
it('should define phrase column', () => {
expect(command.columns).toEqual(['phrase']);
});
it('rejects empty query and invalid limit before fetch', async () => {
const fetchSpy = vi.spyOn(globalThis, 'fetch');
await expect(command.func({ keyword: '', limit: 5 })).rejects.toMatchObject({ code: 'ARGUMENT' });
await expect(command.func({ keyword: 'opencli', limit: 21 })).rejects.toMatchObject({ code: 'ARGUMENT' });
expect(fetchSpy).not.toHaveBeenCalled();
});
it('returns filtered suggestion rows from the public API payload', async () => {
vi.spyOn(globalThis, 'fetch').mockResolvedValue({
ok: true,
json: async () => ['open', ['opencli', '', 'open source']],
});
await expect(command.func({ keyword: 'open', limit: 3 })).resolves.toEqual([
{ phrase: 'opencli' },
{ phrase: 'open source' },
]);
});
it('maps fetch and malformed JSON failures to typed command errors', async () => {
vi.spyOn(globalThis, 'fetch').mockRejectedValueOnce(new Error('offline'));
await expect(command.func({ keyword: 'open', limit: 3 })).rejects.toMatchObject({ code: 'COMMAND_EXEC' });
vi.spyOn(globalThis, 'fetch').mockResolvedValueOnce({
ok: true,
json: async () => { throw new Error('bad json'); },
});
await expect(command.func({ keyword: 'open', limit: 3 })).rejects.toMatchObject({ code: 'COMMAND_EXEC' });
});
});