1
0

search.spec.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import { randomBytes } from "crypto";
  2. import { XMLParser } from "fast-xml-parser";
  3. import { checkSongProps, sendReq } from "./hooks";
  4. import { maxResultsAmt } from "./consts";
  5. describe("Search routes", () => {
  6. //#region /search/top
  7. it("Top search yields expected props", async () => {
  8. const { res, status } = await sendReq("/search/top?q=Lil Nas X - LIGHT AGAIN!");
  9. const body = await res.json();
  10. expect(status).toBe(200);
  11. expect(body?.error).toEqual(false);
  12. expect(body?.matches).toEqual(1);
  13. checkSongProps(body);
  14. });
  15. //#region /search/top xml
  16. it("Top search with format=xml yields valid XML", async () => {
  17. const { res, status } = await sendReq("/search/top?format=xml&q=Lil Nas X - LIGHT AGAIN!");
  18. const body = await res.text();
  19. expect(status).toBe(200);
  20. const parsed = new XMLParser().parse(body);
  21. expect(typeof parsed?.data).toBe("object");
  22. expect(parsed?.data?.error).toEqual(false);
  23. expect(parsed?.data?.matches).toEqual(1);
  24. checkSongProps(parsed?.data);
  25. });
  26. //#region /search
  27. it(`Regular search yields <=${maxResultsAmt} results`, async () => {
  28. const { res, status } = await sendReq("/search?q=Lil Nas X");
  29. const body = await res.json();
  30. expect(status).toBe(200);
  31. expect(body?.error).toEqual(false);
  32. expect(body?.matches).toBeLessThanOrEqual(maxResultsAmt);
  33. checkSongProps(body?.top);
  34. expect(Array.isArray(body?.all)).toBe(true);
  35. body?.all?.forEach((hit: unknown) => checkSongProps(hit));
  36. expect(body?.all?.length).toEqual(body?.matches ?? -1);
  37. });
  38. //#region inv /search
  39. it("Invalid search yields no matches", async () => {
  40. const randText = randomBytes(32).toString("hex");
  41. const { res, status } = await sendReq(`/search?q=${randText}`);
  42. const body = await res.json();
  43. expect(status).toBe(200);
  44. expect(body?.error).toEqual(false);
  45. expect(body?.matches).toEqual(0);
  46. expect(body?.message).toBeUndefined();
  47. });
  48. });