-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathcreate-example-worker.test.ts
More file actions
192 lines (149 loc) · 6.56 KB
/
create-example-worker.test.ts
File metadata and controls
192 lines (149 loc) · 6.56 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import fs from 'fs';
import path from 'path';
import os from 'os';
import { createExampleWorker } from '../../../src/commands/install/create-example-worker';
import { getVersion } from '../../../src/utils/get-version';
describe('createExampleWorker', () => {
let tempDir: string;
let supabasePath: string;
let workerDir: string;
beforeEach(() => {
// Create a temporary directory for testing
tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'pgflow-test-'));
supabasePath = path.join(tempDir, 'supabase');
workerDir = path.join(supabasePath, 'functions', 'greet-user-worker');
});
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 createExampleWorker({
supabasePath,
autoConfirm: true,
});
// Should return true because files were created
expect(result).toBe(true);
// Verify directory was created
expect(fs.existsSync(workerDir)).toBe(true);
// Verify all files exist
const indexPath = path.join(workerDir, 'index.ts');
const denoJsonPath = path.join(workerDir, 'deno.json');
expect(fs.existsSync(indexPath)).toBe(true);
expect(fs.existsSync(denoJsonPath)).toBe(true);
});
it('should create index.ts that imports GreetUser and starts EdgeWorker', async () => {
await createExampleWorker({
supabasePath,
autoConfirm: true,
});
const indexPath = path.join(workerDir, 'index.ts');
const indexContent = fs.readFileSync(indexPath, 'utf8');
// Should import EdgeWorker
expect(indexContent).toContain("import { EdgeWorker } from '@pgflow/edge-worker'");
// Should import GreetUser from flows directory
expect(indexContent).toContain("import { GreetUser } from '../../flows/greet-user.ts'");
// Should start EdgeWorker with GreetUser
expect(indexContent).toContain('EdgeWorker.start(GreetUser)');
});
it('should create deno.json with correct import mappings', async () => {
await createExampleWorker({
supabasePath,
autoConfirm: true,
});
const denoJsonPath = path.join(workerDir, 'deno.json');
const denoJsonContent = fs.readFileSync(denoJsonPath, 'utf8');
const denoJson = JSON.parse(denoJsonContent);
// Verify imports exist
expect(denoJson.imports).toBeDefined();
expect(denoJson.imports['@pgflow/core']).toBeDefined();
expect(denoJson.imports['@pgflow/dsl']).toBeDefined();
expect(denoJson.imports['@pgflow/edge-worker']).toBeDefined();
});
it('should inject package version instead of @latest in deno.json', async () => {
await createExampleWorker({
supabasePath,
autoConfirm: true,
});
const denoJsonPath = path.join(workerDir, 'deno.json');
const denoJsonContent = fs.readFileSync(denoJsonPath, 'utf8');
const denoJson = JSON.parse(denoJsonContent);
const version = getVersion();
// Verify version is not 'unknown'
expect(version).not.toBe('unknown');
// Verify that @latest is NOT used
expect(denoJsonContent).not.toContain('@latest');
// Verify that the actual version is used
expect(denoJson.imports['@pgflow/core']).toBe(`npm:@pgflow/core@${version}`);
expect(denoJson.imports['@pgflow/dsl']).toBe(`npm:@pgflow/dsl@${version}`);
expect(denoJson.imports['@pgflow/edge-worker']).toBe(`jsr:@pgflow/edge-worker@${version}`);
});
it('should not create files when they already exist', async () => {
// Pre-create the directory and files
fs.mkdirSync(workerDir, { recursive: true });
const indexPath = path.join(workerDir, 'index.ts');
const denoJsonPath = path.join(workerDir, 'deno.json');
fs.writeFileSync(indexPath, '// existing content');
fs.writeFileSync(denoJsonPath, '// existing content');
const result = await createExampleWorker({
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(denoJsonPath, 'utf8')).toBe('// existing content');
});
it('should create only missing files when some already exist', async () => {
// Pre-create the directory and one file
fs.mkdirSync(workerDir, { recursive: true });
const indexPath = path.join(workerDir, 'index.ts');
const denoJsonPath = path.join(workerDir, 'deno.json');
// Only create index.ts
fs.writeFileSync(indexPath, '// existing content');
const result = await createExampleWorker({
supabasePath,
autoConfirm: true,
});
// Should return true because deno.json was created
expect(result).toBe(true);
// Verify index.ts was not modified
expect(fs.readFileSync(indexPath, 'utf8')).toBe('// existing content');
// Verify deno.json was created
expect(fs.existsSync(denoJsonPath)).toBe(true);
const denoJsonContent = fs.readFileSync(denoJsonPath, 'utf8');
expect(denoJsonContent).toContain('"imports"');
});
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 createExampleWorker({
supabasePath,
autoConfirm: true,
});
expect(result).toBe(true);
// Verify all parent directories were created
expect(fs.existsSync(supabasePath)).toBe(true);
expect(fs.existsSync(path.join(supabasePath, 'functions'))).toBe(true);
expect(fs.existsSync(workerDir)).toBe(true);
// Verify files exist
expect(fs.existsSync(path.join(workerDir, 'index.ts'))).toBe(true);
expect(fs.existsSync(path.join(workerDir, 'deno.json'))).toBe(true);
});
it('should include subpath exports for Deno import mapping', async () => {
await createExampleWorker({
supabasePath,
autoConfirm: true,
});
const denoJsonPath = path.join(workerDir, 'deno.json');
const denoJsonContent = fs.readFileSync(denoJsonPath, 'utf8');
const denoJson = JSON.parse(denoJsonContent);
const version = getVersion();
// Verify subpath exports include versions (needed for proper Deno import mapping)
expect(denoJson.imports['@pgflow/core/']).toBe(`npm:@pgflow/core@${version}/`);
expect(denoJson.imports['@pgflow/dsl/']).toBe(`npm:@pgflow/dsl@${version}/`);
expect(denoJson.imports['@pgflow/edge-worker/']).toBe(`jsr:@pgflow/edge-worker@${version}/`);
});
});