-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathprepare-files.test.ts
More file actions
208 lines (191 loc) · 8.53 KB
/
prepare-files.test.ts
File metadata and controls
208 lines (191 loc) · 8.53 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import * as FileSystem from "@effect/platform/FileSystem"
import * as Path from "@effect/platform/Path"
import { NodeContext } from "@effect/platform-node"
import { describe, expect, it } from "@effect/vitest"
import { Effect } from "effect"
import type { TemplateConfig } from "../../src/core/domain.js"
import { runCommandExitCode } from "../../src/shell/command-runner.js"
import { prepareProjectFiles } from "../../src/usecases/actions/prepare-files.js"
const withTempDir = <A, E, R>(
use: (tempDir: string) => Effect.Effect<A, E, R>
): Effect.Effect<A, E, R | FileSystem.FileSystem> =>
Effect.scoped(
Effect.gen(function*(_) {
const fs = yield* _(FileSystem.FileSystem)
const tempDir = yield* _(
fs.makeTempDirectoryScoped({
prefix: "docker-git-force-env-"
})
)
return yield* _(use(tempDir))
})
)
const makeGlobalConfig = (root: string, path: Path.Path): TemplateConfig => ({
containerName: "dg-test",
serviceName: "dg-test",
sshUser: "dev",
sshPort: 2222,
repoUrl: "https://github.com/org/repo.git",
repoRef: "main",
gitTokenLabel: undefined,
targetDir: "/home/dev/org/repo",
volumeName: "dg-test-home",
dockerGitPath: path.join(root, ".docker-git"),
authorizedKeysPath: path.join(root, "authorized_keys"),
envGlobalPath: path.join(root, ".orch/env/global.env"),
envProjectPath: path.join(root, ".orch/env/project.env"),
codexAuthPath: path.join(root, ".orch/auth/codex"),
codexSharedAuthPath: path.join(root, ".orch/auth/codex-shared"),
codexHome: "/home/dev/.codex",
dockerNetworkMode: "shared",
dockerSharedNetworkName: "docker-git-shared",
enableMcpPlaywright: false,
pnpmVersion: "10.27.0"
})
const makeProjectConfig = (
outDir: string,
enableMcpPlaywright: boolean,
path: Path.Path,
gitTokenLabel?: string,
codexAuthLabel?: string,
claudeAuthLabel?: string
): TemplateConfig => ({
containerName: "dg-test",
serviceName: "dg-test",
sshUser: "dev",
sshPort: 2222,
repoUrl: "https://github.com/org/repo.git",
repoRef: "main",
gitTokenLabel,
codexAuthLabel,
claudeAuthLabel,
targetDir: "/home/dev/org/repo",
volumeName: "dg-test-home",
dockerGitPath: path.join(outDir, ".docker-git"),
authorizedKeysPath: path.join(outDir, "authorized_keys"),
envGlobalPath: path.join(outDir, ".orch/env/global.env"),
envProjectPath: path.join(outDir, ".orch/env/project.env"),
codexAuthPath: path.join(outDir, ".orch/auth/codex"),
codexSharedAuthPath: path.join(outDir, ".orch/auth/codex-shared"),
codexHome: "/home/dev/.codex",
dockerNetworkMode: "shared",
dockerSharedNetworkName: "docker-git-shared",
enableMcpPlaywright,
pnpmVersion: "10.27.0"
})
const isRecord = (value: unknown): value is Record<string, unknown> =>
typeof value === "object" && value !== null
const readEnableMcpPlaywrightFlag = (value: unknown): boolean | undefined => {
if (!isRecord(value)) {
return undefined
}
const template = value.template
if (!isRecord(template)) {
return undefined
}
const flag = template.enableMcpPlaywright
return typeof flag === "boolean" ? flag : undefined
}
describe("prepareProjectFiles", () => {
it.effect("force-env refresh rewrites managed templates", () =>
withTempDir((root) =>
Effect.gen(function*(_) {
const fs = yield* _(FileSystem.FileSystem)
const path = yield* _(Path.Path)
const outDir = path.join(root, "project")
const globalConfig = makeGlobalConfig(root, path)
const withoutMcp = makeProjectConfig(outDir, false, path)
const withMcp = makeProjectConfig(outDir, true, path, "AGIENS", "agien-codex", "agien-claude")
yield* _(
prepareProjectFiles(outDir, root, globalConfig, withoutMcp, {
force: false,
forceEnv: false
})
)
const dockerfile = yield* _(fs.readFileString(path.join(outDir, "Dockerfile")))
const entrypointPath = path.join(outDir, "entrypoint.sh")
const entrypoint = yield* _(fs.readFileString(entrypointPath))
const composeBefore = yield* _(fs.readFileString(path.join(outDir, "docker-compose.yml")))
const entrypointSyntaxExitCode = yield* _(
runCommandExitCode({
cwd: outDir,
command: "bash",
args: ["-n", entrypointPath]
})
)
expect(entrypointSyntaxExitCode).toBe(0)
expect(dockerfile).toContain("docker-compose-v2")
expect(dockerfile).toContain("gitleaks version")
expect(dockerfile).toContain(
"curl -fsSL --retry 5 --retry-all-errors --retry-delay 2 https://bun.sh/install -o /tmp/bun-install.sh"
)
expect(dockerfile).toContain("bun install attempt ${attempt} failed; retrying...")
expect(entrypoint).toContain('DOCKER_GIT_HOME="/home/dev/.docker-git"')
expect(entrypoint).toContain('SOURCE_SHARED_AUTH="/home/dev/.codex-shared/auth.json"')
expect(entrypoint).toContain('CODEX_LABEL_RAW="$CODEX_AUTH_LABEL"')
expect(entrypoint).toContain('OPENCODE_DATA_DIR="/home/dev/.local/share/opencode"')
expect(entrypoint).toContain('OPENCODE_SHARED_HOME="/home/dev/.codex-shared/opencode"')
expect(entrypoint).toContain('OPENCODE_CONFIG_DIR="/home/dev/.config/opencode"')
expect(entrypoint).toContain('su - dev -s /bin/bash -c "bash -lc')
expect(entrypoint).toContain('. /etc/profile 2>/dev/null || true;')
expect(entrypoint).toContain("codex exec")
expect(entrypoint).not.toContain("codex --approval-mode full-auto")
expect(entrypoint).toContain('"plugin": ["oh-my-opencode"]')
expect(entrypoint).toContain("branch '$REPO_REF' missing; retrying without --branch")
expect(entrypoint).not.toContain("git ls-remote --symref")
expect(entrypoint).toContain("cat > \"$MOVE_SCRIPT\" << 'EOFMOVE'")
expect(entrypoint).toMatch(/\nEOFMOVE\n\s*chmod \+x "\$MOVE_SCRIPT"/)
expect(entrypoint).not.toContain("\n EOFMOVE\n")
expect(composeBefore).toContain("container_name: dg-test")
expect(composeBefore).toContain("restart: unless-stopped")
expect(composeBefore).toContain(":/home/dev/.docker-git")
expect(composeBefore).not.toContain("dg-test-browser")
expect(composeBefore).toContain("docker-git-shared")
expect(composeBefore).toContain("external: true")
yield* _(
prepareProjectFiles(outDir, root, globalConfig, withMcp, {
force: false,
forceEnv: true
})
)
const composeAfter = yield* _(fs.readFileString(path.join(outDir, "docker-compose.yml")))
const configAfterText = yield* _(fs.readFileString(path.join(outDir, "docker-git.json")))
const configAfter = yield* _(Effect.sync((): unknown => JSON.parse(configAfterText)))
expect(composeAfter).toContain("dg-test-browser")
expect(composeAfter).toContain('MCP_PLAYWRIGHT_ENABLE: "1"')
expect(composeAfter).toContain('GITHUB_AUTH_LABEL: "AGIENS"')
expect(composeAfter).toContain('GIT_AUTH_LABEL: "AGIENS"')
expect(composeAfter).toContain('CODEX_AUTH_LABEL: "agien-codex"')
expect(composeAfter).toContain('CLAUDE_AUTH_LABEL: "agien-claude"')
expect(composeAfter).toContain("container_name: dg-test")
expect(composeAfter).toContain("container_name: dg-test-browser")
expect(composeAfter).toContain("container_name: dg-test-browser\n restart: unless-stopped")
expect(composeAfter).toContain("docker-git-shared")
expect(composeAfter).toContain("external: true")
expect(readEnableMcpPlaywrightFlag(configAfter)).toBe(true)
})
).pipe(Effect.provide(NodeContext.layer)))
it.effect("renders project-scoped network when dockerNetworkMode=project", () =>
withTempDir((root) =>
Effect.gen(function*(_) {
const fs = yield* _(FileSystem.FileSystem)
const path = yield* _(Path.Path)
const outDir = path.join(root, "project-mode")
const globalConfig = makeGlobalConfig(root, path)
const projectConfig = {
...makeProjectConfig(outDir, false, path),
dockerNetworkMode: "project"
}
yield* _(
prepareProjectFiles(outDir, root, globalConfig, projectConfig, {
force: false,
forceEnv: false
})
)
const compose = yield* _(fs.readFileString(path.join(outDir, "docker-compose.yml")))
expect(compose).toContain("dg-test-net")
expect(compose).toContain("driver: bridge")
expect(compose).not.toContain("external: true")
})
).pipe(Effect.provide(NodeContext.layer)))
})