-
Notifications
You must be signed in to change notification settings - Fork 269
Expand file tree
/
Copy pathmodule-specifier.test.ts
More file actions
263 lines (213 loc) · 7.68 KB
/
module-specifier.test.ts
File metadata and controls
263 lines (213 loc) · 7.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import { mkdtempSync, mkdirSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { dirname, join } from 'node:path';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import {
clearModuleSpecifierCache,
getImportPath,
} from './module-specifier.js';
function writeJson(path: string, value: unknown): void {
mkdirSync(dirname(path), { recursive: true });
writeFileSync(path, JSON.stringify(value, null, 2), 'utf-8');
}
function writeFile(path: string, contents = ''): void {
mkdirSync(dirname(path), { recursive: true });
writeFileSync(path, contents, 'utf-8');
}
describe('getImportPath', () => {
let testRoot: string;
beforeEach(() => {
clearModuleSpecifierCache();
testRoot = mkdtempSync(join(tmpdir(), 'workflow-module-specifier-'));
});
afterEach(() => {
clearModuleSpecifierCache();
rmSync(testRoot, { recursive: true, force: true });
});
it('uses package subpath import when file matches an export subpath', () => {
const projectRoot = join(testRoot, 'apps/chat');
const workspacePkgDir = join(testRoot, 'packages/agent');
const filePath = join(workspacePkgDir, 'src/server.ts');
writeJson(join(projectRoot, 'package.json'), {
name: 'chat',
dependencies: { '@internal/agent': 'workspace:*' },
});
writeJson(join(workspacePkgDir, 'package.json'), {
name: '@internal/agent',
version: '1.0.0',
exports: {
'./server': './src/server.ts',
},
});
writeFile(filePath, `'use step';\n`);
expect(getImportPath(filePath, projectRoot)).toEqual({
importPath: '@internal/agent/server',
isPackage: true,
});
});
it('falls back to relative import when package has no root export', () => {
const projectRoot = join(testRoot, 'apps/chat');
const workspacePkgDir = join(testRoot, 'packages/agent');
const filePath = join(workspacePkgDir, 'src/server.ts');
writeJson(join(projectRoot, 'package.json'), {
name: 'chat',
dependencies: { '@internal/agent': 'workspace:*' },
});
writeJson(join(workspacePkgDir, 'package.json'), {
name: '@internal/agent',
version: '1.0.0',
exports: {
'./server': './dist/server.js',
},
});
writeFile(filePath, `'use step';\n`);
expect(getImportPath(filePath, projectRoot)).toEqual({
importPath: '../../packages/agent/src/server.ts',
isPackage: false,
});
});
it('uses package root import for root exports', () => {
const projectRoot = join(testRoot, 'apps/chat');
const workspacePkgDir = join(testRoot, 'packages/agent');
const filePath = join(workspacePkgDir, 'src/index.ts');
writeJson(join(projectRoot, 'package.json'), {
name: 'chat',
dependencies: { '@internal/agent': 'workspace:*' },
});
writeJson(join(workspacePkgDir, 'package.json'), {
name: '@internal/agent',
version: '1.0.0',
exports: {
'.': './src/index.ts',
},
});
writeFile(filePath, `'use workflow';\n`);
expect(getImportPath(filePath, projectRoot)).toEqual({
importPath: '@internal/agent',
isPackage: true,
});
});
it('uses package root import when package module points to file', () => {
const projectRoot = join(testRoot, 'apps/chat');
const workspacePkgDir = join(testRoot, 'packages/agent');
const filePath = join(workspacePkgDir, 'src/index.mjs');
writeJson(join(projectRoot, 'package.json'), {
name: 'chat',
dependencies: { '@internal/agent': 'workspace:*' },
});
writeJson(join(workspacePkgDir, 'package.json'), {
name: '@internal/agent',
version: '1.0.0',
module: './src/index.mjs',
main: './dist/index.cjs',
});
writeFile(filePath, `'use workflow';\n`);
expect(getImportPath(filePath, projectRoot)).toEqual({
importPath: '@internal/agent',
isPackage: true,
});
});
it('uses package root import for conditional root exports', () => {
const projectRoot = join(testRoot, 'apps/chat');
const workspacePkgDir = join(testRoot, 'packages/agent');
const filePath = join(workspacePkgDir, 'src/index.js');
writeJson(join(projectRoot, 'package.json'), {
name: 'chat',
dependencies: { '@internal/agent': 'workspace:*' },
});
writeJson(join(workspacePkgDir, 'package.json'), {
name: '@internal/agent',
version: '1.0.0',
exports: {
'.': {
import: './src/index.mjs',
default: './src/index.js',
},
},
});
writeFile(filePath, `'use workflow';\n`);
expect(getImportPath(filePath, projectRoot)).toEqual({
importPath: '@internal/agent',
isPackage: true,
});
});
it('falls back to relative import for deep files in packages without exports', () => {
const projectRoot = join(testRoot, 'apps/chat');
const workspacePkgDir = join(testRoot, 'packages/agent');
const filePath = join(workspacePkgDir, 'lib/tools/dynamic/workflow.ts');
writeJson(join(projectRoot, 'package.json'), {
name: 'chat',
dependencies: { '@internal/agent': 'workspace:*' },
});
writeJson(join(workspacePkgDir, 'package.json'), {
name: '@internal/agent',
version: '1.0.0',
});
writeFile(filePath, `'use workflow';\n`);
expect(getImportPath(filePath, projectRoot)).toEqual({
importPath: '../../packages/agent/lib/tools/dynamic/workflow.ts',
isPackage: false,
});
});
it('uses package root import when package main points to file', () => {
const projectRoot = join(testRoot, 'apps/chat');
const workspacePkgDir = join(testRoot, 'packages/agent');
const filePath = join(workspacePkgDir, 'src/index.ts');
writeJson(join(projectRoot, 'package.json'), {
name: 'chat',
dependencies: { '@internal/agent': 'workspace:*' },
});
writeJson(join(workspacePkgDir, 'package.json'), {
name: '@internal/agent',
version: '1.0.0',
main: './src/index.ts',
});
writeFile(filePath, `'use step';\n`);
expect(getImportPath(filePath, projectRoot)).toEqual({
importPath: '@internal/agent',
isPackage: true,
});
});
it('uses package subpath import for direct node_modules dependencies', () => {
const projectRoot = join(testRoot, 'apps/chat');
const packageDir = join(testRoot, 'apps/chat/node_modules/@workflow/core');
const filePath = join(packageDir, 'dist/serialization.js');
writeJson(join(projectRoot, 'package.json'), {
name: 'chat',
dependencies: { '@workflow/core': '1.0.0' },
});
writeJson(join(packageDir, 'package.json'), {
name: '@workflow/core',
version: '1.0.0',
exports: {
'./serialization': './dist/serialization.js',
},
});
writeFile(filePath, `'use workflow';\n`);
expect(getImportPath(filePath, projectRoot)).toEqual({
importPath: '@workflow/core/serialization',
isPackage: true,
});
});
it('falls back to relative import for transitive node_modules dependencies', () => {
const projectRoot = join(testRoot, 'apps/chat');
const packageDir = join(testRoot, 'apps/chat/node_modules/@workflow/core');
const filePath = join(packageDir, 'dist/serialization.js');
writeJson(join(projectRoot, 'package.json'), {
name: 'chat',
dependencies: { workflow: '1.0.0' },
});
writeJson(join(packageDir, 'package.json'), {
name: '@workflow/core',
version: '1.0.0',
exports: {
'./serialization': './dist/serialization.js',
},
});
writeFile(filePath, `'use workflow';\n`);
expect(getImportPath(filePath, projectRoot)).toEqual({
importPath: './node_modules/@workflow/core/dist/serialization.js',
isPackage: false,
});
});
});