g1t/services/pages/src/route.test.ts

126 lines5,794 bytesCodeBlame

Pick any line to see why it is the way it is: the commit, the pull request and issue it came from, and what the agent was thinking.

Agents and memory, checks and conflicts, profiles, slug renames, custom domains1import assert from "node:assert/strict";
2import { test } from "node:test";
3
4import { parseEntry, redirectTo, route } from "./route.ts";
5import worker from "./index.ts";
6
7test("hosts on g1t.page are the home page, the fallback origin, or an app", () => {
8 assert.deepEqual(route("g1t.page"), { kind: "home" });
9 assert.deepEqual(route("domains.g1t.page"), { kind: "fallback" });
10 assert.deepEqual(route("web-acme.g1t.page"), { kind: "app", label: "web-acme" });
11 assert.deepEqual(route("WEB-Acme.g1t.page."), { kind: "app", label: "web-acme" });
12 assert.deepEqual(route("a.b.g1t.page"), { kind: "invalid" });
13});
14
15test("any other host is a custom domain", () => {
16 assert.deepEqual(route("aut10dmain.io"), { kind: "custom", hostname: "aut10dmain.io" });
17 assert.deepEqual(route("www.example.co.uk"), { kind: "custom", hostname: "www.example.co.uk" });
18 assert.deepEqual(route("xn--bcher-kva.example"), { kind: "custom", hostname: "xn--bcher-kva.example" });
19 assert.deepEqual(route("localhost"), { kind: "invalid" });
20 assert.deepEqual(route("-bad.example"), { kind: "invalid" });
21});
22
23test("entries are read only when they are well formed", () => {
24 assert.deepEqual(parseEntry({ script: "web-acme", redirect: null }), { script: "web-acme", redirect: null });
25 assert.deepEqual(parseEntry({ script: "web-acme", redirect: "example.com" }), { script: "web-acme", redirect: "example.com" });
26 assert.equal(parseEntry({ script: "../x" }), null);
27 assert.equal(parseEntry(null), null);
28 assert.equal(parseEntry("web-acme"), null);
29});
30
31test("redirects keep the path and query", () => {
32 assert.equal(redirectTo("https://www.example.com/a/b?x=1#h", "example.com"), "https://example.com/a/b?x=1");
33});
34
35/** The worker with a namespace that knows `scripts`, and a KV holding `domains`. */
36function env(scripts: Record<string, string>, domains: Record<string, unknown>) {
37 const seen: string[] = [];
38 return {
39 seen,
40 env: {
41 APPS: {
42 get(name: string) {
43 return {
44 async fetch(request: Request) {
45 seen.push(`${name} ${new URL(request.url).hostname}`);
46 if (!(name in scripts)) throw new Error("Worker not found");
47 return new Response(scripts[name]);
48 },
49 };
50 },
51 },
52 DOMAINS: {
53 async get(key: string) {
54 return domains[key] ?? null;
55 },
56 },
57 } as any,
58 };
59}
60
61const call = (url: string, e: any) => worker.fetch(new Request(url), e, {} as any);
62
63test("a custom domain is served by its project's app, with the request's host kept", async () => {
64 const { env: e, seen } = env({ "web-acme": "hello" }, { "app.example.com": { script: "web-acme", redirect: null } });
65 const response = await call("https://app.example.com/docs?q=1", e);
66 assert.equal(response.status, 200);
67 assert.equal(await response.text(), "hello");
68 assert.deepEqual(seen, ["web-acme app.example.com"]);
69});
70
71test("a paired domain redirects with a 308, path and query kept", async () => {
72 const { env: e, seen } = env({}, { "www.example.org": { script: "web-acme", redirect: "example.org" } });
73 const response = await call("https://www.example.org/pricing?plan=pro", e);
74 assert.equal(response.status, 308);
75 assert.equal(response.headers.get("location"), "https://example.org/pricing?plan=pro");
76 assert.deepEqual(seen, []);
77});
78
79test("an unknown custom domain gets the branded 404", async () => {
80 const { env: e } = env({}, {});
81 const response = await call("https://nobody.example.net/", e);
82 assert.equal(response.status, 404);
83 assert.match(await response.text(), /This domain is not set up/);
84});
85
86test("a custom domain whose app is not up says so", async () => {
87 const { env: e } = env({}, { "shop.example.com": { script: "shop-acme", redirect: null } });
88 const response = await call("https://shop.example.com/", e);
89 assert.equal(response.status, 404);
90 assert.match(await response.text(), /Nothing deployed here yet/);
91});
92
93test("g1t.page addresses dispatch by label as before", async () => {
94 const { env: e, seen } = env({ "web-acme": "prod", "web-git-x-acme": "preview" }, {});
95 assert.equal(await (await call("https://web-acme.g1t.page/", e)).text(), "prod");
96 const preview = await call("https://web-git-x-acme.g1t.page/", e);
97 assert.equal(preview.headers.get("x-robots-tag"), "noindex");
98 assert.deepEqual(seen, ["web-acme web-acme.g1t.page", "web-git-x-acme web-git-x-acme.g1t.page"]);
99 assert.equal((await call("https://gone-acme.g1t.page/", e)).status, 404);
100 assert.equal((await call("https://domains.g1t.page/", e)).status, 200);
101});
status.g1t.sh with incident management, invites that land you in the workspace, settings as pages, usage without quotas102
103test("an app's old address redirects to its new one, whatever the old script answers", async () => {
104 // The old script is the paused notice, as when the old workspace reached its limit.
105 const { env: e, seen } = env(
106 { "lab-api-syntaqx": "This app is paused", "lab-api-flagon-io": "api" },
107 { "lab-api-syntaqx.g1t.page": { script: "lab-api-syntaqx", redirect: "lab-api-flagon-io.g1t.page" } },
108 );
109 const response = await call("https://lab-api-syntaqx.g1t.page/v1/items?page=2", e);
110 assert.equal(response.status, 301);
111 assert.equal(response.headers.get("location"), "https://lab-api-flagon-io.g1t.page/v1/items?page=2");
112 assert.equal(response.headers.get("x-robots-tag"), "noindex");
113 assert.deepEqual(seen, []);
114 // The new address is served.
115 assert.equal(await (await call("https://lab-api-flagon-io.g1t.page/", e)).text(), "api");
116});
117
118test("an app address with no redirect, or a failing lookup, is served as it is", async () => {
119 const { env: e } = env({ "shop-acme": "shop" }, {});
120 e.DOMAINS = {
121 async get() {
122 throw new Error("KV is down");
123 },
124 };
125 assert.equal(await (await call("https://shop-acme.g1t.page/", e)).text(), "shop");
126});