search.spec.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import { randomBytes } from "crypto";
  2. import { XMLParser } from "fast-xml-parser";
  3. import { baseUrl, defaultFetchOpts } from "./constants";
  4. import { checkSongProps } from "./hooks";
  5. describe("Search routes", () => {
  6. //#region /search/top
  7. it("Top search yields expected props", async () => {
  8. const res = await fetch(`${baseUrl}/search/top?q=Lil Nas X - LIGHT AGAIN!`, defaultFetchOpts);
  9. const body = await res.json();
  10. expect(res.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 = await fetch(`${baseUrl}/search/top?format=xml&q=Lil Nas X - LIGHT AGAIN!`, defaultFetchOpts);
  18. const body = await res.text();
  19. expect(res.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 <=10 results", async () => {
  28. const res = await fetch(`${baseUrl}/search?q=Lil Nas X`, defaultFetchOpts);
  29. const body = await res.json();
  30. expect(res.status).toBe(200);
  31. expect(body?.error).toEqual(false);
  32. expect(body?.matches).toBeLessThanOrEqual(10);
  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 error", async () => {
  40. const randText = randomBytes(32).toString("hex");
  41. const res = await fetch(`${baseUrl}/search?q=${randText}`, defaultFetchOpts);
  42. const body = await res.json();
  43. expect(res.status).toBe(400);
  44. expect(body?.error).toEqual(true);
  45. expect(body?.matches).toEqual(0);
  46. expect(body?.message).toBeDefined();
  47. });
  48. });