| 1 | import { createHash } from "node:crypto"; |
| 2 | import { readFileSync } from "node:fs"; |
| 3 | import { describe, expect, it } from "vitest"; |
| 4 | import { APP_LOGIN_URL, APP_SIGNUP_URL, APP_URL } from "./i18n/links"; |
| 5 | import { |
| 6 | APP_AUTH_CALLBACK_URL, |
| 7 | CANONICAL_MARK_SHA256, |
| 8 | CANONICAL_MARK_SRC, |
| 9 | canonicalPublicAuthPath, |
| 10 | publicAuthAppDestination, |
| 11 | publicAuthCallbackDestination, |
| 12 | publicAuthKind, |
| 13 | publicAuthRemainder, |
| 14 | } from "./public-auth-routes"; |
| 15 | |
| 16 | const SHANNON_SANS_SHA256 = "e1abb82e681b502950da1b0f25e10de3eeb329ea6549315d828dbddf53a68951"; |
| 17 | const SHANNON_SANS_BYTES = 531084; |
| 18 | const SHANNON_OFL_SHA256 = "19c20bff3654a225895067510552ef14ce64bbcdfb90f3ae04957548e9d2cf4f"; |
| 19 | const SHANNON_OFL_BYTES = 4463; |
| 20 | |
| 21 | describe("public auth remainders and kinds", () => { |
| 22 | it("recognizes the observed 404 paths with and without a locale prefix", () => { |
| 23 | expect(publicAuthRemainder("/signin")).toBe("signin"); |
| 24 | expect(publicAuthRemainder("/en/signin")).toBe("signin"); |
| 25 | expect(publicAuthRemainder("/pt-BR/signup")).toBe("signup"); |
| 26 | expect(publicAuthRemainder("/zh/auth/callback")).toBe("auth/callback"); |
| 27 | expect(publicAuthKind("/signin")).toBe("sign-in"); |
| 28 | expect(publicAuthKind("/en/signin")).toBe("sign-in"); |
| 29 | expect(publicAuthKind("/signup")).toBe("sign-up"); |
| 30 | expect(publicAuthKind("/ja/signup")).toBe("sign-up"); |
| 31 | expect(publicAuthKind("/auth/callback")).toBe("callback"); |
| 32 | expect(publicAuthKind("/en/auth/callback")).toBe("callback"); |
| 33 | expect(publicAuthKind("/install")).toBeNull(); |
| 34 | expect(publicAuthKind("/en/docs")).toBeNull(); |
| 35 | }); |
| 36 | |
| 37 | it("folds login/register aliases onto the public sign-in and create-account routes", () => { |
| 38 | expect(canonicalPublicAuthPath("/login")).toBe("/signin"); |
| 39 | expect(canonicalPublicAuthPath("/en/login")).toBe("/en/signin"); |
| 40 | expect(canonicalPublicAuthPath("/register")).toBe("/signup"); |
| 41 | expect(canonicalPublicAuthPath("/zh/register")).toBe("/zh/signup"); |
| 42 | expect(canonicalPublicAuthPath("/create-account")).toBe("/signup"); |
| 43 | expect(canonicalPublicAuthPath("/signin")).toBeNull(); |
| 44 | expect(canonicalPublicAuthPath("/en/signup")).toBeNull(); |
| 45 | }); |
| 46 | }); |
| 47 | |
| 48 | describe("CWC destinations", () => { |
| 49 | it("sends English (and other) public pages to the default app entry", () => { |
| 50 | expect(publicAuthAppDestination("sign-in", "en")).toBe(APP_LOGIN_URL); |
| 51 | expect(publicAuthAppDestination("sign-up", "ja")).toBe(APP_SIGNUP_URL); |
| 52 | expect(publicAuthAppDestination("sign-in", "zh")).toBe(`${APP_URL}/zh/login`); |
| 53 | expect(publicAuthAppDestination("sign-up", "zh")).toBe(`${APP_URL}/zh/signup`); |
| 54 | }); |
| 55 | |
| 56 | it("hops auth callbacks to the CWC app and preserves the query string", () => { |
| 57 | expect(APP_AUTH_CALLBACK_URL).toBe(`${APP_URL}/auth/callback`); |
| 58 | expect( |
| 59 | publicAuthCallbackDestination(new URL("https://codewhale.net/auth/callback?code=abc&state=1")), |
| 60 | ).toBe(`${APP_AUTH_CALLBACK_URL}?code=abc&state=1`); |
| 61 | expect( |
| 62 | publicAuthCallbackDestination(new URL("https://codewhale.net/en/auth/callback?code=abc")), |
| 63 | ).toBe(`${APP_AUTH_CALLBACK_URL}?code=abc`); |
| 64 | expect( |
| 65 | publicAuthCallbackDestination(new URL("https://codewhale.net/en/signin")), |
| 66 | ).toBeNull(); |
| 67 | }); |
| 68 | }); |
| 69 | |
| 70 | describe("canonical mark", () => { |
| 71 | it("ships the pinned raster generated from the canonical vector", () => { |
| 72 | const bytes = readFileSync(new URL("../public/brand/codewhale-mark.png", import.meta.url)); |
| 73 | expect(createHash("sha256").update(bytes).digest("hex")).toBe(CANONICAL_MARK_SHA256); |
| 74 | expect(CANONICAL_MARK_SRC).toBe("/brand/codewhale-mark.png"); |
| 75 | }); |
| 76 | }); |
| 77 | |
| 78 | describe("website font assets", () => { |
| 79 | it("ships the pinned Shannon Sans face with the complete adjacent OFL notice", () => { |
| 80 | const font = readFileSync(new URL("../public/brand/fonts/ShannonSans-Variable.woff2", import.meta.url)); |
| 81 | const license = readFileSync(new URL("../public/brand/fonts/OFL.txt", import.meta.url)); |
| 82 | expect(font.length).toBe(SHANNON_SANS_BYTES); |
| 83 | expect(createHash("sha256").update(font).digest("hex")).toBe(SHANNON_SANS_SHA256); |
| 84 | expect(license.length).toBe(SHANNON_OFL_BYTES); |
| 85 | expect(createHash("sha256").update(license).digest("hex")).toBe(SHANNON_OFL_SHA256); |
| 86 | }); |
| 87 | }); |
| 88 |