|
| 1 | +import { describe, it, expect, beforeEach, afterEach } from 'vitest'; |
| 2 | +import fs from 'fs'; |
| 3 | +import path from 'path'; |
| 4 | +import os from 'os'; |
| 5 | +import { createFlowsDirectory } from '../../../src/commands/install/create-flows-directory'; |
| 6 | + |
| 7 | +describe('createFlowsDirectory', () => { |
| 8 | + let tempDir: string; |
| 9 | + let supabasePath: string; |
| 10 | + let flowsDir: string; |
| 11 | + |
| 12 | + beforeEach(() => { |
| 13 | + // Create a temporary directory for testing |
| 14 | + tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'pgflow-test-')); |
| 15 | + supabasePath = path.join(tempDir, 'supabase'); |
| 16 | + flowsDir = path.join(supabasePath, 'flows'); |
| 17 | + }); |
| 18 | + |
| 19 | + afterEach(() => { |
| 20 | + // Clean up the temporary directory |
| 21 | + fs.rmSync(tempDir, { recursive: true, force: true }); |
| 22 | + }); |
| 23 | + |
| 24 | + it('should create both files when none exist', async () => { |
| 25 | + const result = await createFlowsDirectory({ |
| 26 | + supabasePath, |
| 27 | + autoConfirm: true, |
| 28 | + }); |
| 29 | + |
| 30 | + // Should return true because files were created |
| 31 | + expect(result).toBe(true); |
| 32 | + |
| 33 | + // Verify directory was created |
| 34 | + expect(fs.existsSync(flowsDir)).toBe(true); |
| 35 | + |
| 36 | + // Verify all files exist |
| 37 | + const indexPath = path.join(flowsDir, 'index.ts'); |
| 38 | + const exampleFlowPath = path.join(flowsDir, 'example_flow.ts'); |
| 39 | + |
| 40 | + expect(fs.existsSync(indexPath)).toBe(true); |
| 41 | + expect(fs.existsSync(exampleFlowPath)).toBe(true); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should create index.ts with barrel export pattern', async () => { |
| 45 | + await createFlowsDirectory({ |
| 46 | + supabasePath, |
| 47 | + autoConfirm: true, |
| 48 | + }); |
| 49 | + |
| 50 | + const indexPath = path.join(flowsDir, 'index.ts'); |
| 51 | + const indexContent = fs.readFileSync(indexPath, 'utf8'); |
| 52 | + |
| 53 | + // Should have export for ExampleFlow |
| 54 | + expect(indexContent).toContain("export { ExampleFlow } from './example_flow.ts'"); |
| 55 | + // Should have documenting comment |
| 56 | + expect(indexContent).toContain('Re-export all flows'); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should create example_flow.ts with named export', async () => { |
| 60 | + await createFlowsDirectory({ |
| 61 | + supabasePath, |
| 62 | + autoConfirm: true, |
| 63 | + }); |
| 64 | + |
| 65 | + const exampleFlowPath = path.join(flowsDir, 'example_flow.ts'); |
| 66 | + const exampleFlowContent = fs.readFileSync(exampleFlowPath, 'utf8'); |
| 67 | + |
| 68 | + // Should use named export (not default) |
| 69 | + expect(exampleFlowContent).toContain('export const ExampleFlow'); |
| 70 | + // Should import Flow from @pgflow/dsl |
| 71 | + expect(exampleFlowContent).toContain("import { Flow } from '@pgflow/dsl'"); |
| 72 | + // Should have correct slug |
| 73 | + expect(exampleFlowContent).toContain("slug: 'example_flow'"); |
| 74 | + // Should have input type |
| 75 | + expect(exampleFlowContent).toContain('type Input'); |
| 76 | + // Should have at least one step |
| 77 | + expect(exampleFlowContent).toContain('.step('); |
| 78 | + }); |
| 79 | + |
| 80 | + it('should not create files when they already exist', async () => { |
| 81 | + // Pre-create the directory and files |
| 82 | + fs.mkdirSync(flowsDir, { recursive: true }); |
| 83 | + |
| 84 | + const indexPath = path.join(flowsDir, 'index.ts'); |
| 85 | + const exampleFlowPath = path.join(flowsDir, 'example_flow.ts'); |
| 86 | + |
| 87 | + fs.writeFileSync(indexPath, '// existing content'); |
| 88 | + fs.writeFileSync(exampleFlowPath, '// existing content'); |
| 89 | + |
| 90 | + const result = await createFlowsDirectory({ |
| 91 | + supabasePath, |
| 92 | + autoConfirm: true, |
| 93 | + }); |
| 94 | + |
| 95 | + // Should return false because no changes were needed |
| 96 | + expect(result).toBe(false); |
| 97 | + |
| 98 | + // Verify files still exist with original content |
| 99 | + expect(fs.readFileSync(indexPath, 'utf8')).toBe('// existing content'); |
| 100 | + expect(fs.readFileSync(exampleFlowPath, 'utf8')).toBe('// existing content'); |
| 101 | + }); |
| 102 | + |
| 103 | + it('should create only missing files when some already exist', async () => { |
| 104 | + // Pre-create the directory and one file |
| 105 | + fs.mkdirSync(flowsDir, { recursive: true }); |
| 106 | + |
| 107 | + const indexPath = path.join(flowsDir, 'index.ts'); |
| 108 | + const exampleFlowPath = path.join(flowsDir, 'example_flow.ts'); |
| 109 | + |
| 110 | + // Only create index.ts |
| 111 | + fs.writeFileSync(indexPath, '// existing content'); |
| 112 | + |
| 113 | + const result = await createFlowsDirectory({ |
| 114 | + supabasePath, |
| 115 | + autoConfirm: true, |
| 116 | + }); |
| 117 | + |
| 118 | + // Should return true because example_flow.ts was created |
| 119 | + expect(result).toBe(true); |
| 120 | + |
| 121 | + // Verify index.ts was not modified |
| 122 | + expect(fs.readFileSync(indexPath, 'utf8')).toBe('// existing content'); |
| 123 | + |
| 124 | + // Verify example_flow.ts was created |
| 125 | + expect(fs.existsSync(exampleFlowPath)).toBe(true); |
| 126 | + |
| 127 | + const exampleContent = fs.readFileSync(exampleFlowPath, 'utf8'); |
| 128 | + expect(exampleContent).toContain('export const ExampleFlow'); |
| 129 | + }); |
| 130 | + |
| 131 | + it('should create parent directories if they do not exist', async () => { |
| 132 | + // Don't create anything - let the function create it all |
| 133 | + expect(fs.existsSync(supabasePath)).toBe(false); |
| 134 | + |
| 135 | + const result = await createFlowsDirectory({ |
| 136 | + supabasePath, |
| 137 | + autoConfirm: true, |
| 138 | + }); |
| 139 | + |
| 140 | + expect(result).toBe(true); |
| 141 | + |
| 142 | + // Verify all parent directories were created |
| 143 | + expect(fs.existsSync(supabasePath)).toBe(true); |
| 144 | + expect(fs.existsSync(flowsDir)).toBe(true); |
| 145 | + |
| 146 | + // Verify files exist |
| 147 | + expect(fs.existsSync(path.join(flowsDir, 'index.ts'))).toBe(true); |
| 148 | + expect(fs.existsSync(path.join(flowsDir, 'example_flow.ts'))).toBe(true); |
| 149 | + }); |
| 150 | +}); |
0 commit comments