Get DuckDuckGo results in Node. https://npmjs.com/package/node-ddg
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
2.0 KiB

  1. import test, {ExecutionContext} from 'ava';
  2. import {Result, SafeSearchEnum, search} from '../source';
  3. test('Searching for "duckduckgo" should return result', async (t: ExecutionContext) => {
  4. t.plan(4);
  5. const results: Result[] = await search({maxResults: 1, query: 'duckduckgo'});
  6. t.true(results.length === 1);
  7. t.true(results[0].body.length > 0);
  8. t.true(results[0].title.length > 0);
  9. t.true(results[0].url.length > 0);
  10. });
  11. test('Specifying "secure: false" should return result', async (t: ExecutionContext) => {
  12. t.plan(4);
  13. const secure = false;
  14. const results: Result[] = await search({
  15. maxResults: 1,
  16. query: 'duckduckgo',
  17. secure
  18. });
  19. t.true(results.length === 1);
  20. t.true(results[0].body.length > 0);
  21. t.true(results[0].title.length > 0);
  22. t.true(results[0].url.length > 0);
  23. });
  24. test('Specifying X results should return X results', async (t: ExecutionContext) => {
  25. t.plan(1);
  26. const maxResults = 3;
  27. const results: Result[] = await search({maxResults, query: 'duckduckgo'});
  28. t.is(results.length, maxResults);
  29. });
  30. test('Specifying "SafeSearch = ON" should not return results on query "porn"', async (t: ExecutionContext) => {
  31. const query = 'porn';
  32. const safeSearch: SafeSearchEnum = SafeSearchEnum.ON;
  33. const expectedError: Error = new Error(
  34. 'No results were found because SafeSearch is enabled.'
  35. );
  36. const error: Error = await t.throwsAsync(search({query, safeSearch}));
  37. t.deepEqual(error, expectedError);
  38. });
  39. test('Having an empty query should return an Error', async (t: ExecutionContext) => {
  40. const expectedError: Error = new Error('The search query cannot be empty.');
  41. const error: Error = await t.throwsAsync(search({query: ''}));
  42. t.deepEqual(error, expectedError);
  43. });
  44. test('Finding no results should return an Error', async (t: ExecutionContext) => {
  45. const query = '"sdf€4jk487ldddHKLGD"';
  46. const expectedError: Error = new Error(
  47. `No results were found with query: ${query}`
  48. );
  49. const error: Error = await t.throwsAsync(search({query}));
  50. t.deepEqual(error, expectedError);
  51. });