|
| 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 { createExampleWorker } from '../../../src/commands/install/create-example-worker'; |
| 6 | +import { getVersion } from '../../../src/utils/get-version'; |
| 7 | + |
| 8 | +describe('createExampleWorker', () => { |
| 9 | + let tempDir: string; |
| 10 | + let supabasePath: string; |
| 11 | + let workerDir: string; |
| 12 | + |
| 13 | + beforeEach(() => { |
| 14 | + // Create a temporary directory for testing |
| 15 | + tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'pgflow-test-')); |
| 16 | + supabasePath = path.join(tempDir, 'supabase'); |
| 17 | + workerDir = path.join(supabasePath, 'functions', 'greet-user-worker'); |
| 18 | + }); |
| 19 | + |
| 20 | + afterEach(() => { |
| 21 | + // Clean up the temporary directory |
| 22 | + fs.rmSync(tempDir, { recursive: true, force: true }); |
| 23 | + }); |
| 24 | + |
| 25 | + it('should create both files when none exist', async () => { |
| 26 | + const result = await createExampleWorker({ |
| 27 | + supabasePath, |
| 28 | + autoConfirm: true, |
| 29 | + }); |
| 30 | + |
| 31 | + // Should return true because files were created |
| 32 | + expect(result).toBe(true); |
| 33 | + |
| 34 | + // Verify directory was created |
| 35 | + expect(fs.existsSync(workerDir)).toBe(true); |
| 36 | + |
| 37 | + // Verify all files exist |
| 38 | + const indexPath = path.join(workerDir, 'index.ts'); |
| 39 | + const denoJsonPath = path.join(workerDir, 'deno.json'); |
| 40 | + |
| 41 | + expect(fs.existsSync(indexPath)).toBe(true); |
| 42 | + expect(fs.existsSync(denoJsonPath)).toBe(true); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should create index.ts that imports GreetUser and starts EdgeWorker', async () => { |
| 46 | + await createExampleWorker({ |
| 47 | + supabasePath, |
| 48 | + autoConfirm: true, |
| 49 | + }); |
| 50 | + |
| 51 | + const indexPath = path.join(workerDir, 'index.ts'); |
| 52 | + const indexContent = fs.readFileSync(indexPath, 'utf8'); |
| 53 | + |
| 54 | + // Should import EdgeWorker |
| 55 | + expect(indexContent).toContain("import { EdgeWorker } from '@pgflow/edge-worker'"); |
| 56 | + // Should import GreetUser from flows directory |
| 57 | + expect(indexContent).toContain("import { GreetUser } from '../../flows/greet-user.ts'"); |
| 58 | + // Should start EdgeWorker with GreetUser |
| 59 | + expect(indexContent).toContain('EdgeWorker.start(GreetUser)'); |
| 60 | + }); |
| 61 | + |
| 62 | + it('should create deno.json with correct import mappings', async () => { |
| 63 | + await createExampleWorker({ |
| 64 | + supabasePath, |
| 65 | + autoConfirm: true, |
| 66 | + }); |
| 67 | + |
| 68 | + const denoJsonPath = path.join(workerDir, 'deno.json'); |
| 69 | + const denoJsonContent = fs.readFileSync(denoJsonPath, 'utf8'); |
| 70 | + const denoJson = JSON.parse(denoJsonContent); |
| 71 | + |
| 72 | + // Verify imports exist |
| 73 | + expect(denoJson.imports).toBeDefined(); |
| 74 | + expect(denoJson.imports['@pgflow/core']).toBeDefined(); |
| 75 | + expect(denoJson.imports['@pgflow/dsl']).toBeDefined(); |
| 76 | + expect(denoJson.imports['@pgflow/edge-worker']).toBeDefined(); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should inject package version instead of @latest in deno.json', async () => { |
| 80 | + await createExampleWorker({ |
| 81 | + supabasePath, |
| 82 | + autoConfirm: true, |
| 83 | + }); |
| 84 | + |
| 85 | + const denoJsonPath = path.join(workerDir, 'deno.json'); |
| 86 | + const denoJsonContent = fs.readFileSync(denoJsonPath, 'utf8'); |
| 87 | + const denoJson = JSON.parse(denoJsonContent); |
| 88 | + |
| 89 | + const version = getVersion(); |
| 90 | + |
| 91 | + // Verify version is not 'unknown' |
| 92 | + expect(version).not.toBe('unknown'); |
| 93 | + |
| 94 | + // Verify that @latest is NOT used |
| 95 | + expect(denoJsonContent).not.toContain('@latest'); |
| 96 | + |
| 97 | + // Verify that the actual version is used |
| 98 | + expect(denoJson.imports['@pgflow/core']).toBe(`npm:@pgflow/core@${version}`); |
| 99 | + expect(denoJson.imports['@pgflow/dsl']).toBe(`npm:@pgflow/dsl@${version}`); |
| 100 | + expect(denoJson.imports['@pgflow/edge-worker']).toBe(`jsr:@pgflow/edge-worker@${version}`); |
| 101 | + }); |
| 102 | + |
| 103 | + it('should not create files when they already exist', async () => { |
| 104 | + // Pre-create the directory and files |
| 105 | + fs.mkdirSync(workerDir, { recursive: true }); |
| 106 | + |
| 107 | + const indexPath = path.join(workerDir, 'index.ts'); |
| 108 | + const denoJsonPath = path.join(workerDir, 'deno.json'); |
| 109 | + |
| 110 | + fs.writeFileSync(indexPath, '// existing content'); |
| 111 | + fs.writeFileSync(denoJsonPath, '// existing content'); |
| 112 | + |
| 113 | + const result = await createExampleWorker({ |
| 114 | + supabasePath, |
| 115 | + autoConfirm: true, |
| 116 | + }); |
| 117 | + |
| 118 | + // Should return false because no changes were needed |
| 119 | + expect(result).toBe(false); |
| 120 | + |
| 121 | + // Verify files still exist with original content |
| 122 | + expect(fs.readFileSync(indexPath, 'utf8')).toBe('// existing content'); |
| 123 | + expect(fs.readFileSync(denoJsonPath, 'utf8')).toBe('// existing content'); |
| 124 | + }); |
| 125 | + |
| 126 | + it('should create only missing files when some already exist', async () => { |
| 127 | + // Pre-create the directory and one file |
| 128 | + fs.mkdirSync(workerDir, { recursive: true }); |
| 129 | + |
| 130 | + const indexPath = path.join(workerDir, 'index.ts'); |
| 131 | + const denoJsonPath = path.join(workerDir, 'deno.json'); |
| 132 | + |
| 133 | + // Only create index.ts |
| 134 | + fs.writeFileSync(indexPath, '// existing content'); |
| 135 | + |
| 136 | + const result = await createExampleWorker({ |
| 137 | + supabasePath, |
| 138 | + autoConfirm: true, |
| 139 | + }); |
| 140 | + |
| 141 | + // Should return true because deno.json was created |
| 142 | + expect(result).toBe(true); |
| 143 | + |
| 144 | + // Verify index.ts was not modified |
| 145 | + expect(fs.readFileSync(indexPath, 'utf8')).toBe('// existing content'); |
| 146 | + |
| 147 | + // Verify deno.json was created |
| 148 | + expect(fs.existsSync(denoJsonPath)).toBe(true); |
| 149 | + |
| 150 | + const denoJsonContent = fs.readFileSync(denoJsonPath, 'utf8'); |
| 151 | + expect(denoJsonContent).toContain('"imports"'); |
| 152 | + }); |
| 153 | + |
| 154 | + it('should create parent directories if they do not exist', async () => { |
| 155 | + // Don't create anything - let the function create it all |
| 156 | + expect(fs.existsSync(supabasePath)).toBe(false); |
| 157 | + |
| 158 | + const result = await createExampleWorker({ |
| 159 | + supabasePath, |
| 160 | + autoConfirm: true, |
| 161 | + }); |
| 162 | + |
| 163 | + expect(result).toBe(true); |
| 164 | + |
| 165 | + // Verify all parent directories were created |
| 166 | + expect(fs.existsSync(supabasePath)).toBe(true); |
| 167 | + expect(fs.existsSync(path.join(supabasePath, 'functions'))).toBe(true); |
| 168 | + expect(fs.existsSync(workerDir)).toBe(true); |
| 169 | + |
| 170 | + // Verify files exist |
| 171 | + expect(fs.existsSync(path.join(workerDir, 'index.ts'))).toBe(true); |
| 172 | + expect(fs.existsSync(path.join(workerDir, 'deno.json'))).toBe(true); |
| 173 | + }); |
| 174 | + |
| 175 | + it('should include subpath exports for Deno import mapping', async () => { |
| 176 | + await createExampleWorker({ |
| 177 | + supabasePath, |
| 178 | + autoConfirm: true, |
| 179 | + }); |
| 180 | + |
| 181 | + const denoJsonPath = path.join(workerDir, 'deno.json'); |
| 182 | + const denoJsonContent = fs.readFileSync(denoJsonPath, 'utf8'); |
| 183 | + const denoJson = JSON.parse(denoJsonContent); |
| 184 | + |
| 185 | + const version = getVersion(); |
| 186 | + |
| 187 | + // Verify subpath exports include versions (needed for proper Deno import mapping) |
| 188 | + expect(denoJson.imports['@pgflow/core/']).toBe(`npm:@pgflow/core@${version}/`); |
| 189 | + expect(denoJson.imports['@pgflow/dsl/']).toBe(`npm:@pgflow/dsl@${version}/`); |
| 190 | + expect(denoJson.imports['@pgflow/edge-worker/']).toBe(`jsr:@pgflow/edge-worker@${version}/`); |
| 191 | + }); |
| 192 | +}); |
0 commit comments