test: Add initial tests for `nrkRadio` #18

Signed-off-by: Tim Hårek Andreassen <tim@harek.no>
This commit is contained in:
Tim Hårek Andreassen 2024-04-04 22:04:17 +02:00
parent e7a0b798cf
commit 6a93c0c084
No known key found for this signature in database
GPG Key ID: E59C7734F0E10EB5
3 changed files with 50 additions and 3 deletions

View File

@ -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 @@
]
}
}
}
}

View File

@ -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<SearchResultList> => {
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;
},
};

41
lib/nrk_test.ts Normal file
View File

@ -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);
});