|
| 1 | +import * as assert from "assert"; |
| 2 | +import { posix } from "path"; |
| 3 | + |
| 4 | +export const host = process.env.HOST; |
| 5 | + |
| 6 | +if (!host) { |
| 7 | + throw new Error("HOST environment variable expected"); |
| 8 | +} |
| 9 | + |
| 10 | +describe("app", () => { |
| 11 | + it("/", async () => { |
| 12 | + const response = await fetch(host); |
| 13 | + assert.ok(response.ok); |
| 14 | + assert.equal(response.headers.get("content-type")?.toLowerCase(), "text/html; charset=utf-8"); |
| 15 | + assert.equal( |
| 16 | + response.headers.get("cache-control"), |
| 17 | + "s-maxage=31536000, stale-while-revalidate", |
| 18 | + ); |
| 19 | + }); |
| 20 | + |
| 21 | + it("/ssg", async () => { |
| 22 | + const response = await fetch(posix.join(host, "ssg")); |
| 23 | + assert.ok(response.ok); |
| 24 | + assert.equal(response.headers.get("content-type")?.toLowerCase(), "text/html; charset=utf-8"); |
| 25 | + assert.equal( |
| 26 | + response.headers.get("cache-control"), |
| 27 | + "s-maxage=31536000, stale-while-revalidate", |
| 28 | + ); |
| 29 | + const text = await response.text(); |
| 30 | + assert.ok(text.includes("SSG")); |
| 31 | + assert.ok(text.includes("Generated")); |
| 32 | + assert.ok(text.includes("UUID")); |
| 33 | + }); |
| 34 | + |
| 35 | + it("/ssr", async () => { |
| 36 | + const response = await fetch(posix.join(host, "ssr")); |
| 37 | + assert.ok(response.ok); |
| 38 | + assert.equal(response.headers.get("content-type")?.toLowerCase(), "text/html; charset=utf-8"); |
| 39 | + assert.equal( |
| 40 | + response.headers.get("cache-control"), |
| 41 | + "private, no-cache, no-store, max-age=0, must-revalidate", |
| 42 | + ); |
| 43 | + const text = await response.text(); |
| 44 | + assert.ok(text.includes("A server generated page!")); |
| 45 | + assert.ok(text.includes("Generated")); |
| 46 | + assert.ok(text.includes("UUID")); |
| 47 | + }); |
| 48 | + |
| 49 | + it("/ssr/streaming", async () => { |
| 50 | + const response = await fetch(posix.join(host, "ssr", "streaming")); |
| 51 | + assert.ok(response.ok); |
| 52 | + assert.equal(response.headers.get("content-type")?.toLowerCase(), "text/html; charset=utf-8"); |
| 53 | + assert.equal( |
| 54 | + response.headers.get("cache-control"), |
| 55 | + "private, no-cache, no-store, max-age=0, must-revalidate", |
| 56 | + ); |
| 57 | + const text = await response.text(); |
| 58 | + assert.ok(text.includes("A server generated page!")); |
| 59 | + assert.ok(text.includes("Streaming!")); |
| 60 | + assert.ok(text.includes("Generated")); |
| 61 | + assert.ok(text.includes("UUID")); |
| 62 | + }).timeout(3000); // Increased timeout to 3000ms to allow for the 2000ms component timeout |
| 63 | + |
| 64 | + it("/isr/time", async () => { |
| 65 | + const response = await fetch(posix.join(host, "isr", "time")); |
| 66 | + assert.ok(response.ok); |
| 67 | + assert.equal(response.headers.get("content-type")?.toLowerCase(), "text/html; charset=utf-8"); |
| 68 | + assert.ok( |
| 69 | + response.headers.get("cache-control")?.includes("s-maxage=10"), |
| 70 | + "Cache-Control header should include s-maxage=10", |
| 71 | + ); |
| 72 | + const text = await response.text(); |
| 73 | + assert.ok(text.includes("A cached page")); |
| 74 | + assert.ok(text.includes("(should be regenerated every 10 seconds)")); |
| 75 | + assert.ok(text.includes("Generated")); |
| 76 | + assert.ok(text.includes("UUID")); |
| 77 | + }); |
| 78 | + |
| 79 | + it("/isr/demand", async () => { |
| 80 | + const response = await fetch(posix.join(host, "isr", "demand")); |
| 81 | + assert.ok(response.ok); |
| 82 | + assert.equal(response.headers.get("content-type")?.toLowerCase(), "text/html; charset=utf-8"); |
| 83 | + assert.equal( |
| 84 | + response.headers.get("cache-control"), |
| 85 | + "s-maxage=31536000, stale-while-revalidate", |
| 86 | + ); |
| 87 | + const text = await response.text(); |
| 88 | + assert.ok(text.includes("A cached page")); |
| 89 | + assert.ok(text.includes("Generated")); |
| 90 | + assert.ok(text.includes("UUID")); |
| 91 | + assert.ok(text.includes("Regenerate page")); |
| 92 | + }); |
| 93 | + |
| 94 | + it("/isr/demand/revalidate", async () => { |
| 95 | + // First, fetch the initial page |
| 96 | + const initialResponse = await fetch(posix.join(host, "isr", "demand")); |
| 97 | + assert.ok(initialResponse.ok); |
| 98 | + const initialText = await initialResponse.text(); |
| 99 | + const initialUUID = initialText.match(/UUID<\/p>\s*<h2>([^<]+)<\/h2>/)?.[1]; |
| 100 | + |
| 101 | + // Trigger revalidation |
| 102 | + const revalidateResponse = await fetch(posix.join(host, "isr", "demand", "revalidate"), { |
| 103 | + method: "POST", |
| 104 | + }); |
| 105 | + assert.equal(revalidateResponse.status, 200); |
| 106 | + |
| 107 | + // Fetch the page again |
| 108 | + const newResponse = await fetch(posix.join(host, "isr", "demand")); |
| 109 | + assert.ok(newResponse.ok); |
| 110 | + const newText = await newResponse.text(); |
| 111 | + const newUUID = newText.match(/UUID<\/p>\s*<h2>([^<]+)<\/h2>/)?.[1]; |
| 112 | + |
| 113 | + // Check if the UUID has changed, indicating successful revalidation |
| 114 | + assert.notEqual(initialUUID, newUUID, "UUID should change after revalidation"); |
| 115 | + }); |
| 116 | + |
| 117 | + it(`404`, async () => { |
| 118 | + const response = await fetch(posix.join(host, Math.random().toString())); |
| 119 | + assert.equal(response.status, 404); |
| 120 | + assert.equal(response.headers.get("content-type")?.toLowerCase(), "text/html; charset=utf-8"); |
| 121 | + assert.equal( |
| 122 | + response.headers.get("cache-control"), |
| 123 | + "private, no-cache, no-store, max-age=0, must-revalidate", |
| 124 | + ); |
| 125 | + }); |
| 126 | +}); |
0 commit comments