| 1 | import { expect, layer } from "@effect/vitest"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as D1 from "../bindings/d1/index.ts"; |
| 4 | import { localRuntimeLayer, startTestWorker } from "./helpers/runtime.ts"; |
| 5 | |
| 6 | layer(localRuntimeLayer)("Module registry defaults", (it) => { |
| 7 | for (const legacy of [false, true]) { |
| 8 | it.effect( |
| 9 | `queries D1 with the ${legacy ? "legacy" : "default"} registry`, |
| 10 | () => |
| 11 | Effect.gen(function* () { |
| 12 | const worker = yield* startTestWorker({ |
| 13 | name: `module-registry-d1-${legacy ? "legacy" : "default"}`, |
| 14 | compatibilityDate: "2026-08-31", |
| 15 | compatibilityFlags: legacy ? ["legacy_module_registry"] : [], |
| 16 | bindings: [ |
| 17 | D1.local({ binding: "DB", id: `module-registry-${legacy}` }), |
| 18 | ], |
| 19 | modules: [ |
| 20 | { |
| 21 | name: "main.js", |
| 22 | type: "ESModule", |
| 23 | content: `export default { |
| 24 | async fetch(request, env) { |
| 25 | const row = await env.DB.prepare('SELECT 42 AS value').first(); |
| 26 | return Response.json({ value: row.value, resolve: typeof import.meta.resolve }); |
| 27 | }, |
| 28 | };`, |
| 29 | }, |
| 30 | ], |
| 31 | }); |
| 32 | expect(yield* worker.fetchJson("/")).toEqual({ |
| 33 | value: 42, |
| 34 | resolve: legacy ? "undefined" : "function", |
| 35 | }); |
| 36 | }), |
| 37 | ); |
| 38 | } |
| 39 | |
| 40 | it.effect("exposes native import.meta without an explicit flag", () => |
| 41 | Effect.gen(function* () { |
| 42 | const worker = yield* startTestWorker({ |
| 43 | name: "module-registry-default", |
| 44 | compatibilityDate: "2026-08-31", |
| 45 | compatibilityFlags: [], |
| 46 | bindings: [], |
| 47 | modules: [ |
| 48 | { |
| 49 | name: "main.js", |
| 50 | type: "ESModule", |
| 51 | content: `export default { |
| 52 | fetch() { |
| 53 | return Response.json({ |
| 54 | main: import.meta.main, |
| 55 | url: import.meta.url, |
| 56 | resolved: import.meta.resolve('./helper.js'), |
| 57 | }); |
| 58 | }, |
| 59 | };`, |
| 60 | }, |
| 61 | ], |
| 62 | }); |
| 63 | expect(yield* worker.fetchJson("/")).toEqual({ |
| 64 | main: true, |
| 65 | url: "file:///bundle/main.js", |
| 66 | resolved: "file:///bundle/helper.js", |
| 67 | }); |
| 68 | }), |
| 69 | ); |
| 70 | }); |