1
-import { describe, it, expect } from 'vitest';
1
+import { describe, it, expect, vi } from 'vitest';
2
import { Effect, Stream, String } from 'effect';
3
import { Command } from '@effect/platform';
4
import ts from 'typescript';
5
+import {
6
+ buildVirtualFileMap,
7
+ formatDiagnostic,
8
+ patchCompilerHostWithVirtualFiles,
9
+} from 'src/generation/typescript/virtual-compiler-host';
10
11
interface AssertTypeScriptIsValidInput {
12
files: {
14
};
15
}
16
17
+const VALIDATION_TYPE_STUBS = /* typescript */ `
18
+ type Record<K extends keyof any, T> = {
19
+ [P in K]: T;
20
+ };
21
+
22
+ declare module "@composio/core" {
23
+ export type TriggerEvent<TPayload> = { payload: TPayload };
24
+ }
25
+`;
26
+
27
/**
28
* Asserts that the provided TypeScript code is syntactically and semantically valid.
29
*/
43
allowImportingTsExtensions: true,
44
} satisfies ts.CompilerOptions;
45
31
- const virtualFileMap = new Map(
32
- Object.entries(files).map(
33
- ([filename, code]) =>
34
- [
35
- filename,
36
- ts.createSourceFile(filename, code, compilerOptions.target, true, ts.ScriptKind.TS),
37
- ] as const
38
- )
46
+ const virtualFileMap = buildVirtualFileMap(
47
+ Object.entries({
48
+ './__validation-stubs.d.ts': VALIDATION_TYPE_STUBS,
49
+ ...files,
50
+ }),
51
+ compilerOptions.target
52
);
53
const virtualFileNames = Array.from(virtualFileMap.keys());
54
55
const tsHost = ts.createCompilerHost(compilerOptions);
43
- tsHost.getSourceFile = (filename, _languageVersion) => {
44
- if (virtualFileMap.has(filename)) {
45
- return virtualFileMap.get(filename);
46
- }
47
-
48
- if (virtualFileMap.has(`./${filename}`)) {
49
- return virtualFileMap.get(filename);
50
- }
51
-
52
- throw new Error(`Unexpected filename ${filename}`);
53
- };
56
+ patchCompilerHostWithVirtualFiles(tsHost, virtualFileMap, 'throw');
57
58
const program = ts.createProgram(virtualFileNames, compilerOptions, tsHost);
59
61
const diagnostics = [...program.getSyntacticDiagnostics(), ...program.getSemanticDiagnostics()];
62
63
// Assert that there are no TypeScript errors
61
- expect(diagnostics).toEqual([]);
64
+ expect(diagnostics.map(formatDiagnostic)).toEqual([]);
65
}
66
67
type AssertTranspiledTypeScriptIsValidInput = {
124
});
125
});
126
127
+ it('[Given] generated code imports Composio core types [Then] no errors are found', () => {
128
+ const code = /* typescript */ `
129
+ import { type TriggerEvent } from '@composio/core';
130
+
131
+ export type Event = TriggerEvent<{ id: string }>;
132
+ `;
133
+
134
+ assertTypeScriptIsValid({ files: { 'index.ts': code } });
135
+ });
136
+
137
it('[Given] invalid TypeScript code [Then] errors are found', () => {
138
const code = /* typescript */ `
139
export const id<T> = (x: T) => T: x;
142
assertTypeScriptIsValid({ files: { 'index.ts': code } });
143
}).toThrowError();
144
});
145
+
146
+ it('[Given] throw fallback [Then] the virtual host does not delegate misses to disk', () => {
147
+ const compilerOptions = {
148
+ target: ts.ScriptTarget.ES2022,
149
+ module: ts.ModuleKind.ES2022,
150
+ } satisfies ts.CompilerOptions;
151
+ const virtualFileMap = buildVirtualFileMap(
152
+ [['index.ts', 'export const ok = true;']],
153
+ compilerOptions.target
154
+ );
155
+ const tsHost = ts.createCompilerHost(compilerOptions);
156
+ const getSourceFile = vi.fn(() => {
157
+ throw new Error('delegated getSourceFile');
158
+ });
159
+ const fileExists = vi.fn(() => {
160
+ throw new Error('delegated fileExists');
161
+ });
162
+
163
+ tsHost.getSourceFile = getSourceFile;
164
+ tsHost.fileExists = fileExists;
165
+ patchCompilerHostWithVirtualFiles(tsHost, virtualFileMap, 'throw');
166
+
167
+ expect(tsHost.fileExists('node_modules/@composio/core/index.d.ts')).toBe(false);
168
+ expect(() =>
169
+ tsHost.getSourceFile('node_modules/@composio/core/index.d.ts', compilerOptions.target)
170
+ ).toThrowError('Unexpected filename node_modules/@composio/core/index.d.ts');
171
+ expect(fileExists).not.toHaveBeenCalled();
172
+ expect(getSourceFile).not.toHaveBeenCalled();
173
+ });
174
});
175
}