| 1 | import { describe, expect, it } from "vite-plus/test"; |
| 2 | |
| 3 | import { |
| 4 | getProjectFaviconCacheKey, |
| 5 | isProjectFaviconFallbackUrl, |
| 6 | PROJECT_FAVICON_FALLBACK_MARKER, |
| 7 | } from "./projectFavicon.ts"; |
| 8 | |
| 9 | describe("project favicon", () => { |
| 10 | it("uses the project and versioned filename as the cache identity", () => { |
| 11 | const firstUrl = "https://environment.example/api/assets/first-signed-token/v1-20-favicon.svg"; |
| 12 | const refreshedUrl = |
| 13 | "https://environment.example/api/assets/refreshed-signed-token/v1-20-favicon.svg"; |
| 14 | |
| 15 | expect(getProjectFaviconCacheKey("environment-1", "/workspace", firstUrl)).toBe( |
| 16 | getProjectFaviconCacheKey("environment-1", "/workspace", refreshedUrl), |
| 17 | ); |
| 18 | expect(getProjectFaviconCacheKey("environment-1", "/workspace", firstUrl)).not.toBe( |
| 19 | getProjectFaviconCacheKey( |
| 20 | "environment-1", |
| 21 | "/workspace", |
| 22 | "https://environment.example/api/assets/refreshed-signed-token/v2-20-favicon.svg", |
| 23 | ), |
| 24 | ); |
| 25 | expect(getProjectFaviconCacheKey("environment-1", "/workspace", firstUrl)).not.toBe( |
| 26 | getProjectFaviconCacheKey("environment-2", "/workspace", firstUrl), |
| 27 | ); |
| 28 | }); |
| 29 | |
| 30 | it("identifies fallback asset URLs by their dedicated filename", () => { |
| 31 | expect( |
| 32 | isProjectFaviconFallbackUrl( |
| 33 | `https://environment.example/api/assets/signed-token/${PROJECT_FAVICON_FALLBACK_MARKER}`, |
| 34 | ), |
| 35 | ).toBe(true); |
| 36 | expect( |
| 37 | isProjectFaviconFallbackUrl(`/api/assets/signed-token/${PROJECT_FAVICON_FALLBACK_MARKER}`), |
| 38 | ).toBe(true); |
| 39 | }); |
| 40 | |
| 41 | it("does not mistake real favicons or query parameters for fallbacks", () => { |
| 42 | expect( |
| 43 | isProjectFaviconFallbackUrl("https://environment.example/api/assets/token/favicon.svg"), |
| 44 | ).toBe(false); |
| 45 | expect( |
| 46 | isProjectFaviconFallbackUrl( |
| 47 | `https://environment.example/api/assets/token/favicon.svg?name=${PROJECT_FAVICON_FALLBACK_MARKER}`, |
| 48 | ), |
| 49 | ).toBe(false); |
| 50 | expect(isProjectFaviconFallbackUrl(null)).toBe(false); |
| 51 | }); |
| 52 | }); |