-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathcreate-flows-directory.test.ts
More file actions
150 lines (117 loc) · 4.94 KB
/
create-flows-directory.test.ts
File metadata and controls
150 lines (117 loc) · 4.94 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
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import fs from 'fs';
import path from 'path';
import os from 'os';
import { createFlowsDirectory } from '../../../src/commands/install/create-flows-directory';
describe('createFlowsDirectory', () => {
let tempDir: string;
let supabasePath: string;
let flowsDir: string;
beforeEach(() => {
// Create a temporary directory for testing
tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'pgflow-test-'));
supabasePath = path.join(tempDir, 'supabase');
flowsDir = path.join(supabasePath, 'flows');
});
afterEach(() => {
// Clean up the temporary directory
fs.rmSync(tempDir, { recursive: true, force: true });
});
it('should create both files when none exist', async () => {
const result = await createFlowsDirectory({
supabasePath,
autoConfirm: true,
});
// Should return true because files were created
expect(result).toBe(true);
// Verify directory was created
expect(fs.existsSync(flowsDir)).toBe(true);
// Verify all files exist
const indexPath = path.join(flowsDir, 'index.ts');
const exampleFlowPath = path.join(flowsDir, 'example-flow.ts');
expect(fs.existsSync(indexPath)).toBe(true);
expect(fs.existsSync(exampleFlowPath)).toBe(true);
});
it('should create index.ts with barrel export pattern', async () => {
await createFlowsDirectory({
supabasePath,
autoConfirm: true,
});
const indexPath = path.join(flowsDir, 'index.ts');
const indexContent = fs.readFileSync(indexPath, 'utf8');
// Should have export for ExampleFlow
expect(indexContent).toContain("export { ExampleFlow } from './example-flow.ts';");
// Should have documenting comment
expect(indexContent).toContain('Re-export all flows');
});
it('should create example-flow.ts with named export', async () => {
await createFlowsDirectory({
supabasePath,
autoConfirm: true,
});
const exampleFlowPath = path.join(flowsDir, 'example-flow.ts');
const exampleFlowContent = fs.readFileSync(exampleFlowPath, 'utf8');
// Should use named export (not default)
expect(exampleFlowContent).toContain('export const ExampleFlow');
// Should import Flow from @pgflow/dsl
expect(exampleFlowContent).toContain("import { Flow } from '@pgflow/dsl'");
// Should have correct slug
expect(exampleFlowContent).toContain("slug: 'exampleFlow'");
// Should have input type
expect(exampleFlowContent).toContain('type Input');
// Should have at least one step
expect(exampleFlowContent).toContain('.step(');
});
it('should not create files when they already exist', async () => {
// Pre-create the directory and files
fs.mkdirSync(flowsDir, { recursive: true });
const indexPath = path.join(flowsDir, 'index.ts');
const exampleFlowPath = path.join(flowsDir, 'example-flow.ts');
fs.writeFileSync(indexPath, '// existing content');
fs.writeFileSync(exampleFlowPath, '// existing content');
const result = await createFlowsDirectory({
supabasePath,
autoConfirm: true,
});
// Should return false because no changes were needed
expect(result).toBe(false);
// Verify files still exist with original content
expect(fs.readFileSync(indexPath, 'utf8')).toBe('// existing content');
expect(fs.readFileSync(exampleFlowPath, 'utf8')).toBe('// existing content');
});
it('should create only missing files when some already exist', async () => {
// Pre-create the directory and one file
fs.mkdirSync(flowsDir, { recursive: true });
const indexPath = path.join(flowsDir, 'index.ts');
const exampleFlowPath = path.join(flowsDir, 'example-flow.ts');
// Only create index.ts
fs.writeFileSync(indexPath, '// existing content');
const result = await createFlowsDirectory({
supabasePath,
autoConfirm: true,
});
// Should return true because example-flow.ts was created
expect(result).toBe(true);
// Verify index.ts was not modified
expect(fs.readFileSync(indexPath, 'utf8')).toBe('// existing content');
// Verify example-flow.ts was created
expect(fs.existsSync(exampleFlowPath)).toBe(true);
const exampleContent = fs.readFileSync(exampleFlowPath, 'utf8');
expect(exampleContent).toContain('export const ExampleFlow');
});
it('should create parent directories if they do not exist', async () => {
// Don't create anything - let the function create it all
expect(fs.existsSync(supabasePath)).toBe(false);
const result = await createFlowsDirectory({
supabasePath,
autoConfirm: true,
});
expect(result).toBe(true);
// Verify all parent directories were created
expect(fs.existsSync(supabasePath)).toBe(true);
expect(fs.existsSync(flowsDir)).toBe(true);
// Verify files exist
expect(fs.existsSync(path.join(flowsDir, 'index.ts'))).toBe(true);
expect(fs.existsSync(path.join(flowsDir, 'example-flow.ts'))).toBe(true);
});
});