Cloudflare serves static assets before the worker runs, so the icon redirect logic was never reached. Renamed the default icon files to default-* prefix. The worker now handles all requests for the original icon paths: redirects to org-specific icons when cookie is set, or to the renamed defaults otherwise. Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
65 lines
2.7 KiB
TypeScript
65 lines
2.7 KiB
TypeScript
import { describe, it, expect } from "vitest";
|
|
import { buildManifest, parseOrgIdFromCookie } from "./index";
|
|
import worker from "./index";
|
|
|
|
describe("parseOrgIdFromCookie", () => {
|
|
it("returns null when no cookie header", () => {
|
|
expect(parseOrgIdFromCookie(null)).toBeNull();
|
|
});
|
|
|
|
it("returns null when x-org-id cookie is missing", () => {
|
|
expect(parseOrgIdFromCookie("other=value; foo=bar")).toBeNull();
|
|
});
|
|
|
|
it("parses x-org-id from cookie string", () => {
|
|
expect(parseOrgIdFromCookie("x-org-id=42; other=value")).toBe(42);
|
|
});
|
|
|
|
it("returns null for non-numeric x-org-id", () => {
|
|
expect(parseOrgIdFromCookie("x-org-id=abc")).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe("buildManifest", () => {
|
|
it("returns default icons when orgId is null", () => {
|
|
const manifest = buildManifest(null);
|
|
expect(manifest.name).toBe("XTablo");
|
|
expect(manifest.icons[0].src).toBe("/pwa-icons/pwa-192x192.png");
|
|
});
|
|
|
|
it("returns org-specific icon URLs from assets.xtablo.com when orgId is provided", () => {
|
|
const manifest = buildManifest(42);
|
|
expect(manifest.name).toBe("XTablo");
|
|
expect(manifest.icons[0].src).toBe("https://assets.xtablo.com/org-icons/42/icon-192.png");
|
|
expect(manifest.icons[1].src).toBe("https://assets.xtablo.com/org-icons/42/icon-512.png");
|
|
expect(manifest.icons[2].src).toBe("https://assets.xtablo.com/org-icons/42/icon-512-maskable.png");
|
|
expect(manifest.icons[2].purpose).toBe("maskable");
|
|
});
|
|
});
|
|
|
|
describe("worker icon redirects", () => {
|
|
it("redirects apple-touch-icon to org-specific icon when cookie is set", async () => {
|
|
const request = new Request("https://app.xtablo.com/pwa-icons/apple-touch-icon-180x180.png", {
|
|
headers: { cookie: "x-org-id=42" },
|
|
});
|
|
const response = await worker.fetch(request);
|
|
expect(response.status).toBe(302);
|
|
expect(response.headers.get("location")).toBe("https://assets.xtablo.com/org-icons/42/icon-180.png");
|
|
});
|
|
|
|
it("redirects apple-touch-icon to default when no cookie", async () => {
|
|
const request = new Request("https://app.xtablo.com/pwa-icons/apple-touch-icon-180x180.png");
|
|
const response = await worker.fetch(request);
|
|
expect(response.status).toBe(302);
|
|
expect(response.headers.get("location")).toBe("https://app.xtablo.com/pwa-icons/default-apple-touch-icon-180x180.png");
|
|
});
|
|
|
|
it("redirects favicon to org-specific icon when cookie is set", async () => {
|
|
const request = new Request("https://app.xtablo.com/pwa-icons/favicon-32x32.png", {
|
|
headers: { cookie: "x-org-id=7" },
|
|
});
|
|
const response = await worker.fetch(request);
|
|
expect(response.status).toBe(302);
|
|
expect(response.headers.get("location")).toBe("https://assets.xtablo.com/org-icons/7/icon-32.png");
|
|
});
|
|
});
|