1import { createResolveLocalPackageImports } from "@effect/bundle/Plugins"
2import { assert, describe, it } from "@effect/vitest"
3import * as Effect from "effect/Effect"
4import type * as EffectPath from "effect/Path"
5import * as fs from "node:fs/promises"
6import * as path from "node:path"
7import { fileURLToPath, pathToFileURL } from "node:url"
8import type { Plugin } from "rollup"
9
10type Resolved = {
11 readonly id: string
12 readonly external: false
13}
14
15type ResolveId = (
16 this: {
17 readonly resolve: (
18 source: string,
19 importer?: string,
20 options?: { readonly skipSelf?: boolean }
21 ) => Promise<Resolved | null>
22 },
23 source: string,
24 importer?: string
25) => Promise<Resolved | null>
26
27const packageDir = fileURLToPath(new URL("../../../effect", import.meta.url))
28const bundleDir = fileURLToPath(new URL("..", import.meta.url))
29const pathService = path as unknown as EffectPath.Path
30
31const getResolveId = (plugin: Plugin): ResolveId => {
32 assert.strictEqual(typeof plugin.resolveId, "function")
33 return plugin.resolveId as ResolveId
34}
35
36const resolved = (id: string): Resolved => ({
37 id,
38 external: false
39})
40
41describe("createResolveLocalPackageImports", () => {
42 it("resolves directory package exports to dist index files", async () => {
43 const resolveId = getResolveId(createResolveLocalPackageImports(pathService))
44 const result = await resolveId.call({
45 resolve: async (source) => {
46 switch (source) {
47 case "effect/package.json":
48 return resolved(path.join(packageDir, "package.json"))
49 case "effect/testing":
50 return resolved(path.join(packageDir, "src", "testing", "index.ts"))
51 default:
52 return null
53 }
54 }
55 }, "effect/testing")
56
57 assert.deepStrictEqual(result, resolved(path.join(packageDir, "dist", "testing", "index.js")))
58 })
59
60 it("keeps flat package exports on flat dist files", async () => {
61 const resolveId = getResolveId(createResolveLocalPackageImports(pathService))
62 const result = await resolveId.call({
63 resolve: async (source) => {
64 switch (source) {
65 case "effect/package.json":
66 return resolved(path.join(packageDir, "package.json"))
67 case "effect/Schema":
68 return resolved(path.join(packageDir, "src", "Schema.ts"))
69 default:
70 return null
71 }
72 }
73 }, "effect/Schema")
74
75 assert.deepStrictEqual(result, resolved(path.join(packageDir, "dist", "Schema.js")))
76 })
77})
78
79describe("Fixtures", () => {
80 it("discovers fixtures when the module path contains spaces", async () => {
81 const root = await fs.mkdtemp(path.join(bundleDir, ".fixtures-test-"))
82 try {
83 const directory = path.join(root, "Effect Work")
84 const modulePath = path.join(directory, "src", "Fixtures.ts")
85 const fixturesDir = path.join(directory, "fixtures")
86 await fs.mkdir(path.dirname(modulePath), { recursive: true })
87 await fs.mkdir(fixturesDir)
88 await fs.copyFile(path.join(bundleDir, "src", "Fixtures.ts"), modulePath)
89 await fs.writeFile(path.join(fixturesDir, "example.ts"), "")
90
91 const { Fixtures } = await import(pathToFileURL(modulePath).href)
92
93 assert.deepStrictEqual(await Effect.runPromise(Fixtures.make), {
94 fixtures: ["example.ts"],
95 fixturesDir: fixturesDir + path.sep
96 })
97 } finally {
98 await fs.rm(root, { recursive: true, force: true })
99 }
100 })
101})