|
| 1 | +import * as fs from "node:fs" |
| 2 | +import * as os from "node:os" |
| 3 | +import * as path from "node:path" |
| 4 | + |
| 5 | +import { NodeContext } from "@effect/platform-node" |
| 6 | +import { describe, expect, it } from "@effect/vitest" |
| 7 | +import { Effect } from "effect" |
| 8 | + |
| 9 | +import type { TemplateConfig } from "../../src/core/domain.js" |
| 10 | +import { prepareProjectFiles } from "../../src/usecases/actions/prepare-files.js" |
| 11 | + |
| 12 | +const withTempDir = <A, E, R>(use: (tempDir: string) => Effect.Effect<A, E, R>): Effect.Effect<A, E, R> => |
| 13 | + Effect.scoped( |
| 14 | + Effect.gen(function*(_) { |
| 15 | + const tempDir = yield* _( |
| 16 | + Effect.acquireRelease( |
| 17 | + Effect.sync(() => fs.mkdtempSync(path.join(os.tmpdir(), "docker-git-force-env-"))), |
| 18 | + (dir) => Effect.sync(() => fs.rmSync(dir, { recursive: true, force: true })) |
| 19 | + ) |
| 20 | + ) |
| 21 | + return yield* _(use(tempDir)) |
| 22 | + }) |
| 23 | + ) |
| 24 | + |
| 25 | +const makeGlobalConfig = (root: string): TemplateConfig => ({ |
| 26 | + containerName: "dg-test", |
| 27 | + serviceName: "dg-test", |
| 28 | + sshUser: "dev", |
| 29 | + sshPort: 2222, |
| 30 | + repoUrl: "https://github.com/org/repo.git", |
| 31 | + repoRef: "main", |
| 32 | + targetDir: "/home/dev/org/repo", |
| 33 | + volumeName: "dg-test-home", |
| 34 | + authorizedKeysPath: path.join(root, "authorized_keys"), |
| 35 | + envGlobalPath: path.join(root, ".orch/env/global.env"), |
| 36 | + envProjectPath: path.join(root, ".orch/env/project.env"), |
| 37 | + codexAuthPath: path.join(root, ".orch/auth/codex"), |
| 38 | + codexSharedAuthPath: path.join(root, ".orch/auth/codex-shared"), |
| 39 | + codexHome: "/home/dev/.codex", |
| 40 | + enableMcpPlaywright: false, |
| 41 | + pnpmVersion: "10.27.0" |
| 42 | +}) |
| 43 | + |
| 44 | +const makeProjectConfig = (outDir: string, enableMcpPlaywright: boolean): TemplateConfig => ({ |
| 45 | + containerName: "dg-test", |
| 46 | + serviceName: "dg-test", |
| 47 | + sshUser: "dev", |
| 48 | + sshPort: 2222, |
| 49 | + repoUrl: "https://github.com/org/repo.git", |
| 50 | + repoRef: "main", |
| 51 | + targetDir: "/home/dev/org/repo", |
| 52 | + volumeName: "dg-test-home", |
| 53 | + authorizedKeysPath: path.join(outDir, "authorized_keys"), |
| 54 | + envGlobalPath: path.join(outDir, ".orch/env/global.env"), |
| 55 | + envProjectPath: path.join(outDir, ".orch/env/project.env"), |
| 56 | + codexAuthPath: path.join(outDir, ".orch/auth/codex"), |
| 57 | + codexSharedAuthPath: path.join(outDir, ".orch/auth/codex-shared"), |
| 58 | + codexHome: "/home/dev/.codex", |
| 59 | + enableMcpPlaywright, |
| 60 | + pnpmVersion: "10.27.0" |
| 61 | +}) |
| 62 | + |
| 63 | +describe("prepareProjectFiles", () => { |
| 64 | + it.effect("force-env refresh rewrites managed templates", () => |
| 65 | + withTempDir((root) => |
| 66 | + Effect.gen(function*(_) { |
| 67 | + const outDir = path.join(root, "project") |
| 68 | + const globalConfig = makeGlobalConfig(root) |
| 69 | + const withoutMcp = makeProjectConfig(outDir, false) |
| 70 | + const withMcp = makeProjectConfig(outDir, true) |
| 71 | + |
| 72 | + yield* _( |
| 73 | + prepareProjectFiles(outDir, root, globalConfig, withoutMcp, { |
| 74 | + force: false, |
| 75 | + forceEnv: false |
| 76 | + }) |
| 77 | + ) |
| 78 | + |
| 79 | + const composeBefore = yield* _( |
| 80 | + Effect.sync(() => fs.readFileSync(path.join(outDir, "docker-compose.yml"), "utf8")) |
| 81 | + ) |
| 82 | + expect(composeBefore).not.toContain("dg-test-browser") |
| 83 | + |
| 84 | + yield* _( |
| 85 | + prepareProjectFiles(outDir, root, globalConfig, withMcp, { |
| 86 | + force: false, |
| 87 | + forceEnv: true |
| 88 | + }) |
| 89 | + ) |
| 90 | + |
| 91 | + const composeAfter = yield* _( |
| 92 | + Effect.sync(() => fs.readFileSync(path.join(outDir, "docker-compose.yml"), "utf8")) |
| 93 | + ) |
| 94 | + const configAfter = yield* _( |
| 95 | + Effect.sync(() => JSON.parse(fs.readFileSync(path.join(outDir, "docker-git.json"), "utf8"))) |
| 96 | + ) |
| 97 | + |
| 98 | + expect(composeAfter).toContain("dg-test-browser") |
| 99 | + expect(composeAfter).toContain('MCP_PLAYWRIGHT_ENABLE: "1"') |
| 100 | + expect(configAfter.template.enableMcpPlaywright).toBe(true) |
| 101 | + }) |
| 102 | + ).pipe(Effect.provide(NodeContext.layer))) |
| 103 | +}) |
0 commit comments