diff --git a/deno.json b/deno.json index e8e12b5..e2e26bd 100644 --- a/deno.json +++ b/deno.json @@ -16,7 +16,8 @@ "@preact/signals": "https://esm.sh/*@preact/signals@1.2.2", "@preact/signals-core": "https://esm.sh/*@preact/signals-core@1.5.1", "$gfm": "https://deno.land/x/gfm@0.6.0/mod.ts", - "openapi-typescript": "npm:openapi-typescript@6.7.5" + "openapi-typescript": "npm:openapi-typescript@6.7.5", + "$std/": "https://deno.land/std@0.221.0/" }, "compilerOptions": { "jsx": "react-jsx", @@ -39,4 +40,4 @@ ] } } -} +} \ No newline at end of file diff --git a/lib/nrk.ts b/lib/nrk.ts index 75328b7..552f916 100644 --- a/lib/nrk.ts +++ b/lib/nrk.ts @@ -42,8 +42,12 @@ async function withDownloadLink( } } +// TODO: Replace throws with `console.error` and return `null` if bad response. export const nrkRadio = { search: async (query: string): Promise => { + if (query === "") { + throw "Empty search query."; + } const [status, response] = await get< searchComponents["schemas"]["searchresult"] >(`${nrkAPI}/radio/search/search?q=${query}`); @@ -99,6 +103,7 @@ export const nrkRadio = { if (status === OK && episode) { return episode; } - throw `Error getting episode ${episodeId}. Status: ${status}. Series: ${seriesId}`; + console.error(`Error getting episode ${episodeId}. Status: ${status}. Series: ${seriesId}`); + return null; }, }; diff --git a/lib/nrk_test.ts b/lib/nrk_test.ts new file mode 100644 index 0000000..0d67a9f --- /dev/null +++ b/lib/nrk_test.ts @@ -0,0 +1,41 @@ +import { assertEquals } from "$std/assert/assert_equals.ts"; +import { assertRejects } from "$std/assert/assert_rejects.ts"; +import { nrkRadio } from "./nrk.ts"; +import { assertGreaterOrEqual } from "$std/assert/assert_greater_or_equal.ts"; +import { assertExists } from "https://deno.land/std@0.216.0/assert/assert_exists.ts"; + +Deno.test("Verify search query `trygd` returns one result: 'Trygdekontoret'", async () => { + const result = await nrkRadio.search("trygd"); + + assertExists(result); + assertEquals(result.length, 1); + assertEquals(result[0].seriesId, "trygdekontoret"); +}); + +Deno.test("Verify empty search query yields error", async () => { + await assertRejects(async () => await nrkRadio.search("")); +}); + +Deno.test("Verify getting series data for 'trygdekontoret' works", async () => { + const result = await nrkRadio.getSerieData("trygdekontoret"); + + assertGreaterOrEqual(result.episodes.length, 1); +}); + +// TODO: This doesn't work because there is no try-catch system, it seems. +// Deno.test("Verify getting series data for 'trygd' does not works", async () => { +// await assertThrows(async () => await nrkRadio.getSerieData("trygd")); +// }); + +Deno.test("Verify getting episodeId 'l_0bc5e55a-46b5-48a5-85e5-5a46b5d8a562' for 'trygdekontoret' works", async () => { + const result = await nrkRadio.getEpisode("trygdekontoret", "l_0bc5e55a-46b5-48a5-85e5-5a46b5d8a562"); + + assertExists(result); + assertEquals(result.duration.seconds, 4152); +}); + +Deno.test("Verify getting episodeId 'null' for 'trygdekontoret' fails", async () => { + const result = await nrkRadio.getEpisode("trygdekontoret", "null"); + + assertEquals(result, null); +});