search.spec.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import { randomBytes } from "crypto";
  2. import { baseUrl, defaultFetchOpts } from "./constants";
  3. import { checkSongProps } from "./hooks";
  4. describe("Search routes", () => {
  5. //#region /search/top
  6. it("Top search yields expected props", async () => {
  7. const res = await fetch(`${baseUrl}/search/top?q=Lil Nas X - LIGHT AGAIN!`, defaultFetchOpts);
  8. const body = await res.json();
  9. expect(res.status).toBe(200);
  10. expect(body?.error).toEqual(false);
  11. expect(body?.matches).toEqual(1);
  12. checkSongProps(body);
  13. });
  14. //#region /search
  15. it("Regular search yields <=10 results", async () => {
  16. const res = await fetch(`${baseUrl}/search?q=Lil Nas X`, defaultFetchOpts);
  17. const body = await res.json();
  18. expect(res.status).toBe(200);
  19. expect(body?.error).toEqual(false);
  20. expect(body?.matches).toBeLessThanOrEqual(10);
  21. checkSongProps(body?.top);
  22. expect(Array.isArray(body?.all)).toBe(true);
  23. body?.all?.forEach((hit: unknown) => checkSongProps(hit));
  24. expect(body?.all?.length).toEqual(body?.matches ?? -1);
  25. });
  26. //#region inv /search
  27. it("Invalid search yields error", async () => {
  28. const randText = randomBytes(32).toString("hex");
  29. const res = await fetch(`${baseUrl}/search?q=${randText}`, defaultFetchOpts);
  30. const body = await res.json();
  31. expect(res.status).toBe(400);
  32. expect(body?.error).toEqual(true);
  33. expect(body?.matches).toEqual(0);
  34. expect(body?.message).toBeDefined();
  35. });
  36. });