Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

test(web): replace vacuous consent and homepage guards (#1175)

Gener32f43729cf90
diff --git a/src/web/src/app/api/privacy/analytics-consent/route.test.ts b/src/web/src/app/api/privacy/analytics-consent/route.test.ts
index 45401dd30..6ea7a3b40 100644
--- a/src/web/src/app/api/privacy/analytics-consent/route.test.ts
+++ b/src/web/src/app/api/privacy/analytics-consent/route.test.ts
@@ -28,6 +28,18 @@ function request(
})
}
+function readConsentCookies(response: Response) {
+ const cookies = response.headers.getSetCookie()
+ expect(cookies.map(cookie => cookie.split("=", 1)[0]).sort()).toEqual([
+ "alook_analytics_consent",
+ "alook_analytics_consent_proof",
+ ])
+ return {
+ readable: cookies.find(cookie => cookie.startsWith("alook_analytics_consent="))!,
+ proof: cookies.find(cookie => cookie.startsWith("alook_analytics_consent_proof="))!,
+ }
+}
+
beforeEach(() => {
vi.resetAllMocks()
mocks.env.BETTER_AUTH_SECRET = "route-consent-signing-secret"
@@ -38,23 +50,21 @@ beforeEach(() => {
describe("POST /api/privacy/analytics-consent", () => {
it.each(["granted", "denied"] as const)("sets the readable and signed %s cookies", async (decision) => {
const response = await POST(request({ decision }))
- const cookies = response.headers.getSetCookie()
+ const { readable, proof: signed } = readConsentCookies(response)
expect(response.status).toBe(200)
expect(response.headers.get("Cache-Control")).toBe("no-store, max-age=0")
expect(await response.json()).toEqual({ decision })
- expect(cookies).toHaveLength(2)
- expect(cookies[0]).toContain(`alook_analytics_consent=v1.${decision}`)
- expect(cookies[0]).toContain("Path=/")
- expect(cookies[0]).toContain("Max-Age=15552000")
- expect(cookies[0]).toContain("SameSite=lax")
- expect(cookies[0]).toContain("Secure")
- expect(cookies[0]).not.toContain("HttpOnly")
- expect(cookies[1]).toContain("alook_analytics_consent_proof=")
- expect(cookies[1]).toContain("HttpOnly")
- expect(cookies[1]).toContain("Secure")
-
- const proof = cookies[1].match(/^[^=]+=([^;]+)/u)?.[1]
+ expect(readable).toContain(`alook_analytics_consent=v1.${decision}`)
+ expect(readable).toContain("Path=/")
+ expect(readable).toContain("Max-Age=15552000")
+ expect(readable).toContain("SameSite=lax")
+ expect(readable).toContain("Secure")
+ expect(readable).not.toContain("HttpOnly")
+ expect(signed).toContain("HttpOnly")
+ expect(signed).toContain("Secure")
+
+ const proof = signed.match(/^[^=]+=([^;]+)/u)?.[1]
await expect(verifyAnalyticsConsentProof(
proof,
mocks.env.BETTER_AUTH_SECRET,
@@ -68,14 +78,16 @@ describe("POST /api/privacy/analytics-consent", () => {
"http://localhost:3000/api/privacy/analytics-consent",
))
expect(response.status).toBe(200)
- expect(response.headers.getSetCookie().every((cookie) => !cookie.includes("Secure"))).toBe(true)
+ const { readable, proof } = readConsentCookies(response)
+ expect(readable).not.toContain("Secure")
+ expect(proof).not.toContain("Secure")
})
it("persists an authenticated decision before issuing its proof", async () => {
mocks.userId = "owner"
const response = await POST(request({ decision: "denied" }))
expect(response.status).toBe(200)
- const proof = response.headers.getSetCookie()[1]?.match(/^[^=]+=([^;]+)/u)?.[1]
+ const proof = readConsentCookies(response).proof.match(/^[^=]+=([^;]+)/u)?.[1]
const verified = await verifyAnalyticsConsentProof(proof, mocks.env.BETTER_AUTH_SECRET)
expect(mocks.recordAnalyticsConsent).toHaveBeenCalledWith(
mocks.env.DB,
@@ -117,7 +129,9 @@ describe("POST /api/privacy/analytics-consent", () => {
))
expect(response.status).toBe(200)
- expect(response.headers.getSetCookie().every((cookie) => !cookie.includes("Secure"))).toBe(true)
+ const { readable, proof } = readConsentCookies(response)
+ expect(readable).not.toContain("Secure")
+ expect(proof).not.toContain("Secure")
})
it.each([
diff --git a/src/web/src/components/analytics-consent.dom.test.tsx b/src/web/src/components/analytics-consent.dom.test.tsx
index 0198b4340..2a01a3e5f 100644
--- a/src/web/src/components/analytics-consent.dom.test.tsx
+++ b/src/web/src/components/analytics-consent.dom.test.tsx
@@ -114,14 +114,13 @@ describe("AnalyticsConsent", () => {
expect(consentCommands().at(-1)?.[2]).toEqual(expect.objectContaining({ analytics_storage: "denied" }))
})
- it("keeps public tracking and page layout when grant persistence fails", async () => {
- render(<><main style={{ height: "100dvh" }} data-testid="consent-page" /><AnalyticsConsent /></>)
+ it("keeps public tracking without an error banner when grant persistence fails", async () => {
+ render(<AnalyticsConsent />)
await waitFor(() => expect(fetch).toHaveBeenCalledOnce())
expect(screen.queryByTestId(tid.analyticsConsentBanner)).not.toBeInTheDocument()
expect(screen.queryByRole("alert")).not.toBeInTheDocument()
expect(await screen.findByTestId("google-tag-manager")).toBeInTheDocument()
expect(screen.getByTestId(tid.ahrefsAnalyticsFrame)).toBeInTheDocument()
- expect(getComputedStyle(screen.getByTestId("consent-page")).height).toBe("100dvh")
expect(document.cookie).not.toContain("alook_analytics_consent=")
expect(document.cookie).not.toContain("_ga=")
})
@@ -162,17 +161,20 @@ describe("AnalyticsConsent", () => {
it.each(["focus", "pageshow", "visibilitychange"])("rereads an explicit denial on %s and disables optional public tracking", async type => {
document.cookie = "alook_analytics_consent=v1.granted; Path=/"
const view = render(<AnalyticsConsent />)
- await screen.findByTestId(tid.ahrefsAnalyticsFrame)
- document.cookie = "alook_analytics_consent=v1.denied; Path=/"
- if (type === "visibilitychange") {
- vi.spyOn(document, "visibilityState", "get").mockReturnValue("visible")
- fireEvent(document, new Event(type))
- } else fireEvent(window, new Event(type))
- expect(screen.queryByTestId(tid.ahrefsAnalyticsFrame)).not.toBeInTheDocument()
- expect(consentCommands().at(-1)?.[2]).toEqual(expect.objectContaining({ analytics_storage: "denied" }))
- expect(fetch).not.toHaveBeenCalled()
- view.unmount()
- vi.restoreAllMocks()
+ let visibilitySpy: { mockRestore(): void } | undefined
+ try {
+ await screen.findByTestId(tid.ahrefsAnalyticsFrame)
+ document.cookie = "alook_analytics_consent=v1.denied; Path=/"
+ if (type === "visibilitychange") {
+ visibilitySpy = vi.spyOn(document, "visibilityState", "get").mockReturnValue("visible")
+ fireEvent(document, new Event(type))
+ } else fireEvent(window, new Event(type))
+ expect(screen.queryByTestId(tid.ahrefsAnalyticsFrame)).not.toBeInTheDocument()
+ expect(consentCommands().at(-1)?.[2]).toEqual(expect.objectContaining({ analytics_storage: "denied" }))
+ expect(fetch).not.toHaveBeenCalled()
+ } finally {
+ try { view.unmount() } finally { visibilitySpy?.mockRestore() }
+ }
})
it("keeps the existing Privacy choices usable and preserves denial on reload", async () => {
diff --git a/src/web/src/components/home/landing-page.render.test.ts b/src/web/src/components/home/landing-page.render.test.ts
index 82283d398..2de1a9ebe 100644
--- a/src/web/src/components/home/landing-page.render.test.ts
+++ b/src/web/src/components/home/landing-page.render.test.ts
@@ -62,8 +62,6 @@ describe("landing section calls to action", () => {
const markup = renderToStaticMarkup(createElement(LandingPage, { isLoggedIn }))
const links = [...markup.matchAll(/<a\b[^>]*data-testid="landing-section-cta-([^"]+)"[^>]*>([\s\S]*?)<\/a>/g)]
expect(links.map((match) => match[1])).toEqual(["product"])
- const labels = links.map((match) => match[2])
- expect(new Set(labels).size).toBe(1)
for (const link of links) {
expect(link[0]).toContain(`href="${isLoggedIn ? "/c/me" : "/sign-in"}"`)
expect(link[2].trim().split(/\s+/).length).toBeLessThanOrEqual(3)
@@ -74,11 +72,11 @@ describe("landing section calls to action", () => {
describe("standalone interactive second screen", () => {
it("inserts a pure workspace before the preserved product scene", () => {
const markup = renderToStaticMarkup(createElement(LandingPage, { isLoggedIn: false }))
- expect(markup.indexOf('id="workspace"')).toBeLessThan(markup.indexOf('id="product"'))
+ expect(Array.from(markup.matchAll(/\sid="(testimonials|workspace|product)"/g), match => match[1]))
+ .toEqual(["testimonials", "workspace", "product"])
expect(markup).not.toContain('src="/demo"')
expect(markup).toContain("Loading Alook workspace")
expect(markup).toContain("/landing/posters/server.webp")
- expect(markup.indexOf('id="testimonials"')).toBeLessThan(markup.indexOf('id="workspace"'))
expect(markup).not.toContain("Reset demo")
expect(markup).not.toContain("Sample conversations")
})
diff --git a/src/web/src/components/home/marketing-nav.dom.test.tsx b/src/web/src/components/home/marketing-nav.dom.test.tsx
index e58033900..fc206b453 100644
--- a/src/web/src/components/home/marketing-nav.dom.test.tsx
+++ b/src/web/src/components/home/marketing-nav.dom.test.tsx
@@ -12,22 +12,29 @@ import { MarketingNav } from "./marketing-nav";
afterEach(() => vi.restoreAllMocks());
+function prefetchLinks(container: HTMLElement) {
+ const links = container.querySelectorAll("[data-prefetch]");
+ expect(links.length).toBeGreaterThan(0);
+ return links;
+}
+
it("defers route prefetch until the homepage navigation is revealed, without changing link destinations", () => {
let bottom = 100;
const { container } = render(<><section className="hero-section" /><MarketingNav isLoggedIn={false} revealAfterHero collapseLinksOnMobile /></>);
vi.spyOn(container.querySelector(".hero-section")!, "getBoundingClientRect").mockImplementation(() => ({ bottom }) as DOMRect);
fireEvent.scroll(window);
- for (const link of container.querySelectorAll("[data-prefetch]")) expect(link).toHaveAttribute("data-prefetch", "disabled");
+ for (const link of prefetchLinks(container)) expect(link).toHaveAttribute("data-prefetch", "disabled");
expect(screen.getAllByRole("link", { name: "Pricing" })[0]).toHaveAttribute("href", "/pricing");
bottom = -1;
fireEvent.scroll(window);
- for (const link of container.querySelectorAll("[data-prefetch]")) expect(link).toHaveAttribute("data-prefetch", "auto");
+ for (const link of prefetchLinks(container)) expect(link).toHaveAttribute("data-prefetch", "auto");
bottom = 100;
fireEvent.scroll(window);
- for (const link of container.querySelectorAll("[data-prefetch]")) expect(link).toHaveAttribute("data-prefetch", "disabled");
+ for (const link of prefetchLinks(container)) expect(link).toHaveAttribute("data-prefetch", "disabled");
});
it("preserves default prefetch outside the homepage reveal behavior", () => {
const { container } = render(<MarketingNav isLoggedIn={false} />);
- for (const link of container.querySelectorAll("[data-prefetch]")) expect(link).toHaveAttribute("data-prefetch", "auto");
+ expect(screen.getByRole("link", { name: /^Pricing$/ })).toHaveAttribute("href", "/pricing");
+ for (const link of prefetchLinks(container)) expect(link).toHaveAttribute("data-prefetch", "auto");
});
diff --git a/src/web/src/lib/analytics-consent-server.test.ts b/src/web/src/lib/analytics-consent-server.test.ts
index d844dae68..d41438247 100644
--- a/src/web/src/lib/analytics-consent-server.test.ts
+++ b/src/web/src/lib/analytics-consent-server.test.ts
@@ -22,14 +22,6 @@ describe("analytics consent proof", () => {
})
})
- it("returns the signed decision time as the consent source version", async () => {
- const proof = await createAnalyticsConsentProof("granted", SECRET, NOW)
- await expect(verifyAnalyticsConsentProof(proof, SECRET, NOW)).resolves.toEqual({
- decision: "granted",
- sourceVersion: Math.floor(NOW / 1000),
- })
- })
-
it("rejects a changed decision or signature", async () => {
const proof = await createAnalyticsConsentProof("granted", SECRET, NOW)
await expect(verifyAnalyticsConsentProof(
@@ -37,7 +29,10 @@ describe("analytics consent proof", () => {
SECRET,
NOW,
)).resolves.toBeNull()
- await expect(verifyAnalyticsConsentProof(`${proof.slice(0, -1)}x`, SECRET, NOW)).resolves.toBeNull()
+ const signature = proof.split(".")[3]!
+ const changedSignature = `${signature[0] === "A" ? "B" : "A"}${signature.slice(1)}`
+ const tampered = `${proof.slice(0, -signature.length)}${changedSignature}`
+ await expect(verifyAnalyticsConsentProof(tampered, SECRET, NOW)).resolves.toBeNull()
})
it("rejects stale, future, malformed, and wrong-secret proofs", async () => {