Skip to content
File

Blob: tests/worker/lib/host-match.test.ts

typescript69 lines
1import { describe, expect, it } from "vitest";
2 
3import { matchSiteHost } from "@/worker/lib/host-match";
4 
5const ENABLED = { PUBLISHED_SITE_DOMAIN: "bland.site" };
6const DISABLED = { PUBLISHED_SITE_DOMAIN: "" };
7 
8function host(url: string): URL {
9 return new URL(url);
10}
11 
12describe("matchSiteHost", () => {
13 it("returns none when the feature is disabled", () => {
14 expect(matchSiteHost(host("https://acme.bland.site/"), DISABLED)).toEqual({ kind: "none" });
15 expect(
16 matchSiteHost(host("https://acme.bland.site/"), { PUBLISHED_SITE_DOMAIN: undefined as unknown as string }),
17 ).toEqual({
18 kind: "none",
19 });
20 });
21 
22 it("matches apex host as apex", () => {
23 expect(matchSiteHost(host("https://bland.site/"), ENABLED)).toEqual({ kind: "apex", baseDomain: "bland.site" });
24 expect(matchSiteHost(host("https://BLAND.SITE/foo"), ENABLED)).toEqual({ kind: "apex", baseDomain: "bland.site" });
25 });
26 
27 it("matches single-label subdomain", () => {
28 expect(matchSiteHost(host("https://acme.bland.site/"), ENABLED)).toEqual({
29 kind: "subdomain",
30 slug: "acme",
31 baseDomain: "bland.site",
32 });
33 expect(matchSiteHost(host("https://Acme.Bland.SITE/foo"), ENABLED)).toEqual({
34 kind: "subdomain",
35 slug: "acme",
36 baseDomain: "bland.site",
37 });
38 });
39 
40 it("rejects multi-label subdomains", () => {
41 expect(matchSiteHost(host("https://foo.bar.bland.site/"), ENABLED)).toEqual({ kind: "none" });
42 });
43 
44 it("rejects empty subdomain", () => {
45 expect(matchSiteHost(host("https://.bland.site/"), ENABLED)).toEqual({ kind: "none" });
46 });
47 
48 it("rejects unrelated hosts", () => {
49 expect(matchSiteHost(host("https://bland.tools/"), ENABLED)).toEqual({ kind: "none" });
50 expect(matchSiteHost(host("https://docs.limic.dev/"), ENABLED)).toEqual({ kind: "none" });
51 expect(matchSiteHost(host("https://bland.example/"), ENABLED)).toEqual({ kind: "none" });
52 // A host that merely ends with the same letters but lacks the dot must not match.
53 expect(matchSiteHost(host("https://evilbland.site/"), ENABLED)).toEqual({ kind: "none" });
54 });
55 
56 it("works for local dev base domain", () => {
57 const local = { PUBLISHED_SITE_DOMAIN: "bland.localhost" };
58 expect(matchSiteHost(host("http://bland.localhost:5173/"), local)).toEqual({
59 kind: "apex",
60 baseDomain: "bland.localhost",
61 });
62 expect(matchSiteHost(host("http://acme.bland.localhost:5173/"), local)).toEqual({
63 kind: "subdomain",
64 slug: "acme",
65 baseDomain: "bland.localhost",
66 });
67 });
68});