The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as FileSystem from "effect/FileSystem"; |
| 4 | import * as Path from "effect/Path"; |
| 5 | import { describe, expect, it } from "vitest"; |
| 6 | import { buildVinextPrerenderKVPairs } from "../PrerenderCache.ts"; |
| 7 | |
| 8 | const fixture = Effect.gen(function* () { |
| 9 | const fs = yield* FileSystem.FileSystem; |
| 10 | const path = yield* Path.Path; |
| 11 | const root = yield* fs.realPath( |
| 12 | path.resolve( |
| 13 | import.meta.dirname, |
| 14 | "../../../../../examples/cloudflare-website-vinext/node_modules/vinext", |
| 15 | ), |
| 16 | ); |
| 17 | const server = yield* fs.makeTempDirectoryScoped({ |
| 18 | prefix: "vinext-prerender-", |
| 19 | }); |
| 20 | const build = Effect.tryPromise(() => buildVinextPrerenderKVPairs(root, server)); |
| 21 | return { fs, path, server, build }; |
| 22 | }); |
| 23 | |
| 24 | const run = <A, E>( |
| 25 | effect: Effect.Effect<A, E, FileSystem.FileSystem | Path.Path | import("effect/Scope").Scope>, |
| 26 | ) => Effect.runPromise(effect.pipe(Effect.scoped, Effect.provide(NodeServices.layer))); |
| 27 | |
| 28 | describe("vinext prerender cache", () => { |
| 29 | it("returns no seeds for missing manifests or prerender output", () => |
| 30 | run( |
| 31 | Effect.gen(function* () { |
| 32 | const { fs, path, server, build } = yield* fixture; |
| 33 | expect(yield* build).toEqual({ |
| 34 | routeCount: 0, |
| 35 | pairs: [], |
| 36 | warnings: [], |
| 37 | }); |
| 38 | yield* fs.writeFileString( |
| 39 | path.join(server, "vinext-prerender.json"), |
| 40 | JSON.stringify({ buildId: "build", routes: [] }), |
| 41 | ); |
| 42 | expect(yield* build).toEqual({ |
| 43 | routeCount: 0, |
| 44 | pairs: [], |
| 45 | warnings: [], |
| 46 | }); |
| 47 | }), |
| 48 | )); |
| 49 | |
| 50 | it("encodes HTML, RSC, and metadata with the runtime cache codec", () => |
| 51 | run( |
| 52 | Effect.gen(function* () { |
| 53 | const { fs, path, server, build } = yield* fixture; |
| 54 | const output = path.join(server, "prerendered-routes"); |
| 55 | yield* fs.makeDirectory(output); |
| 56 | yield* fs.writeFileString(path.join(output, "index.html"), "<h1>prerendered</h1>"); |
| 57 | yield* fs.writeFileString(path.join(output, "index.rsc"), "rsc payload"); |
| 58 | yield* fs.writeFileString( |
| 59 | path.join(server, "vinext-prerender.json"), |
| 60 | JSON.stringify({ |
| 61 | buildId: "build", |
| 62 | routes: [ |
| 63 | { |
| 64 | route: "/", |
| 65 | router: "app", |
| 66 | status: "rendered", |
| 67 | revalidate: 60, |
| 68 | expire: 120, |
| 69 | stale: 30, |
| 70 | tags: ["home"], |
| 71 | }, |
| 72 | ], |
| 73 | }), |
| 74 | ); |
| 75 | const result = yield* build; |
| 76 | expect(result.routeCount).toBe(1); |
| 77 | expect(result.warnings).toEqual([]); |
| 78 | expect(result.pairs).toHaveLength(2); |
| 79 | for (const pair of result.pairs) { |
| 80 | const entry = JSON.parse(pair.value); |
| 81 | expect(entry.value.kind).toBe("APP_PAGE"); |
| 82 | expect(entry.tags).toContain("home"); |
| 83 | expect(entry.cacheControl).toEqual({ |
| 84 | revalidate: 60, |
| 85 | expire: 120, |
| 86 | stale: 30, |
| 87 | }); |
| 88 | expect(entry.revalidateAt - entry.lastModified).toBe(60_000); |
| 89 | expect(entry.expireAt - entry.lastModified).toBe(120_000); |
| 90 | expect(pair.expirationTtl).toBe(30 * 24 * 3600); |
| 91 | } |
| 92 | expect(JSON.parse(result.pairs[0]!.value).value.html).toBe("<h1>prerendered</h1>"); |
| 93 | expect(JSON.parse(result.pairs[1]!.value).value.rscData).toBe("cnNjIHBheWxvYWQ="); |
| 94 | }), |
| 95 | )); |
| 96 | }); |