sites-worker.test.mjs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import assert from "node:assert/strict";
  2. import { access } from "node:fs/promises";
  3. import test from "node:test";
  4. import worker from "../worker/index.js";
  5. test("serves existing static assets without a fallback", async () => {
  6. const calls = [];
  7. const response = await worker.fetch(new Request("https://example.test/assets/app.js"), {
  8. ASSETS: {
  9. fetch: async (request) => {
  10. calls.push(new URL(request.url).pathname);
  11. return new Response("asset", { status: 200 });
  12. },
  13. },
  14. });
  15. assert.equal(response.status, 200);
  16. assert.deepEqual(calls, ["/assets/app.js"]);
  17. });
  18. test("falls back to index.html for an unknown app route", async () => {
  19. const calls = [];
  20. const response = await worker.fetch(
  21. new Request("https://example.test/flow/step-two?source=share", {
  22. headers: { accept: "text/html" },
  23. }),
  24. {
  25. ASSETS: {
  26. fetch: async (request) => {
  27. const url = new URL(request.url);
  28. calls.push(url.pathname + url.search);
  29. return new Response(url.pathname === "/index.html" ? "app" : "missing", {
  30. status: url.pathname === "/index.html" ? 200 : 404,
  31. });
  32. },
  33. },
  34. },
  35. );
  36. assert.equal(response.status, 200);
  37. assert.deepEqual(calls, ["/flow/step-two?source=share", "/index.html"]);
  38. });
  39. test("does not turn missing API or write requests into the app shell", async () => {
  40. for (const request of [
  41. new Request("https://example.test/api/missing", { headers: { accept: "application/json" } }),
  42. new Request("https://example.test/flow", { method: "POST", headers: { accept: "text/html" } }),
  43. ]) {
  44. let calls = 0;
  45. const response = await worker.fetch(request, {
  46. ASSETS: {
  47. fetch: async () => {
  48. calls += 1;
  49. return new Response("missing", { status: 404 });
  50. },
  51. },
  52. });
  53. assert.equal(response.status, 404);
  54. assert.equal(calls, 1);
  55. }
  56. });
  57. test("emits the files required by Sites packaging", async () => {
  58. await access(new URL("../dist/client/index.html", import.meta.url));
  59. await access(new URL("../dist/server/index.js", import.meta.url));
  60. await access(new URL("../dist/.openai/hosting.json", import.meta.url));
  61. });