@livestore/utils-dev 0.0.0-snapshot-1a48725ede0e1642a644d2972e548b7cead6c9a4 → 0.0.0-snapshot-2df821f8699de1c39c3ccfd1a7f790614b64edd9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/.tsbuildinfo.json +1 -1
- package/dist/node/DockerComposeService/DockerComposeService.d.ts +48 -0
- package/dist/node/DockerComposeService/DockerComposeService.d.ts.map +1 -0
- package/dist/node/DockerComposeService/DockerComposeService.js +106 -0
- package/dist/node/DockerComposeService/DockerComposeService.js.map +1 -0
- package/dist/node/DockerComposeService/DockerComposeService.test.d.ts +2 -0
- package/dist/node/DockerComposeService/DockerComposeService.test.d.ts.map +1 -0
- package/dist/node/DockerComposeService/DockerComposeService.test.js +64 -0
- package/dist/node/DockerComposeService/DockerComposeService.test.js.map +1 -0
- package/dist/node/WranglerDevServer/WranglerDevServer.d.ts +51 -0
- package/dist/node/WranglerDevServer/WranglerDevServer.d.ts.map +1 -0
- package/dist/node/WranglerDevServer/WranglerDevServer.js +102 -0
- package/dist/node/WranglerDevServer/WranglerDevServer.js.map +1 -0
- package/dist/node/WranglerDevServer/WranglerDevServer.test.d.ts +2 -0
- package/dist/node/WranglerDevServer/WranglerDevServer.test.d.ts.map +1 -0
- package/dist/node/WranglerDevServer/WranglerDevServer.test.js +186 -0
- package/dist/node/WranglerDevServer/WranglerDevServer.test.js.map +1 -0
- package/dist/node/WranglerDevServer/fixtures/cf-worker.d.ts +8 -0
- package/dist/node/WranglerDevServer/fixtures/cf-worker.d.ts.map +1 -0
- package/dist/node/WranglerDevServer/fixtures/cf-worker.js +11 -0
- package/dist/node/WranglerDevServer/fixtures/cf-worker.js.map +1 -0
- package/dist/node/WranglerDevServer/process-tree-manager.d.ts +55 -0
- package/dist/node/WranglerDevServer/process-tree-manager.d.ts.map +1 -0
- package/dist/node/WranglerDevServer/process-tree-manager.js +178 -0
- package/dist/node/WranglerDevServer/process-tree-manager.js.map +1 -0
- package/dist/node/mod.d.ts +3 -2
- package/dist/node/mod.d.ts.map +1 -1
- package/dist/node/mod.js +3 -2
- package/dist/node/mod.js.map +1 -1
- package/dist/node-vitest/Vitest.d.ts +15 -1
- package/dist/node-vitest/Vitest.d.ts.map +1 -1
- package/dist/node-vitest/Vitest.js +18 -1
- package/dist/node-vitest/Vitest.js.map +1 -1
- package/package.json +2 -2
- package/src/node/DockerComposeService/DockerComposeService.test.ts +91 -0
- package/src/node/DockerComposeService/DockerComposeService.ts +251 -0
- package/src/node/DockerComposeService/test-fixtures/docker-compose.yml +4 -0
- package/src/node/WranglerDevServer/WranglerDevServer.test.ts +268 -0
- package/src/node/WranglerDevServer/WranglerDevServer.ts +206 -0
- package/src/node/WranglerDevServer/fixtures/cf-worker.ts +11 -0
- package/src/node/WranglerDevServer/fixtures/wrangler.toml +11 -0
- package/src/node/WranglerDevServer/process-tree-manager.ts +263 -0
- package/src/node/mod.ts +16 -2
- package/src/node-vitest/Vitest.ts +60 -1
- package/dist/node/vitest-docker-compose-setup.d.ts +0 -32
- package/dist/node/vitest-docker-compose-setup.d.ts.map +0 -1
- package/dist/node/vitest-docker-compose-setup.js +0 -131
- package/dist/node/vitest-docker-compose-setup.js.map +0 -1
- package/dist/node/vitest-wrangler-setup.d.ts +0 -27
- package/dist/node/vitest-wrangler-setup.d.ts.map +0 -1
- package/dist/node/vitest-wrangler-setup.js +0 -96
- package/dist/node/vitest-wrangler-setup.js.map +0 -1
- package/src/node/vitest-docker-compose-setup.ts +0 -182
- package/src/node/vitest-wrangler-setup.ts +0 -132
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Command,
|
|
3
|
+
type CommandExecutor,
|
|
4
|
+
Duration,
|
|
5
|
+
Effect,
|
|
6
|
+
type PlatformError,
|
|
7
|
+
Schedule,
|
|
8
|
+
Schema,
|
|
9
|
+
type Scope,
|
|
10
|
+
Stream,
|
|
11
|
+
} from '@livestore/utils/effect'
|
|
12
|
+
|
|
13
|
+
export class DockerComposeError extends Schema.TaggedError<DockerComposeError>()('DockerComposeError', {
|
|
14
|
+
cause: Schema.Defect,
|
|
15
|
+
message: Schema.String,
|
|
16
|
+
}) {}
|
|
17
|
+
|
|
18
|
+
export interface DockerComposeArgs {
|
|
19
|
+
readonly cwd: string
|
|
20
|
+
readonly serviceName?: string
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface StartOptions {
|
|
24
|
+
readonly detached?: boolean
|
|
25
|
+
readonly env?: Record<string, string>
|
|
26
|
+
readonly healthCheck?: {
|
|
27
|
+
readonly url: string
|
|
28
|
+
readonly timeout?: Duration.Duration
|
|
29
|
+
readonly interval?: Duration.Duration
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface LogsOptions {
|
|
34
|
+
readonly follow?: boolean
|
|
35
|
+
readonly tail?: number
|
|
36
|
+
readonly since?: string
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface DockerComposeOperations {
|
|
40
|
+
readonly pull: Effect.Effect<void, DockerComposeError | PlatformError.PlatformError>
|
|
41
|
+
readonly start: (
|
|
42
|
+
options?: StartOptions,
|
|
43
|
+
) => Effect.Effect<void, DockerComposeError | PlatformError.PlatformError, Scope.Scope>
|
|
44
|
+
readonly stop: Effect.Effect<void, DockerComposeError | PlatformError.PlatformError>
|
|
45
|
+
readonly logs: (
|
|
46
|
+
options?: LogsOptions,
|
|
47
|
+
) => Stream.Stream<string, DockerComposeError | PlatformError.PlatformError, Scope.Scope>
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export class DockerComposeService extends Effect.Service<DockerComposeService>()('DockerComposeService', {
|
|
51
|
+
scoped: (args: DockerComposeArgs) =>
|
|
52
|
+
Effect.gen(function* () {
|
|
53
|
+
const { cwd, serviceName } = args
|
|
54
|
+
|
|
55
|
+
const commandExecutorContext = yield* Effect.context<CommandExecutor.CommandExecutor>()
|
|
56
|
+
|
|
57
|
+
const pull = Effect.gen(function* () {
|
|
58
|
+
yield* Effect.log(`Pulling Docker Compose images in ${cwd}`)
|
|
59
|
+
|
|
60
|
+
yield* Command.make('docker', 'compose', 'pull').pipe(
|
|
61
|
+
Command.workingDirectory(cwd),
|
|
62
|
+
Command.exitCode,
|
|
63
|
+
Effect.flatMap((exitCode: number) =>
|
|
64
|
+
exitCode === 0
|
|
65
|
+
? Effect.void
|
|
66
|
+
: Effect.fail(
|
|
67
|
+
new DockerComposeError({
|
|
68
|
+
cause: new Error(`Docker compose pull failed with exit code ${exitCode}`),
|
|
69
|
+
message: `Docker compose pull failed with exit code ${exitCode}`,
|
|
70
|
+
}),
|
|
71
|
+
),
|
|
72
|
+
),
|
|
73
|
+
Effect.provide(commandExecutorContext),
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
yield* Effect.log(`Successfully pulled Docker Compose images`)
|
|
77
|
+
}).pipe(Effect.withSpan('pullDockerComposeImages'))
|
|
78
|
+
|
|
79
|
+
const start = (options: StartOptions = {}) =>
|
|
80
|
+
Effect.gen(function* () {
|
|
81
|
+
const { detached = true, healthCheck } = options
|
|
82
|
+
|
|
83
|
+
// Build start command
|
|
84
|
+
const baseArgs = ['docker', 'compose', 'up']
|
|
85
|
+
if (detached) baseArgs.push('-d')
|
|
86
|
+
if (serviceName) baseArgs.push(serviceName)
|
|
87
|
+
|
|
88
|
+
const command = yield* Command.make(baseArgs[0]!, ...baseArgs.slice(1)).pipe(
|
|
89
|
+
Command.workingDirectory(cwd),
|
|
90
|
+
Command.env(options.env ?? {}),
|
|
91
|
+
Command.start,
|
|
92
|
+
Effect.catchAll((cause) =>
|
|
93
|
+
Effect.fail(
|
|
94
|
+
new DockerComposeError({
|
|
95
|
+
cause,
|
|
96
|
+
message: `Failed to start Docker Compose services in ${cwd}`,
|
|
97
|
+
}),
|
|
98
|
+
),
|
|
99
|
+
),
|
|
100
|
+
Effect.provide(commandExecutorContext),
|
|
101
|
+
)
|
|
102
|
+
|
|
103
|
+
// Wait for command completion
|
|
104
|
+
yield* command.exitCode.pipe(
|
|
105
|
+
Effect.flatMap((exitCode: number) =>
|
|
106
|
+
exitCode === 0
|
|
107
|
+
? Effect.void
|
|
108
|
+
: Effect.fail(
|
|
109
|
+
new DockerComposeError({
|
|
110
|
+
cause: new Error(`Docker compose exited with code ${exitCode}`),
|
|
111
|
+
message: `Docker Compose failed to start with exit code ${exitCode}`,
|
|
112
|
+
}),
|
|
113
|
+
),
|
|
114
|
+
),
|
|
115
|
+
Effect.provide(commandExecutorContext),
|
|
116
|
+
)
|
|
117
|
+
|
|
118
|
+
// Perform health check if requested
|
|
119
|
+
if (healthCheck) {
|
|
120
|
+
yield* performHealthCheck(healthCheck).pipe(Effect.provide(commandExecutorContext))
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
yield* Effect.log(`Docker Compose services started successfully in ${cwd}`)
|
|
124
|
+
}).pipe(Effect.withSpan('startDockerCompose'))
|
|
125
|
+
|
|
126
|
+
const stop = Effect.gen(function* () {
|
|
127
|
+
yield* Effect.log(`Stopping Docker Compose services in ${cwd}`)
|
|
128
|
+
|
|
129
|
+
const stopCommand = serviceName
|
|
130
|
+
? Command.make('docker', 'compose', 'stop', serviceName)
|
|
131
|
+
: Command.make('docker', 'compose', 'stop')
|
|
132
|
+
|
|
133
|
+
yield* stopCommand.pipe(
|
|
134
|
+
Command.workingDirectory(cwd),
|
|
135
|
+
Command.exitCode,
|
|
136
|
+
Effect.flatMap((exitCode: number) =>
|
|
137
|
+
exitCode === 0
|
|
138
|
+
? Effect.void
|
|
139
|
+
: Effect.fail(
|
|
140
|
+
new DockerComposeError({
|
|
141
|
+
cause: new Error(`Docker compose stop exited with code ${exitCode}`),
|
|
142
|
+
message: `Failed to stop Docker Compose services`,
|
|
143
|
+
}),
|
|
144
|
+
),
|
|
145
|
+
),
|
|
146
|
+
Effect.provide(commandExecutorContext),
|
|
147
|
+
)
|
|
148
|
+
|
|
149
|
+
yield* Effect.log(`Docker Compose services stopped successfully`)
|
|
150
|
+
}).pipe(Effect.withSpan('stopDockerCompose'))
|
|
151
|
+
|
|
152
|
+
const logs = (options: LogsOptions = {}) =>
|
|
153
|
+
Effect.gen(function* () {
|
|
154
|
+
const { follow = false, tail, since } = options
|
|
155
|
+
|
|
156
|
+
const baseArgs = ['docker', 'compose', 'logs']
|
|
157
|
+
if (follow) baseArgs.push('-f')
|
|
158
|
+
if (tail) baseArgs.push('--tail', tail.toString())
|
|
159
|
+
if (since) baseArgs.push('--since', since)
|
|
160
|
+
if (serviceName) baseArgs.push(serviceName)
|
|
161
|
+
|
|
162
|
+
const command = yield* Command.make(baseArgs[0]!, ...baseArgs.slice(1)).pipe(
|
|
163
|
+
Command.workingDirectory(cwd),
|
|
164
|
+
Command.start,
|
|
165
|
+
Effect.catchAll((cause) =>
|
|
166
|
+
Effect.fail(
|
|
167
|
+
new DockerComposeError({
|
|
168
|
+
cause,
|
|
169
|
+
message: `Failed to read Docker Compose logs in ${cwd}`,
|
|
170
|
+
}),
|
|
171
|
+
),
|
|
172
|
+
),
|
|
173
|
+
Effect.provide(commandExecutorContext),
|
|
174
|
+
)
|
|
175
|
+
|
|
176
|
+
return command.stdout.pipe(
|
|
177
|
+
Stream.decodeText('utf8'),
|
|
178
|
+
Stream.splitLines,
|
|
179
|
+
Stream.mapError(
|
|
180
|
+
(cause) =>
|
|
181
|
+
new DockerComposeError({
|
|
182
|
+
cause,
|
|
183
|
+
message: `Error reading Docker Compose logs in ${cwd}`,
|
|
184
|
+
}),
|
|
185
|
+
),
|
|
186
|
+
)
|
|
187
|
+
}).pipe(Stream.unwrapScoped)
|
|
188
|
+
|
|
189
|
+
return { pull, start, stop, logs }
|
|
190
|
+
}),
|
|
191
|
+
}) {}
|
|
192
|
+
|
|
193
|
+
const performHealthCheck = ({
|
|
194
|
+
url,
|
|
195
|
+
timeout = Duration.minutes(2),
|
|
196
|
+
interval = Duration.seconds(2),
|
|
197
|
+
}: {
|
|
198
|
+
url: string
|
|
199
|
+
timeout?: Duration.Duration
|
|
200
|
+
interval?: Duration.Duration
|
|
201
|
+
}): Effect.Effect<void, DockerComposeError, CommandExecutor.CommandExecutor | Scope.Scope> =>
|
|
202
|
+
Effect.gen(function* () {
|
|
203
|
+
yield* Effect.log(`Performing health check on ${url}`)
|
|
204
|
+
|
|
205
|
+
const checkHealth = Command.make('curl', '-f', '-s', url).pipe(
|
|
206
|
+
Command.exitCode,
|
|
207
|
+
Effect.map((code: number) => code === 0),
|
|
208
|
+
Effect.catchAll(() => Effect.succeed(false)),
|
|
209
|
+
)
|
|
210
|
+
|
|
211
|
+
const healthCheck = checkHealth.pipe(
|
|
212
|
+
Effect.repeat({
|
|
213
|
+
while: (healthy: boolean) => !healthy,
|
|
214
|
+
schedule: Schedule.fixed(interval),
|
|
215
|
+
}),
|
|
216
|
+
Effect.timeout(timeout),
|
|
217
|
+
Effect.catchAll(() =>
|
|
218
|
+
Effect.fail(
|
|
219
|
+
new DockerComposeError({
|
|
220
|
+
cause: new Error('Health check timeout'),
|
|
221
|
+
message: `Health check failed for ${url} after ${Duration.toMillis(timeout)}ms`,
|
|
222
|
+
}),
|
|
223
|
+
),
|
|
224
|
+
),
|
|
225
|
+
)
|
|
226
|
+
|
|
227
|
+
yield* healthCheck
|
|
228
|
+
yield* Effect.log(`Health check passed for ${url}`)
|
|
229
|
+
})
|
|
230
|
+
|
|
231
|
+
// Convenience function for scoped Docker Compose operations with automatic cleanup
|
|
232
|
+
export const startDockerComposeServicesScoped = (
|
|
233
|
+
args: DockerComposeArgs & {
|
|
234
|
+
healthCheck?: StartOptions['healthCheck']
|
|
235
|
+
},
|
|
236
|
+
): Effect.Effect<
|
|
237
|
+
void,
|
|
238
|
+
DockerComposeError | PlatformError.PlatformError,
|
|
239
|
+
DockerComposeService | CommandExecutor.CommandExecutor | Scope.Scope
|
|
240
|
+
> =>
|
|
241
|
+
Effect.gen(function* () {
|
|
242
|
+
const dockerCompose = yield* DockerComposeService
|
|
243
|
+
|
|
244
|
+
// Start the services
|
|
245
|
+
yield* dockerCompose.start({
|
|
246
|
+
healthCheck: args.healthCheck,
|
|
247
|
+
})
|
|
248
|
+
|
|
249
|
+
// Add cleanup finalizer to the current scope
|
|
250
|
+
yield* Effect.addFinalizer(() => dockerCompose.stop.pipe(Effect.orDie))
|
|
251
|
+
})
|
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
import { Effect, Exit, Fiber, Layer, Scope } from '@livestore/utils/effect'
|
|
2
|
+
import { PlatformNode } from '@livestore/utils/node'
|
|
3
|
+
import { Vitest } from '@livestore/utils-dev/node-vitest'
|
|
4
|
+
import { expect } from 'vitest'
|
|
5
|
+
import {
|
|
6
|
+
type StartWranglerDevServerArgs,
|
|
7
|
+
WranglerDevServerError,
|
|
8
|
+
WranglerDevServerService,
|
|
9
|
+
} from './WranglerDevServer.ts'
|
|
10
|
+
|
|
11
|
+
const testTimeout = 60_000
|
|
12
|
+
|
|
13
|
+
const withTestCtx = Vitest.makeWithTestCtx({
|
|
14
|
+
timeout: testTimeout,
|
|
15
|
+
makeLayer: () => PlatformNode.NodeContext.layer,
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
const WranglerDevServerTest = (args: Partial<StartWranglerDevServerArgs> = {}) =>
|
|
19
|
+
WranglerDevServerService.Default({
|
|
20
|
+
cwd: `${import.meta.dirname}/fixtures`,
|
|
21
|
+
...args,
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
Vitest.describe('WranglerDevServer', { timeout: testTimeout }, () => {
|
|
25
|
+
Vitest.describe('Basic Operations', () => {
|
|
26
|
+
const withBasicTest = (args: Partial<StartWranglerDevServerArgs> = {}) =>
|
|
27
|
+
Vitest.makeWithTestCtx({
|
|
28
|
+
timeout: testTimeout,
|
|
29
|
+
makeLayer: () => WranglerDevServerTest(args).pipe(Layer.provide(PlatformNode.NodeContext.layer)),
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
Vitest.scopedLive('should start wrangler dev server and return port', (test) =>
|
|
33
|
+
Effect.gen(function* () {
|
|
34
|
+
const server = yield* WranglerDevServerService
|
|
35
|
+
|
|
36
|
+
expect(server.port).toBeGreaterThan(0)
|
|
37
|
+
expect(server.url).toMatch(/http:\/\/localhost:\d+/)
|
|
38
|
+
expect(typeof server.processId).toBe('number')
|
|
39
|
+
expect(server.processId).toBeGreaterThan(0)
|
|
40
|
+
}).pipe(withBasicTest()(test)),
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
Vitest.scopedLive('should use specified port when provided', (test) =>
|
|
44
|
+
Effect.gen(function* () {
|
|
45
|
+
const server = yield* WranglerDevServerService
|
|
46
|
+
|
|
47
|
+
expect(server.port).toBe(54443)
|
|
48
|
+
expect(server.url).toBe(`http://localhost:54443`)
|
|
49
|
+
}).pipe(withBasicTest({ port: 54443 })(test)),
|
|
50
|
+
)
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
Vitest.describe('Resource Management', () => {
|
|
54
|
+
Vitest.scopedLive('should cleanup processes on scope close', (test) =>
|
|
55
|
+
Effect.gen(function* () {
|
|
56
|
+
let processId: number | undefined
|
|
57
|
+
|
|
58
|
+
// Create a separate scope for the server
|
|
59
|
+
const serverScope = yield* Scope.make()
|
|
60
|
+
|
|
61
|
+
const server = yield* Effect.provide(
|
|
62
|
+
WranglerDevServerService,
|
|
63
|
+
WranglerDevServerTest().pipe(Layer.provide(PlatformNode.NodeContext.layer)),
|
|
64
|
+
).pipe(Scope.extend(serverScope))
|
|
65
|
+
|
|
66
|
+
processId = server.processId
|
|
67
|
+
expect(processId).toBeGreaterThan(0)
|
|
68
|
+
expect(server.port).toBeGreaterThan(0)
|
|
69
|
+
expect(server.url).toMatch(/http:\/\/localhost:\d+/)
|
|
70
|
+
|
|
71
|
+
// Close scope to trigger cleanup
|
|
72
|
+
yield* Scope.close(serverScope, Exit.succeed(void 0))
|
|
73
|
+
|
|
74
|
+
// Wait for cleanup to complete
|
|
75
|
+
yield* Effect.sleep('2 seconds')
|
|
76
|
+
|
|
77
|
+
// Verify process is terminated
|
|
78
|
+
const isRunning2 = yield* Effect.promise(() => {
|
|
79
|
+
try {
|
|
80
|
+
process.kill(processId!, 0)
|
|
81
|
+
return Promise.resolve(true)
|
|
82
|
+
} catch {
|
|
83
|
+
return Promise.resolve(false)
|
|
84
|
+
}
|
|
85
|
+
})
|
|
86
|
+
expect(isRunning2).toBe(false)
|
|
87
|
+
}).pipe(withTestCtx(test)),
|
|
88
|
+
)
|
|
89
|
+
|
|
90
|
+
Vitest.scopedLive('should handle interruption with fast cleanup', (test) =>
|
|
91
|
+
Effect.gen(function* () {
|
|
92
|
+
let processId: number | undefined
|
|
93
|
+
|
|
94
|
+
const fiber = yield* Effect.fork(
|
|
95
|
+
Effect.provide(
|
|
96
|
+
Effect.gen(function* () {
|
|
97
|
+
const server = yield* WranglerDevServerService
|
|
98
|
+
processId = server.processId
|
|
99
|
+
yield* Effect.sleep('30 seconds') // Keep running
|
|
100
|
+
return server
|
|
101
|
+
}),
|
|
102
|
+
WranglerDevServerTest().pipe(Layer.provide(PlatformNode.NodeContext.layer)),
|
|
103
|
+
),
|
|
104
|
+
)
|
|
105
|
+
|
|
106
|
+
// Wait for server to start
|
|
107
|
+
yield* Effect.sleep('3 seconds')
|
|
108
|
+
|
|
109
|
+
expect(processId).toBeGreaterThan(0)
|
|
110
|
+
|
|
111
|
+
// Interrupt and measure cleanup time
|
|
112
|
+
const start = Date.now()
|
|
113
|
+
yield* Fiber.interrupt(fiber)
|
|
114
|
+
const elapsed = Date.now() - start
|
|
115
|
+
|
|
116
|
+
// Should use fast cleanup (500ms timeout) + some overhead
|
|
117
|
+
expect(elapsed).toBeLessThan(1500) // Allow some overhead
|
|
118
|
+
|
|
119
|
+
// Wait for cleanup to complete
|
|
120
|
+
yield* Effect.sleep('1 second')
|
|
121
|
+
|
|
122
|
+
// Verify process is terminated
|
|
123
|
+
const isRunningAfter = yield* Effect.promise(() => {
|
|
124
|
+
try {
|
|
125
|
+
process.kill(processId!, 0)
|
|
126
|
+
return Promise.resolve(true)
|
|
127
|
+
} catch {
|
|
128
|
+
return Promise.resolve(false)
|
|
129
|
+
}
|
|
130
|
+
})
|
|
131
|
+
expect(isRunningAfter).toBe(false)
|
|
132
|
+
}).pipe(withTestCtx(test)),
|
|
133
|
+
)
|
|
134
|
+
})
|
|
135
|
+
|
|
136
|
+
Vitest.describe('Error Handling', () => {
|
|
137
|
+
const withErrorTest = (args: Partial<StartWranglerDevServerArgs> = {}) =>
|
|
138
|
+
Vitest.makeWithTestCtx({
|
|
139
|
+
timeout: testTimeout,
|
|
140
|
+
makeLayer: () => WranglerDevServerTest(args).pipe(Layer.provide(PlatformNode.NodeContext.layer)),
|
|
141
|
+
})
|
|
142
|
+
|
|
143
|
+
Vitest.scopedLive('should handle missing wrangler.toml', (test) =>
|
|
144
|
+
Effect.gen(function* () {
|
|
145
|
+
// Since Wrangler can work without a config file, let's test with a truly invalid scenario
|
|
146
|
+
const result = yield* WranglerDevServerService.pipe(Effect.either)
|
|
147
|
+
|
|
148
|
+
// Note: Wrangler might still succeed even with /dev/null, so this test
|
|
149
|
+
// verifies our implementation handles the case properly, whether it succeeds or fails
|
|
150
|
+
expect(['Left', 'Right']).toContain(result._tag)
|
|
151
|
+
|
|
152
|
+
if (result._tag === 'Left') {
|
|
153
|
+
// If it fails, it should be a wrapped error
|
|
154
|
+
expect(result.left).toBeInstanceOf(WranglerDevServerError)
|
|
155
|
+
} else {
|
|
156
|
+
// If it succeeds, the server should be properly configured
|
|
157
|
+
expect(result.right.port).toBeGreaterThan(0)
|
|
158
|
+
}
|
|
159
|
+
}).pipe(withErrorTest({ cwd: '/tmp', wranglerConfigPath: '/dev/null' })(test)),
|
|
160
|
+
)
|
|
161
|
+
|
|
162
|
+
Vitest.scopedLive('should handle invalid working directory', (test) =>
|
|
163
|
+
Effect.gen(function* () {
|
|
164
|
+
const result = yield* WranglerDevServerService.pipe(
|
|
165
|
+
Effect.provide(
|
|
166
|
+
WranglerDevServerTest({
|
|
167
|
+
cwd: '/completely/nonexistent/directory',
|
|
168
|
+
}).pipe(Layer.provide(PlatformNode.NodeContext.layer)),
|
|
169
|
+
),
|
|
170
|
+
Effect.either,
|
|
171
|
+
)
|
|
172
|
+
|
|
173
|
+
expect(result._tag).toBe('Left')
|
|
174
|
+
if (result._tag === 'Left') {
|
|
175
|
+
expect(result.left).toBeInstanceOf(WranglerDevServerError)
|
|
176
|
+
}
|
|
177
|
+
}).pipe(Vitest.withTestCtx(test)),
|
|
178
|
+
)
|
|
179
|
+
|
|
180
|
+
Vitest.scopedLive('should timeout if server fails to start', (test) =>
|
|
181
|
+
Effect.gen(function* () {
|
|
182
|
+
// Create a command that will never output "Ready on"
|
|
183
|
+
const result = yield* WranglerDevServerService.pipe(
|
|
184
|
+
// Override the timeout for this test to be shorter
|
|
185
|
+
Effect.timeout('5 seconds'),
|
|
186
|
+
Effect.either,
|
|
187
|
+
)
|
|
188
|
+
|
|
189
|
+
// This might succeed or fail depending on actual wrangler behavior
|
|
190
|
+
// The main point is testing timeout functionality
|
|
191
|
+
expect(['Left', 'Right']).toContain(result._tag)
|
|
192
|
+
}).pipe(withErrorTest()(test)),
|
|
193
|
+
)
|
|
194
|
+
})
|
|
195
|
+
|
|
196
|
+
Vitest.describe('Process Tree Cleanup', () => {
|
|
197
|
+
const withCleanupTest = (args: Partial<StartWranglerDevServerArgs> = {}) =>
|
|
198
|
+
Vitest.makeWithTestCtx({
|
|
199
|
+
timeout: testTimeout,
|
|
200
|
+
makeLayer: () => WranglerDevServerTest(args).pipe(Layer.provide(PlatformNode.NodeContext.layer)),
|
|
201
|
+
})
|
|
202
|
+
|
|
203
|
+
Vitest.scopedLive('should clean up child workerd processes', (test) =>
|
|
204
|
+
Effect.gen(function* () {
|
|
205
|
+
let processId: number | undefined
|
|
206
|
+
|
|
207
|
+
const server = yield* WranglerDevServerService
|
|
208
|
+
processId = server.processId
|
|
209
|
+
|
|
210
|
+
// Wait for wrangler to spawn workerd children
|
|
211
|
+
yield* Effect.sleep('3 seconds')
|
|
212
|
+
|
|
213
|
+
// Find any child processes (workerd)
|
|
214
|
+
const children = yield* Effect.promise(async () => {
|
|
215
|
+
const { exec } = require('node:child_process')
|
|
216
|
+
const { promisify } = require('node:util')
|
|
217
|
+
const execAsync = promisify(exec)
|
|
218
|
+
|
|
219
|
+
try {
|
|
220
|
+
if (!processId) throw new Error('processId is undefined')
|
|
221
|
+
const { stdout } = await execAsync(`ps -o pid,ppid -ax | grep -E "^\\s*[0-9]+\\s+${processId}\\s*$"`)
|
|
222
|
+
return stdout
|
|
223
|
+
.trim()
|
|
224
|
+
.split('\n')
|
|
225
|
+
.map((line: string) => {
|
|
226
|
+
const match = line.trim().match(/^\s*(\d+)\s+\d+\s*$/)
|
|
227
|
+
return match?.[1] ? Number.parseInt(match[1], 10) : null
|
|
228
|
+
})
|
|
229
|
+
.filter((pid: number | null): pid is number => pid !== null)
|
|
230
|
+
} catch {
|
|
231
|
+
return []
|
|
232
|
+
}
|
|
233
|
+
})
|
|
234
|
+
|
|
235
|
+
console.log(`Found ${children.length} child processes:`, children)
|
|
236
|
+
|
|
237
|
+
// The scope will close here and should clean up all processes
|
|
238
|
+
}).pipe(withCleanupTest()(test)),
|
|
239
|
+
)
|
|
240
|
+
})
|
|
241
|
+
|
|
242
|
+
Vitest.describe('Service Pattern', () => {
|
|
243
|
+
const withServiceTest = (args: Partial<StartWranglerDevServerArgs> = {}) =>
|
|
244
|
+
Vitest.makeWithTestCtx({
|
|
245
|
+
timeout: testTimeout,
|
|
246
|
+
makeLayer: () => WranglerDevServerTest(args).pipe(Layer.provide(PlatformNode.NodeContext.layer)),
|
|
247
|
+
})
|
|
248
|
+
|
|
249
|
+
Vitest.scopedLive('should work with service pattern', (test) =>
|
|
250
|
+
Effect.gen(function* () {
|
|
251
|
+
const server = yield* WranglerDevServerService
|
|
252
|
+
|
|
253
|
+
expect(server.port).toBeGreaterThan(0)
|
|
254
|
+
expect(server.url).toMatch(/http:\/\/localhost:\d+/)
|
|
255
|
+
expect(server.processId).toBeGreaterThan(0)
|
|
256
|
+
}).pipe(withServiceTest()(test)),
|
|
257
|
+
)
|
|
258
|
+
|
|
259
|
+
Vitest.scopedLive('should work with custom port via service', (test) =>
|
|
260
|
+
Effect.gen(function* () {
|
|
261
|
+
const server = yield* WranglerDevServerService
|
|
262
|
+
|
|
263
|
+
expect(server.port).toBe(54444)
|
|
264
|
+
expect(server.url).toBe('http://localhost:54444')
|
|
265
|
+
}).pipe(withServiceTest({ port: 54444 })(test)),
|
|
266
|
+
)
|
|
267
|
+
})
|
|
268
|
+
})
|