-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgenerate.test.ts
More file actions
203 lines (166 loc) · 6.73 KB
/
generate.test.ts
File metadata and controls
203 lines (166 loc) · 6.73 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
// biome-ignore assist/source/organizeImports: vi must be imported before memfs and memfs must be imported before readFile
import { beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';
import { fs as memFs, vol } from 'memfs';
import { readFile } from 'node:fs/promises';
import { join, resolve } from 'node:path';
import { generate } from '../src/core/generate/index.ts';
import { testWasmInit } from './utils/wasm.ts';
vi.mock('node:fs/promises', () => ({
default: memFs.promises,
...memFs.promises,
}));
// We need to mock the version to avoid updating snapshots on every release
vi.mock('../src/core/constants.ts', async () => {
const constants = await vi.importActual('../src/core/constants.ts');
return {
...constants,
PACKAGE_VERSION: 'TEST-VERSION',
};
});
const TESTS_ASSETS_DIR = './tests/assets';
const OUTPUT_DIR = 'output';
const SNAPSHOTS_DIR = './snapshots/generate';
beforeAll(async () => {
await testWasmInit();
});
beforeEach(() => {
vol.reset();
});
describe('generate', () => {
it.each(['hello_world', 'example'])('should generate a bindgen', async (serviceName) => {
const didFile = `${TESTS_ASSETS_DIR}/${serviceName}.did`;
await generate({ didFile, outDir: OUTPUT_DIR });
await expectGeneratedOutput(SNAPSHOTS_DIR, serviceName);
expect(fileExists(`${OUTPUT_DIR}/${serviceName}/${serviceName}.d.ts`)).toBe(false);
});
it.each(['hello_world', 'example'])(
'should generate a bindgen with interface declaration',
async (serviceName) => {
const didFile = `${TESTS_ASSETS_DIR}/${serviceName}.did`;
await generate({
didFile,
outDir: OUTPUT_DIR,
output: { actor: { interfaceFile: true } },
});
await expectGeneratedOutput(SNAPSHOTS_DIR, serviceName);
const interfaceTs = await readFileFromOutput(`${serviceName}.d.ts`);
await expect(interfaceTs).toMatchFileSnapshot(
`${SNAPSHOTS_DIR}/${serviceName}/${serviceName}.d.ts.snapshot`,
);
},
);
it.each(['hello_world', 'example'])(
'should generate a bindgen with declarations only',
async (serviceName) => {
const didFile = `${TESTS_ASSETS_DIR}/${serviceName}.did`;
await generate({
didFile,
outDir: OUTPUT_DIR,
output: { actor: { disabled: true } },
});
await expectGeneratedDeclarations(SNAPSHOTS_DIR, serviceName);
expect(fileExists(`${OUTPUT_DIR}/${serviceName}/${serviceName}.d.ts`)).toBe(false);
expect(fileExists(`${OUTPUT_DIR}/${serviceName}/${serviceName}.ts`)).toBe(false);
},
);
it.each(['hello_world', 'example'])(
'should ignore other options when generating a bindgen with actor disabled',
async (serviceName) => {
const didFile = `${TESTS_ASSETS_DIR}/${serviceName}.did`;
await generate({
didFile,
outDir: OUTPUT_DIR,
output: {
actor: {
disabled: true,
// @ts-expect-error - the interface does not allow this, but we want to test that it is ignored at runtime
interfaceFile: true,
},
},
});
await expectGeneratedDeclarations(SNAPSHOTS_DIR, serviceName);
expect(fileExists(`${OUTPUT_DIR}/${serviceName}/${serviceName}.d.ts`)).toBe(false);
expect(fileExists(`${OUTPUT_DIR}/${serviceName}/${serviceName}.ts`)).toBe(false);
},
);
it('should preserve the .did file', async () => {
const { readFile: realReadFile } =
await vi.importActual<typeof import('node:fs/promises')>('node:fs/promises');
const serviceName = 'hello_world';
const didFile = `${TESTS_ASSETS_DIR}/${serviceName}.did`;
const originalDidFileContent = await realReadFile(didFile, 'utf-8');
// We simulate a .did file in the output directory
const didFilePath = `${OUTPUT_DIR}/${serviceName}.did`;
vol.mkdirSync(OUTPUT_DIR, { recursive: true });
vol.writeFileSync(didFilePath, originalDidFileContent, { encoding: 'utf-8' });
// We also add another (unused) .did file to check that all of them are preserved
const otherDidFilePath = `${OUTPUT_DIR}/other.did`;
vol.writeFileSync(otherDidFilePath, 'other', { encoding: 'utf-8' });
// Assert before and after the generation
expect(fileExists(didFilePath)).toBe(true);
expect(fileExists(otherDidFilePath)).toBe(true);
await generate({
// We must use the file that exists in the real filesystem
// because wasm reads from the real filesystem
didFile,
outDir: OUTPUT_DIR,
});
expect(fileExists(didFilePath)).toBe(true);
expect(fileExists(otherDidFilePath)).toBe(true);
});
it('should abort on existing files unless output.force is true', async () => {
const serviceName = 'hello_world';
const didFile = `${TESTS_ASSETS_DIR}/${serviceName}.did`;
// Pre-create a conflicting declarations file
vol.mkdirSync(`${OUTPUT_DIR}/declarations`, { recursive: true });
vol.writeFileSync(`${OUTPUT_DIR}/declarations/${serviceName}.did.d.ts`, '// existing', {
encoding: 'utf-8',
});
await expect(
generate({
didFile,
outDir: OUTPUT_DIR,
}),
).rejects.toThrow(
new RegExp(
`The generated file already exists: .*declarations\\/${serviceName}\\.did\\.d\\.ts. To overwrite it, use the \`force\` option.`,
'i',
),
);
// With force, it should overwrite and succeed
await expect(
generate({
didFile,
outDir: OUTPUT_DIR,
output: { force: true },
}),
).resolves.toBeUndefined();
});
});
async function readFileFromOutput(path: string): Promise<string> {
return await readFile(resolve(OUTPUT_DIR, path), 'utf-8');
}
function fileExists(path: string): boolean {
return vol.existsSync(path);
}
async function expectGeneratedDeclarations(
snapshotsDir: string,
serviceName: string,
): Promise<void> {
const generatedOutputDir = join(snapshotsDir, serviceName);
const generatedOutputDeclarationsDir = join(generatedOutputDir, 'declarations');
const declarationsJs = await readFileFromOutput(`declarations/${serviceName}.did.js`);
await expect(declarationsJs).toMatchFileSnapshot(
`${generatedOutputDeclarationsDir}/${serviceName}.did.js.snapshot`,
);
const declarationsTs = await readFileFromOutput(`declarations/${serviceName}.did.d.ts`);
await expect(declarationsTs).toMatchFileSnapshot(
`${generatedOutputDeclarationsDir}/${serviceName}.did.d.ts.snapshot`,
);
}
async function expectGeneratedOutput(snapshotsDir: string, serviceName: string): Promise<void> {
const generatedOutputDir = join(snapshotsDir, serviceName);
await expectGeneratedDeclarations(snapshotsDir, serviceName);
const serviceTs = await readFileFromOutput(`${serviceName}.ts`);
await expect(serviceTs).toMatchFileSnapshot(`${generatedOutputDir}/${serviceName}.ts.snapshot`);
}