diff --git a/src/__tests__/app-redirect.test.ts b/src/__tests__/app-redirect.test.ts new file mode 100644 index 0000000..c2094c4 --- /dev/null +++ b/src/__tests__/app-redirect.test.ts @@ -0,0 +1,94 @@ +/** + * /app — Download-Weiterleitung (src/lib/app-redirect.ts, app-redirect-stats.ts). + */ + +import { describe, it, expect, vi, beforeEach } from "vitest"; + +vi.mock("server-only", () => ({})); +const redisStore = new Map>(); +vi.mock("@/lib/redis", () => ({ + redis: { + hincrby: vi.fn(async (key: string, field: string, by: number) => { + const hash = redisStore.get(key) ?? new Map(); + hash.set(field, (hash.get(field) ?? 0) + by); + redisStore.set(key, hash); + return hash.get(field); + }), + expire: vi.fn(async () => 1), + hgetall: vi.fn(async (key: string) => Object.fromEntries(redisStore.get(key) ?? new Map())), + }, +})); + +import { PLAY_STORE_URL } from "@/lib/app-links"; +import { detectPlatform, resolveAppRedirect } from "@/lib/app-redirect"; +import { countAppRedirect, getAppRedirectStats } from "@/lib/app-redirect-stats"; + +const ORIGIN = "https://pawfeed.org"; +const ANDROID = "Mozilla/5.0 (Linux; Android 14; Pixel 8) AppleWebKit/537.36 Chrome/128 Mobile Safari/537.36"; +const IPHONE = "Mozilla/5.0 (iPhone; CPU iPhone OS 18_0 like Mac OS X) AppleWebKit/605.1.15 Version/18.0 Mobile/15E148 Safari/604.1"; +const DESKTOP = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/128 Safari/537.36"; + +const params = (query: string) => new URLSearchParams(query); + +describe("detectPlatform", () => { + it("recognises Android, iPhone and everything else", () => { + expect(detectPlatform(ANDROID)).toBe("android"); + expect(detectPlatform(IPHONE)).toBe("ios"); + expect(detectPlatform(DESKTOP)).toBe("other"); + expect(detectPlatform(null)).toBe("other"); + }); +}); + +describe("resolveAppRedirect", () => { + it("sends Android to Google Play with the UTM values as Play referrer", () => { + const result = resolveAppRedirect(ANDROID, params("utm_source=flyer&utm_campaign=tierheim-xy"), ORIGIN); + expect(result.platform).toBe("android"); + expect(result.source).toBe("flyer"); + expect(result.location.startsWith(PLAY_STORE_URL)).toBe(true); + const referrer = new URL(result.location).searchParams.get("referrer"); + expect(referrer).toBe("utm_source=flyer&utm_campaign=tierheim-xy"); + }); + + it("sends iPhones to the web start page while there is no App Store URL", () => { + const result = resolveAppRedirect(IPHONE, params("utm_source=sticker"), ORIGIN, null); + expect(result.location).toBe("https://pawfeed.org/?utm_source=sticker"); + }); + + it("sends iPhones to the App Store once the URL is configured", () => { + const store = "https://apps.apple.com/app/id123"; + expect(resolveAppRedirect(IPHONE, params(""), ORIGIN, store).location).toBe(store); + }); + + it("turns ?ref= into the invite page on the web", () => { + const result = resolveAppRedirect(DESKTOP, params("ref=bubi-7k3m&utm_source=qr"), ORIGIN, null); + expect(result.location).toBe("https://pawfeed.org/r/BUBI-7K3M?utm_source=qr"); + }); + + it("drops unsafe UTM values and invalid codes instead of passing them on", () => { + const result = resolveAppRedirect(DESKTOP, params("utm_source=