The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 1 | import * as NodeFs from "node:fs/promises"; |
| 2 | import * as NodePath from "node:path"; |
| 3 | import { fileURLToPath } from "node:url"; |
| 4 | import { describe, expect, it } from "vitest"; |
| 5 | import { MODULE_RULES } from "../../../rolldown/plugins/additional-modules.ts"; |
| 6 | import { CLOUDFLARE_BUILT_IN_MODULES } from "../../../rolldown/plugins/cloudflare-externals.ts"; |
| 7 | import { |
| 8 | DURABLE_OBJECT_KEYS, |
| 9 | IGNORED_KEYS, |
| 10 | WORKER_ENTRYPOINT_KEYS, |
| 11 | WORKFLOW_ENTRYPOINT_KEYS, |
| 12 | } from "../../module-runner/keys.shared.ts"; |
| 13 | import { SANDBOX_ORIGIN_REGEXP } from "../../websockets.ts"; |
| 14 | import { AHEAD_OF_RELEASE_BUILT_IN_MODULES } from "./ahead-of-release.ts"; |
| 15 | |
| 16 | // The runtime pieces ported from `@cloudflare/vite-plugin`, checked against |
| 17 | // the installed release: a new handler key, module rule or built-in module |
| 18 | // upstream fails here instead of in an app. |
| 19 | |
| 20 | // The package exports only its ESM entry, which lives in `dist/`. |
| 21 | const pluginDist = NodePath.dirname(fileURLToPath(import.meta.resolve("@cloudflare/vite-plugin"))); |
| 22 | |
| 23 | const read = (file: string) => NodeFs.readFile(NodePath.join(pluginDist, file), "utf8"); |
| 24 | |
| 25 | /** The string elements of the array literal assigned to `name`. */ |
| 26 | const arrayLiteral = (source: string, name: string): Array<string> => { |
| 27 | const match = source.match(new RegExp(`const ${name} = \\[([^\\]]*)\\]`)); |
| 28 | if (!match) throw new Error(`\`${name}\` not found in @cloudflare/vite-plugin`); |
| 29 | return [...match[1]!.matchAll(/"([^"]*)"/g)].map((element) => element[1]!); |
| 30 | }; |
| 31 | |
| 32 | describe("ported constants match @cloudflare/vite-plugin", () => { |
| 33 | it("forwards the same handlers on each export type", async () => { |
| 34 | const runner = await read("workers/runner-worker/index.js"); |
| 35 | expect([...IGNORED_KEYS]).toEqual(arrayLiteral(runner, "IGNORED_KEYS")); |
| 36 | expect([...WORKER_ENTRYPOINT_KEYS]).toEqual(arrayLiteral(runner, "WORKER_ENTRYPOINT_KEYS")); |
| 37 | expect([...DURABLE_OBJECT_KEYS]).toEqual(arrayLiteral(runner, "DURABLE_OBJECT_KEYS")); |
| 38 | expect([...WORKFLOW_ENTRYPOINT_KEYS]).toEqual(arrayLiteral(runner, "WORKFLOW_ENTRYPOINT_KEYS")); |
| 39 | }); |
| 40 | |
| 41 | it("treats the same modules as runtime built-ins", async () => { |
| 42 | const plugin = await read("index.mjs"); |
| 43 | const upstream = arrayLiteral(plugin, "cloudflareBuiltInModules"); |
| 44 | // Built-ins upstream has added on `main` after the installed release. |
| 45 | const ahead = AHEAD_OF_RELEASE_BUILT_IN_MODULES.filter((id) => !upstream.includes(id)); |
| 46 | expect([...CLOUDFLARE_BUILT_IN_MODULES].sort()).toEqual([...upstream, ...ahead].sort()); |
| 47 | }); |
| 48 | |
| 49 | it("imports the same additional module types", async () => { |
| 50 | const plugin = await read("index.mjs"); |
| 51 | const block = plugin.match(/const moduleRules = \[([\s\S]*?)\n\];/)?.[1]; |
| 52 | expect(block).toBeDefined(); |
| 53 | const rules = [...block!.matchAll(/type: "(\w+)",\s*pattern: \/(.*?)\/\s*\n/g)].map( |
| 54 | ([, type, pattern]) => ({ type, pattern }), |
| 55 | ); |
| 56 | expect(MODULE_RULES.map((rule) => ({ type: rule.type, pattern: rule.pattern.source }))).toEqual( |
| 57 | rules, |
| 58 | ); |
| 59 | }); |
| 60 | |
| 61 | it("recognizes the same Sandbox preview origins", async () => { |
| 62 | const plugin = await read("index.mjs"); |
| 63 | const source = plugin.match(/const SANDBOX_ORIGIN_REGEXP = \/(.*)\/(\w*);/); |
| 64 | expect(source).not.toBeNull(); |
| 65 | expect(SANDBOX_ORIGIN_REGEXP.source).toBe(source![1]); |
| 66 | expect(SANDBOX_ORIGIN_REGEXP.flags).toBe(source![2]); |
| 67 | }); |
| 68 | }); |