|
| 1 | +// @vitest-environment node |
| 2 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 3 | + |
| 4 | +// The parts of a real workshop page the lookup reads. |
| 5 | +const workshopPage = (id: string, title: string, app = "730") => ` |
| 6 | + <meta property="og:image" content="https://images.steamusercontent.com/ugc/1/${id}/?imw=512&&ima=fit"> |
| 7 | + <div class="workshopItemTitle">${title}</div> |
| 8 | + <span onclick="ShowAddToCollection( '${id}', '${app}' );" id="AddToCollectionBtn"></span>`; |
| 9 | + |
| 10 | +let details: Record<string, unknown>; |
| 11 | +let page: string; |
| 12 | +let pageFetches: Array<string>; |
| 13 | + |
| 14 | +beforeEach(() => { |
| 15 | + vi.resetModules(); |
| 16 | + pageFetches = []; |
| 17 | + vi.stubGlobal("defineEventHandler", (handler: unknown) => handler); |
| 18 | + vi.stubGlobal("readBody", async (event: any) => event.body); |
| 19 | + vi.stubGlobal("fetch", async (url: string) => { |
| 20 | + if (url.startsWith("https://steamcommunity.com/")) { |
| 21 | + pageFetches.push(url); |
| 22 | + return new Response(page); |
| 23 | + } |
| 24 | + return new Response( |
| 25 | + JSON.stringify({ response: { publishedfiledetails: [details] } }), |
| 26 | + ); |
| 27 | + }); |
| 28 | +}); |
| 29 | + |
| 30 | +afterEach(() => { |
| 31 | + vi.unstubAllGlobals(); |
| 32 | +}); |
| 33 | + |
| 34 | +async function lookup(workshopMapId: string) { |
| 35 | + const handler = (await import("~/server/api/get-workshop-map.post")) |
| 36 | + .default as any; |
| 37 | + return handler({ body: { workshop_map_id: workshopMapId } }); |
| 38 | +} |
| 39 | + |
| 40 | +describe("the workshop map lookup", () => { |
| 41 | + it("returns a public item from Steam's API", async () => { |
| 42 | + details = { |
| 43 | + publishedfileid: "3070290240", |
| 44 | + result: 1, |
| 45 | + title: "Brewery", |
| 46 | + preview_url: "https://images.steamusercontent.com/ugc/2/brewery/", |
| 47 | + }; |
| 48 | + |
| 49 | + expect(await lookup("3070290240")).toEqual(details); |
| 50 | + expect(pageFetches).toEqual([]); |
| 51 | + }); |
| 52 | + |
| 53 | + // Steam's API answers an unlisted item with result 9, as it does a missing one. |
| 54 | + it("reads an unlisted item from its workshop page", async () => { |
| 55 | + details = { publishedfileid: "3820018883", result: 9 }; |
| 56 | + page = workshopPage("3820018883", "de_lbt_mill & co"); |
| 57 | + |
| 58 | + expect(await lookup(" 3820018883 ")).toEqual({ |
| 59 | + publishedfileid: "3820018883", |
| 60 | + title: "de_lbt_mill & co", |
| 61 | + preview_url: "https://images.steamusercontent.com/ugc/1/3820018883/", |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + it("finds nothing when the workshop page is Steam's error page", async () => { |
| 66 | + details = { publishedfileid: "1", result: 9 }; |
| 67 | + page = "<title>Steam Community :: Error</title>"; |
| 68 | + |
| 69 | + expect(await lookup("1")).toBeNull(); |
| 70 | + }); |
| 71 | + |
| 72 | + it("never puts anything but a numeric id into the page link", async () => { |
| 73 | + details = { result: 9 }; |
| 74 | + page = workshopPage("1", "de_dust2"); |
| 75 | + |
| 76 | + expect(await lookup("1&id=1")).toBeNull(); |
| 77 | + expect(pageFetches).toEqual([]); |
| 78 | + }); |
| 79 | + |
| 80 | + it("finds nothing when the page is another game's item", async () => { |
| 81 | + details = { publishedfileid: "42", result: 9 }; |
| 82 | + page = workshopPage("42", "Some Mod", "4000"); |
| 83 | + |
| 84 | + expect(await lookup("42")).toBeNull(); |
| 85 | + }); |
| 86 | +}); |
0 commit comments