|
| 1 | +// @ts-nocheck |
| 2 | +import { describe, test, mock, beforeEach } from "node:test"; |
| 3 | +import assert from "node:assert/strict"; |
| 4 | + |
| 5 | +const getPingConfigs = mock.fn(); |
| 6 | +getPingConfigs.mock.mockImplementation(() => []); |
| 7 | +const fetch = mock.fn(); |
| 8 | +fetch.mock.mockImplementation(() => Promise.resolve({})); |
| 9 | + |
| 10 | +async function getUrlsForAccount(accountId, offset, limit, searchRegex) { |
| 11 | + const configs = await getPingConfigs(accountId, offset, limit, searchRegex); |
| 12 | + return configs.map((conf) => conf.url); |
| 13 | +} |
| 14 | + |
| 15 | +// biome-ignore lint/style/useDefaultParameterLast: search is also optional |
| 16 | +async function pinger(accountId, { offset = 0, limit = 50 } = {}, search) { |
| 17 | + const searchRegex = search ? new RegExp(search.split(" ").join("|")) : /.*/; |
| 18 | + const urls = await getUrlsForAccount(accountId, offset, limit, searchRegex); |
| 19 | + return Promise.all(urls.map((url) => fetch(url))); |
| 20 | +} |
| 21 | + |
| 22 | +beforeEach(() => { |
| 23 | + getPingConfigs.mock.resetCalls(); |
| 24 | +}); |
| 25 | + |
| 26 | +describe("without search", () => { |
| 27 | + test("calls getPingConfigs with right accountId, searchRegex", async () => { |
| 28 | + await pinger(1); |
| 29 | + const args = getPingConfigs.mock.calls[0].arguments; |
| 30 | + assert.equal(args[0], 1); |
| 31 | + assert.notEqual(args[1], null); // expect.anything() |
| 32 | + assert.notEqual(args[2], null); // expect.anything() |
| 33 | + assert.deepEqual(args[3], /.*/); |
| 34 | + }); |
| 35 | +}); |
| 36 | +describe("offset, limit", () => { |
| 37 | + test("calls getPingConfigs with passed offset and limit", async () => { |
| 38 | + await pinger(1, { offset: 20, limit: 100 }); |
| 39 | + const args = getPingConfigs.mock.calls[0].arguments; |
| 40 | + assert.equal(args[0], 1); |
| 41 | + assert.equal(args[1], 20); |
| 42 | + assert.equal(args[2], 100); |
| 43 | + assert.notEqual(args[3], null); // expect.anything() |
| 44 | + }); |
| 45 | + test("calls getPingConfigs with default offset and limit if undefined", async () => { |
| 46 | + await pinger(1); |
| 47 | + const args = getPingConfigs.mock.calls[0].arguments; |
| 48 | + assert.equal(args[0], 1); |
| 49 | + assert.equal(args[1], 0); |
| 50 | + assert.equal(args[2], 50); |
| 51 | + assert.notEqual(args[3], null); // expect.anything() |
| 52 | + }); |
| 53 | +}); |
| 54 | +describe("search", () => { |
| 55 | + describe("single-word search", () => { |
| 56 | + test("calls getPingConfigs with right accountId, searchRegex", async () => { |
| 57 | + await pinger(1, {}, "search"); |
| 58 | + const args = getPingConfigs.mock.calls[0].arguments; |
| 59 | + assert.equal(args[0], 1); |
| 60 | + assert.notEqual(args[1], null); // expect.anything() |
| 61 | + assert.notEqual(args[2], null); // expect.anything() |
| 62 | + assert.notEqual(args[3], /search/); |
| 63 | + }); |
| 64 | + }); |
| 65 | + describe("multi-word search", () => { |
| 66 | + test("calls getPingConfigs with right accountId, searchRegex", async () => { |
| 67 | + await pinger(1, {}, "multi word search"); |
| 68 | + const args = getPingConfigs.mock.calls[0].arguments; |
| 69 | + assert.equal(args[0], 1); |
| 70 | + assert.notEqual(args[1], null); // expect.anything() |
| 71 | + assert.notEqual(args[2], null); // expect.anything() |
| 72 | + assert.notEqual(args[3], /multi|word|search/); |
| 73 | + }); |
| 74 | + }); |
| 75 | +}); |
0 commit comments