The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { fileURLToPath } from "node:url"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import { describe, expect, it } from "vitest"; |
| 4 | import { loadProjectModule } from "../../core/Loader.ts"; |
| 5 | import { makeDataCacheHandler, type DataCacheStore } from "../cache/handler.ts"; |
| 6 | import { kvHttpNamespaceFromEnv } from "../cache/kv-http.ts"; |
| 7 | import createKvDataCacheAdapter from "../cache/kv-runtime.ts"; |
| 8 | import { kvAdapter } from "../cache/kv.ts"; |
| 9 | import { makeVinextCachePlugin } from "../cache/plugin.ts"; |
| 10 | import { redisAdapter } from "../cache/redis.ts"; |
| 11 | import { s3Adapter } from "../cache/s3.ts"; |
| 12 | import { seedPrerenderTo, type SeedSink } from "../cache/seed.ts"; |
| 13 | import { |
| 14 | keySpace, |
| 15 | readEnvString, |
| 16 | restoreArrayBuffers, |
| 17 | serializeForJSON, |
| 18 | validateCacheEntry, |
| 19 | validateTag, |
| 20 | } from "../cache/shared.ts"; |
| 21 | |
| 22 | const memoryStore = (): DataCacheStore & { data: Map<string, string> } => { |
| 23 | const data = new Map<string, string>(); |
| 24 | return { |
| 25 | data, |
| 26 | async getText(key) { |
| 27 | return data.get(key); |
| 28 | }, |
| 29 | async putText(key, value) { |
| 30 | data.set(key, value); |
| 31 | }, |
| 32 | async delete(key) { |
| 33 | data.delete(key); |
| 34 | }, |
| 35 | }; |
| 36 | }; |
| 37 | |
| 38 | describe("vinext cache adapters", () => { |
| 39 | it.each(["redis", "s3", "kv"] as const)( |
| 40 | "injects the %s cache without application configuration", |
| 41 | async (kind) => { |
| 42 | const root = fileURLToPath( |
| 43 | new URL("../../../../../examples/prisma-website-vinext/", import.meta.url), |
| 44 | ); |
| 45 | const plugin = await Effect.runPromise(makeVinextCachePlugin(root, kind)); |
| 46 | const vite = await Effect.runPromise(loadProjectModule<typeof import("vite")>(root, "vite")); |
| 47 | const server = await vite.createServer({ |
| 48 | root, |
| 49 | configFile: false, |
| 50 | plugins: [plugin], |
| 51 | server: { watch: null }, |
| 52 | optimizeDeps: { noDiscovery: true }, |
| 53 | }); |
| 54 | try { |
| 55 | const container = server.environments.ssr.pluginContainer; |
| 56 | const id = await container.resolveId("virtual:vinext-cache-adapters"); |
| 57 | expect(await container.load(id!.id)).toContain(`${kind}-runtime.js`); |
| 58 | const cdnId = await container.resolveId("virtual:vinext-cdn-cache-adapter"); |
| 59 | const cdn = await container.load(cdnId!.id); |
| 60 | expect(cdn).toContain("hasConfiguredDataCache = true"); |
| 61 | expect(cdn).not.toContain(`${kind}-runtime.js`); |
| 62 | } finally { |
| 63 | await server.close(); |
| 64 | } |
| 65 | }, |
| 66 | ); |
| 67 | |
| 68 | it("redisAdapter points at @alchemy.run/redis connect runtime", () => { |
| 69 | const descriptor = redisAdapter({ |
| 70 | urlEnv: "REDIS_URL", |
| 71 | appPrefix: "app", |
| 72 | }); |
| 73 | expect(descriptor.adapter).toMatch(/redis-runtime\.js$/); |
| 74 | expect(descriptor.options).toEqual({ |
| 75 | urlEnv: "REDIS_URL", |
| 76 | appPrefix: "app", |
| 77 | }); |
| 78 | }); |
| 79 | |
| 80 | it("s3Adapter points at the runtime factory", () => { |
| 81 | const descriptor = s3Adapter({ bucketEnv: "CACHE_BUCKET_NAME" }); |
| 82 | expect(descriptor.adapter).toMatch(/s3-runtime\.js$/); |
| 83 | expect(descriptor.options?.bucketEnv).toBe("CACHE_BUCKET_NAME"); |
| 84 | }); |
| 85 | |
| 86 | it("kvAdapter points at the runtime factory", () => { |
| 87 | const descriptor = kvAdapter({ binding: "VINEXT_KV_CACHE" }); |
| 88 | expect(descriptor.adapter).toMatch(/kv-runtime\.js$/); |
| 89 | expect(descriptor.options?.binding).toBe("VINEXT_KV_CACHE"); |
| 90 | }); |
| 91 | |
| 92 | it("KV factory throws without a binding and round-trips through a mock namespace", async () => { |
| 93 | expect(() => createKvDataCacheAdapter({ env: {} })).toThrow(/VINEXT_KV_CACHE/); |
| 94 | const data = new Map<string, string>(); |
| 95 | const ns = { |
| 96 | async get(key: string) { |
| 97 | return data.get(key) ?? null; |
| 98 | }, |
| 99 | async put(key: string, value: string) { |
| 100 | data.set(key, value); |
| 101 | }, |
| 102 | async delete(key: string) { |
| 103 | data.delete(key); |
| 104 | }, |
| 105 | }; |
| 106 | const handler = createKvDataCacheAdapter({ |
| 107 | env: { VINEXT_KV_CACHE: ns }, |
| 108 | }); |
| 109 | await handler.set("page", { |
| 110 | kind: "APP_PAGE", |
| 111 | html: "<p>kv</p>", |
| 112 | tags: ["posts"], |
| 113 | revalidate: 60, |
| 114 | }); |
| 115 | const hit = await handler.get("page"); |
| 116 | expect((hit?.value as { html?: string })?.html).toBe("<p>kv</p>"); |
| 117 | expect(data.size).toBeGreaterThan(0); |
| 118 | }); |
| 119 | |
| 120 | it("round-trips APP_PAGE ArrayBuffers through JSON", () => { |
| 121 | const bytes = new Uint8Array([1, 2, 3, 4]).buffer; |
| 122 | const serialized = serializeForJSON({ |
| 123 | kind: "APP_PAGE", |
| 124 | html: "<p>ok</p>", |
| 125 | rscData: bytes, |
| 126 | }) as Record<string, unknown>; |
| 127 | expect(typeof serialized.rscData).toBe("string"); |
| 128 | const restored = restoreArrayBuffers(serialized); |
| 129 | expect(restored?.rscData).toBeInstanceOf(ArrayBuffer); |
| 130 | expect([...new Uint8Array(restored!.rscData as ArrayBuffer)]).toEqual([1, 2, 3, 4]); |
| 131 | }); |
| 132 | |
| 133 | it("rejects invalid tags and cache entries", () => { |
| 134 | expect(validateTag("")).toBeNull(); |
| 135 | expect(validateTag("ok")).toBe("ok"); |
| 136 | expect(validateTag("a:b")).toBeNull(); |
| 137 | expect(validateCacheEntry({ lastModified: 1, tags: [] })).toBeNull(); |
| 138 | expect( |
| 139 | validateCacheEntry({ |
| 140 | lastModified: 1, |
| 141 | tags: [], |
| 142 | revalidateAt: null, |
| 143 | expireAt: null, |
| 144 | value: null, |
| 145 | }), |
| 146 | ).not.toBeNull(); |
| 147 | }); |
| 148 | |
| 149 | it("namespaces entry and tag keys", () => { |
| 150 | const keys = keySpace("site"); |
| 151 | expect(keys.entryKey("abc")).toBe("site:cache:abc"); |
| 152 | expect(keys.tagKey("posts")).toBe("site:__tag:posts"); |
| 153 | }); |
| 154 | |
| 155 | it("hashes keys that exceed the KV byte limit", () => { |
| 156 | const keys = keySpace(undefined); |
| 157 | expect(keys.entryKey("abc")).toBe("cache:abc"); |
| 158 | const hashed = keys.entryKey("x".repeat(600)); |
| 159 | expect(hashed.startsWith("cache:__hash:")).toBe(true); |
| 160 | expect(hashed.length).toBeLessThan(40); |
| 161 | }); |
| 162 | |
| 163 | it("rejects a non-string redis urlEnv", () => { |
| 164 | expect(() => redisAdapter({ urlEnv: 1 as unknown as string })).toThrow(/urlEnv/); |
| 165 | }); |
| 166 | |
| 167 | it("reads env strings from the bag then process.env", () => { |
| 168 | expect(readEnvString({ REDIS_URL: "redis://bag" }, "REDIS_URL")).toBe("redis://bag"); |
| 169 | expect(readEnvString({ REDIS_URL: "" }, "REDIS_URL")).toBe(process.env.REDIS_URL || undefined); |
| 170 | expect(readEnvString(undefined, "VINEXT_MISSING_ENV_KEY")).toBeUndefined(); |
| 171 | }); |
| 172 | |
| 173 | it("round-trips a cache entry through the shared handler", async () => { |
| 174 | const store = memoryStore(); |
| 175 | const handler = makeDataCacheHandler(store, { appPrefix: "site" }); |
| 176 | await handler.set("page", { |
| 177 | kind: "APP_PAGE", |
| 178 | html: "<p>ok</p>", |
| 179 | tags: ["posts"], |
| 180 | revalidate: 60, |
| 181 | }); |
| 182 | const hit = await handler.get("page"); |
| 183 | expect(hit).not.toBeNull(); |
| 184 | expect((hit?.value as { html?: string })?.html).toBe("<p>ok</p>"); |
| 185 | expect(store.data.has("site:cache:page")).toBe(true); |
| 186 | }); |
| 187 | |
| 188 | it("treats revalidateTag as a miss for tagged entries", async () => { |
| 189 | const store = memoryStore(); |
| 190 | const handler = makeDataCacheHandler(store); |
| 191 | await handler.set("page", { |
| 192 | kind: "APP_PAGE", |
| 193 | html: "<p>old</p>", |
| 194 | tags: ["posts"], |
| 195 | revalidate: 60, |
| 196 | }); |
| 197 | await handler.revalidateTag("posts"); |
| 198 | expect(await handler.get("page")).toBeNull(); |
| 199 | }); |
| 200 | |
| 201 | it("kvHttpNamespaceFromEnv reads accountId + namespaceId", () => { |
| 202 | expect(kvHttpNamespaceFromEnv({})).toBeUndefined(); |
| 203 | expect( |
| 204 | kvHttpNamespaceFromEnv({ |
| 205 | VINEXT_KV_CACHE: { namespaceId: "ns" }, |
| 206 | }), |
| 207 | ).toBeUndefined(); |
| 208 | expect( |
| 209 | kvHttpNamespaceFromEnv({ |
| 210 | VINEXT_KV_CACHE: { namespaceId: "ns", accountId: "acct" }, |
| 211 | }), |
| 212 | ).toEqual({ namespaceId: "ns", accountId: "acct" }); |
| 213 | }); |
| 214 | |
| 215 | it("seedPrerenderTo writes through putText regardless of backend", async () => { |
| 216 | const written: Array<{ key: string; value: string; ttlMs?: number }> = []; |
| 217 | const sink: SeedSink = { |
| 218 | putText: (key, value, ttlMs) => |
| 219 | Effect.sync(() => { |
| 220 | written.push({ key, value, ttlMs }); |
| 221 | }), |
| 222 | }; |
| 223 | const result = await Effect.runPromise( |
| 224 | seedPrerenderTo(sink, { |
| 225 | rootDir: "/tmp/vinext-cache-missing", |
| 226 | label: "memory", |
| 227 | }), |
| 228 | ); |
| 229 | expect(result).toEqual({ count: 0, routeCount: 0 }); |
| 230 | expect(written).toEqual([]); |
| 231 | }); |
| 232 | }); |