-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathcreate-flows-directory.test.ts
More file actions
168 lines (132 loc) · 5.6 KB
/
Copy pathcreate-flows-directory.test.ts
File metadata and controls
168 lines (132 loc) · 5.6 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
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 greetUserPath = path.join(flowsDir, 'greet-user.ts');
expect(fs.existsSync(indexPath)).toBe(true);
expect(fs.existsSync(greetUserPath)).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 GreetUser
expect(indexContent).toContain("export { GreetUser } from './greet-user.ts';");
// Should have documenting comment
expect(indexContent).toContain('Re-export all flows');
});
it('should create greet-user.ts with named export', async () => {
await createFlowsDirectory({
supabasePath,
autoConfirm: true,
});
const greetUserPath = path.join(flowsDir, 'greet-user.ts');
const greetUserContent = fs.readFileSync(greetUserPath, 'utf8');
// Should use named export (not default)
expect(greetUserContent).toContain('export const GreetUser');
// Should import Flow from @pgflow/dsl
expect(greetUserContent).toContain("import { Flow } from '@pgflow/dsl'");
// Should have correct slug
expect(greetUserContent).toContain("slug: 'greetUser'");
// Should have input type with firstName and lastName
expect(greetUserContent).toContain('type Input');
expect(greetUserContent).toContain('firstName');
expect(greetUserContent).toContain('lastName');
});
it('should create greet-user.ts with two steps showing dependsOn', async () => {
await createFlowsDirectory({
supabasePath,
autoConfirm: true,
});
const greetUserPath = path.join(flowsDir, 'greet-user.ts');
const greetUserContent = fs.readFileSync(greetUserPath, 'utf8');
// Should have two steps
expect(greetUserContent).toContain("slug: 'fullName'");
expect(greetUserContent).toContain("slug: 'greeting'");
// Second step should depend on first
expect(greetUserContent).toContain("dependsOn: ['fullName']");
// Second step should access result from first step
expect(greetUserContent).toContain('input.fullName');
});
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 greetUserPath = path.join(flowsDir, 'greet-user.ts');
fs.writeFileSync(indexPath, '// existing content');
fs.writeFileSync(greetUserPath, '// 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(greetUserPath, '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 greetUserPath = path.join(flowsDir, 'greet-user.ts');
// Only create index.ts
fs.writeFileSync(indexPath, '// existing content');
const result = await createFlowsDirectory({
supabasePath,
autoConfirm: true,
});
// Should return true because greet-user.ts was created
expect(result).toBe(true);
// Verify index.ts was not modified
expect(fs.readFileSync(indexPath, 'utf8')).toBe('// existing content');
// Verify greet-user.ts was created
expect(fs.existsSync(greetUserPath)).toBe(true);
const greetUserContent = fs.readFileSync(greetUserPath, 'utf8');
expect(greetUserContent).toContain('export const GreetUser');
});
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, 'greet-user.ts'))).toBe(true);
});
});