-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprojectBuilder.test.ts
More file actions
86 lines (71 loc) · 2.9 KB
/
Copy pathprojectBuilder.test.ts
File metadata and controls
86 lines (71 loc) · 2.9 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
import { describe, expect, it } from 'vitest';
import { readFile, rm } from 'node:fs/promises';
import { join } from 'node:path';
import { tmpdir } from 'node:os';
import type { GeneratorOptions } from '../interface.js';
import { NodeProjectBuilder } from './projectBuilder.js';
import type { BuildStep } from './projectBuilder.js';
const options: GeneratorOptions = {
projectName: 'test-app',
database: 'postgres',
webhooks: false,
appExtensions: [],
};
const tmpDir = join(tmpdir(), 'cpa-project-builder-test');
const read = (p: string) => readFile(join(tmpDir, p), 'utf-8');
function spyStep(tracker: string[], label: string): BuildStep {
return {
execute: async () => {
tracker.push(label);
},
};
}
describe('NodeProjectBuilder', () => {
it('when(true) executes the added step', async () => {
const executed: string[] = [];
await new NodeProjectBuilder('/tmp', options)
.when(true, (b) => b.addStep(spyStep(executed, 'webhooks')))
.build();
expect(executed).toContain('webhooks');
});
it('when(false) skips the step', async () => {
const executed: string[] = [];
await new NodeProjectBuilder('/tmp', options)
.when(false, (b) => b.addStep(spyStep(executed, 'webhooks')))
.build();
expect(executed).toHaveLength(0);
});
it('executes steps in insertion order', async () => {
const order: string[] = [];
await new NodeProjectBuilder('/tmp', options)
.addStep(spyStep(order, 'first'))
.addStep(spyStep(order, 'second'))
.addStep(spyStep(order, 'third'))
.build();
expect(order).toEqual(['first', 'second', 'third']);
});
it('addStep and when return the builder instance for chaining', () => {
const builder = new NodeProjectBuilder('/tmp', options);
expect(builder.addStep(spyStep([], 'x'))).toBe(builder);
expect(builder.when(false, () => {})).toBe(builder);
});
it('generates MySQL env example with the non-default host port', async () => {
await rm(tmpDir, { recursive: true, force: true });
await new NodeProjectBuilder(tmpDir, { ...options, database: 'mysql' }).addEnvExample().build();
const content = await read('.env.example');
expect(content).toContain('DATABASE_URL=mysql://app:app@localhost:3307/test-app');
expect(content).not.toContain('DATABASE_URL=mysql://app:app@localhost:3306/test-app');
await rm(tmpDir, { recursive: true, force: true });
});
it('generates server entry that retries database startup', async () => {
await rm(tmpDir, { recursive: true, force: true });
await new NodeProjectBuilder(tmpDir, options).addServerEntry().build();
const content = await read('src/index.ts');
expect(content).toContain('STARTUP_RETRY_ATTEMPTS = 60');
expect(content).toContain('STARTUP_RETRY_DELAY_MS = 1000');
expect(content).toContain('async function waitForDatabase()');
expect(content).toContain('await waitForDatabase()');
expect(content).toContain('Database is not ready yet');
await rm(tmpDir, { recursive: true, force: true });
});
});